Debian Codegraph @

modules/caddyhttp/rewrite/rewrite_test.go

raw symbols SCIP
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package rewrite
16
17 import (
18 "net/http"
19 "reflect"
20 "regexp"
21 "strings"
22 "testing"
23
24 "github.com/caddyserver/caddy/v2"
25 )
26
27 func TestRewrite(t *testing.T) {
28 repl := caddy.NewReplacer()
29
30 for i, tc := range []struct {
31 input, expect *http.Request
32 rule Rewrite
33 }{
34 {
35 input: newRequest(t, "GET", "/"),
36 expect: newRequest(t, "GET", "/"),
37 },
38 {
39 rule: Rewrite{Method: "GET", URI: "/"},
40 input: newRequest(t, "GET", "/"),
41 expect: newRequest(t, "GET", "/"),
42 },
43 {
44 rule: Rewrite{Method: "POST"},
45 input: newRequest(t, "GET", "/"),
46 expect: newRequest(t, "POST", "/"),
47 },
48
49 {
50 rule: Rewrite{URI: "/foo"},
51 input: newRequest(t, "GET", "/"),
52 expect: newRequest(t, "GET", "/foo"),
53 },
54 {
55 rule: Rewrite{URI: "/foo"},
56 input: newRequest(t, "GET", "/bar"),
57 expect: newRequest(t, "GET", "/foo"),
58 },
59 {
60 rule: Rewrite{URI: "foo"},
61 input: newRequest(t, "GET", "/"),
62 expect: newRequest(t, "GET", "foo"),
63 },
64 {
65 rule: Rewrite{URI: "{http.request.uri}"},
66 input: newRequest(t, "GET", "/bar%3Fbaz?c=d"),
67 expect: newRequest(t, "GET", "/bar%3Fbaz?c=d"),
68 },
69 {
70 rule: Rewrite{URI: "{http.request.uri.path}"},
71 input: newRequest(t, "GET", "/bar%3Fbaz"),
72 expect: newRequest(t, "GET", "/bar%3Fbaz"),
73 },
74 {
75 rule: Rewrite{URI: "/foo{http.request.uri.path}"},
76 input: newRequest(t, "GET", "/bar"),
77 expect: newRequest(t, "GET", "/foo/bar"),
78 },
79 {
80 rule: Rewrite{URI: "/index.php?p={http.request.uri.path}"},
81 input: newRequest(t, "GET", "/foo/bar"),
82 expect: newRequest(t, "GET", "/index.php?p=%2Ffoo%2Fbar"),
83 },
84 {
85 rule: Rewrite{URI: "?a=b&{http.request.uri.query}"},
86 input: newRequest(t, "GET", "/"),
87 expect: newRequest(t, "GET", "/?a=b"),
88 },
89 {
90 rule: Rewrite{URI: "/?c=d"},
91 input: newRequest(t, "GET", "/"),
92 expect: newRequest(t, "GET", "/?c=d"),
93 },
94 {
95 rule: Rewrite{URI: "/?c=d"},
96 input: newRequest(t, "GET", "/?a=b"),
97 expect: newRequest(t, "GET", "/?c=d"),
98 },
99 {
100 rule: Rewrite{URI: "?c=d"},
101 input: newRequest(t, "GET", "/foo"),
102 expect: newRequest(t, "GET", "/foo?c=d"),
103 },
104 {
105 rule: Rewrite{URI: "/?c=d"},
106 input: newRequest(t, "GET", "/foo"),
107 expect: newRequest(t, "GET", "/?c=d"),
108 },
109 {
110 rule: Rewrite{URI: "/?{http.request.uri.query}&c=d"},
111 input: newRequest(t, "GET", "/"),
112 expect: newRequest(t, "GET", "/?c=d"),
113 },
114 {
115 rule: Rewrite{URI: "/foo?{http.request.uri.query}&c=d"},
116 input: newRequest(t, "GET", "/"),
117 expect: newRequest(t, "GET", "/foo?c=d"),
118 },
119 {
120 rule: Rewrite{URI: "?{http.request.uri.query}&c=d"},
121 input: newRequest(t, "GET", "/foo"),
122 expect: newRequest(t, "GET", "/foo?c=d"),
123 },
124 {
125 rule: Rewrite{URI: "{http.request.uri.path}?{http.request.uri.query}&c=d"},
126 input: newRequest(t, "GET", "/foo"),
127 expect: newRequest(t, "GET", "/foo?c=d"),
128 },
129 {
130 rule: Rewrite{URI: "{http.request.uri.path}?{http.request.uri.query}&c=d"},
131 input: newRequest(t, "GET", "/foo"),
132 expect: newRequest(t, "GET", "/foo?c=d"),
133 },
134 {
135 rule: Rewrite{URI: "/index.php?{http.request.uri.query}&c=d"},
136 input: newRequest(t, "GET", "/foo"),
137 expect: newRequest(t, "GET", "/index.php?c=d"),
138 },
139 {
140 rule: Rewrite{URI: "?a=b&c=d"},
141 input: newRequest(t, "GET", "/foo"),
142 expect: newRequest(t, "GET", "/foo?a=b&c=d"),
143 },
144 {
145 rule: Rewrite{URI: "/index.php?{http.request.uri.query}&c=d"},
146 input: newRequest(t, "GET", "/?a=b"),
147 expect: newRequest(t, "GET", "/index.php?a=b&c=d"),
148 },
149 {
150 rule: Rewrite{URI: "/index.php?c=d&{http.request.uri.query}"},
151 input: newRequest(t, "GET", "/?a=b"),
152 expect: newRequest(t, "GET", "/index.php?c=d&a=b"),
153 },
154 {
155 rule: Rewrite{URI: "/index.php?{http.request.uri.query}&p={http.request.uri.path}"},
156 input: newRequest(t, "GET", "/foo/bar?a=b"),
157 expect: newRequest(t, "GET", "/index.php?a=b&p=%2Ffoo%2Fbar"),
158 },
159 {
160 rule: Rewrite{URI: "{http.request.uri.path}?"},
161 input: newRequest(t, "GET", "/foo/bar?a=b&c=d"),
162 expect: newRequest(t, "GET", "/foo/bar"),
163 },
164 {
165 rule: Rewrite{URI: "?qs={http.request.uri.query}"},
166 input: newRequest(t, "GET", "/foo?a=b&c=d"),
167 expect: newRequest(t, "GET", "/foo?qs=a%3Db%26c%3Dd"),
168 },
169 {
170 rule: Rewrite{URI: "/foo?{http.request.uri.query}#frag"},
171 input: newRequest(t, "GET", "/foo/bar?a=b"),
172 expect: newRequest(t, "GET", "/foo?a=b#frag"),
173 },
174 {
175 rule: Rewrite{URI: "/foo{http.request.uri}"},
176 input: newRequest(t, "GET", "/bar?a=b"),
177 expect: newRequest(t, "GET", "/foo/bar?a=b"),
178 },
179 {
180 rule: Rewrite{URI: "/foo{http.request.uri}"},
181 input: newRequest(t, "GET", "/bar"),
182 expect: newRequest(t, "GET", "/foo/bar"),
183 },
184 {
185 rule: Rewrite{URI: "/foo{http.request.uri}?c=d"},
186 input: newRequest(t, "GET", "/bar?a=b"),
187 expect: newRequest(t, "GET", "/foo/bar?c=d"),
188 },
189 {
190 rule: Rewrite{URI: "/foo{http.request.uri}?{http.request.uri.query}&c=d"},
191 input: newRequest(t, "GET", "/bar?a=b"),
192 expect: newRequest(t, "GET", "/foo/bar?a=b&c=d"),
193 },
194 {
195 rule: Rewrite{URI: "{http.request.uri}"},
196 input: newRequest(t, "GET", "/bar?a=b"),
197 expect: newRequest(t, "GET", "/bar?a=b"),
198 },
199 {
200 rule: Rewrite{URI: "{http.request.uri.path}bar?c=d"},
201 input: newRequest(t, "GET", "/foo/?a=b"),
202 expect: newRequest(t, "GET", "/foo/bar?c=d"),
203 },
204 {
205 rule: Rewrite{URI: "/i{http.request.uri}"},
206 input: newRequest(t, "GET", "/%C2%B7%E2%88%B5.png"),
207 expect: newRequest(t, "GET", "/i/%C2%B7%E2%88%B5.png"),
208 },
209 {
210 rule: Rewrite{URI: "/i{http.request.uri}"},
211 input: newRequest(t, "GET", "/·∵.png?a=b"),
212 expect: newRequest(t, "GET", "/i/%C2%B7%E2%88%B5.png?a=b"),
213 },
214 {
215 rule: Rewrite{URI: "/i{http.request.uri}"},
216 input: newRequest(t, "GET", "/%C2%B7%E2%88%B5.png?a=b"),
217 expect: newRequest(t, "GET", "/i/%C2%B7%E2%88%B5.png?a=b"),
218 },
219 {
220 rule: Rewrite{URI: "/bar#?"},
221 input: newRequest(t, "GET", "/foo#fragFirst?c=d"), // not a valid query string (is part of fragment)
222 expect: newRequest(t, "GET", "/bar#?"), // I think this is right? but who knows; std lib drops fragment when parsing
223 },
224 {
225 rule: Rewrite{URI: "/bar"},
226 input: newRequest(t, "GET", "/foo#fragFirst?c=d"),
227 expect: newRequest(t, "GET", "/bar#fragFirst?c=d"),
228 },
229 {
230 rule: Rewrite{URI: "/api/admin/panel"},
231 input: newRequest(t, "GET", "/api/admin%2Fpanel"),
232 expect: newRequest(t, "GET", "/api/admin/panel"),
233 },
234
235 {
236 rule: Rewrite{StripPathPrefix: "/prefix"},
237 input: newRequest(t, "GET", "/foo/bar"),
238 expect: newRequest(t, "GET", "/foo/bar"),
239 },
240 {
241 rule: Rewrite{StripPathPrefix: "/prefix"},
242 input: newRequest(t, "GET", "/prefix/foo/bar"),
243 expect: newRequest(t, "GET", "/foo/bar"),
244 },
245 {
246 rule: Rewrite{StripPathPrefix: "prefix"},
247 input: newRequest(t, "GET", "/prefix/foo/bar"),
248 expect: newRequest(t, "GET", "/foo/bar"),
249 },
250 {
251 rule: Rewrite{StripPathPrefix: "/prefix"},
252 input: newRequest(t, "GET", "/prefix"),
253 expect: newRequest(t, "GET", ""),
254 },
255 {
256 rule: Rewrite{StripPathPrefix: "/prefix"},
257 input: newRequest(t, "GET", "/"),
258 expect: newRequest(t, "GET", "/"),
259 },
260 {
261 rule: Rewrite{StripPathPrefix: "/prefix"},
262 input: newRequest(t, "GET", "/prefix/foo%2Fbar"),
263 expect: newRequest(t, "GET", "/foo%2Fbar"),
264 },
265 {
266 rule: Rewrite{StripPathPrefix: "/prefix"},
267 input: newRequest(t, "GET", "/foo/prefix/bar"),
268 expect: newRequest(t, "GET", "/foo/prefix/bar"),
269 },
270 {
271 rule: Rewrite{StripPathPrefix: "//prefix"},
272 // scheme and host needed for URL parser to succeed in setting up test
273 input: newRequest(t, "GET", "http://host//prefix/foo/bar"),
274 expect: newRequest(t, "GET", "http://host/foo/bar"),
275 },
276 {
277 rule: Rewrite{StripPathPrefix: "//prefix"},
278 input: newRequest(t, "GET", "/prefix/foo/bar"),
279 expect: newRequest(t, "GET", "/prefix/foo/bar"),
280 },
281 {
282 rule: Rewrite{StripPathPrefix: "/a%2Fb/c"},
283 input: newRequest(t, "GET", "/a%2Fb/c/d"),
284 expect: newRequest(t, "GET", "/d"),
285 },
286 {
287 rule: Rewrite{StripPathPrefix: "/a%2Fb/c"},
288 input: newRequest(t, "GET", "/a%2fb/c/d"),
289 expect: newRequest(t, "GET", "/d"),
290 },
291 {
292 rule: Rewrite{StripPathPrefix: "/a/b/c"},
293 input: newRequest(t, "GET", "/a%2Fb/c/d"),
294 expect: newRequest(t, "GET", "/d"),
295 },
296 {
297 rule: Rewrite{StripPathPrefix: "/a%2Fb/c"},
298 input: newRequest(t, "GET", "/a/b/c/d"),
299 expect: newRequest(t, "GET", "/a/b/c/d"),
300 },
301 {
302 rule: Rewrite{StripPathPrefix: "//a%2Fb/c"},
303 input: newRequest(t, "GET", "/a/b/c/d"),
304 expect: newRequest(t, "GET", "/a/b/c/d"),
305 },
306
307 {
308 rule: Rewrite{StripPathSuffix: "/suffix"},
309 input: newRequest(t, "GET", "/foo/bar"),
310 expect: newRequest(t, "GET", "/foo/bar"),
311 },
312 {
313 rule: Rewrite{StripPathSuffix: "suffix"},
314 input: newRequest(t, "GET", "/foo/bar/suffix"),
315 expect: newRequest(t, "GET", "/foo/bar/"),
316 },
317 {
318 rule: Rewrite{StripPathSuffix: "suffix"},
319 input: newRequest(t, "GET", "/foo%2Fbar/suffix"),
320 expect: newRequest(t, "GET", "/foo%2Fbar/"),
321 },
322 {
323 rule: Rewrite{StripPathSuffix: "%2fsuffix"},
324 input: newRequest(t, "GET", "/foo%2Fbar%2fsuffix"),
325 expect: newRequest(t, "GET", "/foo%2Fbar"),
326 },
327 {
328 rule: Rewrite{StripPathSuffix: "/suffix"},
329 input: newRequest(t, "GET", "/foo/suffix/bar"),
330 expect: newRequest(t, "GET", "/foo/suffix/bar"),
331 },
332
333 {
334 rule: Rewrite{URISubstring: []substrReplacer{{Find: "findme", Replace: "replaced"}}},
335 input: newRequest(t, "GET", "/foo/bar"),
336 expect: newRequest(t, "GET", "/foo/bar"),
337 },
338 {
339 rule: Rewrite{URISubstring: []substrReplacer{{Find: "findme", Replace: "replaced"}}},
340 input: newRequest(t, "GET", "/foo/findme/bar"),
341 expect: newRequest(t, "GET", "/foo/replaced/bar"),
342 },
343 {
344 rule: Rewrite{URISubstring: []substrReplacer{{Find: "findme", Replace: "replaced"}}},
345 input: newRequest(t, "GET", "/foo/findme%2Fbar"),
346 expect: newRequest(t, "GET", "/foo/replaced%2Fbar"),
347 },
348
349 {
350 rule: Rewrite{PathRegexp: []*regexReplacer{{Find: "/{2,}", Replace: "/"}}},
351 input: newRequest(t, "GET", "/foo//bar///baz?a=b//c"),
352 expect: newRequest(t, "GET", "/foo/bar/baz?a=b//c"),
353 },
354
355 // regression tests for GHSA-j8px-rmrx-76h9: when the rewrite URI
356 // ends with a literal '?', the first-pass placeholder expansion
357 // may produce a path containing attacker-controlled bytes that
358 // then get split at '?' and fed into buildQueryString, which runs
359 // a SECOND placeholder pass. Bytes injected via a header value (or
360 // any other client-controlled placeholder) must not be treated as
361 // placeholder syntax during this second pass.
362 {
363 // literal header value containing placeholder syntax is not re-expanded into query
364 rule: Rewrite{URI: "/serve/{http.request.header.X-Fwd}?"},
365 input: newRequestWithHeader(t, "GET", "/anything", "X-Fwd", "foo?{env.CADDY_REWRITE_TEST_SECRET}=leak"),
366 expect: newRequest(t, "GET", "/serve/foo?%7Benv.CADDY_REWRITE_TEST_SECRET%7D=leak"),
367 },
368 {
369 // literal header value with placeholder syntax in query position is not re-expanded
370 rule: Rewrite{URI: "/serve/{http.request.header.X-Fwd}?"},
371 input: newRequestWithHeader(t, "GET", "/anything", "X-Fwd", "ok?key={env.CADDY_REWRITE_TEST_SECRET}"),
372 expect: newRequest(t, "GET", "/serve/ok?key=%7Benv.CADDY_REWRITE_TEST_SECRET%7D"),
373 },
374 {
375 // literal header value with embedded file placeholder is not re-expanded
376 rule: Rewrite{URI: "/serve/{http.request.header.X-Fwd}?"},
377 input: newRequestWithHeader(t, "GET", "/anything", "X-Fwd", "ok?path={file./etc/passwd}"),
378 expect: newRequest(t, "GET", "/serve/ok?path=%7Bfile./etc/passwd%7D"),
379 },
380 } {
381 // copy the original input just enough so that we can
382 // compare it after the rewrite to see if it changed
383 urlCopy := *tc.input.URL
384 originalInput := &http.Request{
385 Method: tc.input.Method,
386 RequestURI: tc.input.RequestURI,
387 URL: &urlCopy,
388 }
389
390 // populate the replacer just enough for our tests
391 repl.Set("http.request.uri", tc.input.RequestURI)
392 repl.Set("http.request.uri.path", tc.input.URL.Path)
393 repl.Set("http.request.uri.query", tc.input.URL.RawQuery)
394 for field, vals := range tc.input.Header {
395 repl.Set("http.request.header."+field, strings.Join(vals, ","))
396 }
397
398 // we can't directly call Provision() without a valid caddy.Context
399 // (TODO: fix that) so here we ad-hoc compile the regex
400 for _, rep := range tc.rule.PathRegexp {
401 re, err := regexp.Compile(rep.Find)
402 if err != nil {
403 t.Fatal(err)
404 }
405 rep.re = re
406 }
407
408 changed := tc.rule.Rewrite(tc.input, repl)
409
410 if expected, actual := !reqEqual(originalInput, tc.input), changed; expected != actual {
411 t.Errorf("Test %d: Expected changed=%t but was %t", i, expected, actual)
412 }
413 if expected, actual := tc.expect.Method, tc.input.Method; expected != actual {
414 t.Errorf("Test %d: Expected Method='%s' but got '%s'", i, expected, actual)
415 }
416 if expected, actual := tc.expect.RequestURI, tc.input.RequestURI; expected != actual {
417 t.Errorf("Test %d: Expected RequestURI='%s' but got '%s'", i, expected, actual)
418 }
419 if expected, actual := tc.expect.URL.String(), tc.input.URL.String(); expected != actual {
420 t.Errorf("Test %d: Expected URL='%s' but got '%s'", i, expected, actual)
421 }
422 if expected, actual := tc.expect.URL.RequestURI(), tc.input.URL.RequestURI(); expected != actual {
423 t.Errorf("Test %d: Expected URL.RequestURI()='%s' but got '%s'", i, expected, actual)
424 }
425 if expected, actual := tc.expect.URL.Fragment, tc.input.URL.Fragment; expected != actual {
426 t.Errorf("Test %d: Expected URL.Fragment='%s' but got '%s'", i, expected, actual)
427 }
428 }
429 }
430
431 func TestQueryOpsRenameNoOpCases(t *testing.T) {
432 repl := caddy.NewReplacer()
433
434 for i, tc := range []struct {
435 input *http.Request
436 expect map[string][]string
437 ops *queryOps
438 }{
439 {
440 ops: &queryOps{
441 Rename: []queryOpsArguments{{Key: "ID", Val: "id"}},
442 },
443 input: newRequest(t, "GET", "/?page=test&id=5&test=100"),
444 expect: map[string][]string{"id": {"5"}, "page": {"test"}, "test": {"100"}},
445 },
446 {
447 ops: &queryOps{
448 Rename: []queryOpsArguments{{Key: "id", Val: "id"}},
449 },
450 input: newRequest(t, "GET", "/?page=test&id=5&test=100"),
451 expect: map[string][]string{"id": {"5"}, "page": {"test"}, "test": {"100"}},
452 },
453 {
454 ops: &queryOps{
455 Rename: []queryOpsArguments{{Key: "ID", Val: "id"}},
456 },
457 input: newRequest(t, "GET", "/?page=test&ID=5&test=100"),
458 expect: map[string][]string{"id": {"5"}, "page": {"test"}, "test": {"100"}},
459 },
460 {
461 ops: &queryOps{
462 Rename: []queryOpsArguments{{Key: "ID", Val: "id"}},
463 },
464 input: newRequest(t, "GET", "/?page=test&ID=5&id=7&test=100"),
465 expect: map[string][]string{"id": {"5"}, "page": {"test"}, "test": {"100"}},
466 },
467 } {
468 repl.Set("http.request.uri", tc.input.RequestURI)
469 repl.Set("http.request.uri.path", tc.input.URL.Path)
470 repl.Set("http.request.uri.query", tc.input.URL.RawQuery)
471
472 tc.ops.do(tc.input, repl)
473
474 if actual := tc.input.URL.Query(); !reflect.DeepEqual(tc.expect, map[string][]string(actual)) {
475 t.Errorf("Test %d: Expected query=%v but got %v", i, tc.expect, actual)
476 }
477 }
478 }
479
480 func newRequest(t *testing.T, method, uri string) *http.Request {
481 req, err := http.NewRequest(method, uri, nil)
482 if err != nil {
483 t.Fatalf("error creating request: %v", err)
484 }
485 req.RequestURI = req.URL.RequestURI() // simulate incoming request
486 return req
487 }
488
489 func newRequestWithHeader(t *testing.T, method, uri, headerKey, headerVal string) *http.Request {
490 req := newRequest(t, method, uri)
491 req.Header.Set(headerKey, headerVal)
492 return req
493 }
494
495 // reqEqual if r1 and r2 are equal enough for our purposes.
496 func reqEqual(r1, r2 *http.Request) bool {
497 if r1.Method != r2.Method {
498 return false
499 }
500 if r1.RequestURI != r2.RequestURI {
501 return false
502 }
503 if (r1.URL == nil && r2.URL != nil) || (r1.URL != nil && r2.URL == nil) {
504 return false
505 }
506 if r1.URL == nil && r2.URL == nil {
507 return true
508 }
509 return r1.URL.Scheme == r2.URL.Scheme &&
510 r1.URL.Host == r2.URL.Host &&
511 r1.URL.Path == r2.URL.Path &&
512 r1.URL.RawPath == r2.URL.RawPath &&
513 r1.URL.RawQuery == r2.URL.RawQuery &&
514 r1.URL.Fragment == r2.URL.Fragment
515 }