Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i/p/patterns: disallow /./ and /../ in path patterns #14774

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions interfaces/prompting/patterns/patterns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ func (s *patternsSuite) TestParsePathPatternHappy(c *C) {
"/foo/{a,b}{c,d}{e,f}{g,h,i,j,k}{l,m,n,o,p}{q,r,s,t,u},1,2,3", // expands to 1000, with commas outside groups
"/" + strings.Repeat("{a,", 999) + "a" + strings.Repeat("}", 999),
"/" + strings.Repeat("{", 999) + "a" + strings.Repeat(",a}", 999),
"/foo/.../bar",
"/foo/...",
"/foo/.{bar,baz}",
"/foo/..{bar,baz}",
"/foo/{bar,baz}.",
"/foo/{bar,baz}..",
} {
_, err := patterns.ParsePathPattern(pattern)
c.Check(err, IsNil, Commentf("valid path pattern %q was incorrectly not allowed", pattern))
Expand All @@ -152,6 +158,22 @@ func (s *patternsSuite) TestParsePathPatternUnhappy(c *C) {
`file.txt`,
`invalid path pattern: pattern must start with '/': "file.txt"`,
},
{
`/foo/./bar`,
`invalid path pattern: pattern cannot contain '/./' or '/../': .*`,
},
{
`/foo/../bar`,
`invalid path pattern: pattern cannot contain '/./' or '/../': .*`,
},
{
`/foo/.`,
`invalid path pattern: pattern cannot contain '/./' or '/../': .*`,
},
{
`/foo/..`,
`invalid path pattern: pattern cannot contain '/./' or '/../': .*`,
},
{
`{/,/foo}`,
`invalid path pattern: pattern must start with '/': .*`,
Expand Down
7 changes: 7 additions & 0 deletions interfaces/prompting/patterns/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"errors"
"fmt"
"io"
"regexp"
"strings"
)

Expand Down Expand Up @@ -59,13 +60,19 @@ type token struct {
text string
}

// relpathFinder matches `/./` and `/../` along with their trailing variants `/.` and `/..` in path patterns.
var relpathFinder = regexp.MustCompile(`/\.(\.)?(/|$)`)

func scan(text string) (tokens []token, err error) {
if len(text) == 0 {
return nil, errors.New("pattern has length 0")
}
if text[0] != '/' {
return nil, errors.New("pattern must start with '/'")
}
if relpathFinder.MatchString(text) {
return nil, errors.New("pattern cannot contain '/./' or '/../'")
}

var runes []rune

Expand Down
16 changes: 16 additions & 0 deletions interfaces/prompting/patterns/scan_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ func (s *scanSuite) TestScanUnhappy(c *C) {
`foo`,
`pattern must start with '/'`,
},
{
`/foo/./bar`,
`pattern cannot contain '/./' or '/../'`,
},
{
`/foo/../bar`,
`pattern cannot contain '/./' or '/../'`,
},
{
`/foo/.`,
`pattern cannot contain '/./' or '/../'`,
},
{
`/foo/..`,
`pattern cannot contain '/./' or '/../'`,
},
{
`/foo\`,
`trailing unescaped '\\' character`,
Expand Down
Loading