Skip cname_lookup when host is an IP address

cname_lookup cannot resolve IP addresses as CNAME records, and therefore
should not attempt to resolve the host in that case. The middleware is
skipped when the host is an IP address.

Change-Id: I6961ec205e771116ace1ebcb8c088f3116eb38f0
Fixes: bug #1172289
This commit is contained in:
Matthieu Huin 2013-04-24 18:07:01 +02:00
parent b9a6bcb431
commit b61b177a3f
3 changed files with 24 additions and 0 deletions

View File

@ -27,6 +27,8 @@ maximum lookup depth. If a match is found, the environment's Host header is
rewritten and the request is passed further down the WSGI chain.
"""
import socket
try:
import dns.resolver
from dns.exception import DNSException
@ -58,6 +60,14 @@ def lookup_cname(domain): # pragma: no cover
return 0, None
def is_ip(domain):
try:
socket.inet_aton(domain)
return True
except socket.error:
return False
class CNAMELookupMiddleware(object):
"""
CNAME Lookup Middleware
@ -93,6 +103,8 @@ class CNAMELookupMiddleware(object):
given_domain, port = given_domain.rsplit(':', 1)
if given_domain == self.storage_domain[1:]: # strip initial '.'
return self.app(env, start_response)
if is_ip(given_domain):
return self.app(env, start_response)
a_domain = given_domain
if not a_domain.endswith(self.storage_domain):
if self.memcache is None:

View File

@ -36,6 +36,8 @@ def start_response(*args):
pass
original_lookup = cname_lookup.lookup_cname
class TestCNAMELookup(unittest.TestCase):
def setUp(self):
@ -44,6 +46,15 @@ class TestCNAMELookup(unittest.TestCase):
self.app = cname_lookup.CNAMELookupMiddleware(FakeApp(),
{'lookup_depth': 2})
def test_pass_ip_addresses(self):
cname_lookup.lookup_cname = original_lookup
req = Request.blank('/', environ={'REQUEST_METHOD': 'GET'},
headers={'Host': '10.134.23.198'})
resp = self.app(req.environ, start_response)
self.assertEquals(resp, 'FAKE APP')
def test_passthrough(self):
def my_lookup(d):

View File

@ -1,3 +1,4 @@
dnspython>=1.10.0
eventlet>=0.9.15
greenlet>=0.3.1
netifaces>=0.5