Repository navigation
Apply the private flag to IPv6 hosts in hostname() and url() - #592
yuchengshen931122-creator wants to merge 1 commit into
Conversation
hostname() and url() document `private` as "Embedded IP address is public if
False, private/local if True", but only passed the flag to ipv4(). Every IPv6
literal therefore passed, whatever the value of the flag:
url("http://127.0.0.1/", private=False) # ValidationError
url("http://[::1]/", private=False) # True
url("http://[::ffff:127.0.0.1]/", private=False) # True
url("http://[fd00::1]/", private=False) # True
ipv6() gets a `private` parameter and hostname() passes the flag on. The IPv4
behaviour is not touched: _check_private_ip() is unchanged, and the IPv6 check
uses the counterparts of the ranges it already treats as private (unspecified,
loopback, unique local, link-local, site-local, multicast). An IPv4-mapped
address is judged by the IPv4 address it embeds, with the existing function.
This is the single pull request asked for when python-validators#451, python-validators#452 and python-validators#453 were
closed. See discussion python-validators#591.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
mjanthony819
left a comment
There was a problem hiding this comment.
Verified this locally against the PR head (166 passed across tests/test_hostname.py and tests/test_ip_address.py) and probed the boundary cases through url():
- url("http://[::1]/", private=False) rejects; fd00:: and fe80:: reject; 2606:4700:4700::1111 passes, as documented.
- The ipv4_mapped delegation is the part that matters most for anyone using private=False as an SSRF guard: [::ffff:127.0.0.1] now classifies exactly like 127.0.0.1 (rejected) and [::ffff:1.1.1.1] like 1.1.1.1 (accepted). Routing the mapped address through _check_private_ip makes the two notations impossible to disagree, which is the right call.
- Zone identifiers survive correctly: [fe80::1%eth0] is still classified link-local because the scope id is dropped before the range check.
One thing you might pin with a test: documentation and transition ranges classify as public here - url("http://[2001:db8::1]/", private=False) passes, as does 2002::/16. That mirrors the IPv4 side exactly (192.0.2.0/24 passes too, and the comment says the list deliberately mirrors _check_private_ip rather than stdlib is_private), so it reads as intentional. A parametrize row or two (2001:db8::/32 accepted with private=False, maybe an [::ffff:...] row alongside) would keep that parity from drifting accidentally in either direction.
No objections to the approach. AI assistance covered the analysis and this review text.
hostname()andurl()documentprivateas "Embedded IP address is public ifFalse, private/local ifTrue", but the flag is only passed toipv4(). Every IPv6 literal passes, whatever its value:url("http://[::1]/", private=False),url("http://[::ffff:127.0.0.1]/", private=False)andurl("http://[fd00::1]/", private=False)are allTrue, whileurl("http://127.0.0.1/", private=False)is rejected. Code that usesprivate=Falseto keep requests away from internal hosts can be bypassed with an IPv6 literal (discussion #591).This is the single pull request asked for when #451, #452 and #453 were closed. The existing semantics are kept:
_check_private_ip()and the IPv4 behaviour are not touched.ipv6()gets aprivateparameter that uses the IPv6 counterparts of the ranges the IPv4 check already treats as private: unspecified (::), loopback (::1), unique local (fc00::/7), link-local (fe80::/10), site-local (fec0::/10) and multicast (ff00::/8). An IPv4-mapped address (::ffff:a.b.c.d) is judged by the IPv4 address it embeds, with the existing function.hostname()passes the flag on, andurl()follows throughhostname().One behaviour change besides the fix itself: with
private=True, a public IPv6 host is now rejected, as a public IPv4 host already is. With the defaultprivate=Nonenothing changes.The added tests (45 cases: both directions of the flag for
ipv6(),hostname()andurl(), range boundaries, zone identifiers, CIDR notation, IPv4-mapped addresses) include 42 that fail on master. The full suite including doctests passes on Python 3.13 (1,619 passed); Ruff lint and format, Pyright and Bandit report nothing. Other Python versions are left to CI. The branch merges cleanly with the open #535, #537, #544, #579 and #588.Left out on purpose: other ways of embedding an IPv4 address in an IPv6 one (6to4
2002::/16, NAT6464:ff9b::/96). They can be added to the list if you want them treated as private.Found while writing specification-based tests for a university software-testing course project (NYCU). Prepared with AI assistance; reproduced and tested locally.
🤖 Generated with Claude Code