Skip to content

Commit 1b88947

Browse files
committed
Adds support 'xmlns' action to the libmodsec parser
1 parent 3e8defb commit 1b88947

File tree

6 files changed

+166
-2
lines changed

6 files changed

+166
-2
lines changed

src/Makefile.am

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ ACTIONS = \
121121
actions/transformations/url_decode_uni.cc \
122122
actions/transformations/url_encode.cc \
123123
actions/transformations/utf8_to_unicode.cc \
124-
actions/ver.cc
124+
actions/ver.cc \
125+
actions/xmlns.cc
125126

126127

127128
OPERATORS = \

src/actions/xmlns.cc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
#include "actions/xmlns.h"
17+
18+
#include <iostream>
19+
#include <string>
20+
21+
#include "actions/action.h"
22+
#include "modsecurity/transaction.h"
23+
#include "src/utils.h"
24+
25+
namespace modsecurity {
26+
namespace actions {
27+
28+
29+
bool XmlNS::init(std::string *error) {
30+
size_t pos;
31+
std::string http = "http://";
32+
33+
pos = action.find("=");
34+
if (pos == std::string::npos) {
35+
error->assign("XMLS: Bad format, missing equals sign.");
36+
return false;
37+
}
38+
m_name = std::string(action, 0, pos);
39+
m_value = std::string(action, pos+1, action.size());
40+
41+
if (m_value.empty() or m_name.empty()) {
42+
error->assign("XMLS: XMLNS is invalid. Expecting a " \
43+
"name=value format.");
44+
return false;
45+
}
46+
47+
if (m_value.at(0) == '\'' && m_value.size() > 3) {
48+
m_value.erase(0, 1);
49+
m_value.pop_back();
50+
}
51+
52+
if (m_value.compare(0, http.length(), http) != 0) {
53+
error->assign("XMLS: Missing xmlns href for prefix: " \
54+
"`" + m_value + "'.");
55+
return false;
56+
}
57+
58+
return true;
59+
}
60+
61+
62+
} // namespace actions
63+
} // namespace modsecurity

