Core: limit depth of received JSON to 16 (#6378)

* Core: limit depth of received JSON to 16

This applies to both MultiServer and CommonClient.

This means DataStorage default is limited to 14 levels,
DataStorage op arg is limited to 13 levels.

* Core: fix JSON depth limit check for escape codes

* Core: cythonize JSON depth limit check
This commit is contained in:
black-sliver
2026-08-10 11:03:01 +02:00
committed by GitHub
parent 3359cacedd
commit fe5b49e189
3 changed files with 125 additions and 1 deletions
+33 -1
View File
@@ -170,7 +170,39 @@ def _object_hook(o: typing.Any) -> typing.Any:
return o
decode = JSONDecoder(object_hook=_object_hook).decode
_decode = JSONDecoder(object_hook=_object_hook).decode
try:
from _speedups import check_json_depth as _check_depth
except ImportError:
def _check_depth(s: str, limit: int = 16) -> None:
depth = 1
in_quotes = False
escape = False
for c in s:
if c == "\\" and not escape:
escape = True
continue
if c == '"' and not escape:
in_quotes = not in_quotes
continue
if not in_quotes:
if c in "[{":
depth += 1
if depth > limit:
raise ValueError("JSON document too complex")
elif c in "}]":
depth -= 1
escape = False
if depth != 1: # free check
raise ValueError("JSON document malformed")
def decode(s: str) -> typing.Any:
_check_depth(s) # raises ValueError
return _decode(s)
class Endpoint: