Skip to content

fix(auth): prefix magic-link tokens with non-hex char to prevent QP-escape corruption (po-r80) - #1661

Merged
anuveyatsu merged 1 commit into
mainfrom
polecat/furiosa/po-r80@mt06accl
Aug 19, 2026
Merged

fix(auth): prefix magic-link tokens with non-hex char to prevent QP-escape corruption (po-r80)#1661
anuveyatsu merged 1 commit into
mainfrom
polecat/furiosa/po-r80@mt06accl

Conversation

@anuveyatsu

Copy link
Copy Markdown
Member

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".

13:39  ?token+0VrhYJ…    MALFORMED — no '=' anywhere
08:25  ?token=IY8rn3_…   OK
09:25  ?token=OAsODOhQ…  OK
13:44  ?token=mieb2Fx3…  OK

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…, losing 2B and 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 leaves 2B missing, so it 400s.

Fix

EMAIL_TOKEN_PREFIX = 'z' (not in 0-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.ts changes: 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.sh exit 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

…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.
@anuveyatsu
anuveyatsu merged commit 039d44d into main Aug 19, 2026
9 checks passed
@anuveyatsu
anuveyatsu deleted the polecat/furiosa/po-r80@mt06accl branch August 19, 2026 14:45
@vercel

vercel Bot commented Aug 19, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
www.portaljs.com Ready Ready Preview Aug 19, 2026 2:45pm
7 Skipped Deployments
Project Deployment Actions Updated (UTC)
portaljs-alan-turing Ignored Ignored Aug 19, 2026 2:45pm
portaljs-ckan Ignored Ignored Aug 19, 2026 2:45pm
portaljs-ckan-ssg Ignored Ignored Aug 19, 2026 2:45pm
portaljs-fivethirtyeight Ignored Ignored Aug 19, 2026 2:45pm
portaljs-git-example Ignored Ignored Aug 19, 2026 2:45pm
portaljs-learn Ignored Ignored Aug 19, 2026 2:45pm
portaljs-openspending Ignored Ignored Aug 19, 2026 2:45pm

Request Review

@changeset-bot

changeset-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: e2f87ca

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant