carlos / platform Public

edge: allow four security headers in a static site's _headers

Equipo writes Content-Security-Policy, X-Content-Type-Options,
Referrer-Policy and X-DNS-Prefetch-Control into every devbox preview's
_headers, and ship refused them as "not supported yet" (discussion
1323). Each gets a value check: CSP directive names, nosniff only, the
eight standard referrer policies, on or off. Values are fixed per path,
so the shared cache rules in spec 4.4 are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 839e7a47bf1b91478369a47a21123f7e26a0a869 parent 7f3eb02
4 files changed, +189 −2
  • docs/superpowers/specs/2026-10-29-static-headers-design.md +9 −2
  • internal/edge/headers.go +90 −0
  • internal/edge/headers_test.go +62 −0
  • internal/edge/static_headers_test.go +28 −0
diff --git a/docs/superpowers/specs/2026-10-29-static-headers-design.md b/docs/superpowers/specs/2026-10-29-static-headers-design.md
index 7aa0c651d..9ef9bf205 100644
--- a/docs/superpowers/specs/2026-10-29-static-headers-design.md
+++ b/docs/superpowers/specs/2026-10-29-static-headers-design.md
@@ -1,6 +1,6 @@
# Static sites: response headers by path (`_headers`)
-Status: draft rev 3, 2026-10-29 (rev 2: builder check, two minor points in 3.4 and 4.3; rev 3: Astra R1, findings marked **(R1-n)**; rev 4: Astra R2 READY AFTER FIXES, its three P2s marked **(R2-n)**. Review closed: no further rounds). Discussions #195 (item 2) and #450.
+Status: draft rev 3, 2026-10-29 (rev 2: builder check, two minor points in 3.4 and 4.3; rev 3: Astra R1, findings marked **(R1-n)**; rev 4: Astra R2 READY AFTER FIXES, its three P2s marked **(R2-n)**. Review closed: no further rounds; 2026-11-21: four security headers added to 3.2 for Equipo's devbox previews, discussion 1323). Discussions #195 (item 2) and #450.
Branch `static-headers`. Review budget: at most three Astra rounds; the
review stops at the first round with no P1 (AGENTS.md, "Stopping rules").
@@ -116,6 +116,10 @@ Version 1 allows these, and refuses every other name at ship:
| `Access-Control-Expose-Headers` | a comma-separated list of header names |
| `Cross-Origin-Resource-Policy` | `same-origin`, `same-site` or `cross-origin` |
| `Timing-Allow-Origin` | `*` |
+| `Content-Security-Policy` | policies separated by `,`, each directives separated by `;`; a directive name is letters, digits and `-` (added 2026-11-21, discussion 1323) |
+| `X-Content-Type-Options` | `nosniff` (added 2026-11-21) |
+| `Referrer-Policy` | a comma-separated list of the eight standard policies (added 2026-11-21) |
+| `X-DNS-Prefetch-Control` | `on` or `off` (added 2026-11-21) |
`Access-Control-Allow-Origin` is `*` only because any other value is either
one fixed origin (rarely what a CDN wants) or a list, which needs the edge to
@@ -124,7 +128,10 @@ response sharable (`assetAdmit`, `internal/edge/assetadmit.go`, already
treats `*` as shareable and anything else as not). `Cross-Origin-Resource-Policy`
and `Timing-Allow-Origin` are on the list because a CDN's users need them: a
page with `Cross-Origin-Embedder-Policy: require-corp` cannot load a
-cross-origin file without CORP. Widening the list later is additive.
+cross-origin file without CORP. Widening the list later is additive. The four
+security headers were added for Equipo's devbox previews (discussion 1323):
+each value is fixed per path, the same for every visitor, so none affects
+what the shared cache may store (4.4).
The refusal names the header and says it is not supported yet, so the next
asker files a discussion rather than shipping around it.
diff --git a/internal/edge/headers.go b/internal/edge/headers.go
index b94d06f4e..424927fe6 100644
--- a/internal/edge/headers.go
+++ b/internal/edge/headers.go
@@ -66,6 +66,13 @@ var headerChecks = map[string]func(value string) error{
"Access-Control-Expose-Headers": checkNameList,
"Cross-Origin-Resource-Policy": checkResourcePolicy,
"Timing-Allow-Origin": checkWildcard,
+ // The security headers a page sets for itself (discussion 1323). Each
+ // value is the same for every visitor on a path, so none changes what
+ // the shared response cache may store.
+ "Content-Security-Policy": checkContentSecurityPolicy,
+ "X-Content-Type-Options": checkNosniff,
+ "Referrer-Policy": checkReferrerPolicy,
+ "X-Dns-Prefetch-Control": checkOnOff,
}
// parseHeaders parses a _headers file. It returns the rules it could use and
@@ -309,3 +316,86 @@ func checkResourcePolicy(v string) error {
}
return fmt.Errorf("want same-origin, same-site or cross-origin, not %q", v)
}
+
+// checkContentSecurityPolicy: one or more policies separated by commas, each
+// a list of directives separated by semicolons. A directive is a name of
+// letters, digits and hyphens, then its sources. A trailing semicolon is
+// allowed, as browsers allow it; a policy with no directive is not. Sources
+// are not checked: the value is already visible ASCII, and a browser ignores
+// a source it does not understand.
+func checkContentSecurityPolicy(v string) error {
+ for _, policy := range strings.Split(v, ",") {
+ directives := 0
+ for _, d := range strings.Split(policy, ";") {
+ d = strings.Trim(d, " \t")
+ if d == "" {
+ continue
+ }
+ name, _, _ := strings.Cut(d, " ")
+ if !isDirectiveName(name) {
+ return fmt.Errorf("want directives separated by semicolons, such as \"default-src 'self'\"; %q is not a directive name", name)
+ }
+ directives++
+ }
+ if directives == 0 {
+ return fmt.Errorf("want directives separated by semicolons, such as \"default-src 'self'\", not %q", v)
+ }
+ }
+ return nil
+}
+
+// isDirectiveName reports whether s is a CSP directive name: letters, digits
+// and hyphens (CSP3, section 2.2.1).
+func isDirectiveName(s string) bool {
+ if s == "" {
+ return false
+ }
+ for i := 0; i < len(s); i++ {
+ c := s[i]
+ if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '-') {
+ return false
+ }
+ }
+ return true
+}
+
+// checkNosniff: the one value X-Content-Type-Options has.
+func checkNosniff(v string) error {
+ if !strings.EqualFold(v, "nosniff") {
+ return fmt.Errorf("want nosniff only, not %q", v)
+ }
+ return nil
+}
+
+// referrerPolicies are the policies Referrer-Policy names.
+var referrerPolicies = []string{
+ "no-referrer", "no-referrer-when-downgrade", "origin", "origin-when-cross-origin",
+ "same-origin", "strict-origin", "strict-origin-when-cross-origin", "unsafe-url",
+}
+
+// checkReferrerPolicy: a comma-separated list of policies, the last one a
+// browser knows taking effect, so older browsers can be given a fallback.
+func checkReferrerPolicy(v string) error {
+ for _, p := range strings.Split(v, ",") {
+ p = strings.Trim(p, " \t")
+ known := false
+ for _, rp := range referrerPolicies {
+ if strings.EqualFold(p, rp) {
+ known = true
+ break
+ }
+ }
+ if !known {
+ return fmt.Errorf("want a referrer policy such as no-referrer or strict-origin-when-cross-origin, not %q", p)
+ }
+ }
+ return nil
+}
+
+// checkOnOff: on or off, as X-DNS-Prefetch-Control takes.
+func checkOnOff(v string) error {
+ if !strings.EqualFold(v, "on") && !strings.EqualFold(v, "off") {
+ return fmt.Errorf("want on or off, not %q", v)
+ }
+ return nil
+}
diff --git a/internal/edge/headers_test.go b/internal/edge/headers_test.go
index 9b2f395ae..a6f741397 100644
--- a/internal/edge/headers_test.go
+++ b/internal/edge/headers_test.go
@@ -248,3 +248,65 @@ func TestPackDirRefusesABadHeadersFile(t *testing.T) {
t.Fatalf("PackDir with a nested _headers: %v", err)
}
}
+
+// Equipo's devbox previews ship these four on every path (discussion 1323).
+func TestParseHeadersSecurityHeaders(t *testing.T) {
+ file := "/*\n" +
+ " Content-Security-Policy: default-src 'none'; connect-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; media-src 'self'; form-action 'none'; frame-src 'none'; base-uri 'none'; object-src 'none'; frame-ancestors https://equipo.example\n" +
+ " X-Content-Type-Options: nosniff\n" +
+ " Referrer-Policy: no-referrer\n" +
+ " X-DNS-Prefetch-Control: off\n"
+ hs, err := parseHeaders([]byte(file))
+ if err != nil {
+ t.Fatalf("parseHeaders: %v", err)
+ }
+ got := hs.lookup("a.example", "/index.html")
+ for name, want := range map[string]string{
+ "Content-Security-Policy": "default-src 'none'; connect-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self'; media-src 'self'; form-action 'none'; frame-src 'none'; base-uri 'none'; object-src 'none'; frame-ancestors https://equipo.example",
+ "X-Content-Type-Options": "nosniff",
+ "Referrer-Policy": "no-referrer",
+ "X-Dns-Prefetch-Control": "off",
+ } {
+ if got.Get(name) != want {
+ t.Errorf("%s = %q, want %q", name, got.Get(name), want)
+ }
+ }
+}
+
+func TestParseHeadersSecurityHeaderValues(t *testing.T) {
+ for _, tc := range []struct {
+ name, value string
+ ok bool
+ }{
+ {"Content-Security-Policy", "default-src 'self'", true},
+ {"Content-Security-Policy", "default-src 'self';", true},
+ {"Content-Security-Policy", "upgrade-insecure-requests; default-src https: 'nonce-abc+/='", true},
+ {"Content-Security-Policy", "default-src 'self', frame-ancestors 'none'", true},
+ {"Content-Security-Policy", "Default-Src 'self'", true},
+ {"Content-Security-Policy", ";", false},
+ {"Content-Security-Policy", "default-src 'self',", false},
+ {"Content-Security-Policy", "'self' default-src", false},
+ {"Content-Security-Policy", "default_src 'self'", false},
+ {"X-Content-Type-Options", "nosniff", true},
+ {"X-Content-Type-Options", "NoSniff", true},
+ {"X-Content-Type-Options", "sniff", false},
+ {"Referrer-Policy", "no-referrer", true},
+ {"Referrer-Policy", "no-referrer, strict-origin-when-cross-origin", true},
+ {"Referrer-Policy", "unsafe-url", true},
+ {"Referrer-Policy", "never", false},
+ {"Referrer-Policy", "no-referrer,", false},
+ {"X-DNS-Prefetch-Control", "off", true},
+ {"X-DNS-Prefetch-Control", "on", true},
+ {"X-DNS-Prefetch-Control", "no", false},
+ } {
+ t.Run(tc.name+"/"+tc.value, func(t *testing.T) {
+ _, err := parseHeaders([]byte("/a\n " + tc.name + ": " + tc.value + "\n"))
+ if tc.ok && err != nil {
+ t.Fatalf("refused: %v", err)
+ }
+ if !tc.ok && (err == nil || strings.Contains(err.Error(), "not supported yet")) {
+ t.Fatalf("err = %v, want the value refused", err)
+ }
+ })
+ }
+}
diff --git a/internal/edge/static_headers_test.go b/internal/edge/static_headers_test.go
index 18d0a9a6c..6c96f0c0f 100644
--- a/internal/edge/static_headers_test.go
+++ b/internal/edge/static_headers_test.go
@@ -357,3 +357,31 @@ func seedSentinel(e *Edge) {
TTL: time.Minute,
})
}
+
+// A devbox preview's security headers (discussion 1323) reach the browser
+// through the real edge, on the page and on a HEAD of it.
+func TestStaticHeadersSecurityHeadersServed(t *testing.T) {
+ files := staticHeadersTree()
+ files[headersFile] = "/*\n" +
+ " Content-Security-Policy: default-src 'none'; style-src 'self'; frame-ancestors https://equipo.example\n" +
+ " X-Content-Type-Options: nosniff\n" +
+ " Referrer-Policy: no-referrer\n" +
+ " X-DNS-Prefetch-Control: off\n"
+ srv, _ := headersEdge(t, files)
+ for _, method := range []string{"GET", "HEAD"} {
+ resp, _ := staticDo(t, srv, method, "/", nil)
+ if resp.StatusCode != http.StatusOK {
+ t.Fatalf("%s / = %d, want 200", method, resp.StatusCode)
+ }
+ for name, want := range map[string]string{
+ "Content-Security-Policy": "default-src 'none'; style-src 'self'; frame-ancestors https://equipo.example",
+ "X-Content-Type-Options": "nosniff",
+ "Referrer-Policy": "no-referrer",
+ "X-Dns-Prefetch-Control": "off",
+ } {
+ if got := resp.Header.Values(name); len(got) != 1 || got[0] != want {
+ t.Errorf("%s /: %s = %q, want %q", method, name, got, want)
+ }
+ }
+ }
+}