Skip to content

Commit 94147ca

Browse files
authored
fileserver: map invalid path errors to fs.ErrInvalid, and return 400 for any invalid path errors. (close #7008) (#7017)
1 parent 716d72e commit 94147ca

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

modules/caddyhttp/fileserver/staticfiles.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,10 @@ func (fsrv *FileServer) ServeHTTP(w http.ResponseWriter, r *http.Request, next c
300300
info, err := fs.Stat(fileSystem, filename)
301301
if err != nil {
302302
err = fsrv.mapDirOpenError(fileSystem, err, filename)
303-
if errors.Is(err, fs.ErrNotExist) || errors.Is(err, fs.ErrInvalid) {
303+
if errors.Is(err, fs.ErrNotExist) {
304304
return fsrv.notFound(w, r, next)
305+
} else if errors.Is(err, fs.ErrInvalid) {
306+
return caddyhttp.Error(http.StatusBadRequest, err)
305307
} else if errors.Is(err, fs.ErrPermission) {
306308
return caddyhttp.Error(http.StatusForbidden, err)
307309
}
@@ -611,6 +613,11 @@ func (fsrv *FileServer) mapDirOpenError(fileSystem fs.FS, originalErr error, nam
611613
return originalErr
612614
}
613615

616+
var pathErr *fs.PathError
617+
if errors.As(originalErr, &pathErr) {
618+
return fs.ErrInvalid
619+
}
620+
614621
parts := strings.Split(name, separator)
615622
for i := range parts {
616623
if parts[i] == "" {

0 commit comments

Comments
 (0)