fileserver: hide the data directory when it is relative (#8327)

(cherry picked from commit 9dd12fdb5d)
This commit is contained in:
Kévin Dunglas 2026-11-01 21:57:49 +01:00 • committed by Francis Lavoie
parent 745472789c
commit b6cfed16af
No known key found for this signature in database
2 changed files with 27 additions and 0 deletions

View file

@ -210,6 +210,13 @@ func (fsrv *FileServer) Provision(ctx caddy.Context) error {
fsrv.IndexNames = defaultIndexNames
}
// a relative data directory sits under the working directory and holds private keys
if dataDir := caddy.AppDataDir(); !filepath.IsAbs(dataDir) {
if abs, err := caddy.FastAbs(dataDir); err == nil {
fsrv.Hide = append(fsrv.Hide, abs)
}
}
// for hide paths that are static (i.e. no placeholders), we can transform them into
// absolute paths before the server starts for very slight performance improvement
for i, h := range fsrv.Hide {

View file

@ -122,3 +122,23 @@ func TestBrowseHidesPathQualifiedEntries(t *testing.T) {
})
}
}
func TestHideRelativeDataDir(t *testing.T) {
for _, env := range []string{"XDG_DATA_HOME", "HOME", "AppData", "HOMEDRIVE", "HOMEPATH", "USERPROFILE", "home"} {
t.Setenv(env, "")
}
fsrv := FileServer{Root: "."}
ctx, _ := caddy.NewContext(caddy.Context{Context: context.Background()})
if err := fsrv.Provision(ctx); err != nil {
t.Fatal(err)
}
key := filepath.Join("caddy", "certificates", "local", "localhost", "localhost.key")
if !fileHidden(key, fsrv.Hide) {
t.Errorf("%s is not hidden; hide list: %v", key, fsrv.Hide)
}
if fileHidden("caddyfile.go", fsrv.Hide) {
t.Errorf("caddyfile.go is hidden; hide list: %v", fsrv.Hide)
}
}