Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(serve): return 400 for a deeply nested JSON body

can-gaa-hou43f9c5f2ddf7
diff --git a/laya/serve.py b/laya/serve.py
index c0fc1bc3..75a1a24a 100644
--- a/laya/serve.py
+++ b/laya/serve.py
@@ -252,12 +252,12 @@ def create_app(router: Optional[Any] = None):
pass
raw = await _read_body_capped(request)
try:
- # Every parse failure a client can cause is a ValueError: JSONDecodeError for
- # malformed/empty/truncated bodies, UnicodeDecodeError for invalid UTF-8. A
- # broader catch would also swallow ClientDisconnect and Starlette's own
- # stream errors, reporting a transport or server fault as the client's.
+ # A client can cause ValueError (JSONDecodeError for malformed/empty/truncated
+ # bodies, UnicodeDecodeError for invalid UTF-8) or RecursionError (deeply nested
+ # arrays/objects). A broader catch would also swallow ClientDisconnect and
+ # Starlette's own stream errors, reporting a transport or server fault as the client's.
body = json.loads(raw)
- except ValueError:
+ except (ValueError, RecursionError):
raise HTTPException(status_code=400, detail="request body must be valid JSON")
if not isinstance(body, dict) or "questions" not in body:
raise HTTPException(status_code=400, detail="request body must be an object with a 'questions' field")
diff --git a/tests/test_serve.py b/tests/test_serve.py
index 566660bb..4175c841 100644
--- a/tests/test_serve.py
+++ b/tests/test_serve.py
@@ -97,6 +97,7 @@ def test_missing_questions_is_400(monkeypatch):
b"", # empty body
b"\xff\xfe\x00bad", # invalid UTF-8
b'{"questions": ', # truncated
+ pytest.param(b"[" * 100000, id="deeply-nested"), # raises RecursionError, not ValueError
])
def test_malformed_json_body_is_400(monkeypatch, payload):
"""A body that isn't valid JSON must not fall through to an unstyled 500."""