Raise BadTTL for non-decimal Unicode digits in dns.ttl.from_text()#1279
Merged
rthalley merged 1 commit intoJul 3, 2026
Merged
Conversation
from_text() gated its integer parsing on str.isdigit(), then handed the text to int(). str.isdigit() is True for Unicode "digit" characters whose category is No (e.g. the superscript "\u00b2" or the Ethiopic "\u1369"), but int() only accepts decimal digits, so such input escaped the parser's validation and raised a bare ValueError instead of the documented dns.ttl.BadTTL. This was reachable through the public zone-file parser: a resource-record TTL or a $TTL directive containing such a character crashed dns.zone.from_text() with a ValueError rather than a clean dns.exception.SyntaxError. Switch both isdigit() checks to isdecimal(). For a single character, int()-acceptance is exactly equivalent to isdecimal(), and isdecimal() is a strict subset of isdigit(), so every previously valid TTL still parses while the non-decimal digits are now rejected as BadTTL. Add a regression test covering the fast (all-digit) path and the BIND-style units path.
Owner
|
Thanks! I will look at all the other isdigit() calls as they probably should be isdecimal() too. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
dns.ttl.from_text()raises a bareValueErrorinstead of the documenteddns.ttl.BadTTLwhen the text contains a non-decimal Unicode "digit". This also leaks out of the public zone-file parser as a rawValueErrorrather than a cleandns.exception.SyntaxError.Root cause
from_text()gates its integer parsing onstr.isdigit()and then callsint():str.isdigit()returnsTruefor Unicode characters in category No (e.g. the superscript²U+00B2, or the Ethiopic digit፩U+1369), butint()only accepts decimal digits. So such input passes theisdigit()gate and then blows up insideint()with a bareValueError, escaping the parser's own validation.The function's docstring explicitly promises
:raises dns.ttl.BadTTL:, so this is a contract violation.Before (on
main)After
Fix
Switch both
isdigit()checks toisdecimal().This is exactly behaviour-preserving for every valid TTL. For a single character,
int()-acceptance is identical tostr.isdecimal(), andisdecimal()is a strict subset ofisdigit():So no previously valid TTL is rejected; only the non-decimal "digits" that already crashed are now cleanly reported as
BadTTL.Tests
Added
test_non_decimal_unicode_digits, covering both the all-digit fast path and the BIND-style units path ("\u00b2","1\u00b2","\u00b2w","1\u00b2s","\u1369").Proven to guard the bug (stash only the source fix, keep the test):
Verification
tests/test_ttl.py: 10 passedtests/test_zone.py tests/test_tokenizer.py: 169 passedtest_basic_getaddrinfofailure is a pre-existing environment artifact — the local system resolver returns IPv4-mapped IPv6 fordns.google— present onmainbefore this change, unrelated to it).ruff check,ruff format --check,black --checkclean on the changed files.Diff is
+17 / −2acrossdns/ttl.py,tests/test_ttl.py, and adoc/whatsnew.rstbullet.