Skip to content

fix: reject leading-zero octets in ipv6 IPv4-mapped addresses#1936

Merged
RobinPicard merged 1 commit into
dottxt-ai:mainfrom
NishchayMahor:fix/ipv6-leading-zero-octets
Jul 20, 2026
Merged

fix: reject leading-zero octets in ipv6 IPv4-mapped addresses#1936
RobinPicard merged 1 commit into
dottxt-ai:mainfrom
NishchayMahor:fix/ipv6-leading-zero-octets

Conversation

@NishchayMahor

Copy link
Copy Markdown
Contributor

Fixes #1911.

Same class of bug as #1904, but on the ipv6 side. The two IPv4-mapped branches of types.ipv6 (::ffff:a.b.c.d and x:x::a.b.c.d) embed the octet subpattern 25[0-5]|2[0-4][0-9]|1?[0-9]{1,2}, which accepts 2-digit leading-zero octets like 01 and 00. So ::ffff:01.1.1.1 matched even though the stdlib rejects it:

>>> ipaddress.ip_address("::ffff:01.1.1.1")
ValueError: '::ffff:01.1.1.1' does not appear to be an IPv4 or IPv6 address

Swapped the embedded octet for the canonical no-leading-zero form (25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]) in both branches. Left the standalone ipv4 type to #1904 so these stay independent.

Added the leading-zero cases to the parametrized test. pytest tests/types/test_custom_types.py gives 125 passed.

Thanks @ErenAta16 for filing this with a clean repro.

@RobinPicard
RobinPicard force-pushed the fix/ipv6-leading-zero-octets branch from 0b7588c to 4b34b61 Compare July 20, 2026 07:32
@github-actions

Copy link
Copy Markdown

📚 Documentation preview: https://dottxt-ai.github.io/outlines/pr-preview/pr-1936/

Preview updates automatically with each commit.

@RobinPicard RobinPicard left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@RobinPicard
RobinPicard merged commit dfe1015 into dottxt-ai:main Jul 20, 2026
6 checks passed
@ErenAta16

Copy link
Copy Markdown
Contributor

Verified the regex change, [1-9]?[0-9]|1[0-9][0-9] correctly excludes the leading-zero forms while still accepting a bare single digit. Good test coverage on both the rejected and accepted cases. Thanks for picking this up.

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.

types.ipv6 accepts non-canonical, leading-zero IPv4-mapped addresses

3 participants