Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IP Recognizer has bugs when ipv6 contains double colon :: #1476

Open
yungyuchen521 opened this issue Oct 28, 2024 · 0 comments
Open

IP Recognizer has bugs when ipv6 contains double colon :: #1476

yungyuchen521 opened this issue Oct 28, 2024 · 0 comments

Comments

@yungyuchen521
Copy link

yungyuchen521 commented Oct 28, 2024

Describe the bug

The regex doesn't work correctly on ipv6 with double colons ::

Case Example Matches Expected
Valid address with :: A099::09C0:876A:130B ["A099::"] ["A099::09C0:876A:130B"]
Invalid address with multiple :: 2001::25de::cade ["2001::", "25de::"] []

To Reproduce

import re

PATTERN = re.compile(r"\b(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\b")

VALID_IP_WITH_DOUBLE_COLON = "A099::09C0:876A:130B"
INVALID_IP_WITH_MULTI_DOUBLE_COLON = "2001::25de::cade"

print([r for r in PATTERN.finditer(VALID_IP_WITH_DOUBLE_COLON)])
print([r for r in PATTERN.finditer(INVALID_IP_WITH_MULTI_DOUBLE_COLON)])
# output
[<re.Match object; span=(0, 6), match='A099::'>]
[<re.Match object; span=(0, 6), match='2001::'>, <re.Match object; span=(6, 12), match='25de::'>]

Fixes

Valid address with ::

Root Cause

The regex begins with {1,7}::, then {1,6}::{1}, then {1,5}::{1,2}, ..., and finally {1}::{1,6} and ::{1,7}. For A099::09C0:876A:130B, it stops when seeing A099:: (corresponds to {1,7}::). But it should match {1}::{1,6}.

Solution

Reverse the order of patterns with ::
i.e. ([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)
-> :((:[0-9a-fA-F]{1,4}){1,7}|:)|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:

Invalid address with multiple ::

Root Cause

\b treats colon : as a non-word character. Consequently, 1:, :2 and e: in 2001::25de::cade are consider word boundary.

Solution

Replace \b with a look-around which denies :
i.e. \b......\b -> (?<![\w:])......(?![\w:])

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

No branches or pull requests

1 participant