Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | package integration |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/caddyserver/caddy/v2/caddytest" |
| 11 | ) |
| 12 | |
| 13 | func TestStripRewritePreservesPathAuthorization(t *testing.T) { |
| 14 | root := t.TempDir() |
| 15 | for name, content := range map[string]string{ |
| 16 | "index.html": "ROOT INDEX SECRET", |
| 17 | "bar": "INNER SECRET", |
| 18 | "secret": "DOT SEGMENT SECRET", |
| 19 | } { |
| 20 | if err := os.WriteFile(filepath.Join(root, name), []byte(content), 0o600); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | tester := caddytest.NewTester(t) |
| 26 | tester.InitServer(fmt.Sprintf(` |
| 27 | { |
| 28 | skip_install_trust |
| 29 | admin localhost:2999 |
| 30 | http_port 9080 |
| 31 | grace_period 1ns |
| 32 | } |
| 33 | http://localhost:9080 { |
| 34 | root * %q |
| 35 | |
| 36 | handle_path /foo* { |
| 37 | basic_auth /* { |
| 38 | alice $2a$14$DCYyqvUO/PWT.UifNF0rbuRkRfjrhHViOfVfJklmqrvWUoYcTiN2q |
| 39 | } |
| 40 | file_server |
| 41 | } |
| 42 | |
| 43 | handle /api* { |
| 44 | uri strip_prefix /api |
| 45 | basic_auth /secret* { |
| 46 | alice $2a$14$DCYyqvUO/PWT.UifNF0rbuRkRfjrhHViOfVfJklmqrvWUoYcTiN2q |
| 47 | } |
| 48 | file_server |
| 49 | } |
| 50 | |
| 51 | handle /suffix* { |
| 52 | uri strip_suffix /suffix |
| 53 | basic_auth /* { |
| 54 | alice $2a$14$DCYyqvUO/PWT.UifNF0rbuRkRfjrhHViOfVfJklmqrvWUoYcTiN2q |
| 55 | } |
| 56 | file_server |
| 57 | } |
| 58 | } |
| 59 | `, filepath.ToSlash(root)), "caddyfile") |
| 60 | |
| 61 | for _, requestPath := range []string{ |
| 62 | "/foo", |
| 63 | "/foo/", |
| 64 | "/foobar", |
| 65 | "/foo/index.html", |
| 66 | "/api/secret", |
| 67 | "/api../secret", |
| 68 | "/api..%2fsecret", |
| 69 | "/suffix", |
| 70 | } { |
| 71 | t.Run(requestPath, func(t *testing.T) { |
| 72 | req, err := http.NewRequest(http.MethodGet, "http://localhost:9080"+requestPath, nil) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | tester.AssertResponseCode(req, http.StatusUnauthorized) |
| 77 | }) |
| 78 | } |
| 79 | } |