From 1b2af06c925acbfae4122671251f042090e237b6 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Thu, 1 Oct 2026 18:28:29 +0200 Subject: [PATCH] fix(json2): reject documents nested deeper than 512 levels The parser descends recursively into every array and object without a limit, so deeply nested input exhausts the stack and crashes the process: 50000 levels on an 8 MB main thread and 5000 levels (10 KB of brackets) on a 256 KB thread. sce::Json::Parser::parse now returns JsonErrorParse when a document opens more than 512 nested arrays or objects, which bounds the recursion for any input. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01R5WJS4ZdQynAJesCZKH5hb --- core/libs/prx/libSceJson2/Export.cpp | 15 +++++++++++---- core/libs/tests/GuestJson.cpp | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/core/libs/prx/libSceJson2/Export.cpp b/core/libs/prx/libSceJson2/Export.cpp index d12089775..12f45c2d0 100644 --- a/core/libs/prx/libSceJson2/Export.cpp +++ b/core/libs/prx/libSceJson2/Export.cpp @@ -269,8 +269,11 @@ public: } private: + static constexpr std::size_t MaxNestingDepth = 512; + const char* _text; const char* _end; + std::size_t _depth = 0; void skip() { while (_text < _end && (*_text == ' ' || *_text == '\t' || *_text == '\n' || *_text == '\r')) ++_text; } bool literal(const char* word) { @@ -386,10 +389,12 @@ private: return true; } case '[': { + if (_depth == MaxNestingDepth) return false; ++_text; + ++_depth; SetType(out, TypeArray); skip(); - if (_text < _end && *_text == ']') { ++_text; return true; } + if (_text < _end && *_text == ']') { ++_text; --_depth; return true; } for (;;) { Value item{}; Construct(item); @@ -398,15 +403,17 @@ private: if (!value(*item.node)) return false; skip(); if (_text < _end && *_text == ',') { ++_text; continue; } - if (_text < _end && *_text == ']') { ++_text; return true; } + if (_text < _end && *_text == ']') { ++_text; --_depth; return true; } return false; } } case '{': { + if (_depth == MaxNestingDepth) return false; ++_text; + ++_depth; SetType(out, TypeObject); skip(); - if (_text < _end && *_text == '}') { ++_text; return true; } + if (_text < _end && *_text == '}') { ++_text; --_depth; return true; } for (;;) { skip(); std::string key; @@ -422,7 +429,7 @@ private: if (!value(*pair.value.node)) return false; skip(); if (_text < _end && *_text == ',') { ++_text; continue; } - if (_text < _end && *_text == '}') { ++_text; return true; } + if (_text < _end && *_text == '}') { ++_text; --_depth; return true; } return false; } } diff --git a/core/libs/tests/GuestJson.cpp b/core/libs/tests/GuestJson.cpp index 6a8d48f7d..e30b0b123 100644 --- a/core/libs/tests/GuestJson.cpp +++ b/core/libs/tests/GuestJson.cpp @@ -170,6 +170,28 @@ static void ParseAndRoundTrip() { _ZN3sce4Json5ValueD1Ev(&root); } +static std::string Nested(std::size_t depth, bool objects) { + std::string text; + for (std::size_t i = 0; i < depth; ++i) text += objects ? "{\"a\":" : "["; + text += "1"; + for (std::size_t i = 0; i < depth; ++i) text += objects ? "}" : "]"; + return text; +} + +static void NestingDepth() { + Value root{}; + _ZN3sce4Json5ValueC1Ev(&root); + for (const bool objects : {false, true}) { + const std::string accepted = Nested(512, objects); + Require(_ZN3sce4Json6Parser5parseERNS0_5ValueEPKcm(&root, accepted.c_str(), accepted.size()) == 0); + for (const std::size_t depth : {std::size_t{513}, std::size_t{100000}}) { + const std::string rejected = Nested(depth, objects); + Require(_ZN3sce4Json6Parser5parseERNS0_5ValueEPKcm(&root, rejected.c_str(), rejected.size()) < 0); + } + } + _ZN3sce4Json5ValueD1Ev(&root); +} + static void ObjectsAndArrays() { Object object{}; _ZN3sce4Json6ObjectC1Ev(&object); @@ -255,6 +277,7 @@ static void NullAccess() { int main() { ParseAndRoundTrip(); + NestingDepth(); ObjectsAndArrays(); NullAccess(); }