Repository navigation
Commit 71ec709
authored
fix(security): skip an invalid custom pattern regex instead of panicking (#1763)
## Bug
`sensitive_data_detection.custom_patterns[].regex` comes straight from
the user's config file and is not validated when the config loads.
`buildCustomPattern` passes it to `PatternBuilder.WithRegex`, which uses
`regexp.MustCompile`. One malformed regex therefore panics inside
`security.NewDetector`.
`NewDetector` runs at startup (`runtime.go`, `server.go`, `mcp.go`), on
config reload (`ReloadConfig`), and when the credential input screen is
built. A typo in the config means mcpproxy won't start at all. The
validating loader `patterns.LoadCustomPatterns` exists, but it is not on
this path.
## Trigger
```json
"sensitive_data_detection": {
"enabled": true,
"custom_patterns": [{"name": "x", "regex": "("}]
}
```
Result: `panic: regexp: Compile(`(`): error parsing regexp: missing
closing ): `(``
## Fix
In `buildCustomPattern`, compile the regex with `regexp.Compile` first
and drop the pattern if it is invalid. This matches how the function
already drops a pattern with no name or no regex/keywords. The other
custom patterns and all built-in patterns are still loaded.
## Test
`TestNewDetector_InvalidCustomRegexIsSkipped` configures one broken and
one valid custom pattern. It asserts that `NewDetector` does not panic
and that the valid pattern still detects `TICKET-123456`.
## Commands run
- `CGO_ENABLED=0 go test -count=1 ./internal/security`: ok
- With the fix reverted (`git stash` of `detector.go`), the new test
fails with `NewDetector panicked on an invalid custom regex: regexp:
Compile(`(`): error parsing regexp: missing closing ): `(``.
- `gofmt -l` on the edited files: no output1 parent 569a266 commit 71ec709
2 files changed
Lines changed: 45 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
344 | 345 | | |
345 | 346 | | |
346 | 347 | | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
347 | 353 | | |
348 | 354 | | |
349 | 355 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
0 commit comments