Skip to content

Commit cf22838

Browse files
committed
Randomize the id of every message the resolver sends. (#1395)
Also, provide a method to set the query id to a new value without breaking TSIG if it is in use. Use this method. (cherry picked from commit 269525d)
1 parent e0fce7f commit cf22838

6 files changed

Lines changed: 31 additions & 8 deletions

File tree

‎dns/asyncquery.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ async def _http3(
732732
if url_parts.port is not None:
733733
port = url_parts.port
734734

735-
q.id = 0
735+
q.set_id(0)
736736
wire = q.to_wire()
737737
the_connection: dns.quic.AsyncQuicConnection
738738
if connection:
@@ -804,7 +804,7 @@ async def quic(
804804
if server_hostname is not None and hostname is None:
805805
hostname = server_hostname
806806

807-
q.id = 0
807+
q.set_id(0)
808808
wire = q.to_wire()
809809
the_connection: dns.quic.AsyncQuicConnection
810810
if connection:

‎dns/asyncresolver.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ async def resolve(
8989
nameserver, tcp, backoff = resolution.next_nameserver()
9090
if backoff:
9191
await backend.sleep(backoff)
92+
request.set_random_id()
9293
timeout = self._compute_timeout(start, lifetime, resolution.errors)
9394
try:
9495
response = await nameserver.async_query(

‎dns/message.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,25 @@ def had_tsig(self) -> bool:
801801
def _make_opt(flags=0, payload=DEFAULT_EDNS_PAYLOAD, options=None):
802802
return dns.renderer._make_opt(flags, payload, options)
803803

804+
def set_id(self, id: int):
805+
"""Set the message id to the specified value, and if TSIG is going to be used
806+
to sign this message later, update it accordingly.
807+
808+
:param id: The message id
809+
:type id: int
810+
"""
811+
self.id = id
812+
if self.want_tsig_sign:
813+
assert self.tsig is not None
814+
assert self.tsig.rdtype == dns.rdatatype.TSIG
815+
tsig_rdata = cast(dns.rdtypes.ANY.TSIG.TSIG, self.tsig[0])
816+
self.use_tsig(self.keyring, None, tsig_rdata.fudge, id, tsig_rdata.error, tsig_rdata.other,
817+
tsig_rdata.algorithm)
818+
819+
def set_random_id(self):
820+
"""Assign a newly-generated random query id to the message."""
821+
self.set_id(dns.entropy.random_16())
822+
804823
def use_edns(
805824
self,
806825
edns: int | bool | None = 0,

‎dns/query.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ def _http3(
681681
if url_parts.port is not None:
682682
port = url_parts.port
683683

684-
q.id = 0
684+
q.set_id(0)
685685
wire = q.to_wire()
686686
the_connection: dns.quic.SyncQuicConnection
687687
the_manager: dns.quic.SyncQuicManager
@@ -1444,7 +1444,7 @@ def quic(
14441444
if server_hostname is not None and hostname is None:
14451445
hostname = server_hostname
14461446

1447-
q.id = 0
1447+
q.set_id(0)
14481448
wire = q.to_wire()
14491449
the_connection: dns.quic.SyncQuicConnection
14501450
the_manager: dns.quic.SyncQuicManager

‎dns/resolver.py‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import dns._file_util
3333
import dns.edns
3434
import dns.exception
35-
import dns.flags
3635
import dns.inet
3736
import dns.ipv4
3837
import dns.ipv6
@@ -41,11 +40,9 @@
4140
import dns.nameserver
4241
import dns.query
4342
import dns.rcode
44-
import dns.rdata
4543
import dns.rdataclass
4644
import dns.rdatatype
4745
import dns.rdtypes.ANY.PTR
48-
import dns.rdtypes.svcbbase
4946
import dns.reversename
5047
import dns.tsig
5148

@@ -1317,6 +1314,7 @@ def resolve(
13171314
nameserver, tcp, backoff = resolution.next_nameserver()
13181315
if backoff:
13191316
time.sleep(backoff)
1317+
request.set_random_id()
13201318
timeout = self._compute_timeout(start, lifetime, resolution.errors)
13211319
try:
13221320
response = nameserver.query(

‎doc/whatsnew.rst‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ What's New in dnspython
66
2.9.1 (in development)
77
----------------------
88

9+
* The resolver now assigns a new random query id to every query.
10+
11+
* For transports that require a 0 query id, set it in a way that does not break
12+
TSIG.
13+
914
* ``dns.message.QueryMessage.resolve_chaining()`` now caps the minimum TTL of
1015
a negative response with no SOA record at 300 seconds
1116
(``dns.message.NEGATIVE_TTL_WITHOUT_SOA``). Previously the TTL was left at
@@ -135,7 +140,7 @@ What's New in dnspython
135140
signed response failed with ``BadSignature`` and a response with no TSIG at
136141
all was accepted, as the RFC 8945 section 5.4 check for a missing TSIG was
137142
skipped.
138-
143+
139144
* ``dns.query.inbound_xfr()``, ``dns.asyncquery.inbound_xfr()``, and
140145
``dns.query.xfr()`` now check every response message with
141146
``dns.message.Message.is_response()`` and raise ``dns.query.BadResponse`` if

0 commit comments

Comments
 (0)