From c0563b382bf497197ef3560af1ed88f660218c1c Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sat, 31 Oct 2026 06:52:13 -0500 Subject: [PATCH] fix(mcpx/config): refuse unclosed and empty regexp tool patterns; escape leading ^ in glob classes (#1631) CompileToolPatterns silently mistranslated three entry shapes: an unclosed /regexp fell through to a never-matching exact name, // compiled the empty regexp and matched everything, and a glob class body beginning with ^ was copied verbatim into the RE2 class, inverting it. Now: any entry beginning with / that is not a complete /.../ pair (including the lone /) is a compile error naming the entry, // is refused as an empty regexp, and a body-leading ^ is escaped when the class is not the [!...] negation form. Regression tests cover all three shapes plus controls (well-formed /delete/, /( /, delete_[, empty entry, [!d] negation). go test ./internal/config/ passes on the laptop. --- pkgs/mcpx/internal/config/toolpattern.go | 25 +++++++- pkgs/mcpx/internal/config/toolpattern_test.go | 60 +++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/pkgs/mcpx/internal/config/toolpattern.go b/pkgs/mcpx/internal/config/toolpattern.go index b1f8775ab9..867a2212fe 100644 --- a/pkgs/mcpx/internal/config/toolpattern.go +++ b/pkgs/mcpx/internal/config/toolpattern.go @@ -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) @@ -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 diff --git a/pkgs/mcpx/internal/config/toolpattern_test.go b/pkgs/mcpx/internal/config/toolpattern_test.go index 999e26f64a..ee0f338b07 100644 --- a/pkgs/mcpx/internal/config/toolpattern_test.go +++ b/pkgs/mcpx/internal/config/toolpattern_test.go @@ -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) + } + } +}