From 4b34b611b1f4c55d762f0c3bcaa2d092f915834e Mon Sep 17 00:00:00 2001 From: Nishchay Mahor Date: Sun, 19 Jul 2026 23:27:57 -0700 Subject: [PATCH] fix: reject leading-zero octets in ipv6 IPv4-mapped addresses --- src/outlines/types/__init__.py | 8 ++++---- tests/types/test_custom_types.py | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/outlines/types/__init__.py b/src/outlines/types/__init__.py index a81143b2e..6d2d36c4c 100644 --- a/src/outlines/types/__init__.py +++ b/src/outlines/types/__init__.py @@ -154,10 +154,10 @@ def __getattr__(self, name): r"|[0-9A-Fa-f]{1,4}:(?::[0-9A-Fa-f]{1,4}){1,6}" r"|:(?::[0-9A-Fa-f]{1,4}){1,7}" r"|::" - r"|::(?:[Ff]{4}(?::0{1,4})?:)(?:25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})" - r"(?:\.(?:25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})){3}" - r"|(?:[0-9A-Fa-f]{1,4}:){1,4}:(?:25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})" - r"(?:\.(?:25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})){3}" + r"|::(?:[Ff]{4}(?::0{1,4})?:)(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])" + r"(?:\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}" + r"|(?:[0-9A-Fa-f]{1,4}:){1,4}:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])" + r"(?:\.(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}" ) # Document-specific types diff --git a/tests/types/test_custom_types.py b/tests/types/test_custom_types.py index ae2fa5aea..4157a1ec2 100644 --- a/tests/types/test_custom_types.py +++ b/tests/types/test_custom_types.py @@ -113,6 +113,9 @@ (types.ipv6, "2001::db8::1", False), (types.ipv6, "12345::1", False), (types.ipv6, ":::1", False), + (types.ipv6, "::ffff:01.1.1.1", False), + (types.ipv6, "::ffff:00.0.0.0", False), + (types.ipv6, "1:2:3:4::5.6.7.8", True), (types.semver, "1.2.3", True), (types.semver, "1.2", False), (types.semver, "01.2.3", False),