Skip to content

Commit e523d72

Browse files
authored
Merge pull request #3442 from airween/v3/caseinsruleremovetarget
fix: ignore case when evaluating exceptions
2 parents 52c1d48 + f49b3e5 commit e523d72

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

src/rule_with_operator.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,23 @@ inline void RuleWithOperator::getFinalVars(variables::Variables *vars,
158158
variables::Variables addition;
159159
getVariablesExceptions(*trans, exclusion, &addition); // cppcheck-suppress ctunullpointer
160160

161-
for (int i = 0; i < m_variables->size(); i++) {
161+
for (std::size_t i = 0; i < m_variables->size(); i++) {
162162
Variable *variable = m_variables->at(i);
163163
if (exclusion->contains(variable)) {
164164
continue;
165165
}
166166
if (std::find_if(trans->m_ruleRemoveTargetById.begin(),
167167
trans->m_ruleRemoveTargetById.end(),
168168
[&, variable, this](const auto &m) -> bool {
169-
return m.first == m_ruleId
170-
&& m.second == *variable->m_fullName.get();
169+
const auto& str1 = m.second;
170+
const auto& str2 = *variable->m_fullName.get();
171+
return m.first == m_ruleId &&
172+
str1.size() == str2.size() &&
173+
std::equal(str1.begin(), str1.end(), str2.begin(),
174+
[](char a, char b) {
175+
return std::tolower(static_cast<unsigned char>(a)) ==
176+
std::tolower(static_cast<unsigned char>(b));
177+
}); // end-of std::equal
171178
}) != trans->m_ruleRemoveTargetById.end()) {
172179
continue;
173180
}

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,73 @@
9595
"SecRule REQUEST_FILENAME \"@endsWith /wp-login.php\" \"id:9002100,phase:2,t:none,nolog,pass,ctl:ruleRemoveTargetById=1;ARGS\"",
9696
"SecRule ARGS \"@contains lhebs\" \"id:1,phase:3,t:none,status:202,block,deny,tag:'CRS'\""
9797
]
98+
},
99+
{
100+
"enabled":1,
101+
"version_min":300000,
102+
"title":"Testing CtlRuleRemoveTargetById (4): uppercase `Referer` header",
103+
"expected":{
104+
"http_code": 200
105+
},
106+
"client":{
107+
"ip":"200.249.12.31",
108+
"port":123
109+
},
110+
"request":{
111+
"headers":{
112+
"Host":"localhost",
113+
"User-Agent":"curl/7.38.0",
114+
"Accept":"*/*",
115+
"Cookie": "PHPSESSID=rAAAAAAA2t5uvjq435r4q7ib3vtdjq120",
116+
"Content-Type": "text/xml",
117+
"Referer": "This is an attack"
118+
},
119+
"uri":"/index.html",
120+
"method":"GET",
121+
"body": [ ]
122+
},
123+
"server":{
124+
"ip":"200.249.12.31",
125+
"port":80
126+
},
127+
"rules":[
128+
"SecRuleEngine On",
129+
"SecRule REQUEST_FILENAME \"@unconditionalMatch\" \"id:1,phase:1,pass,t:none,ctl:ruleRemoveTargetById=2;REQUEST_HEADERS:referer\"",
130+
"SecRule REQUEST_HEADERS:Referer \"@contains attack\" \"id:2,phase:1,deny,t:none,log\""
131+
]
132+
},
133+
{
134+
"enabled":1,
135+
"version_min":300000,
136+
"title":"Testing CtlRuleRemoveTargetById (5): lowercase `Referer` header",
137+
"expected":{
138+
"http_code": 200
139+
},
140+
"client":{
141+
"ip":"200.249.12.31",
142+
"port":123
143+
},
144+
"request":{
145+
"headers":{
146+
"Host":"localhost",
147+
"User-Agent":"curl/7.38.0",
148+
"Accept":"*/*",
149+
"Cookie": "PHPSESSID=rAAAAAAA2t5uvjq435r4q7ib3vtdjq120",
150+
"Content-Type": "text/xml",
151+
"referer": "This is an attack"
152+
},
153+
"uri":"/index.html",
154+
"method":"GET",
155+
"body": [ ]
156+
},
157+
"server":{
158+
"ip":"200.249.12.31",
159+
"port":80
160+
},
161+
"rules":[
162+
"SecRuleEngine On",
163+
"SecRule REQUEST_FILENAME \"@unconditionalMatch\" \"id:1,phase:1,pass,t:none,ctl:ruleRemoveTargetById=2;REQUEST_HEADERS:referer\"",
164+
"SecRule REQUEST_HEADERS:Referer \"@contains attack\" \"id:2,phase:1,deny,t:none,log\""
165+
]
98166
}
99167
]

0 commit comments

Comments
 (0)