Skip to content

Commit 12ffe74

Browse files
rouaultgithub-actions[bot]
authored andcommitted
Limit recursion depth in json::parse() to avoid stack call overflow on hostile data
Fixes #4762
1 parent 7570871 commit 12ffe74

4 files changed

Lines changed: 21 additions & 4 deletions

File tree

src/apps/projsync.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,12 @@ int main(int argc, char *argv[]) {
310310
}
311311

312312
try {
313-
const auto j = json::parse(text);
313+
const auto j =
314+
json::parse(text, [](int depth, json::parse_event_t, json &) {
315+
if (depth >= 128)
316+
throw ParsingException("Too deep nesting in JSON content");
317+
return true;
318+
});
314319
bool foundMatchSourceIdCriterion = false;
315320
std::set<std::string> source_ids;
316321
bool foundMatchAreaOfUseCriterion = false;

src/iso19111/io.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7717,7 +7717,11 @@ static BaseObjectNNPtr createFromUserInput(const std::string &text,
77177717
if (!text.empty() && text[0] == '{') {
77187718
json j;
77197719
try {
7720-
j = json::parse(text);
7720+
j = json::parse(text, [](int depth, json::parse_event_t, json &) {
7721+
if (depth >= 128)
7722+
throw ParsingException("Too deep nesting in JSON content");
7723+
return true;
7724+
});
77217725
} catch (const std::exception &e) {
77227726
throw ParsingException(e.what());
77237727
}

src/transformations/defmodel_impl.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,11 @@ std::unique_ptr<MasterFile> MasterFile::parse(const std::string &text) {
348348
std::unique_ptr<MasterFile> dmmf(new MasterFile());
349349
json j;
350350
try {
351-
j = json::parse(text);
351+
j = json::parse(text, [](int depth, json::parse_event_t, json &) {
352+
if (depth >= 128)
353+
throw ParsingException("Too deep nesting in JSON content");
354+
return true;
355+
});
352356
} catch (const std::exception &e) {
353357
throw ParsingException(e.what());
354358
}

src/transformations/tinshift_impl.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ std::unique_ptr<TINShiftFile> TINShiftFile::parse(const std::string &text) {
8080
std::unique_ptr<TINShiftFile> tinshiftFile(new TINShiftFile());
8181
json j;
8282
try {
83-
j = json::parse(text);
83+
j = json::parse(text, [](int depth, json::parse_event_t, json &) {
84+
if (depth >= 128)
85+
throw ParsingException("Too deep nesting in JSON content");
86+
return true;
87+
});
8488
} catch (const std::exception &e) {
8589
throw ParsingException(e.what());
8690
}

0 commit comments

Comments
 (0)