fileserver: ignore case for literal hide entries (#8378)

On case-insensitive filesystems (APFS and NTFS by default), a request using another letter case opens a hidden file anyway: the Caddyfile auto-hidden by httpcaddyfile is served for /caddyfile.

Entries without glob characters now match regardless of letter case. Glob patterns keep their case-sensitive matching. On case-sensitive filesystems, a literal entry also hides names that differ from it only by case.
This commit is contained in:
Kévin Dunglas 2026-11-08 22:52:49 +01:00 committed by GitHub
parent b426776cc7
commit af4d0c37d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 135 additions and 3 deletions

View file

@ -27,10 +27,12 @@ import (
"path"
"path/filepath"
"runtime"
"slices"
"strconv"
"strings"
"syscall"
"time"
"unicode"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
@ -128,9 +130,11 @@ type FileServer struct {
// comparisons are made. For maximum clarity and explicitness, use complete,
// absolute paths; or, for greater portability, use relative paths instead.
//
// Note that hide comparisons are case-sensitive. On case-insensitive
// filesystems, requests with different path casing may still resolve to the
// same file or directory on disk, so hide should not be treated as a
// Entries without glob characters are compared ignoring letter case, since
// case-insensitive filesystems open differently cased paths as the same
// file; glob patterns are compared case-sensitively. Hide is a best-effort
// filter: glob patterns, Unicode normalization variants, symlinks and hard
// links can still reach hidden files, so hide should not be treated as a
// security boundary for sensitive paths.
Hide []string `json:"hide,omitempty"`
@ -724,8 +728,21 @@ func fileHidden(filename string, hide []string) bool {
}
var components []string
var folded string
for _, h := range hide {
if !hasGlobMeta(h) {
// case-insensitive filesystems open other spellings of a literal
// entry as the same file
if folded == "" {
folded = foldCase(filename)
}
if literalHidden(foldCase(h), folded) {
return true
}
continue
}
if !strings.Contains(h, separator) {
// if there is no separator in h, then we assume the user
// wants to hide any files or folders that match that
@ -759,6 +776,31 @@ func fileHidden(filename string, hide []string) bool {
return false
}
// literalHidden reports whether filename is the literal entry h or is inside
// it, or, if h has no separator, has a path component named h.
func literalHidden(h, filename string) bool {
if !strings.Contains(h, separator) {
return slices.Contains(strings.Split(filename, separator), h)
}
after, ok := strings.CutPrefix(filename, h)
return ok && (after == "" || strings.HasPrefix(after, separator))
}
// hasGlobMeta reports whether filepath.Match treats h as a pattern.
func hasGlobMeta(h string) bool {
magic := `*?[\`
if runtime.GOOS == "windows" {
magic = `*?[`
}
return strings.ContainsAny(h, magic)
}
// foldCase maps the letter cases that case-insensitive filesystems treat as
// the same name to one form.
func foldCase(s string) string {
return strings.Map(func(r rune) rune { return unicode.ToLower(unicode.ToUpper(r)) }, s)
}
// notFound returns a 404 error or, if pass-thru is enabled,
// it calls the next handler in the chain.
func (fsrv *FileServer) notFound(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {

View file

@ -17,6 +17,7 @@ package fileserver
import (
"bytes"
"context"
"errors"
"net/http"
"net/http/httptest"
"os"
@ -142,3 +143,42 @@ func TestHideRelativeDataDir(t *testing.T) {
t.Errorf("caddyfile.go is hidden; hide list: %v", fsrv.Hide)
}
}
func TestHideOtherLetterCases(t *testing.T) {
root := t.TempDir()
if err := os.WriteFile(filepath.Join(root, "Caddyfile"), []byte("hidden config"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.Mkdir(filepath.Join(root, "secretdir"), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(root, "secretdir", "p.txt"), []byte("hidden body"), 0o600); err != nil {
t.Fatal(err)
}
fsrv := FileServer{
Root: root,
Hide: []string{filepath.Join(root, "Caddyfile"), "secretdir"},
CanonicalURIs: new(bool),
}
ctx, _ := caddy.NewContext(caddy.Context{Context: context.Background()})
if err := fsrv.Provision(ctx); err != nil {
t.Fatal(err)
}
for _, uri := range []string{"/Caddyfile", "/caddyfile", "/CADDYFILE", "/secretdir/p.txt", "/SecretDir/p.txt"} {
t.Run(uri, func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, uri, nil)
original := *r
reqCtx := context.WithValue(r.Context(), caddy.ReplacerCtxKey, caddy.NewReplacer())
reqCtx = context.WithValue(reqCtx, caddyhttp.OriginalRequestCtxKey, original)
r = r.WithContext(reqCtx)
w := httptest.NewRecorder()
err := fsrv.ServeHTTP(w, r, nil)
var handlerErr caddyhttp.HandlerError
if !errors.As(err, &handlerErr) || handlerErr.StatusCode != http.StatusNotFound {
t.Errorf("got error %v and status %d, want 404", err, w.Code)
}
})
}
}

View file

@ -121,6 +121,56 @@ func TestFileHidden(t *testing.T) {
inputPath: "/second",
expect: true,
},
{
inputHide: []string{"/srv/Caddyfile"},
inputPath: "/srv/caddyfile",
expect: true,
},
{
inputHide: []string{"/srv/Caddyfile"},
inputPath: "/SRV/CADDYFILE",
expect: true,
},
{
inputHide: []string{"secretdir"},
inputPath: "/SecretDir/p.txt",
expect: true,
},
{
inputHide: []string{"/srv/secretdir"},
inputPath: "/srv/SECRETDIR/p.txt",
expect: true,
},
{
inputHide: []string{"/srv/public.txt.gz"},
inputPath: "/srv/PUBLIC.TXT.gz",
expect: true,
},
{
inputHide: []string{"secret"},
inputPath: "/\u017Fecret",
expect: true,
},
{
inputHide: []string{"kelvin"},
inputPath: "/\u212Aelvin",
expect: true,
},
{
inputHide: []string{"Secret"},
inputPath: "/secrets",
expect: false,
},
{
inputHide: []string{"/foo"},
inputPath: "/FOOBAR",
expect: false,
},
{
inputHide: []string{"*.secret"},
inputPath: "/X.SECRET",
expect: false,
},
} {
if runtime.GOOS == "windows" {
if strings.HasPrefix(tc.inputPath, "/") {