Skip to content

armgit / sie

Public Imported from GitHub
0

Open-source inference server and production cluster for all the models your agent needs.

Accept a case-insensitive Bearer prefix on the config API. (#250)

Gyanu Mayankd0fa3cede0b5
diff --git a/packages/sie_config/src/sie_config/config_api.py b/packages/sie_config/src/sie_config/config_api.py
index 1dec84c63..0f78d05f5 100644
--- a/packages/sie_config/src/sie_config/config_api.py
+++ b/packages/sie_config/src/sie_config/config_api.py
@@ -305,7 +305,7 @@ def _emit_audit_log(
body_bytes: int | None = None,
) -> None:
"""Emit a structured audit log entry for config API operations."""
- token = request.headers.get("Authorization", "").removeprefix("Bearer ").strip()
+ token = _extract_bearer_token(request.headers.get("Authorization", ""))
token_id = hashlib.sha256(token.encode()).hexdigest()[:12] if token else None
entry = AuditEntry(
@@ -387,6 +387,19 @@ def _refuse_open_in_prod() -> None:
)
+def _extract_bearer_token(header: str) -> str:
+ """Token from an Authorization header.
+
+ Matches the gateway: trim, then a case-insensitive ``Bearer `` prefix
+ (RFC 7235), then trim the token. A raw value with no prefix is kept as-is
+ so existing clients that send the token alone still work.
+ """
+ value = header.strip()
+ if value.lower().startswith("bearer "):
+ return value[7:].strip()
+ return value
+
+
def _check_read_auth(request: Request) -> None:
"""Validate read auth (inference token or admin token)."""
auth_token = os.environ.get("SIE_AUTH_TOKEN")
@@ -395,7 +408,7 @@ def _check_read_auth(request: Request) -> None:
_refuse_open_in_prod()
return # No auth configured (dev / self-host localhost posture)
- token = request.headers.get("Authorization", "").removeprefix("Bearer ").strip()
+ token = _extract_bearer_token(request.headers.get("Authorization", ""))
if not token:
raise HTTPException(status_code=401, detail="Missing Authorization header")
token_match = (auth_token is not None and hmac.compare_digest(token, auth_token)) or (
@@ -419,7 +432,7 @@ def _check_write_auth(request: Request) -> None:
_refuse_open_in_prod()
return # No auth configured at all (dev / self-host localhost posture)
- token = request.headers.get("Authorization", "").removeprefix("Bearer ").strip()
+ token = _extract_bearer_token(request.headers.get("Authorization", ""))
if not token:
raise HTTPException(status_code=401, detail="Missing Authorization header")
if not hmac.compare_digest(token, admin_token):
diff --git a/packages/sie_config/tests/test_config_api.py b/packages/sie_config/tests/test_config_api.py
index 0e86c8363..ec49585e9 100644
--- a/packages/sie_config/tests/test_config_api.py
+++ b/packages/sie_config/tests/test_config_api.py
@@ -1120,6 +1120,22 @@ class TestConfigAPIExportAuth:
assert resp.status_code == 401
assert "Missing Authorization header" in resp.text
+ def test_lowercase_bearer_prefix_is_accepted(self, monkeypatch) -> None:
+ monkeypatch.setenv("SIE_ADMIN_TOKEN", "the-real-admin")
+ resp = self.client.get(
+ "/v1/configs/export",
+ headers={"Authorization": "bearer the-real-admin"},
+ )
+ assert resp.status_code == 200
+
+ def test_bearer_prefix_is_trimmed_and_case_insensitive(self, monkeypatch) -> None:
+ monkeypatch.setenv("SIE_ADMIN_TOKEN", "the-real-admin")
+ resp = self.client.get(
+ "/v1/configs/export",
+ headers={"Authorization": " BEARER the-real-admin "},
+ )
+ assert resp.status_code == 200
+
def test_export_forbidden_with_wrong_admin_token(self, monkeypatch) -> None:
monkeypatch.setenv("SIE_ADMIN_TOKEN", "the-real-admin")
resp = self.client.get(