fix(time): return None on ASN1Time + Duration overflow instead of panicking#252
Merged
chifflier merged 1 commit intoJul 22, 2026
Conversation
…icking `impl Add<Duration> for ASN1Time` returns `Option<ASN1Time>`, but the body was `Some(ASN1Time::new(self.time + rhs))` and `OffsetDateTime + Duration` panics on overflow (internally `checked_add(...).expect(...)`). So `add` aborted the thread where its signature promises `None`; the sibling `Sub` impl already guards and returns `None`. Use `checked_add` so an out-of-range result yields `None`. Add a regression test (max-year notAfter + Duration -> None; a normal date still -> Some). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
chifflier
self-requested a review
July 22, 2026 07:55
chifflier
approved these changes
Jul 22, 2026
Member
|
Applied, thanks! |
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.
fix(time): return
NoneonASN1Time + Durationoverflow instead of panickingWhat
impl Add<Duration> for ASN1Time(src/time.rs) has return typeOption<ASN1Time>, but thebody was
Some(ASN1Time::new(self.time + rhs)).OffsetDateTime + Durationpanics on overflow(internally
checked_add(...).expect(...)), soaddaborts the thread in exactly the case itsOptionsignature exists to report. The siblingimpl Sub<ASN1Time>right below already guardsand returns
None.fn add(self, rhs: Duration) -> Option<ASN1Time> { - Some(ASN1Time::new(self.time + rhs)) + self.time.checked_add(rhs).map(ASN1Time::new) }Why it matters
self.timecan come from a parsed certificate — e.g. anotAfterof99991231235959Z(RFC 5280 §4.1.2.5's conventional "no well-defined expiration"), which parses to a max-year
date. Downstream code doing
cert.validity().not_after + grace_periodon an untrustedcertificate — a natural "is this cert valid N days from now?" pattern — then panics instead of
getting the
Nonethe API promises.I'd frame this as a correctness/robustness fix rather than a security vulnerability:
x509-parseritself never callsAdd(itstime_to_expirationuses the checkedSub), sothe impact is confined to downstream callers that use the operator. Filed alongside issue #251.
Test
Adds
time::tests::test_add_duration_overflow_returns_none:ASN1Time::from(datetime!(9999-12-31 23:59:59 UTC)) + Duration::days(1)→None(was: panic);+ Duration::days(1)→Some(..)(unchanged).Verified locally:
cargo test --lib time::→3 passed; 0 failed.Found & fixed with the rust-in-peace security pipeline.