| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
# Copyright (C) 2003-2007, 2009-2011 Nominum, Inc. |
| 4 |
# |
| 5 |
# Permission to use, copy, modify, and distribute this software and its |
| 6 |
# documentation for any purpose with or without fee is hereby granted, |
| 7 |
# provided that the above copyright notice and this permission notice |
| 8 |
# appear in all copies. |
| 9 |
# |
| 10 |
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES |
| 11 |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR |
| 13 |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 16 |
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 |
import random |
| 18 |
import socket |
| 19 |
import unittest |
| 20 |
|
| 21 |
import dns.exception |
| 22 |
|
| 23 |
try: |
| 24 |
import ssl |
| 25 |
|
| 26 |
_have_ssl = True |
| 27 |
except Exception: |
| 28 |
_have_ssl = False |
| 29 |
|
| 30 |
import dns.edns |
| 31 |
import dns.message |
| 32 |
import dns.query |
| 33 |
import dns.quic |
| 34 |
import dns.rdatatype |
| 35 |
import dns.resolver |
| 36 |
|
| 37 |
if dns.query._have_httpx: |
| 38 |
import httpx |
| 39 |
|
| 40 |
import tests.util |
| 41 |
|
| 42 |
resolver_v4_addresses = [] |
| 43 |
resolver_v6_addresses = [] |
| 44 |
family = socket.AF_UNSPEC |
| 45 |
if tests.util.have_ipv4(): |
| 46 |
resolver_v4_addresses = [ |
| 47 |
"1.1.1.1", |
| 48 |
"8.8.8.8", |
| 49 |
# '9.9.9.9', |
| 50 |
] |
| 51 |
family = socket.AF_INET |
| 52 |
if tests.util.have_ipv6(): |
| 53 |
resolver_v6_addresses = [ |
| 54 |
"2606:4700:4700::1111", |
| 55 |
"2001:4860:4860::8888", |
| 56 |
# '2620:fe::fe', |
| 57 |
] |
| 58 |
if family == socket.AF_INET: |
| 59 |
# we have both working, go back to UNSPEC |
| 60 |
family = socket.AF_UNSPEC |
| 61 |
else: |
| 62 |
# v6 only |
| 63 |
family = socket.AF_INET6 |
| 64 |
|
| 65 |
KNOWN_ANYCAST_DOH_RESOLVER_URLS = [ |
| 66 |
"https://cloudflare-dns.com/dns-query", |
| 67 |
"https://dns.google/dns-query", |
| 68 |
# 'https://dns11.quad9.net/dns-query', |
| 69 |
] |
| 70 |
|
| 71 |
KNOWN_ANYCAST_DOH3_RESOLVER_URLS = [ |
| 72 |
"https://cloudflare-dns.com/dns-query", |
| 73 |
"https://dns.google/dns-query", |
| 74 |
] |
| 75 |
|
| 76 |
KNOWN_PAD_AWARE_DOH_RESOLVER_URLS = [ |
| 77 |
"https://cloudflare-dns.com/dns-query", |
| 78 |
"https://dns.google/dns-query", |
| 79 |
] |
| 80 |
|
| 81 |
|
| 82 |
@unittest.skipUnless( |
| 83 |
dns.query._have_httpx and tests.util.is_internet_reachable() and _have_ssl, |
| 84 |
"Python httpx cannot be imported; no DNS over HTTPS (DOH)", |
| 85 |
) |
| 86 |
class DNSOverHTTPSTestCaseHttpx(unittest.TestCase): |
| 87 |
def setUp(self): |
| 88 |
self.session = httpx.Client(http1=True, http2=True, verify=True) |
| 89 |
|
| 90 |
def tearDown(self): |
| 91 |
self.session.close() |
| 92 |
|
| 93 |
@tests.util.retry_on_timeout |
| 94 |
def test_get_request(self): |
| 95 |
nameserver_url = random.choice(KNOWN_ANYCAST_DOH_RESOLVER_URLS) |
| 96 |
q = dns.message.make_query("example.com.", dns.rdatatype.A) |
| 97 |
r = dns.query.https( |
| 98 |
q, |
| 99 |
nameserver_url, |
| 100 |
session=self.session, |
| 101 |
post=False, |
| 102 |
timeout=4, |
| 103 |
family=family, |
| 104 |
) |
| 105 |
self.assertTrue(q.is_response(r)) |
| 106 |
|
| 107 |
@tests.util.retry_on_timeout |
| 108 |
def test_post_request(self): |
| 109 |
nameserver_url = random.choice(KNOWN_ANYCAST_DOH_RESOLVER_URLS) |
| 110 |
q = dns.message.make_query("example.com.", dns.rdatatype.A) |
| 111 |
r = dns.query.https( |
| 112 |
q, |
| 113 |
nameserver_url, |
| 114 |
session=self.session, |
| 115 |
post=True, |
| 116 |
timeout=4, |
| 117 |
family=family, |
| 118 |
) |
| 119 |
self.assertTrue(q.is_response(r)) |
| 120 |
|
| 121 |
@tests.util.retry_on_timeout |
| 122 |
def test_build_url_from_ip(self): |
| 123 |
self.assertTrue(resolver_v4_addresses or resolver_v6_addresses) |
| 124 |
if resolver_v4_addresses: |
| 125 |
nameserver_ip = random.choice(resolver_v4_addresses) |
| 126 |
q = dns.message.make_query("example.com.", dns.rdatatype.A) |
| 127 |
# For some reason Google's DNS over HTTPS fails when you POST to |
| 128 |
# https://8.8.8.8/dns-query |
| 129 |
# So we're just going to do GET requests here |
| 130 |
r = dns.query.https( |
| 131 |
q, nameserver_ip, session=self.session, post=False, timeout=4 |
| 132 |
) |
| 133 |
|
| 134 |
self.assertTrue(q.is_response(r)) |
| 135 |
if resolver_v6_addresses: |
| 136 |
nameserver_ip = random.choice(resolver_v6_addresses) |
| 137 |
q = dns.message.make_query("example.com.", dns.rdatatype.A) |
| 138 |
r = dns.query.https( |
| 139 |
q, nameserver_ip, session=self.session, post=False, timeout=4 |
| 140 |
) |
| 141 |
self.assertTrue(q.is_response(r)) |
| 142 |
|
| 143 |
# This test is temporarily disabled as there's an expired certificate issue on one |
| 144 |
# of the servers, so it fails on the part that is supposed to succeed (2023-07-15). |
| 145 |
|
| 146 |
# def test_bootstrap_address_fails(self): |
| 147 |
# # We test this to see if v4 is available |
| 148 |
# if resolver_v4_addresses: |
| 149 |
# ip = "185.228.168.168" |
| 150 |
# invalid_tls_url = "https://{}/doh/family-filter/".format(ip) |
| 151 |
# valid_tls_url = "https://doh.cleanbrowsing.org/doh/family-filter/" |
| 152 |
# q = dns.message.make_query("example.com.", dns.rdatatype.A) |
| 153 |
# # make sure CleanBrowsing's IP address will fail TLS certificate |
| 154 |
# # check. |
| 155 |
# with self.assertRaises(httpx.ConnectError): |
| 156 |
# dns.query.https(q, invalid_tls_url, session=self.session, timeout=4) |
| 157 |
# # And if we don't mangle the URL, it should work. |
| 158 |
# r = dns.query.https( |
| 159 |
# q, |
| 160 |
# valid_tls_url, |
| 161 |
# session=self.session, |
| 162 |
# bootstrap_address=ip, |
| 163 |
# timeout=4, |
| 164 |
# ) |
| 165 |
# self.assertTrue(q.is_response(r)) |
| 166 |
|
| 167 |
@tests.util.retry_on_timeout |
| 168 |
def test_new_session(self): |
| 169 |
nameserver_url = random.choice(KNOWN_ANYCAST_DOH_RESOLVER_URLS) |
| 170 |
q = dns.message.make_query("example.com.", dns.rdatatype.A) |
| 171 |
r = dns.query.https(q, nameserver_url, timeout=4) |
| 172 |
self.assertTrue(q.is_response(r)) |
| 173 |
|
| 174 |
@tests.util.retry_on_timeout |
| 175 |
def test_resolver(self): |
| 176 |
res = dns.resolver.Resolver(configure=False) |
| 177 |
res.nameservers = ["https://dns.google/dns-query"] |
| 178 |
answer = res.resolve("dns.google", "A") |
| 179 |
seen = set([rdata.address for rdata in answer]) |
| 180 |
self.assertTrue("8.8.8.8" in seen) |
| 181 |
self.assertTrue("8.8.4.4" in seen) |
| 182 |
|
| 183 |
@tests.util.retry_on_timeout |
| 184 |
def test_padded_get(self): |
| 185 |
nameserver_url = random.choice(KNOWN_PAD_AWARE_DOH_RESOLVER_URLS) |
| 186 |
q = dns.message.make_query("example.com.", dns.rdatatype.A, use_edns=0, pad=128) |
| 187 |
r = dns.query.https( |
| 188 |
q, nameserver_url, session=self.session, post=False, timeout=4 |
| 189 |
) |
| 190 |
self.assertTrue(q.is_response(r)) |
| 191 |
# the response should have a padding option |
| 192 |
self.assertIsNotNone(r.opt) |
| 193 |
has_pad = False |
| 194 |
for o in r.opt[0].options: |
| 195 |
if o.otype == dns.edns.OptionType.PADDING: |
| 196 |
has_pad = True |
| 197 |
self.assertTrue(has_pad) |
| 198 |
|
| 199 |
|
| 200 |
@unittest.skipUnless( |
| 201 |
dns.quic.have_quic and tests.util.is_internet_reachable() and _have_ssl, |
| 202 |
"Aioquic cannot be imported; no DNS over HTTP3 (DOH3)", |
| 203 |
) |
| 204 |
class DNSOverHTTP3TestCase(unittest.TestCase): |
| 205 |
@tests.util.retry_on_timeout |
| 206 |
def testDoH3GetRequest(self): |
| 207 |
nameserver_url = random.choice(KNOWN_ANYCAST_DOH3_RESOLVER_URLS) |
| 208 |
q = dns.message.make_query("dns.google.", dns.rdatatype.A) |
| 209 |
r = dns.query.https( |
| 210 |
q, |
| 211 |
nameserver_url, |
| 212 |
post=False, |
| 213 |
timeout=4, |
| 214 |
family=family, |
| 215 |
http_version=dns.query.HTTPVersion.H3, |
| 216 |
) |
| 217 |
self.assertTrue(q.is_response(r)) |
| 218 |
|
| 219 |
@tests.util.retry_on_timeout |
| 220 |
def testDoH3PostRequest(self): |
| 221 |
nameserver_url = random.choice(KNOWN_ANYCAST_DOH3_RESOLVER_URLS) |
| 222 |
q = dns.message.make_query("dns.google.", dns.rdatatype.A) |
| 223 |
r = dns.query.https( |
| 224 |
q, |
| 225 |
nameserver_url, |
| 226 |
post=True, |
| 227 |
timeout=4, |
| 228 |
family=family, |
| 229 |
http_version=dns.query.HTTPVersion.H3, |
| 230 |
) |
| 231 |
self.assertTrue(q.is_response(r)) |
| 232 |
|
| 233 |
@tests.util.retry_on_timeout |
| 234 |
def test_build_url_from_ip(self): |
| 235 |
self.assertTrue(resolver_v4_addresses or resolver_v6_addresses) |
| 236 |
if resolver_v4_addresses: |
| 237 |
nameserver_ip = random.choice(resolver_v4_addresses) |
| 238 |
q = dns.message.make_query("example.com.", dns.rdatatype.A) |
| 239 |
# For some reason Google's DNS over HTTPS fails when you POST to |
| 240 |
# https://8.8.8.8/dns-query |
| 241 |
# So we're just going to do GET requests here |
| 242 |
r = dns.query.https( |
| 243 |
q, |
| 244 |
nameserver_ip, |
| 245 |
post=False, |
| 246 |
timeout=4, |
| 247 |
http_version=dns.query.HTTPVersion.H3, |
| 248 |
) |
| 249 |
self.assertTrue(q.is_response(r)) |
| 250 |
if resolver_v6_addresses: |
| 251 |
nameserver_ip = random.choice(resolver_v6_addresses) |
| 252 |
q = dns.message.make_query("example.com.", dns.rdatatype.A) |
| 253 |
r = dns.query.https( |
| 254 |
q, |
| 255 |
nameserver_ip, |
| 256 |
post=False, |
| 257 |
timeout=4, |
| 258 |
http_version=dns.query.HTTPVersion.H3, |
| 259 |
) |
| 260 |
self.assertTrue(q.is_response(r)) |
| 261 |
|
| 262 |
|
| 263 |
if __name__ == "__main__": |
| 264 |
unittest.main() |