reject string-form datetime (tag 0) without a UTC offset - #347
Merged
agronholm merged 3 commits intoSep 30, 2026
Merged
Conversation
datetime.fromisoformat also parses offset-less and date-only ISO 8601 strings, so a tag 0 value that violated RFC 3339 decoded to a naive datetime instead of being rejected. Require a UTC offset so the parsed value is always timezone-aware.
agronholm
requested changes
Sep 29, 2026
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.
Changes
decode_datetime_string(tag 0) hands the payload straight todatetime.fromisoformatand returns whatever comes back. That call is a lenient ISO 8601 parser, not an RFC 3339 validator: it happily accepts offset-less strings like2020-01-01T00:00:00and even date-only ones like2020-01-01, decoding them to a naivedatetime. RFC 8949 §3.4.1 ties tag 0 to RFC 3339, which makes the UTC offset mandatory, so a conformant value should always come back timezone-aware. I noticed this while diffing the Rust decoder against the 5.x pure-Python reference, whosetimestamp_rerequires the offset and rejects these strings; the rewrite lost that guard. The practical risk is that an untrusted sender can hand the application a naive datetime where an aware one is guaranteed, which then throwsTypeError: can't compare offset-naive and offset-aware datetimesdownstream or is silently misread as local time.The fix rejects a parsed value whose
tzinfoisNonewith aCBORDecodeError. A well-formed RFC 3339 string always carriesZor a±HH:MMoffset and therefore always parses to an aware datetime, so no valid input is affected; the existing datetime tests (offsets, fractional seconds, the >6-digit truncation cases) all still pass. Keeping the check inside the decoder means callers get the tag 0 aware-instant guarantee without each having to re-validate.Checklist
tests/) which would fail without your patchdocs/), in case of behavior changes or new featuresdocs/versionhistory.rst).