fix(auth): prefix magic-link tokens with non-hex char to prevent QP-escape corruption (po-r80) - #1661
Merged
Merged
Conversation
…scape corruption (po-r80)
The magic link's only literal '=' is the "?token=" query separator. A
hop in mail delivery mistakes a raw '=' followed by two hex digits for
a quoted-printable escape and decodes it to one byte, corrupting the
link ("?token=2Bxyz..." -> "?token+xyz...", losing "2B" and gaining
'+' = 0x2B). ~12% of random b64url tokens start with two hex-like
chars and are at risk. Prefixing every token with a fixed non-hex char
guarantees the separator's '=' is never followed by two hex digits,
closing the failure class for any token value.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
7 Skipped Deployments
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
P1 — a live sign-in failure. Some magic-link emails arrived with a malformed URL and the user simply could not sign in; the page said only "Try again".
Mechanism
A hop in mail delivery mistakes a raw
=followed by two hex digits for a quoted-printable escape and decodes it to one byte:?token=2Bxyz…→?token+xyz…, losing2Band gaining+(0x2B). The magic link's only literal=is the?token=separator, so a token beginning with two hex-looking chars is at risk. b64url contains 22 hex-looking chars of 64, so (22/64)² ≈ 11.8% of tokens.Confirmed against the observed break: the malformed token body was 41 chars against 43 for all three good ones, and
"2B"+ 41 = 43 — the hypothesis predicts the length anomaly rather than merely fitting it. It also explains why naive repair fails: substituting the=back still leaves2Bmissing, so it 400s.Fix
EMAIL_TOKEN_PREFIX = 'z'(not in0-9a-fA-F), prepended at generation, so the character after?token=is never a hex digit and the escape pattern cannot form for any token value. Closes the class, not the instance.Only
email.tschanges: the prefix is part of the token string and opaque to verification, so lookup by exact string still matches and tokens already in flight stay valid.Test asserts over 1000 generated tokens rather than sampling one — three of four real links were fine, so a single sample proves nothing.
Verification
scripts/verify.shexit 0, 19 commands, stages setup lint typecheck test build. Post-deploy: send live links and confirm every delivered URL reads?token=z.🤖 Generated with Claude Code