mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 15:07:29 +08:00
feat/https-compose-caddy
5
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
fe884612c0 |
fix(subscription): match the Bearer scheme case-insensitively for the Codex usage poll (RFC 7235) (#3966)
## Description
`_build_usage_headers` (in `headroom/subscription/codex_rate_limits.py`)
decides whether to fire the Codex live usage poll (`GET /wham/usage`)
that refreshes the rate-limit window shown to the user. It gates on the
inbound `Authorization` header, but checked the scheme with a
case-sensitive `startswith("Bearer ")`.
The HTTP auth-scheme token is **case-insensitive** per RFC 7235 §2.1, so
a client sending `Authorization: bearer <token>` was rejected,
`_build_usage_headers` returned `None`, and the poll was skipped — the
Codex rate-limit window was never refreshed for that session. This
matches the case-insensitive handling the proxy already applies
elsewhere (`auth_policy.classify_auth_signals`).
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
## Changes Made
- Parse the `Authorization` scheme with `partition(" ")` and compare it
case-insensitively (`scheme.lower() == "bearer"`), still requiring a
non-empty credential. The credential and the forwarded `Authorization`
header value are unchanged.
- Tests: a regression asserting a lowercase `bearer` scheme now builds
the poll headers, plus a guard test confirming a bare scheme / empty
credential / non-bearer value is still rejected.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [x] New tests added for new functionality
- [x] Manual testing performed
### Test Output
```text
$ pytest tests/test_codex_rate_limits.py -k "lowercase_bearer or bare_scheme" -q # before fix
FAILED tests/test_codex_rate_limits.py::TestUsagePollGating::test_build_headers_accepts_lowercase_bearer_scheme
1 failed, 1 passed
$ pytest tests/test_codex_rate_limits.py -q # after fix
43 passed
$ ruff check headroom/subscription/codex_rate_limits.py tests/test_codex_rate_limits.py
All checks passed!
```
## Real Behavior Proof
- Environment: macOS (arm64), Python 3.13, local editable install
(`maturin develop --release`). Driven through the real
`_build_usage_headers` with a lowercase-scheme request.
- Exact command / steps: call `_build_usage_headers({"authorization":
"bearer <token>", "chatgpt-account-id": "acct-42"})` before and after
the patch and report whether the poll headers were built.
- Observed result: The lowercase Bearer usage-poll headers changed from
not built to built, with Authorization and ChatGPT-Account-Id preserved,
as captured below.
```text
=== BEFORE fix ===
usage-poll headers built: NO (poll skipped)
=== AFTER fix ===
usage-poll headers built: YES
Authorization: bearer eyJ.header.sig
ChatGPT-Account-Id: acct-42
```
Before the patch a valid lowercase-scheme Codex session never refreshed
its rate-limit window; after, the poll is scheduled and the
Authorization value is forwarded unchanged.
- Not tested: a live `GET /wham/usage` round-trip against ChatGPT (no
Codex OAuth session in this environment); the header-building gate was
exercised directly.
## Runtime Rollout Safety
- Rollout-managed feature(s): none — request-header gating fix for the
usage poll.
- Minimum rollout channel: n/a
- Stable/default behavior changed: only that a lowercase/other-cased
`Bearer` scheme now triggers the poll as an exact-cased `Bearer` already
did.
- Kill switch / disable path: n/a
- Unsafe override required: no
- Qualification impact: none
- Rollback path: revert this commit.
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md`
## Additional Notes
`mypy` not run locally (not set up in this environment); `ruff
check`/`ruff format` are clean. No new dependencies. Part of the same
RFC 7235 scheme-casing class the proxy already handles in `auth_policy`;
this fixes the Codex usage-poll gate.
<!-- devin-review-badge-begin -->
---
<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/3966"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
Co-authored-by: JD Davis <mxjerrett@gmail.com>
|
||
|
|
65e63c0883 |
fix(security): poll usage only with the operator's own credential (#3863)
## Why Two background usage pollers spent callers' credentials (finding 01-F16): - The Claude subscription tracker adopted any caller's OAuth `Authorization: Bearer` as the account to poll (`notify_active` stored it and `_maybe_poll` used it). - The Codex `/wham/usage` refresher polled with any caller's bearer and `ChatGPT-Account-Id`. On a proxy shared by several callers, a network caller's credential was therefore sent on a request they never made, and their subscription usage appeared on the operator's dashboard. ## What changes - New `headroom/subscription/credential_policy.py` with `is_local_operator_connection()`. It is true only for a direct loopback peer with no `Forwarded`, `X-Real-IP` or `X-Forwarded-*` header (any of the family, including `-Proto` and `-Host` alone). It fails closed on a missing peer. Trusted-gateway CIDRs do not count, because a gateway fronts other principals. - `SubscriptionTracker.notify_active(token, *, from_local_operator=False)` always marks the tracker active, but stores the bearer for polling only when `from_local_operator` is true. - `maybe_schedule_usage_poll(headers, *, from_local_operator=False)` returns without polling unless `from_local_operator` is true. - Both flags default to the safe answer, so a new call site cannot adopt a credential by accident. The Anthropic handler and the Codex WebSocket relay pass `is_local_operator_connection(...)`. Unchanged: `_maybe_poll` ordering, and the fallback to the operator's own credential (`CLAUDE_CODE_OAUTH_TOKEN` or the Claude Code credentials file) when no bearer has been learned. The Copilot quota poller already uses only the host's own GitHub token. ## Behaviour change - Usage pollers never poll with a network caller's credential. A bearer learned from traffic comes only from a direct local caller; otherwise the pollers use the operator-configured credential as before. - A proxy fronted by a gateway on the same host no longer learns a token from gateway traffic. Set `CLAUDE_CODE_OAUTH_TOKEN` there if you want the subscription window. - A single developer running Claude Code on the same machine is unaffected, including when the OAuth token lives in the macOS Keychain rather than a file. ## Tests `tests/test_subscription_poll_credential_policy.py`: - The policy: IPv4 and IPv6 loopback callers are the local operator; LAN peers, a missing peer and each forwarding header are not, including `X-Forwarded-Proto` alone and `X-Forwarded-Host` alone. - The tracker: a network caller marks activity but is never adopted; a local operator's bearer is adopted; a foreign bearer never reaches a usage poll. - The Codex trigger is not driven by a network caller. - End to end through the real `/v1/messages` handler: a LAN caller's bearer is never adopted. The tracker, foreign-bearer, Codex and handler tests fail on `main`. Existing tracker and Codex tests that assert adoption now pass `from_local_operator=True`, so they still test what they meant to. Manual run: `headroom proxy --host 0.0.0.0` with no Claude credentials on the host and the usage URL pointed at a local capture server. On `main`, a LAN caller's bearer was used for polling. On this branch only the loopback operator's bearer was used. Not tested: a live Codex ChatGPT session end to end (the trigger is covered by unit tests). ## Merge order Part of a set. Suggested order: #3852 → #3891 → #3849 → #3860 → #3862 → #3863. This PR goes last. #3891 stops the proxy token being forwarded upstream; this PR stops the pollers adopting any foreign bearer. They touch different files. Revised 2026-10-01 after self-review: removed the `HEADROOM_SUBSCRIPTION_TRAFFIC_TOKEN` setting and reverted the poll re-ordering, so the change is limited to which callers' bearers are stored. Revised 2026-10-01 after review: the local-operator check now rejects every `X-Forwarded-*` header, not just `X-Forwarded-For`, so a gateway sending only `-Proto` or `-Host` fails closed. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/3863" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a> <!-- devin-review-badge-end --> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: JerrettDavis <mxjerrett@gmail.com> |
||
|
|
8c00f7103c |
fix(codex): poll /wham/usage for subscription limits (handshake no longer sends x-codex-* headers) (#924)
## Description Codex's subscription usage window (primary/secondary rate-limit gauges) stopped populating for ChatGPT-OAuth sessions. This PR restores it by polling Codex's dedicated usage endpoint instead of relying on response headers that are no longer sent. ### Why the previous approach no longer works The existing code populates `CodexRateLimitState` from `x-codex-*` rate-limit headers captured on the `/v1/responses` WebSocket handshake (`update_from_headers` at WS accept). That worked when OpenAI returned `x-codex-primary-used-percent`, `x-codex-primary-window-minutes`, etc. on the handshake response. OpenAI has since stopped sending those headers on the ChatGPT WebSocket handshake. I confirmed this by faithfully replaying a real Plus-account handshake (both `prewarm` and regular `request_kind`): no `x-codex-*` headers come back on either. This matches OpenAI's own move to a dedicated usage endpoint (`GET /backend-api/codex/usage` in CodexApi mode) and reports such as openai/codex#14728. So `update_from_headers` now runs on every accept but finds nothing to parse, and the window silently stays empty. The headers aren't coming back, so there is nothing to fix in the parsing path. The data now lives behind a request we have to make ourselves. ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) ## Changes Made - `subscription/codex_rate_limits.py`: - `parse_codex_usage_payload()` / `update_from_usage_payload()` — map the `GET /backend-api/wham/usage` JSON (`rate_limit.primary_window` / `secondary_window` with `used_percent`, `limit_window_seconds`, `reset_at`; `credits`; `rate_limit_reached_type`) into the existing `CodexRateLimitState`. `limit_window_seconds` is converted to window-minutes with the same round-up codex-rs uses (`(secs + 59) // 60`). - `maybe_schedule_usage_poll()` — fire-and-forget, throttled to one request per 60s, scoped to ChatGPT sessions (requires both a Bearer token and `ChatGPT-Account-Id`; API-key traffic is skipped). Uses an in-flight guard so concurrent accepts don't stack polls. Endpoint is overridable via `HEADROOM_CODEX_USAGE_URL`. - `proxy/handlers/openai.py`: - At the Codex WS accept site, after the now-usually-empty `update_from_headers` block, schedule the usage poll. Wrapped in `contextlib.suppress` and fully non-blocking so it can never delay or fail the WebSocket accept. The old header-capture path is intentionally left in place as a no-cost fallback in case OpenAI restores the headers. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [x] Manual testing performed (live `/wham/usage` replay against a Plus account returned HTTP 200 with the expected schema; payload fixture in tests mirrors that real shape) ## Test Output ``` $ uv run pytest tests/test_codex_rate_limits.py -q ........................................ [100%] 41 passed in 0.16s $ uv run ruff check headroom/subscription/codex_rate_limits.py headroom/proxy/handlers/openai.py tests/test_codex_rate_limits.py All checks passed! $ uv run mypy headroom/subscription/codex_rate_limits.py Success: no issues found in 1 source file ``` ## Additional Notes - New tests cover: full-payload mapping, window-minutes round-up, credits balance kept only when `has_credits`, promo object vs string, empty payload returns `None`, missing `used_percent` skipped, header-gating (requires Bearer + account-id), poll throttling, and no-event-loop safety. - Scoping to `ChatGPT-Account-Id` keeps the poll off API-key traffic, and the 60s throttle plus in-flight guard bound it to at most one lightweight GET per minute per running proxy. --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com> |
||
|
|
efd2ac1ca4 |
chore: renormalize line endings to LF
`.gitattributes` declares `*.py text eol=lf` and `*.sh text eol=lf`, but 74 files (73 .py, 1 .sh) are stored in the index with CRLF line endings, violating that contract. Every macOS/Linux clone reports these files as "modified" on fresh checkout because git's diff engine sees the stored bytes don't match the attribute contract, even though the working tree and index match byte-for-byte. Running `git add --renormalize .` rewrites each affected blob so the stored form matches the attribute declaration. No semantic changes — every affected file's diff is "N insertions, N deletions" with inserts and deletes being the same lines modulo line endings. Follow-up commit adds `.git-blame-ignore-revs` so `git blame` / GitHub blame skip this mechanical commit. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> |
||
|
|
a3abbec4a3 |
feat: add OpenAI Codex rate-limit window tracking
Passively capture x-codex-* response headers from proxied Codex API calls and surface them in /stats and the dashboard. Unlike Anthropic subscription window tracking (which polls a dedicated OAuth endpoint), Codex embeds rate-limit data directly in every API response header — no polling, no new credentials needed. Changes: - headroom/subscription/codex_rate_limits.py: new module with CodexRateLimitWindow, CodexCreditsSnapshot, CodexRateLimitSnapshot data models and a thread-safe CodexRateLimitState singleton; parse_codex_rate_limits() parses x-codex-primary/secondary-used-percent, window-minutes, reset-at, credits, limit-name, and promo-message headers - headroom/subscription/__init__.py: re-export new public symbols - headroom/proxy/handlers/openai.py: call get_codex_rate_limit_state().update_from_headers() after each proxied /v1/chat/completions and /v1/responses response - headroom/proxy/server.py: add codex_rate_limits key to /stats via _get_codex_rate_limit_stats() helper - headroom/dashboard/templates/dashboard.html: new OpenAI Codex Rate-Limit Window panel (primary + secondary progress bars, credits balance, limit name, reset countdown); hidden when no data - tests/test_codex_rate_limits.py: 25 unit tests covering parsing, window labels, reset time, credits, state updates - docs/screenshots/subscription_window_active.png: updated screenshot showing both Anthropic and Codex panels in the real dashboard Header schema (from codex-rs/codex-api/src/rate_limits.rs): x-codex-primary-used-percent / x-codex-primary-window-minutes / x-codex-primary-reset-at (and secondary- variants) x-codex-credits-has-credits / -unlimited / -balance x-codex-limit-name / x-codex-promo-message Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> |