Skip to content

Open-source AI coworkers that each get a computer of their own: a browser, files and tools, with every action decided before it happens and recorded after. Bring any AG-UI agent.

harden: constant-time comparison for the shared agent token, across all Bots (CWE-208) (#670)

Anupam Mediratta15c75b88e30e
diff --git a/agent-adk/src/main.py b/agent-adk/src/main.py
index eeca9152..7541a3fd 100644
--- a/agent-adk/src/main.py
+++ b/agent-adk/src/main.py
@@ -4,6 +4,7 @@ ADK is Gemini-first and model-agnostic after that, so the provider stays the per
reads LiteLLM model strings, and OpenBot writes the one it was told.
"""
+import hmac
import os
import sys
from pathlib import Path
@@ -74,7 +75,7 @@ async def refuse_without_the_server_token(request: Request, call_next):
if request.url.path != "/health":
expected = (os.environ.get("MANAGED_AGENT_TOKEN") or "").strip()
offered = (request.headers.get(TOKEN_HEADER) or "").strip()
- if not expected or offered != expected:
+ if not expected or not hmac.compare_digest(offered.encode("utf-8"), expected.encode("utf-8")):
return JSONResponse({"error": "unauthorised"}, status_code=401)
return await call_next(request)
diff --git a/agent-ag2/src/main.py b/agent-ag2/src/main.py
index 0dcf1eb2..455dcbf1 100644
--- a/agent-ag2/src/main.py
+++ b/agent-ag2/src/main.py
@@ -1,5 +1,6 @@
"""AG2 as a Bot. AG-UI is an extra in AG2's own package, so `ag2[ag-ui]` is the dependency."""
+import hmac
import os
import sys
from pathlib import Path
@@ -52,7 +53,7 @@ async def refuse_without_the_server_token(request: Request, call_next):
if request.url.path != "/health":
expected = (os.environ.get("MANAGED_AGENT_TOKEN") or "").strip()
offered = (request.headers.get(TOKEN_HEADER) or "").strip()
- if not expected or offered != expected:
+ if not expected or not hmac.compare_digest(offered.encode("utf-8"), expected.encode("utf-8")):
return JSONResponse({"error": "unauthorised"}, status_code=401)
return await call_next(request)
diff --git a/agent-agno/src/main.py b/agent-agno/src/main.py
index 3a545b04..cb4571b9 100644
--- a/agent-agno/src/main.py
+++ b/agent-agno/src/main.py
@@ -5,6 +5,7 @@ is the whole dependency and `AGUIApp` is the whole integration. Nothing of the p
here, which is the rule.
"""
+import hmac
import os
import sys
from pathlib import Path
@@ -70,7 +71,7 @@ async def refuse_without_the_server_token(request: Request, call_next):
expected = (os.environ.get("MANAGED_AGENT_TOKEN") or "").strip()
offered = (request.headers.get(TOKEN_HEADER) or "").strip()
# Unset means unconfigured, not open.
- if not expected or offered != expected:
+ if not expected or not hmac.compare_digest(offered.encode("utf-8"), expected.encode("utf-8")):
return JSONResponse({"error": "unauthorised"}, status_code=401)
return await call_next(request)
diff --git a/agent-claude-sdk/requirements-test.txt b/agent-claude-sdk/requirements-test.txt
index 5b240e09..a91d60c6 100644
--- a/agent-claude-sdk/requirements-test.txt
+++ b/agent-claude-sdk/requirements-test.txt
@@ -1 +1,2 @@
+httpx==0.28.1
pytest==9.0.2
diff --git a/agent-claude-sdk/src/main.py b/agent-claude-sdk/src/main.py
index bc75bf04..dced048a 100644
--- a/agent-claude-sdk/src/main.py
+++ b/agent-claude-sdk/src/main.py
@@ -8,6 +8,7 @@ The precedence trap is the thing to get right: `ANTHROPIC_API_KEY` wins over the
deployment that sets both silently bills the key and the plan goes unused. OpenBot sets one.
"""
+import hmac
import os
from ag_ui_claude_sdk import add_claude_fastapi_endpoint
@@ -50,7 +51,7 @@ async def refuse_without_the_server_token(request: Request, call_next):
if request.url.path != "/health":
expected = (os.environ.get("MANAGED_AGENT_TOKEN") or "").strip()
offered = (request.headers.get(TOKEN_HEADER) or "").strip()
- if not expected or offered != expected:
+ if not expected or not hmac.compare_digest(offered.encode("utf-8"), expected.encode("utf-8")):
return JSONResponse({"error": "unauthorised"}, status_code=401)
return await call_next(request)
diff --git a/agent-claude-sdk/tests/test_auth_middleware.py b/agent-claude-sdk/tests/test_auth_middleware.py
new file mode 100644
index 00000000..35a82ae1
--- /dev/null
+++ b/agent-claude-sdk/tests/test_auth_middleware.py
@@ -0,0 +1,75 @@
+"""The token check on a route the middleware actually guards.
+
+`/health` is exempt on purpose, so a test that sends tokens there cannot tell a correct
+comparison from a broken one — every case answers 200 regardless of the token. `/model/cancel`
+is protected, and calling it with a `threadId` nothing has ever seen is a safe way to reach a real
+route handler without a Claude subprocess: `OpenBotClaudeAgentAdapter.interrupt()` no-ops when the
+thread isn't tracked.
+
+The non-ASCII case is the one that distinguishes `hmac.compare_digest` on `str` (raises,
+answering 500) from `hmac.compare_digest` on UTF-8 bytes (returns `False`, answering 401) — a
+header byte outside ASCII decodes to a non-ASCII Python `str` the same way a byte from the wire
+would, once Starlette applies its latin-1 header decoding.
+"""
+
+import asyncio
+import importlib
+import sys
+from pathlib import Path
+
+import httpx
+import pytest
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+
+TOKEN = "valid-test-token"
+
+
+def app(monkeypatch):
+ monkeypatch.setenv("ANTHROPIC_API_KEY", "synthetic-test-key")
+ monkeypatch.delenv("CLAUDE_CODE_OAUTH_TOKEN", raising=False)
+ monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN)
+ from src import main
+
+ return importlib.reload(main).app
+
+
+def _post(app, path, headers):
+ async def send():
+ async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://harness") as client:
+ return await client.post(path, headers=headers, json={"threadId": "never-seen-thread"})
+
+ return asyncio.run(send())
+
+
+def test_the_correct_token_reaches_the_route(app):
+ response = _post(app, "/model/cancel", {"x-openbot-agent-token": TOKEN})
+ assert response.status_code == 200
+ assert response.json() == {"ok": True}
+
+
+def test_a_wrong_token_is_rejected(app):
+ response = _post(app, "/model/cancel", {"x-openbot-agent-token": "wrong-token"})
+ assert response.status_code == 401
+
+
+def test_a_missing_token_is_rejected(app):
+ response = _post(app, "/model/cancel", {})
+ assert response.status_code == 401
+
+
+def test_a_non_ascii_token_is_rejected_not_a_server_error(app):
+ """`hmac.compare_digest` raises on a non-ASCII `str`; comparing bytes never does."""
+ response = _post(app, "/model/cancel", {"x-openbot-agent-token": b"\xe9\xe9\xe9\xe9"})
+ assert response.status_code == 401
+
+
[email protected]("headers", [{}, {"x-openbot-agent-token": "wrong-token"}])
+def test_health_stays_exempt_regardless_of_token(app, headers):
+ async def send():
+ async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://harness") as client:
+ return await client.get("/health", headers=headers)
+
+ response = asyncio.run(send())
+ assert response.status_code == 200
diff --git a/agent-crewai/src/main.py b/agent-crewai/src/main.py
index 1e31ff12..d3f47aab 100644
--- a/agent-crewai/src/main.py
+++ b/agent-crewai/src/main.py
@@ -9,6 +9,7 @@ The contract with the rest of OpenBot is the same one the other Bots meet, and i
serve AG-UI on a port, answer `/health`, and refuse anybody who does not carry the server's token.
"""
+import hmac
import os
import sys
from collections.abc import Mapping
@@ -191,7 +192,7 @@ async def refuse_without_the_server_token(request: Request, call_next):
offered = (request.headers.get(TOKEN_HEADER) or "").strip()
# An unset token means unconfigured, not open. A Bot that answers anybody because nobody
# set a secret is the failure this check exists for.
- if not expected or offered != expected:
+ if not expected or not hmac.compare_digest(offered.encode("utf-8"), expected.encode("utf-8")):
return JSONResponse({"error": "unauthorised"}, status_code=401)
return await call_next(request)
diff --git a/agent-langgraph-agui/src/main.py b/agent-langgraph-agui/src/main.py
index 349d46f7..d51915fa 100644
--- a/agent-langgraph-agui/src/main.py
+++ b/agent-langgraph-agui/src/main.py
@@ -5,6 +5,7 @@ against writing adapters. This is the same framework served through `ag-ui-langg
package the AG-UI project maintains, so the protocol stops being ours to keep working.
"""
+import hmac
import os
import sys
from pathlib import Path
@@ -241,7 +242,7 @@ async def refuse_without_the_server_token(request: Request, call_next):
if request.url.path != "/health":
expected = (os.environ.get("MANAGED_AGENT_TOKEN") or "").strip()
offered = (request.headers.get(TOKEN_HEADER) or "").strip()
- if not expected or offered != expected:
+ if not expected or not hmac.compare_digest(offered.encode("utf-8"), expected.encode("utf-8")):
return JSONResponse({"error": "unauthorised"}, status_code=401)
return await call_next(request)
diff --git a/agent-langroid/src/main.py b/agent-langroid/src/main.py
index 7dafb4ed..dd2c2215 100644
--- a/agent-langroid/src/main.py
+++ b/agent-langroid/src/main.py
@@ -1,5 +1,6 @@
"""Langroid as a Bot, through `ag-ui-langroid`, which AG-UI maintains."""
+import hmac
import os
import sys
from pathlib import Path
@@ -59,7 +60,7 @@ async def refuse_without_the_server_token(request: Request, call_next):
if request.url.path != "/health":
expected = (os.environ.get("MANAGED_AGENT_TOKEN") or "").strip()
offered = (request.headers.get(TOKEN_HEADER) or "").strip()
- if not expected or offered != expected:
+ if not expected or not hmac.compare_digest(offered.encode("utf-8"), expected.encode("utf-8")):
return JSONResponse({"error": "unauthorised"}, status_code=401)
return await call_next(request)
diff --git a/agent-llamaindex/src/main.py b/agent-llamaindex/src/main.py
index 12478fc3..48b81e51 100644
--- a/agent-llamaindex/src/main.py
+++ b/agent-llamaindex/src/main.py
@@ -4,6 +4,7 @@ The AG-UI support is LlamaIndex's own package, `llama-index-protocols-ag-ui`, an
FastAPI router. Mount it and stop.
"""
+import hmac
import os
import sys
from pathlib import Path
@@ -63,7 +64,7 @@ async def refuse_without_the_server_token(request: Request, call_next):
if request.url.path != "/health":
expected = (os.environ.get("MANAGED_AGENT_TOKEN") or "").strip()
offered = (request.headers.get(TOKEN_HEADER) or "").strip()
- if not expected or offered != expected:
+ if not expected or not hmac.compare_digest(offered.encode("utf-8"), expected.encode("utf-8")):
return JSONResponse({"error": "unauthorised"}, status_code=401)
return await call_next(request)
diff --git a/agent-microsoft/src/main.py b/agent-microsoft/src/main.py
index 9f387050..5fd4a3e1 100644
--- a/agent-microsoft/src/main.py
+++ b/agent-microsoft/src/main.py
@@ -1,5 +1,6 @@
"""Microsoft Agent Framework as a Bot, through `agent-framework-ag-ui`, which Microsoft publishes."""
+import hmac
import os
import sys
from pathlib import Path
@@ -49,7 +50,7 @@ async def refuse_without_the_server_token(request: Request, call_next):
if request.url.path != "/health":
expected = (os.environ.get("MANAGED_AGENT_TOKEN") or "").strip()
offered = (request.headers.get(TOKEN_HEADER) or "").strip()
- if not expected or offered != expected:
+ if not expected or not hmac.compare_digest(offered.encode("utf-8"), expected.encode("utf-8")):
return JSONResponse({"error": "unauthorised"}, status_code=401)
return await call_next(request)
diff --git a/agent-pydantic-ai/src/main.py b/agent-pydantic-ai/src/main.py
index b83c93ea..c8743daa 100644
--- a/agent-pydantic-ai/src/main.py
+++ b/agent-pydantic-ai/src/main.py
@@ -4,6 +4,7 @@ AG-UI is built into Pydantic AI itself, as the `ag-ui` extra on `pydantic-ai-sli
carries its own ASGI app and there is nothing to bridge.
"""
+import hmac
import os
import sys
from pathlib import Path
@@ -63,7 +64,7 @@ async def refuse_without_the_server_token(request: Request, call_next):
if request.url.path != "/health":
expected = (os.environ.get("MANAGED_AGENT_TOKEN") or "").strip()
offered = (request.headers.get(TOKEN_HEADER) or "").strip()
- if not expected or offered != expected:
+ if not expected or not hmac.compare_digest(offered.encode("utf-8"), expected.encode("utf-8")):
return JSONResponse({"error": "unauthorised"}, status_code=401)
return await call_next(request)
diff --git a/agent-strands/src/main.py b/agent-strands/src/main.py
index d1d2f9eb..1e68149e 100644
--- a/agent-strands/src/main.py
+++ b/agent-strands/src/main.py
@@ -1,5 +1,6 @@
"""AWS Strands as a Bot, through `ag_ui_strands`, which AG-UI maintains."""
+import hmac
import os
import sys
from pathlib import Path
@@ -44,7 +45,7 @@ async def refuse_without_the_server_token(request: Request, call_next):
if request.url.path != "/health":
expected = (os.environ.get("MANAGED_AGENT_TOKEN") or "").strip()
offered = (request.headers.get(TOKEN_HEADER) or "").strip()
- if not expected or offered != expected:
+ if not expected or not hmac.compare_digest(offered.encode("utf-8"), expected.encode("utf-8")):
return JSONResponse({"error": "unauthorised"}, status_code=401)
return await call_next(request)