| author | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-10-30 18:30:21 UTC |
| committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-10-30 18:34:58 UTC |
| parent | 9d9bbf067ac893283bbd588e022c60ffafa9681e |
| server/dir.go | +11 | -2 |
| server/dir_test.go | +32 | -0 |
| server/write_test.go | +59 | -0 |
diff --git a/server/dir.go b/server/dir.go index 4add0fa..446cbf9 100644 --- a/server/dir.go +++ b/server/dir.go @@ -29,7 +29,8 @@ func NewFS(root string, fs http.FileSystem, opts config.DirOpts) *FileSystem { } // prefixMatch returns the value associated with the longest prefix of name in -// m, or false if no prefix matches. +// m, or false if no prefix matches. Prefixes match on path segment boundaries: +// "/pub/" matches "/pub" and "/pub/x", but not "/public". func prefixMatch(m map[string]bool, name string) bool { if name == "" { name = "/" @@ -40,7 +41,7 @@ func prefixMatch(m map[string]bool, name string) bool { value := false for p, val := range m { p = filepath.Clean(p) - if strings.HasPrefix(name, p) && len(p) > len(longestP) { + if hasPathPrefix(name, p) && len(p) > len(longestP) { longestP = p value = val } @@ -49,6 +50,14 @@ func prefixMatch(m map[string]bool, name string) bool { return value } +// hasPathPrefix returns true if name is p or is under it. Both must be clean. +func hasPathPrefix(name, p string) bool { + if p == "/" { + return strings.HasPrefix(name, "/") + } + return name == p || strings.HasPrefix(name, p+"/") +} + func ListingEnabled(opts *config.DirOpts, name string) bool { return prefixMatch(opts.Listing, name) } diff --git a/server/dir_test.go b/server/dir_test.go index d46dfd3..5e43094 100644 --- a/server/dir_test.go +++ b/server/dir_test.go @@ -24,6 +24,11 @@ func TestPrefixMatch(t *testing.T) { {"/readonly", false}, {"/deep/sub/x", true}, + // Siblings that share the string prefix, but not the path segment, + // don't match it; they get the value from "/" instead. + {"/readonly2", true}, + {"/readonly2/x", true}, + // Empty name treated as "/". {"", true}, } @@ -39,3 +44,30 @@ func TestPrefixMatch(t *testing.T) { t.Errorf("prefixMatch(nil, ...) = true, want false") } } + +// Without a "/" entry, siblings of an enabled prefix must not be enabled. +func TestPrefixMatchSiblings(t *testing.T) { + m := map[string]bool{"/pub/": true} + cases := []struct { + name string + want bool + }{ + {"/pub", true}, + {"/pub/", true}, + {"/pub/x", true}, + {"/pub/sub/x", true}, + + {"/", false}, + {"/public", false}, + {"/public/secret.txt", false}, + {"/pubfoo", false}, + {"/pub.txt", false}, + {"/other/pub/x", false}, + } + for _, c := range cases { + got := prefixMatch(m, c.name) + if got != c.want { + t.Errorf("prefixMatch(%q) = %v, want %v", c.name, got, c.want) + } + } +} diff --git a/server/write_test.go b/server/write_test.go index 181e837..2dcd2fe 100644 --- a/server/write_test.go +++ b/server/write_test.go @@ -3,6 +3,8 @@ package server import ( "net/http" "net/http/httptest" + "os" + "path/filepath" "strings" "testing" @@ -52,3 +54,60 @@ func TestPutLastModified(t *testing.T) { "round-trip)", resp.StatusCode) } } + +// TestWriteSiblingPrefix checks that enabling PUT and DELETE on a prefix does +// not enable them on sibling paths that merely share its leading characters. +func TestWriteSiblingPrefix(t *testing.T) { + root := t.TempDir() + if err := os.Mkdir(filepath.Join(root, "public"), 0o755); err != nil { + t.Fatal(err) + } + secret := filepath.Join(root, "public", "secret.txt") + if err := os.WriteFile(secret, []byte("secret"), 0o644); err != nil { + t.Fatal(err) + } + + fs := NewFS(root, http.Dir(root), config.DirOpts{ + Put: map[string]bool{"/pub/": true}, + Delete: map[string]bool{"/pub/": true}, + }) + + del := func(path string) *http.Response { + req := httptest.NewRequest("DELETE", "http://unused"+path, nil) + tr := trace.New("test", path) + req = req.WithContext(trace.NewContext(req.Context(), tr)) + w := httptest.NewRecorder() + handleDelete(fs, w, req) + return w.Result() + } + + // Siblings are rejected, and the existing file is left untouched. + resp := put(fs, "/public/secret.txt", "overwritten", "") + if resp.StatusCode != http.StatusMethodNotAllowed { + t.Errorf("PUT sibling: status = %d, want 405", resp.StatusCode) + } + resp = put(fs, "/pubfoo", "new", "") + if resp.StatusCode != http.StatusMethodNotAllowed { + t.Errorf("PUT sibling file: status = %d, want 405", resp.StatusCode) + } + resp = del("/public/secret.txt") + if resp.StatusCode != http.StatusMethodNotAllowed { + t.Errorf("DELETE sibling: status = %d, want 405", resp.StatusCode) + } + if got, err := os.ReadFile(secret); err != nil || string(got) != "secret" { + t.Errorf("sibling file changed: %q, %v", got, err) + } + if _, err := os.Stat(filepath.Join(root, "pubfoo")); err == nil { + t.Errorf("PUT sibling created /pubfoo") + } + + // The prefix itself still works. + resp = put(fs, "/pub/f.txt", "one", "") + if resp.StatusCode != http.StatusCreated { + t.Errorf("PUT in prefix: status = %d, want 201", resp.StatusCode) + } + resp = del("/pub/f.txt") + if resp.StatusCode != http.StatusNoContent { + t.Errorf("DELETE in prefix: status = %d, want 204", resp.StatusCode) + } +}