Skip to content

Commit 25e5543

Browse files
zimmerleFelipe Zimmerle
authored andcommitted
Allow empty arrays in JSON parser
Issue #1576
1 parent 7af8363 commit 25e5543

File tree

2 files changed

+177
-2
lines changed

2 files changed

+177
-2
lines changed

apache2/msc_json.c

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,60 @@ static int yajl_number(void *ctx, const char *value, size_t length)
150150
return json_add_argument(msr, value, length);
151151
}
152152

153+
static int yajl_start_array(void *ctx) {
154+
modsec_rec *msr = (modsec_rec *) ctx;
155+
156+
if (!msr->json->current_key && !msr->json->prefix) {
157+
msr->json->prefix = apr_pstrdup(msr->mp, "array");
158+
msr->json->current_key = apr_pstrdup(msr->mp, "array");
159+
}
160+
else if (msr->json->prefix) {
161+
msr->json->prefix = apr_psprintf(msr->mp, "%s.%s", msr->json->prefix,
162+
msr->json->current_key);
163+
}
164+
else {
165+
msr->json->prefix = apr_pstrdup(msr->mp, msr->json->current_key);
166+
}
167+
168+
if (msr->txcfg->debuglog_level >= 9) {
169+
msr_log(msr, 9, "New JSON hash context (prefix '%s')", msr->json->prefix);
170+
}
171+
172+
173+
return 1;
174+
}
175+
176+
177+
static int yajl_end_array(void *ctx) {
178+
modsec_rec *msr = (modsec_rec *) ctx;
179+
unsigned char *separator = (unsigned char *) NULL;
180+
181+
/**
182+
* If we have no prefix, then this is the end of a top-level hash and
183+
* we don't do anything
184+
*/
185+
if (msr->json->prefix == NULL) return 1;
186+
187+
/**
188+
* Current prefix might or not include a separator character; top-level
189+
* hash keys do not have separators in the variable name
190+
*/
191+
separator = strrchr(msr->json->prefix, '.');
192+
193+
if (separator) {
194+
msr->json->prefix = apr_pstrmemdup(msr->mp, msr->json->prefix,
195+
separator - msr->json->prefix);
196+
}
197+
else {
198+
/**
199+
* TODO: Check if it is safe to do this kind of pointer tricks
200+
*/
201+
msr->json->prefix = (unsigned char *) NULL;
202+
}
203+
204+
return 1;
205+
}
206+
153207
/**
154208
* Callback for a new hash, which indicates a new subtree, labeled as the current
155209
* argument name, is being created
@@ -237,8 +291,8 @@ int json_init(modsec_rec *msr, char **error_msg) {
237291
yajl_start_map,
238292
yajl_map_key,
239293
yajl_end_map,
240-
NULL /* yajl_start_array */,
241-
NULL /* yajl_end_array */
294+
yajl_start_array,
295+
yajl_end_array
242296
};
243297

244298
if (error_msg == NULL) return -1;

tests/regression/rule/15-json.t

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,126 @@
3535
),
3636
),
3737
),
38+
},
39+
{
40+
type => "rule",
41+
comment => "json parser - issue #1576 - 1",
42+
conf => qq(
43+
SecRuleEngine On
44+
SecRequestBodyAccess On
45+
SecDebugLog $ENV{DEBUG_LOG}
46+
SecDebugLogLevel 9
47+
SecRule REQUEST_HEADERS:Content-Type "application/json" \\
48+
"id:'200001',phase:1,t:none,t:lowercase,pass,nolog,ctl:requestBodyProcessor=JSON"
49+
SecRule ARGS "bar" "id:'200441',phase:3,log"
50+
),
51+
match_log => {
52+
error => [ qr/ModSecurity: Warning. Pattern match "bar" at ARGS:foo.|ModSecurity: JSON support was not enabled/s, 1 ],
53+
debug => [ qr/ARGS:foo|ARGS:mod|ARGS:ops.ops.ops|ARGS:ops.ops.ops|ARGS:ops.ops|ARGS:ops.ops|ARGS:ops.ops.eins.eins|ARGS:ops.ops.eins.eins|ARGS:whee/, 1 ],
54+
},
55+
match_response => {
56+
status => qr/^200$/,
57+
},
58+
request => new HTTP::Request(
59+
POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
60+
[
61+
"Content-Type" => "application/json",
62+
],
63+
normalize_raw_request_data(
64+
q(
65+
{
66+
"foo":"bar",
67+
"mod":"sec",
68+
"ops":[
69+
[
70+
"um",
71+
"um e meio"
72+
],
73+
"dois",
74+
"tres",
75+
{
76+
"eins":[
77+
"zwei",
78+
"drei"
79+
]
80+
}
81+
],
82+
"whee":"lhebs"
83+
}
84+
),
85+
),
86+
),
87+
},
88+
{
89+
type => "rule",
90+
comment => "json parser - issue #1576 - 2",
91+
conf => qq(
92+
SecRuleEngine On
93+
SecRequestBodyAccess On
94+
SecDebugLog $ENV{DEBUG_LOG}
95+
SecDebugLogLevel 9
96+
SecRule REQUEST_HEADERS:Content-Type "application/json" \\
97+
"id:'200001',phase:1,t:none,t:lowercase,pass,nolog,ctl:requestBodyProcessor=JSON"
98+
SecRule ARGS "um" "id:'200441',phase:3,log"
99+
),
100+
match_log => {
101+
error => [ qr/ModSecurity: Warning. Pattern match "um" at ARGS:array.array|ModSecurity: JSON support was not enabled/s, 1 ],
102+
debug => [ qr/ARGS:array.array|ARGS:array.array/, 1 ],
103+
},
104+
match_response => {
105+
status => qr/^200$/,
106+
},
107+
request => new HTTP::Request(
108+
POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
109+
[
110+
"Content-Type" => "application/json",
111+
],
112+
normalize_raw_request_data(
113+
q(
114+
[
115+
"um",
116+
"um e meio"
117+
]
118+
),
119+
),
120+
),
121+
},
122+
{
123+
type => "rule",
124+
comment => "json parser - issue #1576 - 3",
125+
conf => qq(
126+
SecRuleEngine On
127+
SecRequestBodyAccess On
128+
SecDebugLog $ENV{DEBUG_LOG}
129+
SecDebugLogLevel 9
130+
SecRule REQUEST_HEADERS:Content-Type "application/json" \\
131+
"id:'200001',phase:1,t:none,t:lowercase,pass,nolog,ctl:requestBodyProcessor=JSON"
132+
SecRule ARGS "seis" "id:'200441',phase:3,log"
133+
),
134+
match_log => {
135+
error => [ qr/ModSecurity: Warning. Pattern match "seis" at ARGS:array.array.cinco.|ModSecurity: JSON support was not enabled/s, 1 ],
136+
debug => [ qr/ARGS:array.array|ARGS:array.array|ARGS:array.array.tres|ARGS:array.array.cinco/, 1 ],
137+
},
138+
match_response => {
139+
status => qr/^200$/,
140+
},
141+
request => new HTTP::Request(
142+
POST => "http://$ENV{SERVER_NAME}:$ENV{SERVER_PORT}/test.txt",
143+
[
144+
"Content-Type" => "application/json",
145+
],
146+
normalize_raw_request_data(
147+
q(
148+
[
149+
"um",
150+
"um e meio",
151+
{
152+
"tres": "quatro",
153+
"cinco": "seis"
154+
}
155+
]
156+
),
157+
),
158+
),
38159
}
39160

0 commit comments

Comments
 (0)