carlos / platform Public

edge: parseMaxAge refuses negative seconds and saturates huge ones before converting

A max-age or s-maxage of -9223372037 wrapped into a positive Duration and
18446744074 into about 290 ms. Parse as int64, refuse negatives, saturate to
a named 100-year ceiling so each caller's own cap applies.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 23fc238bc5a5a99431a953e4884c29b274aa7931 parent a895bfe
3 files changed, +100 −6
  • internal/edge/assetadmit_test.go +18 −0
  • internal/edge/edge.go +36 −6
  • internal/edge/edge_test.go +46 −0
diff --git a/internal/edge/assetadmit_test.go b/internal/edge/assetadmit_test.go
index 39b6bb38c..6d5bdd41b 100644
--- a/internal/edge/assetadmit_test.go
+++ b/internal/edge/assetadmit_test.go
@@ -36,6 +36,16 @@ func TestAssetAdmitTable(t *testing.T) {
mutate: func(h http.Header) { h.Set("Cache-Control", "public, max-age=31536000, s-maxage=60") }},
{name: "max-age over a year is capped", status: 200, declared: true, store: true, lifetime: year,
mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=999999999") }},
+ {name: "max-age just over a year is capped", status: 200, declared: true, store: true, lifetime: year,
+ mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=31536001") }},
+ {name: "huge max-age saturates then is capped", status: 200, declared: true, store: true, lifetime: year,
+ mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=18446744074") }},
+ {name: "max-age beyond int64 saturates then is capped", status: 200, declared: true, store: true, lifetime: year,
+ mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=99999999999999999999") }},
+ {name: "huge s-maxage saturates then is capped", status: 200, declared: true, store: true, lifetime: year,
+ mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=60, s-maxage=18446744074") }},
+ {name: "s-maxage just over a year is capped", status: 200, declared: true, store: true, lifetime: year,
+ mutate: func(h http.Header) { h.Set("Cache-Control", "s-maxage=31536001") }},
{name: "vary accept-encoding stores, any case and spacing", status: 200, declared: true, store: true, lifetime: year,
mutate: func(h http.Header) { h.Set("Vary", " accept-ENCODING ") }},
{name: "identity content-encoding stores", status: 200, declared: true, store: true, lifetime: year,
@@ -67,6 +77,14 @@ func TestAssetAdmitTable(t *testing.T) {
mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=600, s-maxage=0") }},
{name: "malformed max-age refused", status: 200, declared: true, reason: "no-lifetime",
mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=soon") }},
+ {name: "negative max-age that wraps positive refused", status: 200, declared: true, reason: "no-lifetime",
+ mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=-9223372037") }},
+ {name: "negative s-maxage that wraps positive refused", status: 200, declared: true, reason: "no-lifetime",
+ mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=60, s-maxage=-9223372037") }},
+ {name: "negative max-age refused", status: 200, declared: true, reason: "no-lifetime",
+ mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=-1") }},
+ {name: "negative beyond int64 refused", status: 200, declared: true, reason: "no-lifetime",
+ mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=-99999999999999999999") }},
{name: "vary origin refused", status: 200, declared: true, reason: "vary",
mutate: func(h http.Header) { h.Set("Vary", "Origin") }},
{name: "vary cookie refused", status: 200, declared: true, reason: "vary",
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index d138d7e2a..d8fa3a125 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -2184,17 +2184,17 @@ func parseMaxAge(cc string) (time.Duration, bool) {
case "no-store", "no-cache", "private":
return 0, false
case "max-age":
- secs, err := strconv.Atoi(strings.TrimSpace(arg))
- if err != nil {
+ d, ok := parseDeltaSeconds(arg)
+ if !ok {
return 0, false
}
- ttl, found = time.Duration(secs)*time.Second, true
+ ttl, found = d, true
case "s-maxage":
- secs, err := strconv.Atoi(strings.TrimSpace(arg))
- if err != nil {
+ d, ok := parseDeltaSeconds(arg)
+ if !ok {
return 0, false
}
- shared, sharedFound = time.Duration(secs)*time.Second, true
+ shared, sharedFound = d, true
}
}
if sharedFound {
@@ -2206,6 +2206,36 @@ func parseMaxAge(cc string) (time.Duration, bool) {
return ttl, true
}
+// maxDeltaSeconds is the ceiling parseDeltaSeconds saturates to: a hundred
+// years, far past any cap a caller applies (five minutes in the shared cache,
+// 365 days for the asset cache) and far short of overflowing a Duration.
+const maxDeltaSeconds = 100 * 365 * 24 * time.Hour
+
+// parseDeltaSeconds reads a max-age or s-maxage argument (RFC 9111 1.2.2: a
+// non-negative integer of seconds). Anything that is not one, including a
+// negative number, is malformed and refuses the response. A number too large
+// for a Duration saturates to maxDeltaSeconds before it is multiplied, so a
+// value like 18446744074 cannot wrap into a few hundred milliseconds and a
+// negative one cannot wrap into a positive lifetime; the caller's own cap then
+// applies to the ceiling.
+func parseDeltaSeconds(arg string) (time.Duration, bool) {
+ arg = strings.TrimSpace(arg)
+ secs, err := strconv.ParseInt(arg, 10, 64)
+ if err != nil {
+ if errors.Is(err, strconv.ErrRange) && !strings.HasPrefix(arg, "-") {
+ return maxDeltaSeconds, true
+ }
+ return 0, false
+ }
+ if secs < 0 {
+ return 0, false
+ }
+ if secs > int64(maxDeltaSeconds/time.Second) {
+ return maxDeltaSeconds, true
+ }
+ return time.Duration(secs) * time.Second, true
+}
+
// hasCCDirective reports whether cc carries the named directive.
func hasCCDirective(cc, want string) bool {
for name := range ccDirectives(cc) {
diff --git a/internal/edge/edge_test.go b/internal/edge/edge_test.go
index 2f92f98dc..c2643bfe7 100644
--- a/internal/edge/edge_test.go
+++ b/internal/edge/edge_test.go
@@ -2324,3 +2324,49 @@ func TestSidecarRouteIsNeverServable(t *testing.T) {
t.Fatalf("GET %s = %d, want 404 — a sidecar must be unreachable", host, code)
}
}
+
+// parseMaxAge multiplies seconds into a Duration; a number that does not fit
+// must neither wrap into a positive lifetime (negative input) nor into a few
+// hundred milliseconds (huge input). Both directives, either order.
+func TestParseMaxAgeOverflow(t *testing.T) {
+ tests := []struct {
+ cc string
+ want time.Duration
+ ok bool
+ }{
+ {"max-age=600", 600 * time.Second, true},
+ {"max-age=-9223372037", 0, false},
+ {"s-maxage=-9223372037", 0, false},
+ {"max-age=60, s-maxage=-9223372037", 0, false},
+ {"s-maxage=-9223372037, max-age=60", 0, false},
+ {"max-age=-1", 0, false},
+ {"max-age=-99999999999999999999", 0, false},
+ {"max-age=18446744074", maxDeltaSeconds, true},
+ {"s-maxage=18446744074", maxDeltaSeconds, true},
+ {"max-age=60, s-maxage=18446744074", maxDeltaSeconds, true},
+ {"max-age=18446744074, s-maxage=60", 60 * time.Second, true},
+ {"max-age=9223372036854775807", maxDeltaSeconds, true},
+ {"max-age=99999999999999999999", maxDeltaSeconds, true},
+ {"s-maxage=99999999999999999999", maxDeltaSeconds, true},
+ {"max-age=soon", 0, false},
+ }
+ for _, tc := range tests {
+ got, ok := parseMaxAge(tc.cc)
+ if got != tc.want || ok != tc.ok {
+ t.Errorf("parseMaxAge(%q) = %v, %v; want %v, %v", tc.cc, got, ok, tc.want, tc.ok)
+ }
+ }
+}
+
+// The shared response cache applies its own ceiling on top of the saturated
+// value, so a huge max-age is held for the ceiling and a wrapping one is not
+// held at all.
+func TestParseMaxAgeOverflowThroughTheSharedCacheCeiling(t *testing.T) {
+ huge, ok := parseMaxAge("max-age=18446744074")
+ if !ok || min(huge, DefaultCacheMaxTTL) != DefaultCacheMaxTTL {
+ t.Fatalf("huge max-age = %v, %v; want it to reach the cache ceiling %v", huge, ok, DefaultCacheMaxTTL)
+ }
+ if ttl, ok := parseMaxAge("max-age=-9223372037"); ok || ttl > 0 {
+ t.Fatalf("wrapping negative max-age = %v, %v; want refused", ttl, ok)
+ }
+}