fix: clamp current_age to >=0 for future Date headers (RFC 9111 §4.2.3)#3
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes HTTP freshness evaluation when a cached response’s Date header is ahead of the local clock (clock skew), ensuring current_age never becomes negative per RFC 9111 §4.2.3 so future-dated responses aren’t incorrectly treated as fresh.
Changes:
- Introduce
clampedAge(date)to clampnow - dateto>= 0per RFC 9111 §4.2.3. - Use
clampedAgeingetFreshness,isActuallyStale, andcheckStaleIfErrorLifetimeto prevent negative age affecting freshness decisions. - Add a regression test covering the future-
Dateclock-skew scenario.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| httpcache.go | Adds clampedAge helper and applies it to freshness/staleness calculations to prevent negative current_age. |
| httpcache_clockskew_test.go | Adds regression test ensuring a future Date header with no freshness info is treated as stale. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+15
to
+20
| // Date 2s in the future, no Cache-Control and no Expires (lifetime 0). | ||
| respHeaders.Set("Date", time.Now().Add(2*time.Second).UTC().Format(time.RFC1123)) | ||
|
|
||
| if got := getFreshness(respHeaders, http.Header{}); got == fresh { | ||
| t.Fatalf("future Date with no freshness info treated as fresh; want stale (RFC 9111 §4.2.3); got %s", freshnessString(got)) | ||
| } |
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.
Closes #2
Problem
When a response carries a
Dateheader ahead of the local clock (clock skew), the freshness logic computes a negative current age. A response with no freshness information (noCache-Control, noExpires) has a freshness lifetime of0, so the testlifetime > currentAgebecomes0 > -2s→true, and the response is wrongly markedfreshand served from cache.RFC 9111 §4.2.3
The age algorithm clamps the apparent age:
so
current_agecan never be negative. Themax(0, ...)was missing from the three freshness sites (getFreshness,isActuallyStale,checkStaleIfErrorLifetime), all of which usedclock.since(date)directly.Change
Add a small
clampedAge(date)helper (max(0, now - date)) and use it at the three sites. With the clamp, a future-dated response with no freshness info evaluates0 > 0→false→ correctly stale.A regression test (
TestFreshnessFutureDateClockSkew) is added; it fails before the change and passes after. Full test suite remains green.🤖 Generated with Claude Code