Public: anyone can read this; the crew can change it.

Auth scheme `bearer` in any letter case resolves the caller, and the 401 challenge tells the truth #14998

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

A token sent as bearer (lowercase, or any other letter case) now signs the caller in, the same as Bearer . The 401 challenge now says error="invalidtoken" only when a bearer was actually checked and refused. The finding.

Notes

shipped production is serving it deploy 6dfa34b

opened this

A token sent as bearer <secret> (lowercase, or any other letter case) now signs the caller in, the same as Bearer <secret>. The 401 challenge now says error="invalid_token" only when a bearer was actually checked and refused.

The finding. src/http/token_header.ts only recognised the exact spelling Bearer , so bearer x was treated as no credentials at all. But the challenge in src/http/oauth.ts (bearerChallenge) detected a bearer ignoring case (/^Bearer\s+\S/i). So a valid lowercase token got a 401 saying the token was invalid, even though it was never checked. The same was true of headers like Bearer<TAB>x.

The fix.

  • token_header.ts now matches Bearer and token in any letter case, as the HTTP spec says schemes should be (RFC 9110 §11.1).
  • It adds bearerSecret(), which reads only the Bearer scheme.
  • bearerChallenge now uses bearerSecret(), so the challenge and the sign-in check read the header the same way.

The other callers of tokenSecret / isTokenScheme in server.ts, api/timers.ts, api/print.ts and spawn/vouch.ts give the same answers for every header they accepted before. They now also accept other letter cases of the scheme.

The finding sits in token_header.ts and oauth.ts. src/http/caller.ts, which the issue lists, contains no header parsing, so it is unchanged.

Not done here. The app-host proxy in src/apps/proxy.ts (bearerAdmission, navigating) still checks for startsWith("Bearer ") exactly. It is a separate boundary with its own rules; changing it is a follow-up, not part of this finding.

Tests. test/bearer-scheme-case.test.ts checks the parser and the challenge directly, and runs a real server. It shows:

  • bearer, BEARER and Bearer all get 200 on /api/v1/user as the same account;
  • a refused lowercase bearer gets 401 with invalid_token;
  • Bearer<TAB>x and Bearer get no invalid_token.

On main the test fails. bun scripts/fast.ts named 2,267 test files for this change, too broad to run, so I ran these 19 instead: the new test plus every test file that mentions the challenge, invalid_token, www-authenticate or the token header. All 254 tests pass, and the gate and tsc --noEmit are clean.

For the finding in #14778, which is sealed so I could not read its exact wording:

To try it: make a token at /settings/tokens, then:

  • curl -H 'authorization: bearer <token>' <preview>/api/v1/user answers with your account;
  • curl -i -H 'authorization: bearer wrong' <preview>/api/v1/user answers 401 with www-authenticate: … error="invalid_token".

🤖 Generated with Claude Code

Closes #14988

commented

A token sent as bearer <secret> (lowercase, or any other letter case) now signs the caller in, the same as Bearer <secret>. The 401 challenge now says error="invalid_token" only when a bearer was actually checked and refused.

The finding. src/http/token_header.ts only recognised the exact spelling Bearer , so bearer x was treated as no credentials at all. But the challenge in src/http/oauth.ts (bearerChallenge) detected a bearer ignoring case (/^Bearer\s+\S/i). So a valid lowercase token got a 401 saying the token was invalid, even though it was never checked. The same was true of headers like Bearer<TAB>x.

The fix.

  • token_header.ts now matches Bearer and token in any letter case, as the HTTP spec says schemes should be (RFC 9110 §11.1).
  • It adds bearerSecret(), which reads only the Bearer scheme.
  • bearerChallenge now uses bearerSecret(), so the challenge and the sign-in check read the header the same way.

The other callers of tokenSecret / isTokenScheme in server.ts, api/timers.ts, api/print.ts and spawn/vouch.ts give the same answers for every header they accepted before. They now also accept other letter cases of the scheme.

The finding sits in token_header.ts and oauth.ts. src/http/caller.ts, which the issue lists, contains no header parsing, so it is unchanged.

Not done here. The app-host proxy in src/apps/proxy.ts (bearerAdmission, navigating) still checks for startsWith("Bearer ") exactly. It is a separate boundary with its own rules; changing it is a follow-up, not part of this finding.

Tests. test/bearer-scheme-case.test.ts checks the parser and the challenge directly, and runs a real server. It shows:

  • bearer, BEARER and Bearer all get 200 on /api/v1/user as the same account;
  • a refused lowercase bearer gets 401 with invalid_token;
  • Bearer<TAB>x and Bearer get no invalid_token.

On main the test fails. bun scripts/fast.ts named 2,267 test files for this change, too broad to run, so I ran these 19 instead: the new test plus every test file that mentions the challenge, invalid_token, www-authenticate or the token header. All 254 tests pass, and the gate and tsc --noEmit are clean.

For the finding in #14778, which is sealed so I could not read its exact wording:

To try it: make a token at /settings/tokens, then:

  • curl -H 'authorization: bearer <token>' <preview>/api/v1/user answers with your account;
  • curl -i -H 'authorization: bearer wrong' <preview>/api/v1/user answers 401 with www-authenticate: … error="invalid_token".

🤖 Generated with Claude Code

Approved

Clean, small, and correct. Scheme matching is case-insensitive per RFC 9110, the secret is still trimmed and required, and bearerChallenge now reads the header through the same bearerSecret parser as caller resolution, so invalid_token is only claimed for a bearer that was tried. No new dependency, no new input surface, tests cover parser, challenge and a real server. The app-host proxy still matching exact Bearer is disclosed as a follow-up; worth an issue.

Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs