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): a lone surrogate in the body was a 500 instead of a 400

PerryLink06462bf52b06
diff --git a/laya/serve.py b/laya/serve.py
index 2cd30922..7637dcb3 100644
--- a/laya/serve.py
+++ b/laya/serve.py
@@ -38,6 +38,7 @@ import hmac
import json
import logging
import os
+import re
import time
from contextlib import asynccontextmanager
from typing import Any, Dict, Optional
@@ -182,6 +183,38 @@ def _check_request_limits(state: Any, questions: Any) -> None:
detail="state too large (%d > %d chars)" % (state_len, MAX_STATE_CHARS))
+# One surrogate code point in a Python string. Compiled once: a nested body can hold many
+# strings, and `re.search` scans each at C speed, where the per-character Python loop this
+# replaced cost ~170 ms on a near-cap body on the event loop.
+_LONE_SURROGATE = re.compile("[\ud800-\udfff]")
+
+
+def _has_lone_surrogate(value: Any) -> bool:
+ """True if any string in the parsed body contains an unpaired surrogate code point.
+
+ `json.loads` accepts a `\\udXXX` escape and builds a `str` holding that code point, which
+ cannot be encoded to UTF-8. A *pair* of escapes is combined into one ordinary astral
+ character by the decoder, so it never appears here and an emoji still works.
+
+ Walks with an explicit stack rather than recursing: `json.loads` accepts nesting far deeper
+ than Python's default recursion limit, so a recursive walk turned a body the parser handles
+ into a `RecursionError` -- one 500 replaced by another. The size checks run first, so the
+ strings reached here are bounded by `MAX_STATE_CHARS` and `MAX_QUESTIONS`.
+ """
+ stack = [value]
+ while stack:
+ item = stack.pop()
+ if isinstance(item, str):
+ if _LONE_SURROGATE.search(item):
+ return True
+ elif isinstance(item, dict):
+ stack.extend(item.keys())
+ stack.extend(item.values())
+ elif isinstance(item, (list, tuple)):
+ stack.extend(item)
+ return False
+
+
async def _read_body_capped(request: Any) -> bytes:
"""Read the request body, refusing to buffer more than ``MAX_BODY_BYTES``.
@@ -354,6 +387,16 @@ def create_app(router: Optional[Any] = None):
state = body.get("state")
questions = body["questions"]
_check_request_limits(state, questions)
+ # After the size checks, so an oversized body is refused before anything walks it, and
+ # `MAX_STATE_CHARS`/`MAX_QUESTIONS` bound what the walk can reach. A `\udXXX` escape with
+ # no pair is legal JSON that cannot be UTF-8 encoded, so the tokenizer raised `TypeError`
+ # from inside `build_sequence` and the client's own mistake came back as a 500
+ # "inference failed". A *paired* surrogate is an ordinary astral character (an emoji) by
+ # the time `json.loads` is done, so only lone ones are rejected here.
+ if _has_lone_surrogate(body):
+ raise HTTPException(status_code=400,
+ detail="request body contains an unpaired surrogate escape; "
+ "those cannot be encoded as UTF-8")
model = _resolve_model(body.get("model"))
if gate is None:
gate = asyncio.Lock()
diff --git a/tests/test_serve.py b/tests/test_serve.py
index ae52aae2..b3f42180 100644
--- a/tests/test_serve.py
+++ b/tests/test_serve.py
@@ -529,3 +529,68 @@ def test_admission_slot_is_released_after_inference(monkeypatch):
client = TestClient(create_app(router=FakeRouter()))
assert client.post("/v1/systemone", json=REQ).status_code == 200
assert client.post("/v1/systemone", json=REQ).status_code == 200
+
+
+def test_an_unpaired_surrogate_is_a_caller_error_not_a_server_fault():
+ r"""A `\udXXX` escape with no pair parses as JSON and then cannot be UTF-8 encoded.
+
+ The tokenizer raised `TypeError` from inside `build_sequence`, and `serve` maps only
+ `ValueError` to 422, so a malformed string in the client's own body came back as a 500
+ "inference failed" -- a server fault plus an operator traceback for the caller's mistake.
+ A *paired* surrogate is an ordinary astral character (an emoji) and must keep working.
+ """
+ seen = []
+
+ class RecordingRouter:
+ loaded = ["english"]
+
+ def predict(self, state, questions, model=None):
+ seen.append(state)
+ return {"model": "stub", "answers": {}, "usage": {}}
+
+ client = TestClient(create_app(router=RecordingRouter()), raise_server_exceptions=False)
+
+ lone = [
+ b'{"state":"\\ud800","questions":{"q":{"type":"noul","instructions":"x"}}}',
+ b'{"state":"ok","questions":{"q":{"type":"noul","instructions":"\\udfff"}}}',
+ b'{"state":"ok","questions":{"q":{"type":"choice","instructions":"x",'
+ b'"criteria":["\\ud800","b"]}}}',
+ ]
+ for body in lone:
+ res = client.post("/v1/systemone", content=body,
+ headers={"content-type": "application/json"})
+ assert res.status_code == 400, (body, res.status_code, res.text)
+ assert "unpaired surrogate" in res.text, res.text
+
+ # a paired surrogate is one astral character by the time json.loads is done: it must reach
+ # the router rather than be rejected
+ seen.clear()
+ ok = json.dumps({"state": "hi \U0001f600",
+ "questions": {"q": {"type": "noul", "instructions": "x"}}}).encode("utf-8")
+ res = client.post("/v1/systemone", content=ok,
+ headers={"content-type": "application/json"})
+ assert res.status_code == 200, (res.status_code, res.text)
+ assert seen and "\U0001f600" in str(seen[0]), seen
+
+
+def test_a_deeply_nested_state_is_not_a_recursion_error():
+ """`json.loads` accepts nesting far deeper than Python's recursion limit.
+
+ A recursive walk over the parsed body therefore turned a body the parser handles into a
+ `RecursionError`, i.e. one 500 replaced by another. The walk uses an explicit stack, so the
+ depth the JSON parser accepts is the depth this handles.
+ """
+ class NestedRouter:
+ loaded = ["english"]
+
+ def predict(self, state, questions, model=None):
+ return {"model": "stub", "answers": {}, "usage": {}}
+
+ client = TestClient(create_app(router=NestedRouter()), raise_server_exceptions=False)
+ questions = {"q": {"type": "noul", "instructions": "x"}}
+
+ for depth in (200, 600, 1200):
+ body = json.dumps({"state": ["x" * 3] * depth, "questions": questions}).encode("utf-8")
+ res = client.post("/v1/systemone", content=body,
+ headers={"content-type": "application/json"})
+ assert res.status_code == 200, (depth, res.status_code, res.text)