From 0037c32270a290589a42635b61a7810d889543fc Mon Sep 17 00:00:00 2001 From: Namraa Patel Date: Thu, 29 Oct 2026 10:55:30 +0530 Subject: [PATCH] Fix DevUI authentication for non-ASCII bearer tokens --- .../devui/agent_framework_devui/_server.py | 20 ++++++++++- .../packages/devui/tests/devui/test_server.py | 36 +++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/python/packages/devui/agent_framework_devui/_server.py b/python/packages/devui/agent_framework_devui/_server.py index 30d8ab48318..486d20f54de 100644 --- a/python/packages/devui/agent_framework_devui/_server.py +++ b/python/packages/devui/agent_framework_devui/_server.py @@ -446,7 +446,25 @@ async def auth_middleware(request: Request, call_next: Callable[[Request], Await ) token = auth_header.replace("Bearer ", "", 1).strip() - if not secrets.compare_digest(token, expected_token): + # Starlette decodes header bytes as Latin-1. Encode the presented token + # back to bytes so compare_digest() can compare the original header byte + # values without the non-ASCII string restriction. + try: + token_bytes = token.encode("latin-1") + expected_token_bytes = expected_token.encode("latin-1") + except UnicodeEncodeError: + return JSONResponse( + status_code=401, + content={ + "error": { + "message": "Invalid authentication token", + "type": "authentication_error", + "code": "invalid_token", + } + }, + ) + + if not secrets.compare_digest(token_bytes, expected_token_bytes): return JSONResponse( status_code=401, content={ diff --git a/python/packages/devui/tests/devui/test_server.py b/python/packages/devui/tests/devui/test_server.py index 3c85085001a..1bcc2c339a7 100644 --- a/python/packages/devui/tests/devui/test_server.py +++ b/python/packages/devui/tests/devui/test_server.py @@ -918,6 +918,42 @@ def test_meta_endpoint_requires_auth(monkeypatch): assert ok.status_code == 200 +def test_non_ascii_presented_token_returns_401(): + """Non-ASCII presented token with ASCII configured token returns 401, not 500.""" + server = DevServer(auth_token="secret") + app = server.get_app() + + with TestClient(app, base_url="http://127.0.0.1") as client: + response = client.get("/v1/entities", headers={"Authorization": b"Bearer s\xe9cret"}) + assert response.status_code == 401 + assert response.json()["error"]["code"] == "invalid_token" + + +def test_non_latin1_configured_token_does_not_crash(): + """A configured token that cannot be represented in Latin-1 returns 401.""" + server = DevServer(auth_token="secret\u20ac") # € is outside Latin-1 + app = server.get_app() + + with TestClient(app, base_url="http://127.0.0.1") as client: + response = client.get("/v1/entities", headers={"Authorization": b"Bearer secret"}) + assert response.status_code == 401 + assert response.json()["error"]["code"] == "invalid_token" + + +def test_ascii_tokens_still_work(): + """ASCII tokens continue to work correctly after the non-ASCII fix.""" + server = DevServer(auth_token="secret") + app = server.get_app() + + with TestClient(app, base_url="http://127.0.0.1") as client: + response = client.get("/v1/entities", headers={"Authorization": "Bearer secret"}) + assert response.status_code == 200 + + response = client.get("/v1/entities", headers={"Authorization": "Bearer wrong"}) + assert response.status_code == 401 + assert response.json()["error"]["code"] == "invalid_token" + + def test_loopback_bind_rejects_non_allowlisted_host_header(monkeypatch): """A loopback-bound server must reject requests with a non-loopback Host header.