edge: delta-seconds must be 1*DIGIT before it may saturate
strconv reports overflow without reading the rest of the argument, so max-age=999999999999999999999oops took the ceiling. Check the syntax first.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
3 files changed,
+66
−17
internal/edge/assetadmit_test.go+12 −0internal/edge/edge.go+24 −17internal/edge/edge_test.go+30 −0
CI failed — run details
diff --git a/internal/edge/assetadmit_test.go b/internal/edge/assetadmit_test.go| index 6d5bdd41b..4641ab14d 100644 |
| --- a/internal/edge/assetadmit_test.go |
| +++ b/internal/edge/assetadmit_test.go |
| @@ -85,6 +85,18 @@ func TestAssetAdmitTable(t *testing.T) { |
| 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: "max-age overflow with a suffix refused", status: 200, declared: true, reason: "no-lifetime", |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=999999999999999999999oops") }}, |
| + {name: "max-age plus sign refused", status: 200, declared: true, reason: "no-lifetime", |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=+5") }}, |
| + {name: "max-age hex refused", status: 200, declared: true, reason: "no-lifetime", |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=0x10") }}, |
| + {name: "s-maxage overflow with a suffix refused", status: 200, declared: true, reason: "no-lifetime", |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=60, s-maxage=999999999999999999999oops") }}, |
| + {name: "s-maxage plus sign refused", status: 200, declared: true, reason: "no-lifetime", |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=60, s-maxage=+5") }}, |
| + {name: "s-maxage hex refused", status: 200, declared: true, reason: "no-lifetime", |
| + mutate: func(h http.Header) { h.Set("Cache-Control", "max-age=60, s-maxage=0x10") }}, |
| {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 d8fa3a125..0e0020e34 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -2184,13 +2184,13 @@ func parseMaxAge(cc string) (time.Duration, bool) { |
| case "no-store", "no-cache", "private": |
| return 0, false |
| case "max-age": |
| - d, ok := parseDeltaSeconds(arg) |
| + d, ok := parseDeltaSeconds(strings.TrimSpace(arg)) |
| if !ok { |
| return 0, false |
| } |
| ttl, found = d, true |
| case "s-maxage": |
| - d, ok := parseDeltaSeconds(arg) |
| + d, ok := parseDeltaSeconds(strings.TrimSpace(arg)) |
| if !ok { |
| return 0, false |
| } |
| @@ -2211,26 +2211,33 @@ func parseMaxAge(cc string) (time.Duration, bool) { |
| // 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. |
| +// parseDeltaSeconds reads a max-age or s-maxage argument. RFC 9111 1.2.2 |
| +// defines delta-seconds as 1*DIGIT: ASCII digits only, so no sign, no spaces, |
| +// no suffix, no hex. Anything else is malformed and refuses the response. |
| +// Syntax is checked before the number is, because strconv reports overflow |
| +// without looking at the characters after it, so "99999999999999999999oops" |
| +// must not reach the saturation below. A well-formed 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; the caller's |
| +// own cap then applies to the ceiling. |
| +// |
| +// parseMaxAge trims the argument first, because the edge has always tolerated |
| +// whitespace around the "=" ("s-maxage = 120") and a test pins it; the number |
| +// itself is strict. |
| 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 |
| - } |
| + if arg == "" { |
| return 0, false |
| } |
| - if secs < 0 { |
| + for i := 0; i < len(arg); i++ { |
| + if arg[i] < '0' || arg[i] > '9' { |
| + return 0, false |
| + } |
| + } |
| + secs, err := strconv.ParseUint(arg, 10, 64) |
| + if err != nil && !errors.Is(err, strconv.ErrRange) { |
| return 0, false |
| } |
| - if secs > int64(maxDeltaSeconds/time.Second) { |
| + if err != nil || secs > uint64(maxDeltaSeconds/time.Second) { |
| return maxDeltaSeconds, true |
| } |
| return time.Duration(secs) * time.Second, true |
diff --git a/internal/edge/edge_test.go b/internal/edge/edge_test.go| index c2643bfe7..89c2f5278 100644 |
| --- a/internal/edge/edge_test.go |
| +++ b/internal/edge/edge_test.go |
| @@ -2349,6 +2349,20 @@ func TestParseMaxAgeOverflow(t *testing.T) { |
| {"max-age=99999999999999999999", maxDeltaSeconds, true}, |
| {"s-maxage=99999999999999999999", maxDeltaSeconds, true}, |
| {"max-age=soon", 0, false}, |
| + {"max-age=", 0, false}, |
| + {"max-age= 5", 5 * time.Second, true}, // whitespace around "=" is tolerated, as it always was |
| + } |
| + // Malformed arguments that Go's number parser would accept or part-read: |
| + // the argument must be 1*DIGIT, nothing else, on either directive. |
| + type row = struct { |
| + cc string |
| + want time.Duration |
| + ok bool |
| + } |
| + for _, bad := range []string{"999999999999999999999oops", "18446744074oops", "+5", "0x10", "5s", "1e3", "5.0", `"5"`} { |
| + for _, dir := range []string{"max-age", "s-maxage"} { |
| + tests = append(tests, row{dir + "=" + bad, 0, false}, row{"max-age=60, " + dir + "=" + bad, 0, false}) |
| + } |
| } |
| for _, tc := range tests { |
| got, ok := parseMaxAge(tc.cc) |
| @@ -2370,3 +2384,19 @@ func TestParseMaxAgeOverflowThroughTheSharedCacheCeiling(t *testing.T) { |
| t.Fatalf("wrapping negative max-age = %v, %v; want refused", ttl, ok) |
| } |
| } |
| + |
| +func TestParseDeltaSecondsIsOneOrMoreDigits(t *testing.T) { |
| + for _, bad := range []string{"", "999999999999999999999oops", "+5", " 5", "5 ", "0x10", "-0", "-1", "5s", "\u0665"} { |
| + if d, ok := parseDeltaSeconds(bad); ok || d != 0 { |
| + t.Errorf("parseDeltaSeconds(%q) = %v, %v; want refused", bad, d, ok) |
| + } |
| + } |
| + for arg, want := range map[string]time.Duration{ |
| + "0": 0, "5": 5 * time.Second, "007": 7 * time.Second, |
| + "99999999999999999999999": maxDeltaSeconds, |
| + } { |
| + if d, ok := parseDeltaSeconds(arg); !ok || d != want { |
| + t.Errorf("parseDeltaSeconds(%q) = %v, %v; want %v, true", arg, d, ok, want) |
| + } |
| + } |
| +} |