fix: don't add // authority when stringifying opaque-path protocols - #352
sarathfrancis90 wants to merge 1 commit into
Conversation
parseURL keeps the path of opaque-path schemes (data:, blob:, javascript:,
vbscript:) verbatim, but stringifyParsedURL unconditionally appended // after
any protocol. As a result stringifyParsedurl(/sitelet?url=https%3A%2F%2Fgithub.com%2Funjs%2Fufo%2Fpull%2FparseURL%28%2522data%3Atext%2Fplain%2522))
returned "data://text/plain", and the extra slashes compounded on every
round-trip ("data:////text/plain", ...). normalizeURL was likewise
non-idempotent for these URLs.
Only emit the // authority for non-opaque protocols (or protocol-relative
URLs).
📝 WalkthroughWalkthrough
ChangesOpaque Protocol Fix in stringifyParsedURL
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
test/normalize.test.ts (1)
45-46: ⚡ Quick winExtend normalizeURL opaque cases to
javascript:andvbscript:.Since opaque handling now explicitly includes those schemes, add normalize identity checks for both to prevent partial coverage drift.
Suggested patch
"data:text/plain;base64,aGVsbG8=": "data:text/plain;base64,aGVsbG8=", "blob:https://example.com/uuid": "blob:https://example.com/uuid", + "javascript:alert('hello')": "javascript:alert('hello')", + "vbscript:msgbox('hello')": "vbscript:msgbox('hello')", };🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@test/normalize.test.ts` around lines 45 - 46, The normalizeURL test cases for opaque URL schemes are incomplete. In the test/normalize.test.ts file around lines 45-46, add identity check test cases for the javascript: and vbscript: schemes to the test object, following the same pattern as the existing data: and blob: scheme cases. Each new case should verify that URLs with these schemes are normalized to themselves unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@test/normalize.test.ts`:
- Around line 45-46: The normalizeURL test cases for opaque URL schemes are
incomplete. In the test/normalize.test.ts file around lines 45-46, add identity
check test cases for the javascript: and vbscript: schemes to the test object,
following the same pattern as the existing data: and blob: scheme cases. Each
new case should verify that URLs with these schemes are normalized to themselves
unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 156b2e19-d527-4fe8-a326-98258cd75ca9
📒 Files selected for processing (3)
src/parse.tstest/normalize.test.tstest/utilities.test.ts
I noticed
stringifyParsedurl(/sitelet?url=https%3A%2F%2Fgithub.com%2Funjs%2Fufo%2Fpull%2FparseURL%28x))corruptsdata:URLs (and other opaque-path schemes), and the damage compounds on every round-trip:Same for
blob:,javascript:andvbscript:.parseURLcorrectly keeps the opaque path of these schemes (that was fixed in #158), butstringifyParsedURLalways appended//after any protocol — opaque-path schemes have no//authority, so the extra slashes break the URL and the parse↔stringify round-trip.The fix only emits the
//authority for non-opaque protocols (or protocol-relative URLs).file://,http://,//hostand protocol-relative URLs are unaffected.Found by fuzzing the parse→stringify round-trip /
normalizeURLidempotency. Added stringify round-trip cases plus twonormalizeURLcases; full suite, lint and typecheck pass.Summary by CodeRabbit
Release Notes
data:,blob:,javascript:,vbscript:). These special protocols now preserve their format during round-trip serialization without incorrectly appending authority prefixes.