forked from Annihil/mod_defender
-
Notifications
You must be signed in to change notification settings - Fork 2
/
JsonValidator.cpp
273 lines (251 loc) · 8.35 KB
/
JsonValidator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/* _ _ __ _
* _ __ ___ ___ __| | __| | ___ / _| ___ _ __ __| | ___ _ __
* | '_ ` _ \ / _ \ / _` | / _` |/ _ \ |_ / _ \ '_ \ / _` |/ _ \ '__|
* | | | | | | (_) | (_| | | (_| | __/ _| __/ | | | (_| | __/ |
* |_| |_| |_|\___/ \__,_|___\__,_|\___|_| \___|_| |_|\__,_|\___|_|
* |_____|
* Copyright (c) 2017 Annihil
* Released under the GPLv3
*/
#include "JsonValidator.hpp"
#include "RuntimeScanner.hpp"
bool JsonValidator::jsonForward(json_t &js) {
while ((*(js.src + js.off) == ' ' ||
*(js.src + js.off) == '\t' ||
*(js.src + js.off) == '\n' ||
*(js.src + js.off) == '\r') && js.off < js.len) {
js.off++;
}
js.c = *(js.src + js.off);
return true;
}
/*
** used to fast forward in json POSTS,
** we skip whitespaces/tab/CR/LF
*/
bool JsonValidator::jsonSeek(json_t &js, unsigned char seek) {
jsonForward(js);
return js.c == seek;
}
/*
** extract a quoted strings,
** JSON spec only supports double-quoted strings,
** so do we.
*/
bool JsonValidator::jsonQuoted(json_t &js, str_t *ve) {
u_char *vn_start, *vn_end = NULL;
if (*(js.src + js.off) != '"')
return false;
js.off++;
vn_start = js.src + js.off;
/* extract varname inbetween "..."*/
while (js.off < js.len) {
/* skip next character if backslashed */
if (*(js.src + js.off) == '\\') {
js.off += 2;
if (js.off >= js.len) break;
}
if (*(js.src + js.off) == '"') {
vn_end = js.src + js.off;
js.off++;
break;
}
js.off++;
}
if (!vn_start || !vn_end)
return false;
if (!*vn_start || !*vn_end)
return false;
ve->data = vn_start;
ve->len = vn_end - vn_start;
return true;
}
/*
** an array is values separated by ','
*/
bool JsonValidator::jsonArray(json_t &js) {
bool rc;
js.c = *(js.src + js.off);
if (js.c != '[' || js.depth > JSON_MAX_DEPTH)
return false;
js.off++;
do {
rc = jsonVal(js);
/* if we cannot extract the value,
we may have reached array end. */
if (!rc)
break;
jsonForward(js);
if (js.c == ',') {
js.off++;
jsonForward(js);
} else break;
} while (true);
return js.c == ']';
}
bool JsonValidator::jsonVal(json_t &js) {
str_t val;
bool ret;
val.data = NULL;
val.len = 0;
jsonForward(js);
if (js.c == '"') {
ret = jsonQuoted(js, &val);
if (ret) {
/* parse extracted values. */
string jsckey = string((char *) js.ckey.data, js.ckey.len);
string value = string((char *) val.data, val.len);
transform(jsckey.begin(), jsckey.end(), jsckey.begin(), tolower);
transform(value.begin(), value.end(), value.begin(), tolower);
scanner.basestrRuleset(BODY, jsckey, value, bodyRules);
scanner.logg(LOG_LVL_DEBUG, scanner.errorLogFile, "JSON '%s' : '%s'\n", (char *) js.ckey.data,
(char *) val.data);
}
return ret;
}
if ((js.c >= '0' && js.c <= '9') || js.c == '-') {
val.data = js.src + js.off;
while (((*(js.src + js.off) >= '0' && *(js.src + js.off) <= '9') ||
*(js.src + js.off) == '.' || *(js.src + js.off) == '-') && js.off < js.len) {
val.len++;
js.off++;
}
/* parse extracted values. */
string jsckey = string((char *) js.ckey.data, js.ckey.len);
string value = string((char *) val.data, val.len);
transform(jsckey.begin(), jsckey.end(), jsckey.begin(), tolower);
transform(value.begin(), value.end(), value.begin(), tolower);
scanner.basestrRuleset(BODY, jsckey, value, bodyRules);
scanner.logg(LOG_LVL_DEBUG, scanner.errorLogFile, "JSON '%s' : '%s'\n", (char *) js.ckey.data,
(char *) val.data);
return true;
}
if (!strncasecmp((const char *) (js.src + js.off), (const char *) "true", 4) ||
!strncasecmp((const char *) (js.src + js.off), (const char *) "false", 5) ||
!strncasecmp((const char *) (js.src + js.off), (const char *) "null", 4)) {
js.c = *(js.src + js.off);
/* we don't check static values, do we ?! */
val.data = js.src + js.off;
if (js.c == 'F' || js.c == 'f') {
js.off += 5;
val.len = 5;
} else {
js.off += 4;
val.len = 4;
}
/* parse extracted values. */
string jsckey = string((char *) js.ckey.data, js.ckey.len);
string value = string((char *) val.data, val.len);
transform(jsckey.begin(), jsckey.end(), jsckey.begin(), tolower);
transform(value.begin(), value.end(), value.begin(), tolower);
scanner.basestrRuleset(BODY, jsckey, value, bodyRules);
scanner.logg(LOG_LVL_DEBUG, scanner.errorLogFile, "JSON '%s' : '%s'\n", (char *) js.ckey.data,
(char *) val.data);
return true;
}
if (js.c == '[') {
ret = jsonArray(js);
if (js.c != ']')
return false;
js.off++;
return (ret);
}
if (js.c == '{') {
/*
** if sub-struct, parse key without value :
** "foobar" : { "bar" : [1,2,3]} => "foobar" parsed alone.
** this is to avoid "foobar" left unparsed, as we won't have
** key/value here with "foobar" as a key.
*/
string jsckey = string((char *) js.ckey.data, js.ckey.len);
transform(jsckey.begin(), jsckey.end(), jsckey.begin(), tolower);
scanner.basestrRuleset(BODY, jsckey, empty, bodyRules);
ret = jsonObj(js);
jsonForward(js);
if (js.c != '}')
return false;
js.off++;
return (ret);
}
return false;
}
bool JsonValidator::jsonObj(json_t &js) {
js.c = *(js.src + js.off);
if (js.c != '{' || js.depth > JSON_MAX_DEPTH)
return false;
js.off++;
do {
jsonForward(js);
/* check subs (arrays, objects) */
switch (js.c) {
case '[': /* array */
js.depth++;
jsonArray(js);
if (!jsonSeek(js, ']'))
return false;
js.off++;
js.depth--;
break;
case '{': /* sub-object */
js.depth++;
jsonObj(js);
if (js.c != '}')
return false;
js.off++;
js.depth--;
break;
case '"': /* key : value, extract and parse. */
if (!jsonQuoted(js, &(js.ckey)))
return false;
if (!jsonSeek(js, ':'))
return false;
js.off++;
jsonForward(js);
if (!jsonVal(js))
return false;
default:
break;
}
jsonForward(js);
/* another element ? */
if (js.c == ',') {
js.off++;
jsonForward(js);
continue;
} else if (js.c == '}') {
js.depth--;
/* or maybe we just finished parsing this object */
return true;
} else {
/* nothing we expected, die. */
return false;
}
} while (js.off < js.len);
return false;
}
/*
** Parse a JSON request
*/
void JsonValidator::jsonParse(u_char *src, unsigned long len) {
json_t js;
js.json.data = js.src = src;
js.json.len = js.len = len;
if (!jsonSeek(js, '{')) {
scanner.applyRuleMatch(scanner.parser.invalidJson, 1, BODY, "missing opening brace", empty,
false);
return;
}
if (!jsonObj(js)) {
scanner.applyRuleMatch(scanner.parser.invalidJson, 1, BODY, "malformed json object", empty,
false);
scanner.logg(LOG_LVL_NOTICE, scanner.errorLogFile, "jsonObj returned error, apply invalid json.\n");
return;
}
/* we are now on closing bracket, check for garbage. */
js.off++;
jsonForward(js);
if (js.off != js.len) {
scanner.applyRuleMatch(scanner.parser.invalidJson, 1, BODY, "garbage after the closing brace",
empty, false);
}
}