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): serve no docs or schema when a bearer is required

thread3df8ce27bf6896
diff --git a/docs/http-api.md b/docs/http-api.md
index c9d805b8..f326fdb3 100644
--- a/docs/http-api.md
+++ b/docs/http-api.md
@@ -42,7 +42,7 @@ Everything is environment variables, so one image serves a laptop dev run and a
| `LAYA_MAX_TOKEN_BUDGET` | server-side ceiling on the per-request `max_len` and `head_max_len` overrides; a larger value is a `422`. Unparseable or non-positive input logs a warning and falls back | `8192` |
| `LAYA_DEFAULT_MODEL` | checkpoint a state with no language evidence falls back to; aliases such as `ml` resolve the way core resolves them, and an unresolvable name stops the server at startup | `multilingual` |
| `LAYA_EXTRA_MODELS` | JSON object `{name: source}` registering extra checkpoints beside the bundled ones: a Hub repo id or local checkpoint directory as a string, or a `["repo", "subfolder"]` pair. Names get the same `model=` pin a built-in does; a malformed value or bad name stops the server at startup | none |
-| `LAYA_API_KEY` | if set, require `Authorization: Bearer <key>` | none |
+| `LAYA_API_KEY` | if set, require `Authorization: Bearer <key>` on every route, and stop serving `/docs`, `/redoc` and `/openapi.json` (FastAPI's own routes, which per-route auth never covered) | none |
| `LAYA_LOG_LEVEL` | uvicorn log level | `info` |
| `LAYA_MAX_CONCURRENT` | requests admitted past auth at once; excess gets `503` | `16` |
| `LAYA_MAX_BATCH_TOKENS` | tokens one `/v1/systemone/batch` FORWARD PASS may collate (`states` x questions x row width); a larger batch is split into several passes, not refused | `131072` |
diff --git a/laya/serve.py b/laya/serve.py
index 788b1f4b..aab99132 100644
--- a/laya/serve.py
+++ b/laya/serve.py
@@ -37,6 +37,8 @@ env var meaning defaul
``LAYA_IDLE_UNLOAD_SECONDS`` unload checkpoints after this many idle 0 (off)
seconds; the next request loads them again.
``LAYA_API_KEY`` if set, require ``Authorization: Bearer <it>`` (none)
+ on every route, and do not serve the
+ interactive docs or the OpenAPI schema
``LAYA_ROOT_PATH`` public URL prefix behind a reverse proxy (none)
``LAYA_LOG_LEVEL`` uvicorn log level info
``LAYA_MAX_CONCURRENT`` cap on requests past auth at once; excess 16
@@ -1235,11 +1237,20 @@ def create_app(router: Optional[Any] = None):
pool.shutdown(wait=True, cancel_futures=True)
pool_closed = True
+ # Auth is enforced per route below, and FastAPI's own documentation routes are not written by
+ # this module -- on a deployment with LAYA_API_KEY set they answered 200 to an unauthenticated
+ # caller, publishing the whole API shape and an interactive console next to a service the
+ # operator had locked down. When a key is configured there is no way to authenticate them
+ # without adding a second auth path, so they are not served at all; run the server without a
+ # key (or read docs/http-api.md) when you need the schema.
app = FastAPI(
title="laya-serve",
summary="Laya System-1 decisions over the TypeSafe Jev /v1/systemone protocol",
root_path=os.environ.get("LAYA_ROOT_PATH", ""),
lifespan=lifespan,
+ docs_url=None if api_key else "/docs",
+ redoc_url=None if api_key else "/redoc",
+ openapi_url=None if api_key else "/openapi.json",
)
# Compared as bytes, not str. `hmac.compare_digest` raises TypeError when a str
diff --git a/tests/test_serve.py b/tests/test_serve.py
index 6ba279bf..304ddee9 100644
--- a/tests/test_serve.py
+++ b/tests/test_serve.py
@@ -110,6 +110,23 @@ def test_root_path_from_environment_updates_openapi_and_keeps_routes(monkeypatch
assert client.get("/openapi.json").json()["servers"] == [{"url": "/laya"}]
[email protected]("path", ["/openapi.json", "/docs", "/redoc"])
+def test_no_docs_or_schema_on_a_locked_down_deployment(monkeypatch, path):
+ """FastAPI's own documentation routes are not written by this module, so per-route auth never
+ covered them: with LAYA_API_KEY set they answered 200 to an unauthenticated caller, publishing
+ the API shape and an interactive console beside a service the operator had locked down."""
+ client, _ = _client(monkeypatch, api_key="s3cret")
+ assert client.get(path).status_code == 404
+ # The decision routes still refuse without the bearer, so the app itself is up.
+ assert client.post("/v1/systemone", json=REQ).status_code == 401
+
+
+def test_docs_and_schema_are_served_without_a_key(monkeypatch):
+ client, _ = _client(monkeypatch)
+ assert client.get("/openapi.json").status_code == 200
+ assert client.get("/docs").status_code == 200
+
+
@pytest.mark.parametrize("root_path", [None, ""])
def test_root_path_defaults_to_empty(monkeypatch, root_path):
if root_path is None: