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 | // 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 fileserver |
| 16 | |
| 17 | import ( |
| 18 | "context" |
| 19 | "errors" |
| 20 | "net/http" |
| 21 | "net/http/httptest" |
| 22 | "net/url" |
| 23 | "os" |
| 24 | "path/filepath" |
| 25 | "testing" |
| 26 | |
| 27 | "github.com/caddyserver/caddy/v2" |
| 28 | "github.com/caddyserver/caddy/v2/modules/caddyhttp" |
| 29 | ) |
| 30 | |
| 31 | func TestFileServerRejectsShortNameInParentComponent(t *testing.T) { |
| 32 | root := t.TempDir() |
| 33 | ordinaryNames := []string{ |
| 34 | "ordinary~name-with-long-suffix.txt", |
| 35 | "my f~1.txt", |
| 36 | "my file~1.txt", |
| 37 | "café~name.txt", |
| 38 | } |
| 39 | for _, name := range ordinaryNames { |
| 40 | if err := os.WriteFile(filepath.Join(root, name), []byte(name), 0o600); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | fsrv := FileServer{ |
| 46 | Root: root, |
| 47 | CanonicalURIs: new(bool), |
| 48 | } |
| 49 | ctx, _ := caddy.NewContext(caddy.Context{Context: context.Background()}) |
| 50 | if err := fsrv.Provision(ctx); err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | |
| 54 | for _, requestPath := range []string{ |
| 55 | "/PROTEC~1/this-is-a-long-final-filename.txt", |
| 56 | "/public/caf%C3%A9~1/this-is-a-long-final-filename.txt", |
| 57 | } { |
| 58 | t.Run(requestPath, func(t *testing.T) { |
| 59 | err := fsrv.ServeHTTP( |
| 60 | httptest.NewRecorder(), |
| 61 | newPrecompressedRequest(t, requestPath), |
| 62 | nil, |
| 63 | ) |
| 64 | var handlerErr caddyhttp.HandlerError |
| 65 | if !errors.As(err, &handlerErr) { |
| 66 | t.Fatalf("expected HandlerError, got %v", err) |
| 67 | } |
| 68 | if handlerErr.StatusCode != http.StatusBadRequest { |
| 69 | t.Fatalf("status = %d, want %d", handlerErr.StatusCode, http.StatusBadRequest) |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | |
| 74 | for _, name := range ordinaryNames { |
| 75 | t.Run(name, func(t *testing.T) { |
| 76 | w := httptest.NewRecorder() |
| 77 | if err := fsrv.ServeHTTP(w, newPrecompressedRequest(t, "/"+url.PathEscape(name)), nil); err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | if w.Code != http.StatusOK { |
| 81 | t.Fatalf("status = %d, want %d", w.Code, http.StatusOK) |
| 82 | } |
| 83 | if got := w.Body.String(); got != name { |
| 84 | t.Fatalf("body = %q, want %q", got, name) |
| 85 | } |
| 86 | }) |
| 87 | } |
| 88 | } |