Skip to content

fix(format-url): preserve all colons in auth and encode all # in search - #98472

Open
suletetes wants to merge 4 commits into
vercel:canaryfrom
suletetes:fix/format-url-auth-colons
Open

fix(format-url): preserve all colons in auth and encode all # in search#98472
suletetes wants to merge 4 commits into
vercel:canaryfrom
suletetes:fix/format-url-auth-colons

Conversation

@suletetes

Copy link
Copy Markdown

What

Two one-line correctness fixes to formatUrl (Next's port of Node's legacy url.format), each caused by a non-global replace that only handled the first occurrence.

  • Auth colons: encodeURIComponent(auth).replace(/%3A/i, ':')/%3A/gi. The non-global flag re-decoded only the first %3A, so user:pa:ss came out as user:pa%3Ass@…. Node decodes every colon in userinfo.
  • Search hashes: search.replace('#', '%23')search.replace(/#/g, '%23'). Only the first # was encoded, so ?a=1#b#c yielded ?a=1%23b#c instead of ?a=1%23b%23c.

Why

Both diverged from Node's url.format. Verified byte-for-byte parity against Node for multi-colon (user:pa:ss, a:b:c:d), single-colon, @-in-auth, query object, multi-# search, and bare-hash inputs. The g flag only affects strings with more than one match, so single-occurrence inputs (the common case) are unchanged.

Tests

New test/unit/format-url.test.ts (7 cases), all exact toBe assertions verified against Node:

  • multi-colon auth (2 and 3+ colons) regression guards
  • single-colon auth, @-encoding, query object, hash normalization
  • multi-# search encoding regression guard

npx jest test/unit/format-url.test.ts → 7 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