Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(subscription): match the Bearer scheme case-insensitively for the Codex usage poll (RFC 7235) (#3966)

Rajat Nagdafe884612c07d
diff --git a/headroom/subscription/codex_rate_limits.py b/headroom/subscription/codex_rate_limits.py
index 311510a92..096735fb1 100644
--- a/headroom/subscription/codex_rate_limits.py
+++ b/headroom/subscription/codex_rate_limits.py
@@ -423,7 +423,12 @@ def _build_usage_headers(request_headers: dict[str, str]) -> dict[str, str] | No
"""
lower = {str(k).lower(): v for k, v in request_headers.items()}
auth = str(lower.get("authorization", ""))
- if not auth.startswith("Bearer ") or not auth[len("Bearer ") :].strip():
+ # The auth-scheme token is case-insensitive per RFC 7235 2.1, so a client
+ # sending "authorization: bearer <token>" must be recognized the same as
+ # "Bearer <token>". A case-sensitive check skipped the usage poll for such
+ # requests, so the Codex rate-limit window was never refreshed for them.
+ scheme, sep, credentials = auth.partition(" ")
+ if not sep or scheme.lower() != "bearer" or not credentials.strip():
return None
account_id = lower.get("chatgpt-account-id")
if not account_id:
diff --git a/tests/test_codex_rate_limits.py b/tests/test_codex_rate_limits.py
index 417254862..bbdbd1d4a 100644
--- a/tests/test_codex_rate_limits.py
+++ b/tests/test_codex_rate_limits.py
@@ -368,6 +368,28 @@ class TestUsagePollGating:
assert headers["User-Agent"] == "codex_exec/0.139.0"
assert headers["originator"] == "codex_exec"
+ def test_build_headers_accepts_lowercase_bearer_scheme(self):
+ """RFC 7235 2.1: the auth scheme is case-insensitive.
+
+ A client sending `authorization: bearer <token>` must still trigger the
+ usage poll; a case-sensitive check previously returned None, so the
+ Codex rate-limit window was never refreshed for such requests.
+ """
+ headers = _build_usage_headers(
+ {
+ "authorization": "bearer abc.def.ghi",
+ "chatgpt-account-id": "acct-1",
+ }
+ )
+ assert headers is not None
+ assert headers["Authorization"] == "bearer abc.def.ghi"
+ assert headers["ChatGPT-Account-Id"] == "acct-1"
+
+ def test_build_headers_still_rejects_a_bare_scheme_or_non_bearer(self):
+ assert _build_usage_headers({"authorization": "Bearer", "chatgpt-account-id": "a"}) is None
+ assert _build_usage_headers({"authorization": "bearer ", "chatgpt-account-id": "a"}) is None
+ assert _build_usage_headers({"authorization": "sk-live", "chatgpt-account-id": "a"}) is None
+
def test_try_begin_poll_throttles(self):
state = CodexRateLimitState()
assert state._try_begin_poll(60.0) is True