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

chuangbo opened this Issue Feb 14, 2012 · 3 comments


None yet
5 participants

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

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

    >>> validipaddr('')
    >>> validipaddr('')
    >>> validipaddr('192.168.1')
    >>> validipaddr('192 .168. 1.1 ')

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

    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


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