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): enforce MAX_BODY_BYTES when the client sends no Content-Length (#296)

keemsisi06a6c83e43bd
diff --git a/laya/serve.py b/laya/serve.py
index a99fe9d9..94e3a660 100644
--- a/laya/serve.py
+++ b/laya/serve.py
@@ -33,6 +33,7 @@ deferred into the functions that need them, so ``import laya.serve`` stays cheap
and touches no GPU -- which is what keeps the Nix ``pythonImportsCheck`` honest.
"""
import hmac
+import json
import os
from typing import Any, Dict, Optional
@@ -112,6 +113,32 @@ def _check_request_limits(state: Any, questions: Any) -> None:
detail="state too large (%d > %d chars)" % (state_len, MAX_STATE_CHARS))
+async def _read_body_capped(request: Any) -> bytes:
+ """Read the request body, refusing to buffer more than ``MAX_BODY_BYTES``.
+
+ ``Content-Length`` cannot be the only gate. It is a value the client chooses,
+ and under ``Transfer-Encoding: chunked`` it is absent altogether -- HTTP/2 and
+ HTTP/3 have no such header at all -- so a request that simply omits it was
+ read into memory in full, whatever its size. The body is streamed here and
+ abandoned as soon as it exceeds the cap, so the limit holds for every framing
+ rather than only for clients that announce their length honestly.
+ """
+ from fastapi import HTTPException
+
+ total = 0
+ chunks = []
+ async for chunk in request.stream():
+ if not chunk:
+ continue
+ total += len(chunk)
+ if total > MAX_BODY_BYTES:
+ # Stop reading rather than draining the rest: the peer is already over
+ # the limit and nothing further can make the request acceptable.
+ raise HTTPException(status_code=413, detail="request body too large")
+ chunks.append(chunk)
+ return b"".join(chunks)
+
+
def _apply_thread_limit():
"""Honour LAYA_THREADS by capping torch's intra-op thread count for CPU
inference. Returns the value applied, or None if unset/invalid. torch is
@@ -175,10 +202,20 @@ def create_app(router: Optional[Any] = None):
summary="Laya System-1 decisions over the TypeSafe Jev /v1/systemone protocol",
)
+ # Compared as bytes, not str. `hmac.compare_digest` raises TypeError when a str
+ # operand holds a non-ASCII character, and Starlette decodes request headers as
+ # latin-1 -- so `Authorization: Bearer s\xe9cret`, which is legal on the wire,
+ # made the comparison itself raise. That surfaced as HTTP 500 plus a traceback
+ # in the log, reachable by any unauthenticated client with one byte. Encoding
+ # both sides first keeps the comparison constant-time and total: every header a
+ # client can send now answers 401.
+ expected_auth = ("Bearer " + api_key).encode("utf-8", "surrogateescape") if api_key else b""
+
def _check_auth(authorization: Optional[str]) -> None:
if api_key is None:
return
- if not hmac.compare_digest(authorization or "", "Bearer " + api_key):
+ supplied = (authorization or "").encode("utf-8", "surrogateescape")
+ if not hmac.compare_digest(supplied, expected_auth):
raise HTTPException(status_code=401, detail="invalid or missing bearer token")
@app.get("/health")
@@ -193,18 +230,22 @@ def create_app(router: Optional[Any] = None):
async def systemone(request: Request, authorization: Optional[str] = Header(default=None)):
nonlocal gate
_check_auth(authorization)
+ # A declared length over the cap is rejected before anything is read; the
+ # streaming cap below is what actually enforces it, for bodies that declare
+ # no length or understate it.
if request.headers.get("content-length"):
try:
if int(request.headers["content-length"]) > MAX_BODY_BYTES:
raise HTTPException(status_code=413, detail="request body too large")
except ValueError:
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.
- body = await request.json()
+ body = json.loads(raw)
except ValueError:
raise HTTPException(status_code=400, detail="request body must be valid JSON")
if not isinstance(body, dict) or "questions" not in body:
diff --git a/tests/test_serve.py b/tests/test_serve.py
index 66db5c43..566660bb 100644
--- a/tests/test_serve.py
+++ b/tests/test_serve.py
@@ -3,12 +3,20 @@
A fake Router is injected so nothing loads a checkpoint; we only assert that the
HTTP layer maps requests/responses and enforces auth as hs-jev expects.
"""
+import json
+
import pytest
fastapi = pytest.importorskip("fastapi")
from fastapi.testclient import TestClient # noqa: E402
-from laya.serve import _apply_thread_limit, _env_bool, _resolve_model, create_app # noqa: E402
+from laya.serve import ( # noqa: E402
+ MAX_BODY_BYTES,
+ _apply_thread_limit,
+ _env_bool,
+ _resolve_model,
+ create_app,
+)
class FakeRouter:
@@ -118,6 +126,79 @@ def test_auth_required_when_key_set(monkeypatch):
assert ok.status_code == 200
+def test_auth_rejects_a_non_ascii_header(monkeypatch):
+ """A hostile Authorization header must answer 401, not raise.
+
+ `hmac.compare_digest` raises TypeError when a str operand holds a non-ASCII
+ character, and Starlette decodes request headers as latin-1. So
+ `Authorization: Bearer s\xe9cret` -- legal on the wire -- used to make the
+ comparison itself raise, which FastAPI turned into HTTP 500 with a traceback
+ in the log, reachable by any unauthenticated client.
+ """
+ client, _ = _client(monkeypatch, api_key="s3cret")
+ for header in (
+ "Bearer s\u00e9cret".encode("latin-1"), # non-ASCII inside the token
+ "B\u00ebarer s3cret".encode("latin-1"), # non-ASCII in the scheme
+ b"Bearer \xff\xfe", # bytes that are not valid UTF-8
+ ):
+ r = client.post("/v1/systemone", json=REQ, headers={"Authorization": header})
+ assert r.status_code == 401, (header, r.status_code)
+
+
+def _chunked(payload: bytes):
+ """Send `payload` with no Content-Length, i.e. Transfer-Encoding: chunked."""
+ yield payload
+
+
+def test_body_limit_holds_without_content_length(monkeypatch):
+ """The body cap must not depend on the client declaring its length.
+
+ Content-Length is a value the client chooses and chunked transfer-encoding
+ omits it entirely (HTTP/2 and /3 have no such header), so checking only the
+ header let a request of any size be read into memory in full. The state and
+ question-count guards do not cover this: state stays tiny and there is one
+ question -- the payload is large because the question's own text is.
+ """
+ client, fake = _client(monkeypatch)
+ oversized = json.dumps({
+ "state": "ok",
+ "questions": {"a": {"type": "choice",
+ "instructions": "A" * (MAX_BODY_BYTES + 1024),
+ "criteria": {"y": None, "z": None}}},
+ }).encode()
+ assert len(oversized) > MAX_BODY_BYTES
+
+ declared = client.post("/v1/systemone", content=oversized,
+ headers={"content-type": "application/json"})
+ assert declared.status_code == 413
+
+ undeclared = client.post("/v1/systemone", content=_chunked(oversized),
+ headers={"content-type": "application/json"})
+ assert undeclared.status_code == 413
+ # And it was refused before reaching inference, which is the point: the pool
+ # is one worker wide, so a body that gets that far blocks every other client.
+ assert fake.calls == []
+
+
+def test_a_request_within_the_limit_still_works_without_content_length(monkeypatch):
+ """The cap must not break legitimate chunked clients."""
+ client, fake = _client(monkeypatch)
+ body = json.dumps(REQ).encode()
+ r = client.post("/v1/systemone", content=_chunked(body),
+ headers={"content-type": "application/json"})
+ assert r.status_code == 200
+ assert len(fake.calls) == 1
+
+
+def test_body_read_preserves_parse_error_codes(monkeypatch):
+ """Reading the body ourselves must keep 400 for anything unparseable."""
+ client, _ = _client(monkeypatch)
+ for payload in (b"", b"{not json", b'{"questions":{},"state":"\xff\xfe"}'):
+ r = client.post("/v1/systemone", content=payload,
+ headers={"content-type": "application/json"})
+ assert r.status_code == 400, (payload, r.status_code)
+
+
def test_health(monkeypatch):
client, _ = _client(monkeypatch)
r = client.get("/health")