Skip to content

Commit

Permalink
Check for empty strings and zero bytes in is_valid_ip.
Browse files Browse the repository at this point in the history
  • Loading branch information
bdarnell committed Aug 25, 2013
1 parent 8e0e4e1 commit f4ad3b7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tornado/netutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ def is_valid_ip(ip):
Supports IPv4 and IPv6.
"""
if not ip or '\x00' in ip:
# getaddrinfo resolves empty strings to localhost, and truncates
# on zero bytes.
return False
try:
res = socket.getaddrinfo(ip, 0, socket.AF_UNSPEC,
socket.SOCK_STREAM,
Expand Down
4 changes: 4 additions & 0 deletions tornado/test/netutil_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,7 @@ def test_is_valid_ip(self):
self.assertTrue(not is_valid_ip('localhost'))
self.assertTrue(not is_valid_ip('4.4.4.4<'))
self.assertTrue(not is_valid_ip(' 127.0.0.1'))
self.assertTrue(not is_valid_ip(''))
self.assertTrue(not is_valid_ip(' '))
self.assertTrue(not is_valid_ip('\n'))
self.assertTrue(not is_valid_ip('\x00'))

0 comments on commit f4ad3b7

Please sign in to comment.