edge: _headers limits cover every line and path line; Cache-Control quoted commas
The per-line limit now applies to comments and blank lines. The path-line cap counts every path line, usable or not. Cache-Control splits only at commas outside quoted strings.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
internal/edge/headers.go+36 −7internal/edge/headers_test.go+65 −0
Intent
<task-notification> <task-id>ad6f49ee546021a6b</task-id> <tool-use-id>toolu_018BEq42vPBb7kpWhMZdgT4j</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/0f246987-ca6b-4093-b104-f930443a8208/tasks/ad6f49ee546021a6b.output</output-file> <status>completed</status> <summary>Agent "Implement Task 1: _headers parser" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: DONE_WITH_CONCERNS Commits: - c8d4d0fd edge: factor the _redirects source pattern out for reuse - 6e35306e edge: parse _headers for static sites; ship refuses a bad one (#195, #450) Tests: the targeted run (ParseHeaders, HeaderLookup, PackDirRefusesABadHeaders, plus the Redirect, SPAFallback, DirectorySlash and StaticWithoutRedirects groups) passes. `gofmt -l` is clean. RED was a build failure on `undefined: parseHeaders`. Concerns: - Every `go` command failed in the sandbox with `~/.cache/go-build ... read-only file system`, and `GOFLAGS=-mod=mod GOPROXY=off` did not fix it. I ran the `go test` and `go vet` commands with the sandbox disabled and left GOCACHE at its default. Nothing else ran unsandboxed. - I drafted `headers.go` before the tests, then moved it aside and restored it after the RED run, so the failing run was genuine. - amadan task 986 is advanced to Done. Report: /home/paulca/amadan.net/carlos/platform/.claude/worktrees/static-headers/.superpowers/sdd/2026-10-29-static-headers/task-1-report.md</result> <usage><subagent_tokens>85807</subagent_tokens><tool_uses>31</tool_uses><duration_ms>260668</duration_ms></usage> </task-notification>
<task-notification> <task-id>br67f61hv</task-id> <tool-use-id>toolu_01Bh885B8CdDy22Ww2a2H7D6</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/0f246987-ca6b-4093-b104-f930443a8208/tasks/br67f61hv.output</output-file> <status>completed</status> <summary>Background command "Run the Astra review of Task 1" completed (exit code 0)</summary> </task-notification>
Attributed to this commit: 44.9 g CO₂e (5,473,726 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on static-headers, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/edge/headers.go b/internal/edge/headers.go| index 9aaeed23f..b94d06f4e 100644 |
| --- a/internal/edge/headers.go |
| +++ b/internal/edge/headers.go |
| @@ -85,6 +85,7 @@ func parseHeaders(data []byte) (*headerRules, error) { |
| cur *headerRule // the path line being read; nil before one, or after a bad one |
| curPath string // cur's text, for the error naming it |
| seenPath bool // a path line, good or bad, has been read |
| + pathLines int // path lines read, good or bad, against the limit |
| hadHeader bool // cur has had at least one header line, good or bad |
| ) |
| // finish closes the current path line: a rule only if a header survived. |
| @@ -108,7 +109,12 @@ func parseHeaders(data []byte) (*headerRules, error) { |
| n := i + 1 |
| line = strings.TrimSuffix(line, "\r") |
| trimmed := strings.Trim(line, " \t") |
| + // The line limit holds for every line, comments and blanks included. |
| + long := len(line) > headersMaxLine |
| if trimmed == "" || strings.HasPrefix(trimmed, "#") { |
| + if long { |
| + bad(n, fmt.Errorf("longer than %d characters", headersMaxLine)) |
| + } |
| continue |
| } |
| indented := line[0] == ' ' || line[0] == '\t' |
| @@ -116,7 +122,8 @@ func parseHeaders(data []byte) (*headerRules, error) { |
| if !indented { |
| finish() |
| seenPath = true |
| - if len(line) > headersMaxLine { |
| + pathLines++ |
| + if long { |
| bad(n, fmt.Errorf("longer than %d characters", headersMaxLine)) |
| continue |
| } |
| @@ -124,15 +131,17 @@ func parseHeaders(data []byte) (*headerRules, error) { |
| bad(n, fmt.Errorf("path line %q: want a path and nothing else", trimmed)) |
| continue |
| } |
| + if pathLines > headersMaxRules { |
| + // Counted whether or not the earlier lines were usable, so |
| + // the cap is on the file, not on the rules that survived. |
| + bad(n, fmt.Errorf("more than %d path lines", headersMaxRules)) |
| + continue |
| + } |
| pp, err := parsePattern(trimmed) |
| if err != nil { |
| bad(n, err) |
| continue |
| } |
| - if len(hs.rules) == headersMaxRules { |
| - bad(n, fmt.Errorf("more than %d path lines", headersMaxRules)) |
| - continue |
| - } |
| cur, curPath = &headerRule{line: n, pathPattern: pp, set: http.Header{}}, trimmed |
| continue |
| } |
| @@ -145,7 +154,7 @@ func parseHeaders(data []byte) (*headerRules, error) { |
| continue // under a path line that was refused |
| } |
| hadHeader = true |
| - if len(line) > headersMaxLine { |
| + if long { |
| bad(n, fmt.Errorf("longer than %d characters", headersMaxLine)) |
| continue |
| } |
| @@ -232,10 +241,30 @@ func isToken(s string) bool { |
| return true |
| } |
| +// splitOutsideQuotes splits v at the commas that are not inside a quoted |
| +// string, so no-cache="X-One, X-Two" stays one directive. An unterminated |
| +// quote leaves the rest of v in one piece, which the caller then refuses. |
| +func splitOutsideQuotes(v string) []string { |
| + var parts []string |
| + start, quoted := 0, false |
| + for i := 0; i < len(v); i++ { |
| + switch v[i] { |
| + case '"': |
| + quoted = !quoted |
| + case ',': |
| + if !quoted { |
| + parts = append(parts, v[start:i]) |
| + start = i + 1 |
| + } |
| + } |
| + } |
| + return append(parts, v[start:]) |
| +} |
| + |
| // checkCacheControl: a comma-separated list of directives, each a token or |
| // "token=value" where the value is a token or a quoted string. |
| func checkCacheControl(v string) error { |
| - for _, d := range strings.Split(v, ",") { |
| + for _, d := range splitOutsideQuotes(v) { |
| d = strings.Trim(d, " \t") |
| name, val, hasVal := strings.Cut(d, "=") |
| if !isToken(name) { |
diff --git a/internal/edge/headers_test.go b/internal/edge/headers_test.go| index a6fb03f93..9b2f395ae 100644 |
| --- a/internal/edge/headers_test.go |
| +++ b/internal/edge/headers_test.go |
| @@ -103,6 +103,42 @@ func TestParseHeadersLimits(t *testing.T) { |
| t.Errorf("a 2,049-character path line: %v, want \"longer than 2048\"", err) |
| } |
| + // The line limit holds for comments and blank lines too. |
| + for name, line := range map[string]string{ |
| + "comment": "# " + strings.Repeat("x", headersMaxLine-1), |
| + "blank": strings.Repeat(" ", headersMaxLine+1), |
| + "indented one": " # " + strings.Repeat("x", headersMaxLine-3), |
| + } { |
| + if len(line) != headersMaxLine+1 { |
| + t.Fatalf("%s: test line is %d characters, want %d", name, len(line), headersMaxLine+1) |
| + } |
| + file := "/a\n Cache-Control: no-store\n" + line + "\n" |
| + if _, err := parseHeaders([]byte(file)); err == nil || !strings.Contains(err.Error(), "line 3: longer than 2048") { |
| + t.Errorf("a 2,049-character %s line: %v, want \"line 3: longer than 2048\"", name, err) |
| + } |
| + } |
| + |
| + // The path-line cap counts every path line, usable or not: a valid path |
| + // after 100 refused ones is the 101st and is not an active rule. |
| + b.Reset() |
| + for range headersMaxRules { |
| + b.WriteString("/a/*/b\n Cache-Control: no-store\n") |
| + } |
| + b.WriteString("/late\n Cache-Control: no-store\n") |
| + hs, err = parseHeaders([]byte(b.String())) |
| + if err == nil || !strings.Contains(err.Error(), "line 201: more than 100") || len(hs.rules) != 0 { |
| + t.Errorf("a valid path after 100 refused ones: %d rules, err %v; want no rules and \"line 201: more than 100\"", len(hs.rules), err) |
| + } |
| + b.Reset() |
| + for range headersMaxRules { |
| + b.WriteString("/h\n") |
| + } |
| + b.WriteString("/late\n Cache-Control: no-store\n") |
| + hs, err = parseHeaders([]byte(b.String())) |
| + if err == nil || !strings.Contains(err.Error(), "more than 100") || len(hs.rules) != 0 { |
| + t.Errorf("a valid path after 100 headerless ones: %d rules, err %v; want no rules and \"more than 100\"", len(hs.rules), err) |
| + } |
| + |
| big := strings.Repeat("# filler\n", headersMaxBytes/len("# filler\n")+1) |
| if _, err := parseHeaders([]byte(big)); err == nil || !strings.Contains(err.Error(), "the limit is") { |
| t.Errorf("a file over 256 KiB: %v, want \"the limit is\"", err) |
| @@ -139,6 +175,35 @@ func TestHeaderLookupLastRuleWins(t *testing.T) { |
| } |
| } |
| +func TestParseHeadersCacheControlQuotes(t *testing.T) { |
| + for _, ok := range []string{ |
| + `no-cache="X-One, X-Two"`, |
| + `private="Set-Cookie", max-age=60`, |
| + `public, no-cache="a,b,c", immutable`, |
| + } { |
| + if _, err := parseHeaders([]byte("/a\n Cache-Control: " + ok + "\n")); err != nil { |
| + t.Errorf("Cache-Control: %s refused: %v", ok, err) |
| + } |
| + } |
| + hs, _ := parseHeaders([]byte("/a\n Cache-Control: no-cache=\"X-One, X-Two\"\n")) |
| + if got := hs.rules[0].set.Get("Cache-Control"); got != `no-cache="X-One, X-Two"` { |
| + t.Errorf("quoted comma stored as %q", got) |
| + } |
| + for _, bad := range []string{ |
| + `no-cache="X-One`, |
| + `no-cache="X-One, X-Two`, |
| + `no-cache=X-One"`, |
| + `no-cache="a"b"`, |
| + `no-cache="a\\b"`, |
| + `no-cache="a", , public`, |
| + `max-age=`, |
| + } { |
| + if _, err := parseHeaders([]byte("/a\n Cache-Control: " + bad + "\n")); err == nil || !strings.Contains(err.Error(), "Cache-Control") { |
| + t.Errorf("Cache-Control: %s = %v, want a Cache-Control refusal", bad, err) |
| + } |
| + } |
| +} |
| + |
| func TestParseHeadersComments(t *testing.T) { |
| hs, err := parseHeaders([]byte("/a\n Cache-Control: no-store\n # note\n Timing-Allow-Origin: *\n")) |
| if err != nil || len(hs.rules) != 1 || len(hs.rules[0].set) != 2 { |