Support IPv6 in http.cookiejar when deciding whether a string is a HDN or a IP addr (original) (raw)

Hi everyone.

in http.cookiejar we’ve got constant IPV4_RE which is widely used to decide whether a string is a HDN or a IP address

IPV4_RE = re.compile(r"\.\d+$", re.ASCII)

it is used like this:


def liberal_is_HDN(text):
    """Return True if text is a sort-of-like a host domain name.

    For accepting/blocking domains.

    """
    if IPV4_RE.search(text):
        return False
    return True

It seems like it only detects that whether the string ends with a dot and some number. Well IMO

the author wrote that in func is_HDN:

    # XXX
    # This may well be wrong.  Which RFC is HDN defined in, if any (for
    #  the purposes of RFC 2965)?
    # For the current implementation, what about IPv6?  Remember to look
    #  at other uses of IPV4_RE also, if change this.

Since we’ve got stdlib ipaddress after 3.3, why couldn’t we just simply use

import ipaddress
def is_ip(text):
    try:
        ipaddress.ip_address(text)
        return True
    except ValueError:
        return False

to cope with the ip problem?

I want to create a PR about this (change IPV4_RE regex into is_ip()). Please let me know if you have any suggestions. I deeply appreciate it for your time!

ericvsmith (Eric V. Smith) June 17, 2025, 10:42am 2

How is it widely used? Just in this module, or in 3rd party code?

LamentXU (LamentXU) June 17, 2025, 10:57am 3

Just in this moudle, but in various functions

ericvsmith (Eric V. Smith) June 17, 2025, 11:42am 4

Thanks! This seems like a reasonable change to me, though I’ll include the caveat that I’ve never looked at the code.

LamentXU (LamentXU) June 17, 2025, 2:11pm 5

I’ve already created a issue and a PR (which is closed because it has already been there for a while tho lol, nvm, it can be reopened at anytime). Maybe I’ll reopen it after you review the codes? Thank you for your effort!

ericvsmith (Eric V. Smith) June 17, 2025, 11:02pm 6

I’m not really the right person to look at it, but I’ll try to take a look as time allows.