mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-12 20:03:34 +00:00
fileserver: hide the data directory when it is relative (#8327)
(cherry picked from commit 9dd12fdb5d)
This commit is contained in:
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user