diff --git a/tornado/netutil.py b/tornado/netutil.py index 3703718043..9dc8506ebf 100644 --- a/tornado/netutil.py +++ b/tornado/netutil.py @@ -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, diff --git a/tornado/test/netutil_test.py b/tornado/test/netutil_test.py index cf587bcbd8..c47e58fa34 100644 --- a/tornado/test/netutil_test.py +++ b/tornado/test/netutil_test.py @@ -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'))