mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-19 22:39:38 +00:00
fileserver: treat a path below a regular file as not found (#8131)
Since #7017, every *fs.PathError from stat becomes fs.ErrInvalid, and that includes ENOTDIR. So a request like /index.php/foo got 400 instead of 404, and pass_thru no longer fell through to the next handler. Map ENOTDIR to fs.ErrNotExist before that check. Other path errors, such as NUL bytes, overly long names, or transient I/O failures, keep their current handling.
This commit is contained in:
parent
7ef5c60ed0
commit
7ba5e147ac
2 changed files with 55 additions and 0 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue