Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore case in DNS forwards lookup results #2650

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions python/nav/asyncdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Resolver(object):

def __init__(self):
self._resolvers = cycle(
[client.Resolver('/etc/resolv.conf') for _i in range(3)]
[client.Resolver("/etc/resolv.conf") for _i in range(3)]
)
self.results = defaultdict(list)
self._finished = False
Expand Down Expand Up @@ -136,7 +136,7 @@ def lookup(self, name):
"""Returns a deferred object with all records related to hostname"""

if isinstance(name, str):
name = name.encode('idna')
name = name.encode("idna")

resolver = next(self._resolvers)
return [resolver.lookupAddress(name), resolver.lookupIPV6Address(name)]
Expand All @@ -148,7 +148,7 @@ def _extract_records(result, name):

for record_list in result:
for record in record_list:
if str(record.name) == name:
if str(record.name).lower() == name.lower():
if record.type == dns.A:
address_list.append(
socket.inet_ntop(socket.AF_INET, record.payload.address)
Expand Down
42 changes: 42 additions & 0 deletions tests/unittests/asyncdns_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from mock import Mock
from twisted.names import dns

from nav.asyncdns import ForwardResolver

BUICK_NAME = "buick.lab.uninett.no"


def test_forward_lookup_should_work_with_ipv4_results():
record = Mock()
record.name = BUICK_NAME
record.type = dns.A
record.payload.address = b"\xac\x00\x00\x01"
result = [[record]]
result_name, address_list = ForwardResolver._extract_records(result, BUICK_NAME)

assert result_name == BUICK_NAME
assert address_list == ["172.0.0.1"]


def test_forward_lookup_should_work_with_ipv6_results():
record = Mock()
record.name = BUICK_NAME
record.type = dns.AAAA
record.payload.address = b" \x01\r\xb833DDUUffww\x88\x88"
result = [[record]]
result_name, address_list = ForwardResolver._extract_records(result, BUICK_NAME)

assert result_name == BUICK_NAME
assert address_list == ["2001:db8:3333:4444:5555:6666:7777:8888"]


def test_forward_lookup_should_work_with_different_case_name_results():
record = Mock()
record.name = "bUiCk.LaB.uNiNeTt.No"
record.type = dns.A
record.payload.address = b"\x9e&\x98\xa2"
result = [[record]]
result_name, address_list = ForwardResolver._extract_records(result, BUICK_NAME)

assert result_name == BUICK_NAME
assert address_list == ["158.38.152.162"]