Skip to content

Commit b8160cc

Browse files
martinhsvzimmerle
authored andcommitted
Fix Cookie header parsing issues
1 parent 7ba7763 commit b8160cc

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
v3.0.4 - YYYY-MMM-DD (to be released)
22
-------------------------------------
33

4+
- Fix Cookie header parsing issues
5+
[Issue #2201 - @airween, @martinhsv]
46
- Fix rules with nolog are logging to part H
57
[Issue #2196 - @martinhsv]
68
- Fix argument key-value pair parsing cases

src/transaction.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,18 @@ int Transaction::addRequestHeader(const std::string& key,
549549
if (keyl == "cookie") {
550550
size_t localOffset = m_variableOffset;
551551
size_t pos;
552+
552553
std::vector<std::string> cookies = utils::string::ssplit(value, ';');
554+
555+
// Get rid of any optional whitespace after the cookie-string
556+
// (i.e. after the end of the final cookie-pair)
557+
if (!cookies.empty()) {
558+
std::string& final_cookie_pair = cookies.back();
559+
while (!final_cookie_pair.empty() && isspace(final_cookie_pair.back())) {
560+
final_cookie_pair.pop_back();
561+
}
562+
}
563+
553564
for (const std::string &c : cookies) {
554565
// skip empty substring, eg "Cookie: ;;foo=bar"
555566
if (c.empty() == true) {

test/test-cases/regression/variable-REQUEST_COOKIES.json

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
"enabled":1,
44
"version_min":300000,
5-
"title":"Testing Variables :: REQUEST_COOKIES (1/5)",
5+
"title":"Testing Variables :: REQUEST_COOKIES (1/6)",
66
"client":{
77
"ip":"200.249.12.31",
88
"port":123
@@ -42,7 +42,7 @@
4242
{
4343
"enabled":1,
4444
"version_min":300000,
45-
"title":"Testing Variables :: REQUEST_COOKIES (2/5)",
45+
"title":"Testing Variables :: REQUEST_COOKIES (2/6)",
4646
"client":{
4747
"ip":"200.249.12.31",
4848
"port":123
@@ -82,7 +82,7 @@
8282
{
8383
"enabled":1,
8484
"version_min":300000,
85-
"title":"Testing Variables :: REQUEST_COOKIES (3/5)",
85+
"title":"Testing Variables :: REQUEST_COOKIES (3/6)",
8686
"client":{
8787
"ip":"200.249.12.31",
8888
"port":123
@@ -122,7 +122,7 @@
122122
{
123123
"enabled":1,
124124
"version_min":300000,
125-
"title":"Testing Variables :: REQUEST_COOKIES (4/5)",
125+
"title":"Testing Variables :: REQUEST_COOKIES (4/6)",
126126
"client":{
127127
"ip":"200.249.12.31",
128128
"port":123
@@ -162,7 +162,7 @@
162162
{
163163
"enabled":1,
164164
"version_min":300000,
165-
"title":"Testing Variables :: REQUEST_COOKIES (5/5)",
165+
"title":"Testing Variables :: REQUEST_COOKIES (5/6)",
166166
"client":{
167167
"ip":"200.249.12.31",
168168
"port":123
@@ -198,6 +198,46 @@
198198
"SecRuleEngine On",
199199
"SecRule REQUEST_COOKIES \"@contains test \" \"id:1,pass,t:trim\""
200200
]
201+
},
202+
{
203+
"enabled":1,
204+
"version_min":300000,
205+
"title":"Testing Variables :: REQUEST_COOKIES (6/6)",
206+
"client":{
207+
"ip":"200.249.12.31",
208+
"port":123
209+
},
210+
"server":{
211+
"ip":"200.249.12.31",
212+
"port":80
213+
},
214+
"request":{
215+
"headers":{
216+
"Host":"localhost",
217+
"User-Agent":"curl/7.38.0",
218+
"Accept":"*/*",
219+
"Cookie":"aaa=bbb;abc=def "
220+
},
221+
"uri":"/?key=value&key=other_value",
222+
"method":"GET"
223+
},
224+
"response":{
225+
"headers":{
226+
"Date":"Mon, 13 Jul 2015 20:02:41 GMT",
227+
"Last-Modified":"Sun, 26 Oct 2014 22:33:37 GMT",
228+
"Content-Type":"text/html"
229+
},
230+
"body":[
231+
"no need."
232+
]
233+
},
234+
"expected":{
235+
"debug_log":"Target value: \"def\" \\(Variable: REQUEST_COOKIES:abc\\)"
236+
},
237+
"rules":[
238+
"SecRuleEngine On",
239+
"SecRule REQUEST_COOKIES:abc \"@rx ^def$\" \"id:1,pass\""
240+
]
201241
}
202242
]
203243

0 commit comments

Comments
 (0)