-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheaders_test.go
More file actions
344 lines (285 loc) · 12.5 KB
/
Copy pathheaders_test.go
File metadata and controls
344 lines (285 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"gofr.dev/pkg/gofr/datasource/file"
"gofr.dev/pkg/gofr/logging"
)
// Verbatim excerpt of the `_headers` file zop.dev has published for months —
// every header in it silently discarded, because nothing on the serving path
// read the file. Using the real shape keeps the parser honest about comments,
// blank-line separation, two-space indentation and bare `/` patterns.
const realHeadersFile = `# Security headers applied site-wide.
# CSP is intentionally NOT set here.
/*
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=(), interest-cohort=()
/_astro/*
Cache-Control: public, max-age=31536000, immutable
/images/*
Cache-Control: public, max-age=2592000
/docs/*.html
Cache-Control: public, max-age=60, stale-while-revalidate=300
/*.html
Cache-Control: public, max-age=300
/
Cache-Control: public, max-age=3600, stale-while-revalidate=86400
/robots.txt
Cache-Control: public, max-age=3600
`
func TestParseHeaderRules(t *testing.T) {
rules := parseHeaderRules(realHeadersFile)
assert.Len(t, rules, 7, "one rule per pattern block")
tests := []struct {
name string
urlPath string
want map[string]string
}{
{
"site-wide security headers reach every page",
"/pricing",
map[string]string{
"X-Frame-Options": "DENY",
"X-Content-Type-Options": "nosniff",
"Referrer-Policy": "strict-origin-when-cross-origin",
// The value must survive commas and parentheses intact.
"Permissions-Policy": "camera=(), microphone=(), geolocation=(), interest-cohort=()",
// Semicolons must not be treated as separators either.
"Strict-Transport-Security": "max-age=31536000; includeSubDomains; preload",
},
},
{
"hashed assets get the immutable cache",
"/_astro/app.DY-PF2h0.js",
map[string]string{
"Cache-Control": "public, max-age=31536000, immutable",
"X-Frame-Options": "DENY",
},
},
{
"images get their own shorter cache",
"/images/blog/x.webp",
map[string]string{"Cache-Control": "public, max-age=2592000"},
},
{
"the root pattern matches only the root",
"/",
map[string]string{"Cache-Control": "public, max-age=3600, stale-while-revalidate=86400"},
},
{
"an exact path matches",
"/robots.txt",
map[string]string{"Cache-Control": "public, max-age=3600"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
header := http.Header{}
rules.apply(header, tt.urlPath)
for name, want := range tt.want {
assert.Equal(t, want, header.Get(name), name)
}
})
}
}
// Later blocks override earlier ones, so `/docs/x.html` must end up with the
// docs cache and not the generic `/*.html` one that follows it in file order.
func TestHeaderRulesPrecedence(t *testing.T) {
rules := parseHeaderRules(realHeadersFile)
header := http.Header{}
rules.apply(header, "/about/index.html")
assert.Equal(t, "public, max-age=300", header.Get("Cache-Control"), "generic html rule")
header = http.Header{}
rules.apply(header, "/docs/zopnight/introduction.html")
// /docs/*.html appears before /*.html, so the generic rule wins by order —
// this pins the documented precedence rather than an assumed one.
assert.Equal(t, "public, max-age=300", header.Get("Cache-Control"))
}
func TestParseHeaderRulesMalformed(t *testing.T) {
tests := []struct {
name string
content string
want int
}{
{"empty file", "", 0},
{"comments only", "# a\n# b\n", 0},
{"header before any pattern is dropped", " X-Foo: bar\n", 0},
{"pattern with no headers still parses", "/*\n", 1},
{"header line without a colon is skipped", "/*\n NotAHeader\n X-Ok: 1\n", 1},
{"blank header name is skipped", "/*\n : value\n", 1},
{"blank header value is skipped", "/*\n X-Empty:\n", 1},
{"CRLF line endings", "/*\r\n X-Ok: 1\r\n", 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Len(t, parseHeaderRules(tt.content), tt.want)
})
}
// A malformed line must not cost the file its other headers.
rules := parseHeaderRules("/*\n NotAHeader\n X-Ok: 1\n")
header := http.Header{}
rules.apply(header, "/anything")
assert.Equal(t, "1", header.Get("X-Ok"))
}
func TestHeaderValuesContainingColons(t *testing.T) {
// Only the first colon separates name from value.
rules := parseHeaderRules("/*\n Content-Security-Policy: default-src https://a.test; img-src *\n")
header := http.Header{}
rules.apply(header, "/x")
assert.Equal(t, "default-src https://a.test; img-src *", header.Get("Content-Security-Policy"))
}
func TestNoHeadersFileIsANoOp(t *testing.T) {
dir := setupTestDir(t)
fs := file.NewLocalFileSystem(logging.NewMockLogger(logging.ERROR))
assert.Empty(t, loadHeaderRules(fs, dir), "a site without _headers gets no rules")
h := &staticFileHandler{fs: fs, staticFilePath: dir, defaultExtension: ".html", next: http.NotFoundHandler()}
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/style.css", http.NoBody)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Empty(t, rec.Header().Get("X-Frame-Options"))
assert.Empty(t, rec.Header().Get("Cache-Control"))
}
func TestServeHTTPAppliesHeaderRules(t *testing.T) {
dir := setupTestDir(t)
writeFile(t, dir, headersFileName, realHeadersFile)
fs := file.NewLocalFileSystem(logging.NewMockLogger(logging.ERROR))
rules := loadHeaderRules(fs, dir)
assert.Len(t, rules, 7, "rules load from disk")
newHandler := func() *staticFileHandler {
return &staticFileHandler{
fs: fs, staticFilePath: dir, defaultExtension: ".html",
next: http.NotFoundHandler(), headerRules: rules,
}
}
t.Run("a served page carries the site-wide security headers", func(t *testing.T) {
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/style.css", http.NoBody)
rec := httptest.NewRecorder()
newHandler().ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "DENY", rec.Header().Get("X-Frame-Options"))
assert.Equal(t, "nosniff", rec.Header().Get("X-Content-Type-Options"))
})
// Netlify applies _headers to error responses too, and a 404 that leaks
// framing protection is exactly as exploitable as a 200 that does.
t.Run("a 404 carries them too", func(t *testing.T) {
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/nope", http.NoBody)
rec := httptest.NewRecorder()
newHandler().ServeHTTP(rec, req)
assert.Equal(t, http.StatusNotFound, rec.Code)
assert.Equal(t, "DENY", rec.Header().Get("X-Frame-Options"))
})
// ...but not its caching. A site's Cache-Control describes the pages it
// publishes; applying a page rule to a miss would pin a file that is merely
// not propagated yet into every cache downstream for the rule's lifetime.
//
// `/missing.html` is the path that proves it: it matches the `/*.html`
// block, so without the fix the 404 inherits max-age=300. An extensionless
// miss would not — it only matches `/*`, which declares no Cache-Control.
t.Run("a 404 does not inherit the site's Cache-Control", func(t *testing.T) {
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/missing.html", http.NoBody)
rec := httptest.NewRecorder()
newHandler().ServeHTTP(rec, req)
assert.Equal(t, http.StatusNotFound, rec.Code)
assert.Empty(t, rec.Header().Get("Cache-Control"), "a miss must not be cacheable by a page rule")
assert.Equal(t, "DENY", rec.Header().Get("X-Frame-Options"), "security headers still apply")
// The same rule really does apply on a hit — otherwise this proves nothing.
hit := httptest.NewRecorder()
hitReq := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/docs.html", http.NoBody)
newHandler().ServeHTTP(hit, hitReq)
assert.Equal(t, "public, max-age=300", hit.Header().Get("Cache-Control"),
"control: /*.html caches a page that exists")
})
// The rules must not be able to strip Vary and let a CDN cross-serve
// markdown to a browser.
t.Run("Vary: Accept survives on a negotiable route", func(t *testing.T) {
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/docs", http.NoBody)
rec := httptest.NewRecorder()
newHandler().ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code, "control: /docs resolves to docs.html")
assert.Equal(t, "Accept", rec.Header().Get("Vary"))
})
// A hashed bundle can never resolve to markdown, so keying caches on Accept
// would fragment them for nothing — on exactly the responses the same
// _headers file marks immutable.
t.Run("no Vary on an immutable asset", func(t *testing.T) {
writeFile(t, dir, "_astro/app.DY-PF2h0.js", "console.log(1)")
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/_astro/app.DY-PF2h0.js", http.NoBody)
rec := httptest.NewRecorder()
newHandler().ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code, "control: the asset is really served")
assert.Contains(t, rec.Header().Get("Cache-Control"), "immutable", "control: the asset rule applies")
assert.Empty(t, rec.Header().Get("Vary"), "an unnegotiable asset must not fragment caches")
})
}
// TestWellKnownCarriesHeaderRules covers the paths this server hands off rather
// than serves. .well-known is delegated so that ACME challenges are not given an
// extension or swallowed by the SPA fallback — but a site declaring
// X-Frame-Options for `/*` means the whole site, and a delegated path is still
// one this server answered for.
func TestWellKnownCarriesHeaderRules(t *testing.T) {
dir := setupTestDir(t)
writeFile(t, dir, headersFileName, realHeadersFile)
fs := file.NewLocalFileSystem(logging.NewMockLogger(logging.ERROR))
rules := loadHeaderRules(fs, dir)
newHandler := func(next http.Handler) *staticFileHandler {
return &staticFileHandler{
fs: fs, staticFilePath: dir, defaultExtension: ".html",
next: next, headerRules: rules,
}
}
// The delegate still decides the body and status: passing through must not
// mean passing through unprotected.
t.Run("a delegated response carries the site-wide security headers", func(t *testing.T) {
served := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("acme-challenge-token"))
})
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet,
"/.well-known/acme-challenge/token.html", http.NoBody)
rec := httptest.NewRecorder()
newHandler(served).ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "acme-challenge-token", rec.Body.String(), "the delegate still writes the body")
assert.Equal(t, "DENY", rec.Header().Get("X-Frame-Options"))
assert.Equal(t, "nosniff", rec.Header().Get("X-Content-Type-Options"))
})
// A delegate that succeeds is serving a real file, so it keeps the caching
// the site asked for.
t.Run("a delegated 200 keeps the site's Cache-Control", func(t *testing.T) {
served := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet,
"/.well-known/security.html", http.NoBody)
rec := httptest.NewRecorder()
newHandler(served).ServeHTTP(rec, req)
assert.Equal(t, "public, max-age=300", rec.Header().Get("Cache-Control"),
"a real delegated file is still a page the site publishes")
})
// ...but a delegated miss must not be cached, for the same reason a directly
// served miss must not be.
t.Run("a delegated 404 does not inherit Cache-Control", func(t *testing.T) {
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet,
"/.well-known/acme-challenge/absent.html", http.NoBody)
rec := httptest.NewRecorder()
newHandler(http.NotFoundHandler()).ServeHTTP(rec, req)
assert.Equal(t, http.StatusNotFound, rec.Code)
assert.Empty(t, rec.Header().Get("Cache-Control"))
assert.Equal(t, "DENY", rec.Header().Get("X-Frame-Options"), "security headers still apply")
})
// Delegation itself must survive: this path is how ACME issues certificates.
t.Run("the path is still handed to the delegate untouched", func(t *testing.T) {
var gotPath string
spy := http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { gotPath = r.URL.Path })
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet,
"/.well-known/acme-challenge/xyz", http.NoBody)
newHandler(spy).ServeHTTP(httptest.NewRecorder(), req)
assert.Equal(t, "/.well-known/acme-challenge/xyz", gotPath, "no extension, no rewriting")
})
}