src/actions/xmlns.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* ModSecurity, http://www.modsecurity.org/
3+
* Copyright (c) 2015 Trustwave Holdings, Inc. (http://www.trustwave.com/)
4+
*
5+
* You may not use this file except in compliance with
6+
* the License. You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* If any of the files related to licensing are missing or if you have any
11+
* other questions related to licensing please contact Trustwave Holdings, Inc.
12+
* directly using the email address security@modsecurity.org.
13+
*
14+
*/
15+
16+
#include <string>
17+
18+
#include "actions/action.h"
19+
20+
#ifndef SRC_ACTIONS_XMLNS_H_
21+
#define SRC_ACTIONS_XMLNS_H_
22+
23+
class Transaction;
24+
25+
namespace modsecurity {
26+
class Transaction;
27+
namespace actions {
28+
29+
30+
class XmlNS : public Action {
31+
public:
32+
explicit XmlNS(std::string action) : Action(action) { }
33+
34+
bool evaluate(Rule *rule, Transaction *transaction) override {
35+
return true;
36+
}
37+
38+
bool init(std::string *error);
39+
40+
private:
41+
std::string m_name;
42+
std::string m_value;
43+
};
44+
45+
46+
} // namespace actions
47+
} // namespace modsecurity
48+
49+
#endif // SRC_ACTIONS_XMLNS_H_

src/parser/seclang-parser.yy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Driver;
3939
#include "actions/tag.h"
4040
#include "actions/transformations/transformation.h"
4141
#include "actions/transformations/none.h"
42+
#include "actions/xmlns.h"
4243
#include "operators/operator.h"
4344
#include "modsecurity/rule.h"
4445
#include "utils/geo_lookup.h"
@@ -261,6 +262,7 @@ using modsecurity::Variables::XML;
261262
%token <std::string> ACTION_REV
262263
%token <std::string> ACTION_VER
263264
%token <std::string> ACTION_MATURITY
265+
%token <std::string> ACTION_XMLNS
264266
%token <std::string> LOG_DATA
265267
%token <std::string> TRANSFORMATION
266268
%token <std::string> ACTION_CTL_AUDIT_ENGINE
@@ -1011,6 +1013,15 @@ act:
10111013
{
10121014
$$ = new Maturity($1);
10131015
}
1016+
| ACTION_XMLNS
1017+
{
1018+
std::string error;
1019+
$$ = new modsecurity::actions::XmlNS($1);
1020+
if ($$->init(&error) == false) {
1021+
driver.error(@0, error);
1022+
YYERROR;
1023+
}
1024+
}
10141025
| ACTION_CTL_BDY_XML
10151026
{
10161027
/* not ready yet. */

src/parser/seclang-scanner.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ using modsecurity::split;
2323
%}
2424
%option noyywrap nounput batch debug noinput
2525

26-
ACTION (?i:accuracy|append|block|capture|chain|deny|deprecatevar|drop|expirevar|id:[0-9]+|id:'[0-9]+'|log|multiMatch|noauditlog|nolog|pass|pause|prepend|proxy|sanitiseArg|sanitiseMatched|sanitiseMatchedBytes|sanitiseRequestHeader|sanitiseResponseHeader|setrsc|setenv|status:[0-9]+|xmlns)
26+
ACTION (?i:accuracy|append|block|capture|chain|deny|deprecatevar|drop|expirevar|id:[0-9]+|id:'[0-9]+'|log|multiMatch|noauditlog|nolog|pass|pause|prepend|proxy|sanitiseArg|sanitiseMatched|sanitiseMatchedBytes|sanitiseRequestHeader|sanitiseResponseHeader|setrsc|setenv|status:[0-9]+)
27+
ACTION_XMLNS (?i:xmlns)
2728
ACTION_ALLOW (?i:allow)
2829
ACTION_INITCOL (?i:initcol)
2930

@@ -386,6 +387,7 @@ CONFIG_DIR_UNICODE_MAP_FILE (?i:SecUnicodeMapFile)
386387
{ACTION_SETVAR}:{VAR_FREE_TEXT_SPACE_COMMA} {
387388
return yy::seclang_parser::make_ACTION_SETVAR(strchr(yytext, ':') + 1, *driver.loc.back());
388389
}
390+
{ACTION_XMLNS}:{FREE_TEXT_SPACE_COMMA_QUOTE} { return yy::seclang_parser::make_ACTION_XMLNS(strchr(yytext, ':') + 1, *driver.loc.back()); }
389391
390392
{LOG_DATA}:'{FREE_TEXT_QUOTE}' { return yy::seclang_parser::make_LOG_DATA(strchr(yytext, ':') + 1, *driver.loc.back()); }
391393
{ACTION_MSG}:'{FREE_TEXT_QUOTE}' { return yy::seclang_parser::make_ACTION_MSG(strchr(yytext, ':') + 1, *driver.loc.back()); }
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
[
2+
{
3+
"enabled":1,
4+
"version_min":300000,
5+
"title":"Testing action :: XMLNS (parser error 1)",
6+
"expected":{
7+
"parser_error": "XMLS: Bad format, missing equals sign"
8+
},
9+
"rules":[
10+
"SecRule REQUEST_HEADERS:Content-Type \"^text/xml$\" \"id:500008,phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML\"",
11+
"SecRule REQUEST_HEADERS:User-Agent \"^(.*)$\" \"id:123,xmlns:soap'http://schemas.xmlsoap.org/soap/envelope/'\""
12+
]
13+
},
14+
{
15+
"enabled":1,
16+
"version_min":300000,
17+
"title":"Testing action :: XMLNS (parser error 2)",
18+
"expected":{
19+
"parser_error": "XMLS: XMLNS is invalid. Expecting a name=value format."
20+
},
21+
"rules":[
22+
"SecRule REQUEST_HEADERS:Content-Type \"^text/xml$\" \"id:500008,phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML\"",
23+
"SecRule REQUEST_HEADERS:User-Agent \"^(.*)$\" \"id:123,xmlns:=\""
24+
]
25+
},
26+
{
27+
"enabled":1,
28+
"version_min":300000,
29+
"title":"Testing action :: XMLNS (parser error 3)",
30+
"expected":{
31+
"parser_error": "XMLS: Missing xmlns href for prefix: `schemas.xmlsoap.org/soap/envelope/'."
32+
},
33+
"rules":[
34+
"SecRule REQUEST_HEADERS:Content-Type \"^text/xml$\" \"id:500008,phase:1,t:none,t:lowercase,nolog,pass,ctl:requestBodyProcessor=XML\"",
35+
"SecRule REQUEST_HEADERS:User-Agent \"^(.*)$\" \"id:123,xmlns:soap='schemas.xmlsoap.org/soap/envelope/'\""
36+
]
37+
}
38+
]

0 commit comments

Comments
 (0)