mirror of
https://github.com/filebrowser/filebrowser
synced 2026-04-21 13:27:17 +00:00
fix: enforce directory boundary in rule path matching (#5889)
This commit is contained in:
parent
7dbf7a3528
commit
8adf127c7d
2 changed files with 41 additions and 1 deletions
|
|
@ -31,7 +31,16 @@ func (r *Rule) Matches(path string) bool {
|
|||
return r.Regexp.MatchString(path)
|
||||
}
|
||||
|
||||
return strings.HasPrefix(path, r.Path)
|
||||
if path == r.Path {
|
||||
return true
|
||||
}
|
||||
|
||||
prefix := r.Path
|
||||
if prefix != "/" && !strings.HasSuffix(prefix, "/") {
|
||||
prefix += "/"
|
||||
}
|
||||
|
||||
return strings.HasPrefix(path, prefix)
|
||||
}
|
||||
|
||||
// Regexp is a wrapper to the native regexp type where we
|
||||
|
|
|
|||
|
|
@ -2,6 +2,37 @@ package rules
|
|||
|
||||
import "testing"
|
||||
|
||||
func TestRuleMatches(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
rulePath string
|
||||
testPath string
|
||||
want bool
|
||||
}{
|
||||
{"exact match", "/uploads", "/uploads", true},
|
||||
{"child path", "/uploads", "/uploads/file.txt", true},
|
||||
{"sibling prefix", "/uploads", "/uploads_backup/secret.txt", false},
|
||||
{"root rule", "/", "/anything", true},
|
||||
{"trailing slash rule", "/uploads/", "/uploads/file.txt", true},
|
||||
{"trailing slash no sibling", "/uploads/", "/uploads_backup/file.txt", false},
|
||||
{"nested child", "/data/shared", "/data/shared/docs/file.txt", true},
|
||||
{"nested sibling", "/data/shared", "/data/shared_private/file.txt", false},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
r := &Rule{Path: tc.rulePath}
|
||||
got := r.Matches(tc.testPath)
|
||||
if got != tc.want {
|
||||
t.Errorf("Rule{Path: %q}.Matches(%q) = %v; want %v", tc.rulePath, tc.testPath, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatchHidden(t *testing.T) {
|
||||
cases := map[string]bool{
|
||||
"/": false,
|
||||
|
|
|
|||
Loading…
Reference in a new issue