web.net.validipaddr('192 .168. 1.1 ') should be False (ip address with space) #140

Open
chuangbo opened this Issue Feb 14, 2012 · 3 comments

Comments

Projects
None yet
5 participants

' 192. 168. 1.2 ' is not a valid ip addr :)

Returns True if `address` is a valid IPv4 address.

    >>> validipaddr('192.168.1.1')
    True
    >>> validipaddr('192.168.1.800')
    False
    >>> validipaddr('192.168.1')
    False
    >>> validipaddr('192 .168. 1.1 ')
    False

Looking at the code it seems to be properly handled by int:

try:
    octets = address.split('.')
    if len(octets) != 4:
        return False
    for x in octets:
        if not (0 <= int(x) <= 255):
            return False
except ValueError:
    return False
return True

Why would you want this to fail specifically? For a web-framework I think it's fair to allow for some error recovery such as this, and I'm sure there are other frameworks out there that allow for more rigorous IP address validation.

anandology was assigned Feb 14, 2012

Contributor

benthepoet commented Aug 22, 2012

This method definitely seems weak. If the framework tells someone that an improperly formatted address is valid then they're going to use that address in it's improper form.

This really should be done using a regular expression. That would invalidate an address with spaces while ensuring that there 4 octets in the range of 0 - 255.

Perhaps adding an optional argument strict that panicks when it sees spaces,etc is not a bad idea.
However, I see no 'need' for that.

Using regexps and enforcing stricter and correct checking would definitely be better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment