fix(auth): decode Basic-auth credentials as UTF-8#1463
Conversation
atob() returns Latin-1, so UTF-8 credential bytes were never decoded and
users with non-ASCII passwords (RFC 7617 charset="UTF-8") could never
authenticate. Decode the base64 payload through TextDecoder("utf-8") and
advertise charset="UTF-8" in the WWW-Authenticate challenge.
Co-Authored-By: Claude Fable 5 <[email protected]>
pi0x
left a comment
There was a problem hiding this comment.
Review: Approve (posted as comment — GitHub blocks self-approval on this account)
Clean, correctly-scoped fix. Verified locally on the branch: pnpm vitest run test/auth.test.ts → 36/36 pass, pnpm lint (oxlint + oxfmt) clean.
Correctness
- The decode idiom
new TextDecoder("utf-8", { fatal: false }).decode(Uint8Array.from(atob(b64auth), c => c.charCodeAt(0)))is the standard, correct way to round-trip UTF-8 credential bytes thatatob()would otherwise leave as Latin-1. fatal: falseis the right call: invalid UTF-8 produces U+FFFD replacements → auth mismatch → 401, never an unhandled 500. Invalid base64 still throws fromatob()and is caught → 401 (covered by the existing "responds 401 for invalid base64" test).- Colon-split via
indexOf(":")/slicestill keeps:characters in passwords (existing multi-colon tests pass). - Challenge
Basic realm="...", charset="UTF-8"matches the RFC 7617 §2.1 example verbatim and is valid per RFC 7235 auth-param grammar.
Tests
- The UTF-8 regression test exercises a genuine multi-byte password (
pä$$wörd🔒, including a 4-byte emoji) end-to-end and asserts a 200. The challenge test assertscharset="UTF-8"on the 401.Bufferusage is consistent with the rest of the file.
Scope / style
- Change is limited to the auth decode, the challenge header, and tests — no scope creep. Style, formatting, and comments follow AGENTS.md conventions.
🤖 Generated with Claude Code
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
Warning Review limit reached
Next review available in: 49 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Try strict UTF-8 decoding first and keep the raw atob() Latin-1 interpretation when the bytes are not valid UTF-8, so legacy (pre-RFC 7617) clients sending Latin-1 credentials can still authenticate. This also avoids collapsing distinct invalid byte sequences into U+FFFD replacement characters. Co-Authored-By: Claude Fable 5 <[email protected]>
|
Good idea — implemented in 8face06. The decode is now: strict UTF-8 first ( Notes from evaluating it:
Added a regression test for a genuinely Latin-1-encoded credential ( |
Bug
requireBasicAuthdecoded the base64Authorizationheader withatob(), which returns a Latin-1 string. UTF-8 credential bytes were therefore never decoded, so a user with a non-ASCII password (RFC 7617charset="UTF-8") could never authenticate — the server compares againstTextEncoder/UTF-8 values, guaranteeing a mismatch.Fix
Decode the base64 payload through
TextDecoder("utf-8")so multi-byte credentials round-trip correctly, and advertisecharset="UTF-8"in theWWW-Authenticatechallenge per RFC 7617. Added regression tests covering a non-ASCII password and the challenge header.🤖 Generated with Claude Code