mirror of
https://github.com/ArchipelagoMW/Archipelago.git
synced 2026-08-25 11:06:49 -07:00
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:
+33
-1
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user