Skip to content
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
25 changes: 23 additions & 2 deletions pkgs/mcpx/internal/config/toolpattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,20 @@ func CompileToolPatterns(list []string) (ToolPatterns, error) {
var p ToolPatterns
for _, s := range list {
switch {
case len(s) >= 2 && strings.HasPrefix(s, "/") && strings.HasSuffix(s, "/"):
case strings.HasPrefix(s, "/"):
// An entry beginning with / is a regexp entry, and a regexp
// entry is a complete /.../ pair: an unclosed entry (including
// the lone /) silently fell through to an exact name that
// matches nothing a server offers, and // compiled the empty
// regexp, which matches every name -- the two silent
// mistranslations a deny entry must never make (#1631). Both
// are compile errors naming the entry.
if len(s) < 2 || !strings.HasSuffix(s, "/") {
return ToolPatterns{}, fmt.Errorf("tool pattern %q: unclosed regexp (missing closing /)", s)
}
if len(s) == 2 {
return ToolPatterns{}, fmt.Errorf("tool pattern %q: empty regexp", s)
}
re, err := regexp.Compile(s[1 : len(s)-1])
if err != nil {
return ToolPatterns{}, fmt.Errorf("tool pattern %q: %w", s, err)
Expand Down Expand Up @@ -103,11 +116,19 @@ func globRegexp(glob string) (*regexp.Regexp, error) {
if neg {
b.WriteString("^")
}
first := true
for _, r := range glob[body : j+k] {
if r == '\\' || r == '[' || r == ']' {
// A ^ first in the body is a literal class member in the
// glob; copied verbatim it would land first in the RE2 class
// and negate it (#1631). Escape it there. (The [!...] form
// already emitted its own negation ^, and there a
// body-leading ^ is a member of the negated set, which RE2
// reads correctly unescaped.)
if r == '\\' || r == '[' || r == ']' || (r == '^' && first && !neg) {
b.WriteByte('\\')
}
b.WriteRune(r)
first = false
}
b.WriteString("]")
i = j + k
Expand Down
60 changes: 60 additions & 0 deletions pkgs/mcpx/internal/config/toolpattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,63 @@ func TestExtraIncludeToolsAppendsAcrossFiles(t *testing.T) {
}
}
}

// #1631: an entry beginning with / that is not a complete /.../
// pair silently became an exact name matching nothing, and //
// compiled the empty regexp and matched everything. All three
// shapes are compile errors naming the entry.
func TestCompileToolPatternsRefusesMalformedRegexpEntries(t *testing.T) {
for _, entry := range []string{"/delete", "/", "//"} {
_, err := config.CompileToolPatterns([]string{entry})
if err == nil {
t.Errorf("config.CompileToolPatterns([%q]): expected an error, got nil", entry)
continue
}
if !strings.Contains(err.Error(), entry) {
t.Errorf("config.CompileToolPatterns([%q]): error must name the entry, got %v", entry, err)
}
}
}

// #1631: a glob class whose body begins with ^ keeps the glob
// meaning -- ^ is a literal member -- instead of inverting the
// class in RE2. The [!...] negation form is unchanged.
func TestCompileToolPatternsCaretClassIsLiteral(t *testing.T) {
p, err := config.CompileToolPatterns([]string{"[^d]elete_*"})
if err != nil {
t.Fatalf("CompileToolPatterns: %v", err)
}
for _, name := range []string{"delete_x", "^elete_x"} {
if !p.Match(name) {
t.Errorf("[^d]elete_* must match %q (^ and d are class members)", name)
}
}
if p.Match("xelete_x") {
t.Errorf("[^d]elete_* must not match xelete_x (the class is not a negation)")
}
neg, err := config.CompileToolPatterns([]string{"[!d]elete_*"})
if err != nil {
t.Fatalf("CompileToolPatterns: %v", err)
}
if neg.Match("delete_x") || !neg.Match("xelete_x") {
t.Errorf("[!d]elete_* negation unchanged: delete_x must not match, xelete_x must")
}
}

// Controls around the #1631 shapes: a well-formed regexp entry is
// unanchored, a malformed regexp body still fails through RE2, an
// unterminated class still fails, and the empty entry still fails.
func TestCompileToolPatternsControlsUnchanged(t *testing.T) {
p, err := config.CompileToolPatterns([]string{"/delete/"})
if err != nil {
t.Fatalf("CompileToolPatterns: %v", err)
}
if !p.Match("undelete_x") {
t.Errorf("/delete/ is an unanchored regexp and must match undelete_x")
}
for _, entry := range []string{"/(/", "delete_[", ""} {
if _, err := config.CompileToolPatterns([]string{entry}); err == nil {
t.Errorf("config.CompileToolPatterns([%q]): expected an error, got nil", entry)
}
}
}
Loading