forked from owasp-modsecurity/ModSecurity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_backend_simdjson.cc
More file actions
518 lines (447 loc) · 18.9 KB
/
json_backend_simdjson.cc
File metadata and controls
518 lines (447 loc) · 18.9 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
* ModSecurity, http://www.modsecurity.org/
* Copyright (c) 2015 - 2024 Trustwave Holdings, Inc. (http://www.trustwave.com/)
*
* You may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* If any of the files related to licensing are missing or if you have any
* other questions related to licensing please contact Trustwave Holdings, Inc.
* directly using the email address security@modsecurity.org.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "src/request_body_processor/json_backend.h"
#include "src/request_body_processor/json_backend_common.h"
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include "src/request_body_processor/json_instrumentation.h"
#include "simdjson.h"
namespace modsecurity::RequestBodyProcessor {
namespace {
using json_backend_common::finishSinkCall;
using json_backend_common::makeResult;
JsonParseResult fromSimdjsonError(simdjson::error_code error) {
switch (error) {
case simdjson::UTF8_ERROR:
return makeResult(JsonParseStatus::Utf8Error,
std::string("Invalid UTF-8 in JSON body: ")
+ simdjson::error_message(error));
case simdjson::EMPTY:
case simdjson::UNCLOSED_STRING:
case simdjson::INCOMPLETE_ARRAY_OR_OBJECT:
case simdjson::INSUFFICIENT_PADDING:
return makeResult(JsonParseStatus::TruncatedInput,
std::string("Incomplete JSON body: ")
+ simdjson::error_message(error));
case simdjson::DEPTH_ERROR:
case simdjson::TAPE_ERROR:
case simdjson::STRING_ERROR:
case simdjson::T_ATOM_ERROR:
case simdjson::F_ATOM_ERROR:
case simdjson::N_ATOM_ERROR:
case simdjson::NUMBER_ERROR:
case simdjson::BIGINT_ERROR:
case simdjson::UNESCAPED_CHARS:
case simdjson::TRAILING_CONTENT:
return makeResult(JsonParseStatus::ParseError,
std::string("Invalid JSON body: ")
+ simdjson::error_message(error));
case simdjson::CAPACITY:
case simdjson::OUT_OF_CAPACITY:
case simdjson::MEMALLOC:
return makeResult(JsonParseStatus::InternalError,
std::string("JSON parser backend failure: ")
+ simdjson::error_message(error));
default:
return makeResult(JsonParseStatus::InternalError,
std::string("JSON backend failed: ")
+ simdjson::error_message(error));
}
}
std::size_t effectiveTechnicalMaxDepth(
const JsonBackendParseOptions &options) {
return options.technical_max_depth > 0
? static_cast<std::size_t>(options.technical_max_depth) : 1;
}
std::string_view trimTrailingJsonWhitespace(std::string_view token) {
while (!token.empty()) {
if (const char tail = token.back();
tail != ' ' && tail != '\t' && tail != '\n' && tail != '\r') {
break;
}
token.remove_suffix(1);
}
return token;
}
/*
* The ondemand parser is reused per thread because simdjson benefits from
* keeping its internal buffers warm across parses. thread_local storage keeps
* the parser isolated to the calling thread, so no parser state is shared
* across transactions running on different threads. The parse and full
* document traversal both complete inside parseDocumentWithSimdjson(), so no
* parser-backed state escapes this function. We intentionally do not add an
* automatic release/recreate heuristic here: the vendored simdjson API
* explicitly supports parser reuse, and retained capacity after unusually
* large inputs remains a conscious tradeoff rather than an accidental leak.
*/
simdjson::ondemand::parser &getReusableSimdjsonParser() {
thread_local std::unique_ptr<simdjson::ondemand::parser> parser;
if (parser == nullptr) {
#ifdef MSC_JSON_AUDIT_INSTRUMENTATION
const auto parser_start = std::chrono::steady_clock::now();
parser = std::make_unique<simdjson::ondemand::parser>();
recordSimdjsonParserConstruction(static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - parser_start).count()));
#else
parser = std::make_unique<simdjson::ondemand::parser>();
#endif
}
return *parser;
}
std::size_t clampRequestedMaxDepth(std::size_t input_size,
const JsonBackendParseOptions &options) {
const std::size_t requested_depth = effectiveTechnicalMaxDepth(options);
const std::size_t max_possible_depth = (input_size / 2) + 1;
return std::min(requested_depth, std::max<std::size_t>(1,
max_possible_depth));
}
simdjson::error_code prepareParser(simdjson::ondemand::parser *parser,
std::size_t input_size, const JsonBackendParseOptions &options) {
if (parser == nullptr) {
return simdjson::MEMALLOC;
}
const JsonBackendParseOptions default_options;
std::size_t required_max_depth = parser->max_depth();
if (options.technical_max_depth != default_options.technical_max_depth) {
required_max_depth = clampRequestedMaxDepth(input_size, options);
}
if (parser->capacity() >= input_size
&& parser->max_depth() == required_max_depth) {
return simdjson::SUCCESS;
}
// simdjson reuses parser buffers across parses. allocate() can grow the
// per-thread parser to satisfy a larger document or different max-depth,
// but it does not proactively shrink retained capacity for later, smaller
// inputs. In simdjson 4.6.1 the max-depth parameter is only enforced by
// simdjson's development checks, so we keep passing it here for that
// internal guardrail while our own walker enforces technical_max_depth at
// runtime using current_depth().
return parser->allocate(input_size, required_max_depth);
}
template <typename ResultType, typename TargetType>
JsonParseResult getResult(ResultType &&result, TargetType *target) {
if (auto error = std::forward<ResultType>(result).get(*target); error) {
return fromSimdjsonError(error);
}
return makeResult(JsonParseStatus::Ok);
}
class JsonBackendWalker {
public:
JsonBackendWalker(JsonEventSink *sink,
const JsonBackendParseOptions &options)
: m_sink(sink),
m_technical_max_depth(effectiveTechnicalMaxDepth(options)) { }
JsonParseResult walk(simdjson::ondemand::document *document) {
bool is_scalar = false;
JsonParseResult result = getResult(document->is_scalar(), &is_scalar);
if (!result.ok()) {
return result;
}
if (is_scalar) {
return walkDocumentScalar(document);
}
simdjson::ondemand::value root_value;
result = getResult(document->get_value(), &root_value);
if (!result.ok()) {
return result;
}
return walkValue(root_value);
}
private:
JsonParseResult walkDocumentScalar(simdjson::ondemand::document *document) {
simdjson::ondemand::json_type type;
JsonParseResult result = getResult(document->type(), &type);
if (!result.ok()) {
return result;
}
switch (type) {
case simdjson::ondemand::json_type::string: {
std::string_view decoded;
result = getResult(document->get_string(), &decoded);
if (!result.ok()) {
return result;
}
return finishSinkCall(m_sink->on_string(decoded),
"handling a root string");
}
case simdjson::ondemand::json_type::number: {
std::string_view raw_number;
result = getResult(document->raw_json_token(), &raw_number);
if (!result.ok()) {
return result;
}
return finishSinkCall(m_sink->on_number(
trimTrailingJsonWhitespace(raw_number)),
"handling a root number");
}
case simdjson::ondemand::json_type::boolean: {
bool boolean_value = false;
result = getResult(document->get_bool(), &boolean_value);
if (!result.ok()) {
return result;
}
return finishSinkCall(m_sink->on_boolean(boolean_value),
"handling a root boolean");
}
case simdjson::ondemand::json_type::null: {
bool is_null = false;
result = getResult(document->is_null(), &is_null);
if (!result.ok()) {
return result;
}
if (!is_null) {
return makeResult(JsonParseStatus::InternalError,
"Root scalar classified as null but failed validation.");
}
return finishSinkCall(m_sink->on_null(),
"handling a root null");
}
case simdjson::ondemand::json_type::unknown:
return makeResult(JsonParseStatus::ParseError,
"Invalid JSON token encountered in simdjson backend.");
case simdjson::ondemand::json_type::object:
case simdjson::ondemand::json_type::array:
return makeResult(JsonParseStatus::InternalError,
"Unexpected root scalar container encountered in simdjson backend.");
}
return makeResult(JsonParseStatus::InternalError,
"Unsupported root scalar type encountered in simdjson backend.");
}
JsonParseResult walkValue(simdjson::ondemand::value value) {
simdjson::ondemand::json_type type;
if (JsonParseResult result = getResult(value.type(), &type);
!result.ok()) {
return result;
}
switch (type) {
case simdjson::ondemand::json_type::object:
if (auto result = enforceTechnicalDepth(value); !result.ok()) {
return result;
}
return walkObject(value);
case simdjson::ondemand::json_type::array:
if (auto result = enforceTechnicalDepth(value); !result.ok()) {
return result;
}
return walkArray(value);
case simdjson::ondemand::json_type::string:
return walkString(value);
case simdjson::ondemand::json_type::number:
return walkNumber(value);
case simdjson::ondemand::json_type::boolean:
return walkBoolean(value);
case simdjson::ondemand::json_type::null: {
return finishSinkCall(m_sink->on_null(),
"handling a null value");
}
case simdjson::ondemand::json_type::unknown:
return makeResult(JsonParseStatus::ParseError,
"Invalid JSON token encountered in simdjson backend.");
}
return makeResult(JsonParseStatus::InternalError,
"Unsupported JSON token type encountered.");
}
JsonParseResult walkObject(simdjson::ondemand::value value) {
simdjson::ondemand::object object;
JsonParseResult result = getResult(value.get_object(), &object);
if (!result.ok()) {
return result;
}
JsonSinkStatus sink_status = m_sink->on_start_object();
if (JsonParseResult sink_result = finishSinkCall(
sink_status, "starting an object"); !sink_result.ok()) {
return sink_result;
}
for (auto field_result : object) {
simdjson::ondemand::field field;
std::string_view key;
simdjson::ondemand::value child;
result = getResult(std::move(field_result), &field);
if (!result.ok()) {
return result;
}
result = getResult(field.unescaped_key(), &key);
if (!result.ok()) {
return result;
}
sink_status = m_sink->on_key(key);
if (JsonParseResult sink_result = finishSinkCall(
sink_status, "processing an object key");
!sink_result.ok()) {
return sink_result;
}
child = field.value();
result = walkValue(child);
if (!result.ok()) {
return result;
}
}
return finishSinkCall(m_sink->on_end_object(), "ending an object");
}
JsonParseResult walkArray(simdjson::ondemand::value value) {
simdjson::ondemand::array array;
JsonParseResult result = getResult(value.get_array(), &array);
if (!result.ok()) {
return result;
}
JsonSinkStatus sink_status = m_sink->on_start_array();
if (JsonParseResult sink_result = finishSinkCall(
sink_status, "starting an array"); !sink_result.ok()) {
return sink_result;
}
for (auto element_result : array) {
simdjson::ondemand::value element;
result = getResult(std::move(element_result), &element);
if (!result.ok()) {
return result;
}
result = walkValue(element);
if (!result.ok()) {
return result;
}
}
return finishSinkCall(m_sink->on_end_array(), "ending an array");
}
JsonParseResult walkString(simdjson::ondemand::value value) {
std::string_view decoded;
if (JsonParseResult result = getResult(value.get_string(), &decoded);
!result.ok()) {
return result;
}
return finishSinkCall(m_sink->on_string(decoded), "handling a string");
}
JsonParseResult walkNumber(simdjson::ondemand::value value) {
std::string_view raw_number = trimTrailingJsonWhitespace(
value.raw_json_token());
return finishSinkCall(m_sink->on_number(raw_number),
"handling a number");
}
JsonParseResult walkBoolean(simdjson::ondemand::value value) {
bool boolean_value = false;
if (JsonParseResult result = getResult(value.get_bool(),
&boolean_value); !result.ok()) {
return result;
}
return finishSinkCall(m_sink->on_boolean(boolean_value),
"handling a boolean");
}
JsonParseResult enforceTechnicalDepth(simdjson::ondemand::value value) const {
const int32_t current_depth = value.current_depth();
if (current_depth <= 0) {
return makeResult(JsonParseStatus::InternalError,
"Invalid current depth reported by simdjson backend.");
}
if (static_cast<std::size_t>(current_depth) > m_technical_max_depth) {
return makeResult(JsonParseStatus::ParseError,
"JSON nesting depth exceeds backend technical max depth.");
}
return makeResult(JsonParseStatus::Ok);
}
JsonEventSink *m_sink;
const std::size_t m_technical_max_depth;
};
struct PreparedSimdjsonInput {
simdjson::padded_string_view view{};
simdjson::padded_string owned_copy{};
};
PreparedSimdjsonInput prepareMutableSimdjsonInput(std::string *input) {
PreparedSimdjsonInput prepared;
// The production request-body path owns a mutable std::string, so we can
// pad that buffer in place and keep the logical JSON length in the
// returned padded_string_view. This removes the extra padded_string copy
// while still satisfying simdjson's padding requirement explicitly.
prepared.view = simdjson::pad(*input);
return prepared;
}
PreparedSimdjsonInput prepareConstSimdjsonInput(const std::string &input) {
PreparedSimdjsonInput prepared;
prepared.view = simdjson::padded_string_view(input);
// The const path must not guess about std::string capacity. We only parse
// directly when simdjson itself confirms that the existing allocation
// and/or trailing whitespace provide sufficient padding.
if (prepared.view.has_sufficient_padding()) {
return prepared;
}
#ifdef MSC_JSON_AUDIT_INSTRUMENTATION
const auto padded_start = std::chrono::steady_clock::now();
prepared.owned_copy = simdjson::padded_string(input);
recordSimdjsonPaddedCopy(input.size(), static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - padded_start).count()));
#else
prepared.owned_copy = simdjson::padded_string(input);
#endif
prepared.view = prepared.owned_copy;
return prepared;
}
JsonParseResult parsePreparedDocumentWithSimdjson(
simdjson::padded_string_view input, JsonEventSink *sink,
const JsonBackendParseOptions &options) {
simdjson::ondemand::parser &parser = getReusableSimdjsonParser();
// This only prepares parser capacity and max-depth bookkeeping. Buffer
// lifetime and padding must already have been handled by the caller.
if (auto error = prepareParser(&parser, input.length(), options); error) {
return fromSimdjsonError(error);
}
simdjson::ondemand::document document;
#ifdef MSC_JSON_AUDIT_INSTRUMENTATION
const auto iterate_start = std::chrono::steady_clock::now();
if (auto error = parser.iterate(input).get(document); error) {
recordSimdjsonIterate(static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - iterate_start).count()));
return fromSimdjsonError(error);
}
recordSimdjsonIterate(static_cast<std::uint64_t>(
std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::steady_clock::now() - iterate_start).count()));
#else
if (auto error = parser.iterate(input).get(document); error) {
return fromSimdjsonError(error);
}
#endif
JsonBackendWalker walker(sink, options);
return walker.walk(&document);
}
JsonParseResult validateSinkAndParsePreparedDocument(
simdjson::padded_string_view input, JsonEventSink *sink,
const JsonBackendParseOptions &options) {
if (sink == nullptr) {
return makeResult(JsonParseStatus::InternalError,
JsonSinkStatus::InternalError, "JSON event sink is null.");
}
return parsePreparedDocumentWithSimdjson(input, sink, options);
}
} // namespace
JsonParseResult parseDocumentWithSimdjson(std::string &input,
JsonEventSink *sink, const JsonBackendParseOptions &options) {
PreparedSimdjsonInput prepared = prepareMutableSimdjsonInput(&input);
return validateSinkAndParsePreparedDocument(prepared.view, sink, options);
}
JsonParseResult parseDocumentWithSimdjson(const std::string &input,
JsonEventSink *sink, const JsonBackendParseOptions &options) {
PreparedSimdjsonInput prepared = prepareConstSimdjsonInput(input);
return validateSinkAndParsePreparedDocument(prepared.view, sink, options);
}
} // namespace modsecurity::RequestBodyProcessor