Skip to content

Commit

Permalink
fix(#96) some minor corrections to escaping pattern base
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatcuk committed Jan 13, 2025
1 parent 1e7ad31 commit 4b5670c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ If SplitPattern cannot find somewhere to split the pattern (for example,
`meta*/**`), it will return "." and the unaltered pattern (`meta*/**` in this
example).

Note that SplitPattern will also unescape any meta characters in the returned
base string, so that it can be passed straight to os.DirFS().

Of course, it is your responsibility to decide if the returned base path is
"safe" in the context of your application. Perhaps you could use Match() to
validate against a list of approved base directories?
Expand Down
25 changes: 14 additions & 11 deletions doublestar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ var matchTests = []MatchTest{
{"**/*.txt", "abc/【test】.txt", true, true, nil, !onWindows, false, false, true, 1, 1},
{"**/【*", "abc/【test】.txt", true, true, nil, !onWindows, false, false, true, 1, 1},
{"**/{a,b}", "a/b", true, true, nil, !onWindows, false, false, true, 5, 5},
{"a/*/*/d", "a/b/c/d", true, true, nil, false, false, true, true, 1, 1},
// unfortunately, io/fs can't handle this, so neither can Glob =(
{"broken-symlink", "broken-symlink", true, true, nil, false, false, true, false, 1, 1},
{"broken-symlink/*", "a", false, false, nil, false, true, true, true, 0, 0},
Expand All @@ -150,16 +151,17 @@ var matchTests = []MatchTest{
{"working-sym*/*", "working-symlink/c", true, true, nil, false, false, true, !onWindows, 1, 1},
{"b/**/f", "b/symlink-dir/f", true, true, nil, false, false, false, !onWindows, 2, 2},
{"*/symlink-dir/*", "b/symlink-dir/f", true, true, nil, !onWindows, false, true, !onWindows, 2, 2},
{"e/\\[owner\\]/*", "e/[owner]/p.tsx", true, true, nil, false, false, true, !onWindows, 1, 0},
{"e/**", "e/**", true, true, nil, false, false, false, !onWindows, 13, 6},
{"e/**", "e/*", true, true, nil, false, false, false, !onWindows, 13, 6},
{"e/**", "e/?", true, true, nil, false, false, false, !onWindows, 13, 6},
{"e/**", "e/[", true, true, nil, false, false, false, true, 13, 6},
{"e/**", "e/]", true, true, nil, false, false, false, true, 13, 6},
{"e/**", "e/[]", true, true, nil, false, false, false, true, 13, 6},
{"e/**", "e/{", true, true, nil, false, false, false, true, 13, 6},
{"e/**", "e/}", true, true, nil, false, false, false, true, 13, 6},
{"e/**", "e/\\", true, true, nil, false, false, false, !onWindows, 13, 6},
{"e/\\[x\\]/*", "e/[x]/[y]", true, true, nil, false, false, true, true, 1, 1},
{"e/\\[x\\]/*/z", "e/[x]/[y]/z", true, true, nil, false, false, true, true, 1, 1},
{"e/**", "e/**", true, true, nil, false, false, false, !onWindows, 14, 6},
{"e/**", "e/*", true, true, nil, false, false, false, !onWindows, 14, 6},
{"e/**", "e/?", true, true, nil, false, false, false, !onWindows, 14, 6},
{"e/**", "e/[", true, true, nil, false, false, false, true, 14, 6},
{"e/**", "e/]", true, true, nil, false, false, false, true, 14, 6},
{"e/**", "e/[]", true, true, nil, false, false, false, true, 14, 6},
{"e/**", "e/{", true, true, nil, false, false, false, true, 14, 6},
{"e/**", "e/}", true, true, nil, false, false, false, true, 14, 6},
{"e/**", "e/\\", true, true, nil, false, false, false, !onWindows, 14, 6},
{"e/*", "e/*", true, true, nil, false, false, true, !onWindows, 11, 5},
{"e/?", "e/?", true, true, nil, false, false, true, !onWindows, 7, 4},
{"e/?", "e/*", true, true, nil, false, false, true, !onWindows, 7, 4},
Expand Down Expand Up @@ -794,7 +796,7 @@ func TestMain(m *testing.M) {
mkdirp("test", "axbxcxdxe", "xxx")
mkdirp("test", "axbxcxdxexxx")
mkdirp("test", "b")
mkdirp("test", "e")
mkdirp("test", "e", "[x]", "[y]")

// create test files
touch("test", "a", "abc")
Expand Down Expand Up @@ -823,6 +825,7 @@ func TestMain(m *testing.M) {
touch("test", "e", "{")
touch("test", "e", "}")
touch("test", "e", "[]")
touch("test", "e", "[x]", "[y]", "z")

touch("test", "}")

Expand Down
2 changes: 1 addition & 1 deletion glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (g *glob) doGlob(fsys fs.FS, pattern string, m []string, firstSegment, befo
}

var dirs []string
dirs, err = g.doGlob(fsys, unescapeMeta(dir), matches, false, beforeMeta)
dirs, err = g.doGlob(fsys, dir, matches, false, beforeMeta)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion globwalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (g *glob) doGlobWalk(fsys fs.FS, pattern string, firstSegment, beforeMeta b
return g.globDirWalk(fsys, unescapeMeta(dir), pattern, firstSegment, beforeMeta, fn)
}

return g.doGlobWalk(fsys, unescapeMeta(dir), false, beforeMeta, func(p string, d fs.DirEntry) error {
return g.doGlobWalk(fsys, dir, false, beforeMeta, func(p string, d fs.DirEntry) error {
if err := g.globDirWalk(fsys, p, pattern, firstSegment, false, fn); err != nil {
return err
}
Expand Down
9 changes: 6 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import (
// `meta*/**`), it will return "." and the unaltered pattern (`meta*/**` in
// this example).
//
// Note that SplitPattern will also unescape any meta characters in the
// returned base string, so that it can be passed straight to os.DirFS().
//
// Of course, it is your responsibility to decide if the returned base path is
// "safe" in the context of your application. Perhaps you could use Match() to
// validate against a list of approved base directories?
Expand All @@ -52,7 +55,7 @@ func SplitPattern(p string) (base, pattern string) {
if splitIdx == 0 {
return "/", p[1:]
} else if splitIdx > 0 {
return p[:splitIdx], p[splitIdx+1:]
return unescapeMeta(p[:splitIdx]), p[splitIdx+1:]
}

return
Expand Down Expand Up @@ -117,14 +120,14 @@ func FilepathGlob(pattern string, opts ...GlobOption) (matches []string, err err
return []string{filepath.FromSlash(pattern)}, nil
}

fs := os.DirFS(unescapeMeta(base))
fs := os.DirFS(base)
if matches, err = Glob(fs, f, opts...); err != nil {
return nil, err
}
for i := range matches {
// use path.Join because we used ToSlash above to ensure our paths are made
// of forward slashes, no matter what the system uses
matches[i] = filepath.FromSlash(path.Join(unescapeMeta(base), matches[i]))
matches[i] = filepath.FromSlash(path.Join(base, matches[i]))
}
return
}
Expand Down

0 comments on commit 4b5670c

Please sign in to comment.