Skip to content

feat(auth): support x-api-key for proxy API key auth - #844

Closed
SSinist3r wants to merge 3 commits into
Soju06:mainfrom
SSinist3r:support-x-api-key-auth
Closed

SSinist3r wants to merge 3 commits into
Soju06:mainfrom
SSinist3r:support-x-api-key-auth

Conversation

@SSinist3r

Copy link
Copy Markdown

Summary

Supports x-api-key alongside Bearer tokens for proxy-facing API key authentication so compatible clients and reverse proxies can authenticate without header rewrites. Authorization remains the preferred source when both are present, with x-api-key used as a fallback only when needed.

Type of change

  • feat: — new user-facing feature or capability
  • fix: — bug fix (no behavior change beyond the bug)
  • refactor: — internal refactor (no behavior change, no API change)
  • docs: — documentation only
  • chore: / ci: / build: — tooling, CI, packaging
  • test: — test-only change
  • Breaking change (also append ! after the type, e.g. feat!: or include BREAKING CHANGE: footer)

Linked issue:

OpenSpec

  • This PR includes / updates an OpenSpec change
  • Not applicable — bug fix that matches the existing spec
  • Not applicable — docs / CI / chore only
  • This PR touches a codex-faithful path (image pipeline, request/response
    shape, SSE framing, OAuth flow) and preserves upstream-equivalent behavior

Change directory: openspec/changes/support-x-api-key-auth/

Changes

  • Accepts x-api-key for proxy API key validation and self-service usage lookup.
  • Preserves Bearer-first precedence, with fallback to x-api-key when Authorization is absent, malformed, or invalid.
  • Keeps ChatGPT caller-identity flows Bearer-only and strips inbound x-api-key from proxied requests.
  • Adds regression coverage for auth precedence, websocket auth, usage lookup, and header filtering.

Test plan

# uv run pytest tests/unit/test_hot_path_caches.py -q
# uv run pytest tests/unit/test_proxy_api_websocket_auth.py -q
# uv run pytest tests/unit/test_proxy_utils.py -q
# uv run pytest tests/integration/test_auth_middleware.py -q
# uv run pytest tests/integration/test_v1_usage.py -q

Screenshots / output (optional)

Example behavior:

  • Authorization: Bearer + x-api-key: now authenticates successfully
  • GET /v1/usage accepts a valid x-api-key
  • x-api-key is ignored for Bearer-only caller identity paths

Checklist

  • Title is in Conventional Commits format (()?: ).
  • Linked the related issue / discussion above — Pi custom provider (openai-codex-responses) needs x-api-key support to use codex-lb without header rewrites.
  • Added or updated tests covering the change.
  • Ran uv run pre-commit run local-ci --hook-stage manual --all-files or the relevant make subset locally.
  • If touching specs: openspec validate --specs passes and /opsx:verify is clean.
  • CHANGELOG is not edited by hand (release-please handles it).

Accept codex-lb API keys from either Authorization: Bearer or x-api-key across the proxy auth path, with Authorization tried first and x-api-key used as a fallback when needed.

This is primarily to support clients like Pi custom providers using the openai-codex-responses API type against codex-lb, where sending x-api-key is the practical integration path while keeping existing Bearer-based flows intact.
@SSinist3r SSinist3r changed the title Adds x-api-key support for proxy auth feat(auth): support x-api-key for proxy API key auth May 28, 2026
@Soju06

Soju06 commented May 31, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Soju06 Soju06 added the 🤖 codex: ok [@codex review] says no issues found. label May 31, 2026
@Soju06

Soju06 commented Jun 1, 2026

Copy link
Copy Markdown
Owner

Hermes owner-review blocker: x-api-key auth is lost on HTTP bridge owner-forward paths.

