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

(cherry picked from commit 9dd12fdb5d)
This commit is contained in:
Kévin Dunglas authored and Francis Lavoie committed 2026-11-03 00:53:50 -05:00
1 parent 745472789c
commit b6cfed16af
2 files changed
+27

No files matched your search

@@ -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 {
@@ -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)
}
}