fix(url): reject bare IPv6 addresses in z.url()#6103
Open
MerlijnW70 wants to merge 1 commit into
Open
Conversation
The WHATWG URL constructor parses a bare IPv6 address whose first group starts with a hex letter (e.g. "fe80::1") as `scheme:opaque-path`, so z.url() accepted it as a valid URL while correctly rejecting "::1" and "2001:db8::1". Reject any input that is itself a valid IPv6 literal by probing `http://[<input>]`, guarded by a cheap pre-check so schemed URIs like "c:" or "mailto:x" and ordinary URLs are unaffected. Fixes colinhacks#6031 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Fixes #6031.
Problem
z.url()accepts bare IPv6 link-local addresses such asfe80::1as valid URLs, while correctly rejecting::1,2001:db8::1, and192.168.1.1:The cause is the WHATWG
URLconstructor: a bare IPv6 address whose first group starts with a hex letter (fe80:) is parsed asscheme:opaque-path, sonew URL("fe80::1")succeeds.Fix
Before normal URL handling, reject any input that is itself a valid IPv6 literal by probing
new URL("http://[<input>]")— the same technique Zod'sipv6()validator already uses. A cheap pre-check (only hex digits, colons, dots and a zone%, and no/) keeps schemed URIs and ordinary URLs out of this branch, so the existing behaviour is preserved:::1,2001:db8::1,fe80::1,fe80::abcd:1234, full-formfe80:0000:…:0001,dead:beef:…(×8)http://[2001:db8::1],https://example.com,c:,mailto:foo@bar.com,face:b00cTests
Adds a regression test in
url.test.tscovering both the rejected bare-IPv6 cases and the preserved valid cases. Verified red without the fix, green with it; full suite (3817 tests) passes.