Skip to content

Verify TSIG MAC before checking timestamp so BadSignature is not pree… - #1293

Merged
rthalley merged 1 commit into
rthalley:mainfrom
adityaanikam:tsig-verify-before-badtime-1287
Aug 21, 2026
Merged

rthalley merged 1 commit into
rthalley:mainfrom
adityaanikam:tsig-verify-before-badtime-1287

Conversation

@adityaanikam

Copy link
Copy Markdown
Contributor

Closes #1287.

validate() checked the timestamp before verifying the MAC, so a request with both a bad signature and a bad timestamp reported BadTime instead of BadSignature, RFC 8945 5.2 expects MAC validity to be established first. rthalley confirmed following the RFC is the right call here, with the behavior change noted in a release note rather than treated as something needing extra caution.

The reorder itself is a two line move, no other logic changed. The existing test_validate had a latent issue that only surfaced once the ordering changed: its BadTime case used a key with a different secret than the one that actually signed the message, so the MAC would also fail, but the old ordering never reached that check, checking time first meant it never mattered. I fixed that case to use the correct signing key so it isolates a genuine timestamp-only failure, and added a new test for the actual scenario from the issue, a bad MAC combined with a bad timestamp now correctly raises BadSignature.

Comment thread tests/test_tsig.py
now = int(time.time())
wrong_key = dns.tsig.Key(keyname, "abcd", "hmac-sha256")
with self.assertRaises(dns.tsig.BadSignature):
dns.tsig.validate(w, wrong_key, keyname, tsig, now + 1000, b"", tsig_start)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly declaring and using a wrong_key might increase readability.

@jschlyter jschlyter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. I've also read the RFCs and agree with the change.

@rthalley
rthalley merged commit 524a489 into rthalley:main Aug 21, 2026
0 of 11 checks passed
@rthalley

Copy link
Copy Markdown
Owner

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TSIG validation raises BadTime before verifying MAC (RFC 8945 §5.2)

3 participants