Skip to content

Commit 1f45d6c

Browse files
committed
Adds full support to the libxml action
Issue #1148
1 parent a9e6716 commit 1f45d6c

File tree

7 files changed

+125
-33
lines changed

7 files changed

+125
-33
lines changed

headers/modsecurity/rule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class Rule {
5252
std::vector<actions::Action *> actions_runtime_pos;
5353

5454
std::vector<std::string> getActionNames();
55+
std::vector<actions::Action *> getActionsByName(const std::string& name);
5556

5657
std::vector<Variables::Variable *> *variables;
5758
int phase;

src/actions/xmlns.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@ bool XmlNS::init(std::string *error) {
3535
error->assign("XMLS: Bad format, missing equals sign.");
3636
return false;
3737
}
38-
m_name = std::string(m_parser_payload, 0, pos);
39-
m_value = std::string(m_parser_payload, pos+1, m_parser_payload.size());
38+
m_scope = std::string(m_parser_payload, 0, pos);
39+
m_href = std::string(m_parser_payload, pos+1, m_parser_payload.size());
4040

41-
if (m_value.empty() || m_name.empty()) {
41+
if (m_href.empty() || m_scope.empty()) {
4242
error->assign("XMLS: XMLNS is invalid. Expecting a " \
4343
"name=value format.");
4444
return false;
4545
}
4646

47-
if (m_value.at(0) == '\'' && m_value.size() > 3) {
48-
m_value.erase(0, 1);
49-
m_value.pop_back();
47+
if (m_href.at(0) == '\'' && m_href.size() > 3) {
48+
m_href.erase(0, 1);
49+
m_href.pop_back();
5050
}
5151

52-
if (m_value.compare(0, http.length(), http) != 0) {
52+
if (m_href.compare(0, http.length(), http) != 0) {
5353
error->assign("XMLS: Missing xmlns href for prefix: " \
54-
"`" + m_value + "'.");
54+
"`" + m_href + "'.");
5555
return false;
5656
}
5757

src/actions/xmlns.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ class XmlNS : public Action {
3737

3838
bool init(std::string *error);
3939

40-
private:
41-
std::string m_name;
42-
std::string m_value;
40+
std::string m_scope;
41+
std::string m_href;
4342
};
4443

4544

src/rule.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,4 +527,25 @@ bool Rule::evaluate(Transaction *trasn) {
527527
return ret;
528528
}
529529

530+
531+
std::vector<actions::Action *> Rule::getActionsByName(const std::string& name) {
532+
std::vector<actions::Action *> ret;
533+
for (auto &z : this->actions_runtime_pos) {
534+
if (z->m_name == name) {
535+
ret.push_back(z);
536+
}
537+
}
538+
for (auto &z : this->actions_runtime_pre) {
539+
if (z->m_name == name) {
540+
ret.push_back(z);
541+
}
542+
}
543+
for (auto &z : this->actions_conf) {
544+
if (z->m_name == name) {
545+
ret.push_back(z);
546+
}
547+
}
548+
return ret;
549+
}
550+
530551
} // namespace modsecurity

src/variables/xml.cc

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#include <fcntl.h>
2424
#include <libxml/xmlschemas.h>
2525
#include <libxml/xpath.h>
26+
#include <libxml/tree.h>
27+
#include <libxml/parser.h>
28+
#include <libxml/xpathInternals.h>
2629

2730
#include <iostream>
2831
#include <string>
@@ -33,11 +36,14 @@
3336
#include "modsecurity/transaction.h"
3437

3538
#include "src/request_body_processor/xml.h"
39+
#include "src/actions/action.h"
40+
#include "src/actions/xmlns.h"
3641

3742
namespace modsecurity {
3843
namespace Variables {
3944

4045
void XML::evaluateInternal(Transaction *t,
46+
Rule *rule,
4147
std::vector<const collection::Variable *> *l) {
4248
xmlXPathContextPtr xpathCtx;
4349
xmlXPathObjectPtr xpathObj;
@@ -60,8 +66,10 @@ void XML::evaluateInternal(Transaction *t,
6066
/* Is there an XML document tree at all? */
6167
if (t->m_xml->m_data.doc == NULL) {
6268
/* Sorry, we've got nothing to give! */
69+
t->debug(1, "XML: No XML document found, returning.");
6370
return;
6471
}
72+
6573
if (param.empty() == true) {
6674
/* Invocation without an XPath expression makes sense
6775
* with functions that manipulate the document tree.
@@ -70,39 +78,32 @@ void XML::evaluateInternal(Transaction *t,
7078
std::string("[XML document tree]" + param)));
7179
return;
7280
}
81+
7382
/* Process the XPath expression. */
7483
xpathExpr = (const xmlChar*)param.c_str();
7584
xpathCtx = xmlXPathNewContext(t->m_xml->m_data.doc);
7685
if (xpathCtx == NULL) {
7786
t->debug(1, "XML: Unable to create new XPath context.");
7887
return;
7988
}
80-
#if 0
81-
/* Look through the actionset of the associated rule
82-
* for the namespace information. Register them if any are found.
83-
*/
84-
tarr = apr_table_elts(rule->actionset->actions);
85-
telts = (const apr_table_entry_t*)tarr->elts;
86-
for (i = 0; i < tarr->nelts; i++) {
87-
msre_action *action = (msre_action *)telts[i].val;
88-
89-
if (strcasecmp(action->metadata->name, "xmlns") == 0) {
90-
char *prefix, *href;
91-
92-
if (parse_name_eq_value(mptmp, action->param, &prefix, &href) < 0) return -1;
93-
if ((prefix == NULL)||(href == NULL)) return -1;
94-
95-
if(xmlXPathRegisterNs(xpathCtx, (const xmlChar*)prefix, (const xmlChar*)href) != 0) {
96-
msr_log(msr, 1, "Failed to register XML namespace href \"%s\" prefix \"%s\".",
97-
log_escape(mptmp, prefix), log_escape(mptmp, href));
98-
return -1;
89+
90+
if (rule == NULL) {
91+
t->debug(2, "XML: Can't look for xmlns, internal error.");
92+
} else {
93+
std::vector<actions::Action *> acts = rule->getActionsByName("xmlns");
94+
for (auto &x : acts) {
95+
actions::XmlNS *z = (actions::XmlNS *)x;
96+
if (xmlXPathRegisterNs(xpathCtx, (const xmlChar*)z->m_scope.c_str(),
97+
(const xmlChar*)z->m_href.c_str()) != 0) {
98+
t->debug(1, "Failed to register XML namespace href \"" + \
99+
z->m_href + "\" prefix \"" + z->m_scope + "\".");
100+
return;
99101
}
100102

101-
msr_log(msr, 4, "Registered XML namespace href \"%s\" prefix \"%s\".",
102-
log_escape(mptmp, prefix), log_escape(mptmp, href));
103+
t->debug(4, "Registered XML namespace href \"" + z->m_href + \
104+
"\" prefix \"" + z->m_scope + "\"");
103105
}
104106
}
105-
#endif
106107

107108
/* Initialise XPath expression. */
108109
xpathObj = xmlXPathEvalExpression(xpathExpr, xpathCtx);

src/variables/xml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class XML : public Variable {
3636
: Variable(_name) { }
3737

3838
void evaluateInternal(Transaction *transaction,
39+
Rule *rule,
3940
std::vector<const collection::Variable *> *l) override;
4041
};
4142

test/test-cases/regression/action-xmlns.json

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,74 @@
3434
"SecRule REQUEST_HEADERS:Content-Type \"^text/xml$\" \"id:500008,phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML\"",
3535
"SecRule REQUEST_HEADERS:User-Agent \"^(.*)$\" \"id:123,xmlns:soap='schemas.xmlsoap.org/soap/envelope/'\""
3636
]
37+
},
38+
{
39+
"enabled":1,
40+
"version_min":300000,
41+
"title":"Testing XML request body parser (validate ok)",
42+
"expected":{
43+
"debug_log": "Target value: \"39.95\" \(Variable: XML:\/bookstore\/book\/price\[text\(\)\]\)"
44+
},
45+
"client":{
46+
"ip":"200.249.12.31",
47+
"port":123
48+
},
49+
"request":{
50+
"headers":{
51+
"Host":"localhost",
52+
"User-Agent":"curl/7.38.0",
53+
"Accept":"*/*",
54+
"Cookie": "PHPSESSID=rAAAAAAA2t5uvjq435r4q7ib3vtdjq120",
55+
"Content-Type": "text/xml"
56+
},
57+
"uri":"/?key=value&key=other_value",
58+
"method":"POST",
59+
"body": [
60+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
61+
"<bookstore>",
62+
"<book category=\"COOKING\">",
63+
"<title lang=\"en\">Everyday Italian</title>",
64+
"<author>Giada De Laurentiis</author>",
65+
"<year>2005</year>",
66+
"<price>30.00</price>",
67+
"</book>",
68+
69+
"<book category=\"CHILDREN\">",
70+
"<title lang=\"en\">Harry Potter</title>",
71+
"<author>J K. Rowling</author>",
72+
"<year>2005</year>",
73+
"<price>29.99</price>",
74+
"</book>",
75+
76+
"<book category=\"WEB\">",
77+
"<title lang=\"en\">XQuery Kick Start</title>",
78+
"<author>James McGovern</author>",
79+
"<author>Per Bothner</author>",
80+
"<author>Kurt Cagle</author>",
81+
"<author>James Linn</author>",
82+
"<author>Vaidyanathan Nagarajan</author>",
83+
"<year>2003</year>",
84+
"<price>49.99</price>",
85+
"</book>",
86+
87+
"<book category=\"WEB\">",
88+
"<title lang=\"en\">Learning XML</title>",
89+
"<author>Erik T. Ray</author>",
90+
"<year>2003</year>",
91+
"<price>39.95</price>",
92+
"</book>",
93+
"</bookstore>"
94+
]
95+
},
96+
"server":{
97+
"ip":"200.249.12.31",
98+
"port":80
99+
},
100+
"rules":[
101+
"SecRuleEngine On",
102+
"SecRequestBodyAccess On",
103+
"SecRule REQUEST_HEADERS:Content-Type \"^text/xml$\" \"id:500005,phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML\"",
104+
"SecRule XML:/bookstore/book/price[text()] \"Fred\" \"phase:3,id:123,xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'\""
105+
]
37106
}
38107
]

0 commit comments

Comments
 (0)