Evidence:

  • app/modules/proxy/service.py:599-613 saves only the inbound Authorization as proxy_api_authorization, then passes filter_inbound_headers(headers) onward.
  • app/core/clients/proxy.py:68-70 now strips x-api-key from filter_inbound_headers().
  • app/modules/proxy/service.py:1793 / :14757-14764 re-add only Authorization before forwarding to the owner replica.
  • The owner handler re-authenticates before streaming: app/modules/proxy/api.py:585 -> _validate_internal_bridge_api_key(), which needs either Authorization or x-api-key after this PR (app/core/auth/dependencies.py:74-83).

Result: when api_key_auth_enabled=true and HTTP bridge owner-forwarding happens, requests authenticated with only x-api-key reach the owner without any API key and fail 401. The documented fallback case Authorization: Bearer invalid + valid x-api-key also fails after forwarding because the invalid Authorization is the only auth header restored.

Please preserve the validated proxy API key credential for internal owner-forward re-auth (or otherwise make internal bridge auth use the already-validated reservation/context safely) and add a regression test for owner-forward with x-api-key-only and invalid-Bearer+valid-x-api-key.

@Soju06 Soju06 added the hermes: needs-followup Hermes left a blocker/comment that needs follow-up observation label Jun 1, 2026
@Soju06 Soju06 removed the 🤖 codex: ok [@codex review] says no issues found. label Jun 1, 2026
@Komzpa Komzpa removed the hermes: needs-followup Hermes left a blocker/comment that needs follow-up observation label Jun 1, 2026
@Komzpa

Komzpa commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Soju06 Soju06 added the 🤖 codex: ok [@codex review] says no issues found. label Jun 1, 2026
@Soju06

Soju06 commented Jun 2, 2026

Copy link
Copy Markdown
Owner

Hermes owner-review policy blocker: this is not OpenAI official authentication behavior, so I don't think we should merge it as a default /v1/* auth contract expansion without an explicit product decision.

Evidence / concern:

  • OpenAI's documented API auth is HTTP Bearer auth: Authorization: Bearer OPENAI_API_KEY_OR_ACCESS_TOKEN. x-api-key is not part of the official OpenAI API auth surface.
  • This PR makes protected proxy routes accept x-api-key as a first-class fallback (app/core/auth/dependencies.py) and updates the OpenSpec to require that behavior (openspec/changes/support-x-api-key-auth/specs/api-keys/spec.md). That means this is a codex-lb-specific compatibility extension, not OpenAI spec parity.
  • The PR body mentions Pi/custom-provider compatibility, but there is no linked issue or concrete evidence that the affected client cannot send Bearer auth or that a reverse-proxy/header-rewrite workaround is not viable.
  • The current fallback behavior also accepts Authorization: Bearer <invalid> plus a valid x-api-key. That weakens the canonical meaning of Authorization as the primary credential and expands auth semantics beyond “Bearer-compatible” behavior.

Requested fix / decision before merge:

  1. Either drop this feature and keep codex-lb's OpenAI-compatible surfaces Bearer-only, or
  2. explicitly document it as a codex-lb inbound compatibility extension, not OpenAI parity, with a linked issue/use-case explaining why core support is needed instead of a proxy rewrite, and consider narrowing it behind an opt-in setting
  3. If accepted, I recommend not falling back when a syntactically valid but invalid Bearer token is present; allow x-api-key only when Authorization is absent or malformed

The implementation now looks technically safe with upstream stripping and owner-forward coverage, so this is a product/API-contract blocker rather than a code-path correctness blocker.

@Soju06 Soju06 added the hermes: needs-followup Hermes left a blocker/comment that needs follow-up observation label Jun 2, 2026
@Komzpa

Komzpa commented Jun 2, 2026

Copy link
Copy Markdown
Collaborator

Folded into #703.

#703 now carries the x-api-key proxy API-key auth work from this PR together with the dashboard guest/read-only auth branch, based on main.

Survivor:

Closing this PR so maintainers only have one auth/dashboard branch to review and merge.

@Komzpa Komzpa closed this Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖 codex: ok [@codex review] says no issues found. hermes: needs-followup Hermes left a blocker/comment that needs follow-up observation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants