Skip to content

Commit 97aa574

Browse files
author
MarcoFalke
committed
Squashed 'src/univalue/' changes from 5a58a46..98261b1
98261b1 Merge #22: Clamp JSON object depth to PHP limit 54c4015 Clamp JSON object depth to PHP limit git-subtree-dir: src/univalue git-subtree-split: 98261b1
1 parent fa0b3da commit 97aa574

File tree

5 files changed

+17
-0
lines changed

5 files changed

+17
-0
lines changed

Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ TEST_FILES = \
9595
$(TEST_DATA_DIR)/fail41.json \
9696
$(TEST_DATA_DIR)/fail42.json \
9797
$(TEST_DATA_DIR)/fail44.json \
98+
$(TEST_DATA_DIR)/fail45.json \
9899
$(TEST_DATA_DIR)/fail3.json \
99100
$(TEST_DATA_DIR)/fail4.json \
100101
$(TEST_DATA_DIR)/fail5.json \
@@ -105,6 +106,7 @@ TEST_FILES = \
105106
$(TEST_DATA_DIR)/pass1.json \
106107
$(TEST_DATA_DIR)/pass2.json \
107108
$(TEST_DATA_DIR)/pass3.json \
109+
$(TEST_DATA_DIR)/pass4.json \
108110
$(TEST_DATA_DIR)/round1.json \
109111
$(TEST_DATA_DIR)/round2.json \
110112
$(TEST_DATA_DIR)/round3.json \

lib/univalue_read.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
#include "univalue.h"
99
#include "univalue_utffilter.h"
1010

11+
/*
12+
* According to stackexchange, the original json test suite wanted
13+
* to limit depth to 22. Widely-deployed PHP bails at depth 512,
14+
* so we will follow PHP's lead, which should be more than sufficient
15+
* (further stackexchange comments indicate depth > 32 rarely occurs).
16+
*/
17+
static const size_t MAX_JSON_DEPTH = 512;
18+
1119
static bool json_isdigit(int ch)
1220
{
1321
return ((ch >= '0') && (ch <= '9'));
@@ -323,6 +331,9 @@ bool UniValue::read(const char *raw, size_t size)
323331
stack.push_back(newTop);
324332
}
325333

334+
if (stack.size() > MAX_JSON_DEPTH)
335+
return false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]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]

test/unitester.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ static const char *filenames[] = {
114114
"fail41.json", // invalid unicode: unfinished UTF-8
115115
"fail42.json", // valid json with garbage following a nul byte
116116
"fail44.json", // unterminated string
117+
"fail45.json", // nested beyond max depth
117118
"fail3.json",
118119
"fail4.json", // extra comma
119120
"fail5.json",
@@ -124,6 +125,7 @@ static const char *filenames[] = {
124125
"pass1.json",
125126
"pass2.json",
126127
"pass3.json",
128+
"pass4.json",
127129
"round1.json", // round-trip test
128130
"round2.json", // unicode
129131
"round3.json", // bare string

0 commit comments

Comments
 (0)