From e3da6b07a0729db9ba5bc6b09952add79206c7e5 Mon Sep 17 00:00:00 2001 From: Samuel Merritt Date: Fri, 3 May 2013 14:08:08 -0700 Subject: [PATCH] Skip IPv6 addresses in cname_lookup middleware. It already skips IPv4 addresses, and since IPv6 is the future of the Internet*, we should probably do the right thing with those too. * IPv6: Just two years away for over fifteen years! Change-Id: I54f1db4e936fd38d05ac8b5c709efba76525b9d2 --- swift/common/middleware/cname_lookup.py | 8 ++++++-- test/unit/common/middleware/test_cname_lookup.py | 6 +++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/swift/common/middleware/cname_lookup.py b/swift/common/middleware/cname_lookup.py index a29a86e9cf..31574bc6a4 100644 --- a/swift/common/middleware/cname_lookup.py +++ b/swift/common/middleware/cname_lookup.py @@ -62,10 +62,14 @@ def lookup_cname(domain): # pragma: no cover def is_ip(domain): try: - socket.inet_aton(domain) + socket.inet_pton(socket.AF_INET, domain) return True except socket.error: - return False + try: + socket.inet_pton(socket.AF_INET6, domain) + return True + except socket.error: + return False class CNAMELookupMiddleware(object): diff --git a/test/unit/common/middleware/test_cname_lookup.py b/test/unit/common/middleware/test_cname_lookup.py index cfe1366a46..b64104e210 100644 --- a/test/unit/common/middleware/test_cname_lookup.py +++ b/test/unit/common/middleware/test_cname_lookup.py @@ -47,7 +47,6 @@ class TestCNAMELookup(unittest.TestCase): {'lookup_depth': 2}) def test_pass_ip_addresses(self): - cname_lookup.lookup_cname = original_lookup req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'}, @@ -55,6 +54,11 @@ class TestCNAMELookup(unittest.TestCase): resp = self.app(req.environ, start_response) self.assertEquals(resp, 'FAKE APP') + req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'}, + headers={'Host': 'fc00:7ea1:f155::6321:8841'}) + resp = self.app(req.environ, start_response) + self.assertEquals(resp, 'FAKE APP') + def test_passthrough(self): def my_lookup(d):