Skip to content

fix(auth): reject untrusted browser origins for local OAuth callbacks - #149

Merged
pierreraby merged 3 commits into
patlux:mainfrom
karaaslanz:fix/oauth-callback-browser-origin-check-20261011
Oct 11, 2026
Merged

pierreraby merged 3 commits into
patlux:mainfrom
karaaslanz:fix/oauth-callback-browser-origin-check-20261011

Conversation

@karaaslanz

Copy link
Copy Markdown
Collaborator

Summary

  • Reject local OAuth callback requests and preflights that include an untrusted browser Origin, before any callback payload is processed.
  • Keep Origin-less clients compatible, and preserve the existing trusted Command Code / localhost development origins and login cancellation flow.
  • Add focused regression tests for both an untrusted text/plain POST and an untrusted OPTIONS preflight.

Why

CORS response headers do not authorize side-effecting requests: cross-origin text/plain POSTs can reach the local listener without requiring the browser to expose a response. Previously, an unsolicited callback error could terminate the pending login even if the request originated from another website. This adds an explicit server-side browser-Origin check.

Validation

  • node --test tests/test-oauth.ts — 18 passed, including two new regressions.
  • npm run typecheck — passed.
  • Prettier check on src/auth-server.ts and tests/test-oauth.ts — passed.
  • No external service calls or production testing. All cases used a synthetic localhost server.

Limitations

This is defense in depth for browser-originated requests, not a claim of account takeover or credential disclosure. Local processes can omit/spoof Origin. The separate question of whether the Studio error callback should echo OAuth state remains outside this PR; the existing compatibility behavior is unchanged.

Review of the trusted-origin allowlist is welcome.

@pierreraby pierreraby left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved on exact head 6cc45b4 after an independent security review and local validation.

Local validation: node --test tests/test-oauth.ts 18/18 including the two new regressions, tsc --noEmit clean, Prettier clean. The fresh security review verified the gate runs before every side effect (preflight, URL/method routing, body accumulation, JSON.parse, the parsed.error rejection path, and closeServer) and found no browser-reachable bypass: Origin: null, case/scheme/host variants, duplicated Origin headers, DNS rebinding and redirect chains all fail closed, and GET navigations reach only 404/405 without body processing. The threat-model exploit (cross-origin text/plain POST with a forged access_denied) is pinned by the new regression and asserts the server keeps listening and the legitimate callback still resolves.

Non-blocking notes for a follow-up:

  1. Consider pinning Origin: null -> 403 and an Origin-less POST to /callback -> 200 in tests, so the browser-scoped contract cannot regress silently.
  2. Pre-existing, out of scope here: the parsed.error path is not state-bound, so any Origin-less local request can cancel a pending login without the state token. Requiring state === expectedState there would close it.
  3. The trusted allowlist is preserved unchanged; we will confirm the Studio transfer page's posting origin with one real browser login (the flow constructs a same-origin https://commandcode.ai/studio/auth/cli URL) and fall back to a quick patch if it differs.

Thanks for the thorough PR and threat-model write-up!

@pierreraby pierreraby left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approved at 48b2907 after the branch was refreshed onto current main.

The delta from the previously reviewed 6cc45b4 is exactly one merge commit bringing main into the branch: the PR's own source delta is byte-identical (same normalized diff SHA-256 for src/auth-server.ts + tests/test-oauth.ts versus the reviewed base and versus current main), and no conflict was resolved. Full local suite on this integrated head passes with both real hosts required (386 tests, 0 fail, 0 skip, pi 1.1.0 + OMP 18.4.2 against mocks), Prettier and git diff --check clean, and all 13 GitHub checks are green on this exact head.

The security review verdict and the three non-blocking follow-ups from my previous approval stand unchanged.

@pierreraby
pierreraby merged commit 6c8773b into patlux:main Oct 11, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants