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(security): scope option budgets to HTTP serving

KunjShah9515ac1f481b18
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 11818be5..7e498437 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -5,6 +5,9 @@ on:
push:
branches: [main]
+permissions:
+ contents: read
+
concurrency:
group: ci-${{ github.ref }}
# Cancel outdated pull request runs. On main, let a started run finish: merges land minutes
diff --git a/laya/serve.py b/laya/serve.py
index afecb966..40ef3151 100644
--- a/laya/serve.py
+++ b/laya/serve.py
@@ -57,6 +57,10 @@ _KNOWN_MODELS = {"english", "multilingual", "typed-decisions"}
MAX_QUESTIONS = 64
MAX_STATE_CHARS = 50000
MAX_BODY_BYTES = 2 * 1024 * 1024
+# HTTP-only amplification guard; the library keeps its head_max_len-aware budget.
+MAX_CHOICE_OPTIONS = 100
+MAX_SCORE_LEVELS = 32
+MAX_TOTAL_OPTIONS = 512
# Public Hugging Face ids, accepted so a client can name a checkpoint. The root bundle is
# deliberately absent: the documented ``convaiinnovations/laya`` value means
# "let the Router choose", rather than pinning the English checkpoint.
@@ -113,6 +117,35 @@ def _check_request_limits(state: Any, questions: Any) -> None:
if len(questions) > MAX_QUESTIONS:
raise HTTPException(status_code=413,
detail="too many questions (%d > %d)" % (len(questions), MAX_QUESTIONS))
+
+ total_options = 0
+ for qid, question in questions.items():
+ if not isinstance(question, dict):
+ continue
+ crit = question.get("criteria")
+ qtype = question.get("type")
+ if qtype == "choice" and isinstance(crit, (dict, list)):
+ count = len(crit)
+ total_options += count
+ if count > MAX_CHOICE_OPTIONS:
+ raise HTTPException(
+ status_code=413,
+ detail="too many choice options for %r (%d > %d)" % (qid, count, MAX_CHOICE_OPTIONS),
+ )
+ elif qtype == "score" and isinstance(crit, list):
+ count = len(crit)
+ total_options += count
+ if count > MAX_SCORE_LEVELS:
+ raise HTTPException(
+ status_code=413,
+ detail="too many score levels for %r (%d > %d)" % (qid, count, MAX_SCORE_LEVELS),
+ )
+ if total_options > MAX_TOTAL_OPTIONS:
+ raise HTTPException(
+ status_code=413,
+ detail="too many answer options across questions (%d > %d)" % (total_options, MAX_TOTAL_OPTIONS),
+ )
+
try:
state_len = len(state) if isinstance(state, str) else len(str(state))
except Exception:
diff --git a/tests/test_audit_regressions.py b/tests/test_audit_regressions.py
index 44d79ecd..411c7072 100644
--- a/tests/test_audit_regressions.py
+++ b/tests/test_audit_regressions.py
@@ -20,6 +20,50 @@ class FakeRouter:
}
+def test_serve_rejects_large_answer_option_map_before_inference():
+ from fastapi.testclient import TestClient
+
+ class RecordingRouter(FakeRouter):
+ def __init__(self):
+ self.calls = []
+
+ def predict(self, state, questions, model=None):
+ self.calls.append((state, questions))
+ return super().predict(state, questions, model=model)
+
+ router = RecordingRouter()
+ client = TestClient(create_app(router=router))
+ response = client.post("/v1/systemone", json={
+ "state": "hello",
+ "questions": {
+ "q": {
+ "type": "choice",
+ "instructions": "pick one",
+ "criteria": {str(i): None for i in range(101)},
+ }
+ },
+ })
+ assert response.status_code == 413
+ assert router.calls == []
+
+
+def test_serve_rejects_total_answer_option_amplification():
+ from fastapi.testclient import TestClient
+
+ client = TestClient(create_app(router=FakeRouter()))
+ questions = {
+ "q%d" % i: {
+ "type": "choice",
+ "instructions": "pick one",
+ "criteria": {str(j): None for j in range(100)},
+ }
+ for i in range(6)
+ }
+ response = client.post("/v1/systemone", json={"state": "hello", "questions": questions})
+ assert response.status_code == 413
+ assert "across questions" in response.json()["detail"]
+
+
def test_serve_rejects_oversized_stream_without_content_length():
from fastapi.testclient import TestClient