diff --git a/modules/caddyhttp/fileserver/staticfiles.go b/modules/caddyhttp/fileserver/staticfiles.go index 284a1752e..43dd63aa9 100644 --- a/modules/caddyhttp/fileserver/staticfiles.go +++ b/modules/caddyhttp/fileserver/staticfiles.go @@ -29,6 +29,7 @@ import ( "runtime" "strconv" "strings" + "syscall" "time" "unicode/utf8" @@ -689,6 +690,11 @@ func (fsrv *FileServer) mapDirOpenError(fileSystem fs.FS, originalErr error, nam return originalErr } + // a regular file used as a directory, e.g. /index.php/foo + if errors.Is(originalErr, syscall.ENOTDIR) { + return fs.ErrNotExist + } + if _, ok := errors.AsType[*fs.PathError](originalErr); ok { return fs.ErrInvalid } diff --git a/modules/caddyhttp/fileserver/staticfiles_test.go b/modules/caddyhttp/fileserver/staticfiles_test.go index a588dcb2b..e7c942f07 100644 --- a/modules/caddyhttp/fileserver/staticfiles_test.go +++ b/modules/caddyhttp/fileserver/staticfiles_test.go @@ -18,6 +18,7 @@ import ( "bytes" "compress/gzip" "context" + "errors" "fmt" "net/http" "net/http/httptest" @@ -29,6 +30,7 @@ import ( "time" "github.com/caddyserver/caddy/v2" + "github.com/caddyserver/caddy/v2/modules/caddyhttp" "github.com/caddyserver/caddy/v2/modules/caddyhttp/encode" ) @@ -440,3 +442,50 @@ func (p testPrecompressed) AcceptEncoding() string { func (p testPrecompressed) Suffix() string { return p.suffix } + +// A path below a regular file does not exist, so it is a 404 that pass_thru +// falls through, while a path containing a NUL byte is malformed and is a 400. +func TestServeHTTPStatErrors(t *testing.T) { + root := t.TempDir() + if err := os.WriteFile(filepath.Join(root, "file.txt"), nil, 0o600); err != nil { + t.Fatal(err) + } + + fsrv := FileServer{Root: root} + ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()}) + defer cancel() + if err := fsrv.Provision(ctx); err != nil { + t.Fatal(err) + } + + for i, tc := range []struct { + path string + passThru bool + wantStatus int + wantNext bool + }{ + {path: "/file.txt/child", wantStatus: http.StatusNotFound}, + {path: "/file.txt/child", passThru: true, wantNext: true}, + {path: "/file.txt%00", wantStatus: http.StatusBadRequest}, + } { + fsrv.PassThru = tc.passThru + r := httptest.NewRequest(http.MethodGet, tc.path, nil) + r = r.WithContext(context.WithValue(r.Context(), caddy.ReplacerCtxKey, caddy.NewReplacer())) + var calledNext bool + next := caddyhttp.HandlerFunc(func(http.ResponseWriter, *http.Request) error { + calledNext = true + return nil + }) + + err := fsrv.ServeHTTP(httptest.NewRecorder(), r, next) + if tc.wantNext { + if err != nil || !calledNext { + t.Errorf("Test %d: expected the next handler, got called=%t err=%v", i, calledNext, err) + } + continue + } + if he, ok := errors.AsType[caddyhttp.HandlerError](err); !ok || he.StatusCode != tc.wantStatus { + t.Errorf("Test %d: expected status %d, got %v", i, tc.wantStatus, err) + } + } +}