Skip to content

fix(tools): validate user-provided regex patterns in MCP tools#40752

Merged
pavelfeldman merged 4 commits intomicrosoft:mainfrom
SebTardif:fix/mcp-regex-validation
May 9, 2026
Merged

fix(tools): validate user-provided regex patterns in MCP tools#40752
pavelfeldman merged 4 commits intomicrosoft:mainfrom
SebTardif:fix/mcp-regex-validation

Conversation

@SebTardif
Copy link
Copy Markdown
Contributor

@SebTardif SebTardif commented May 9, 2026

Summary

  • MCP tools that accept grep/filter parameters pass them directly to new RegExp() without validation
  • An invalid pattern (e.g., [a-) crashes the tool with an unhandled exception
  • Wrap in try-catch and throw a clear error message that includes the original RegExp error

Affected tools: trace actions grep, trace requests grep, network filter.

Failure scenario: An MCP client sends a grep/filter parameter with invalid regex syntax (e.g., unclosed bracket [a-). The new RegExp() constructor throws a SyntaxError that propagates unhandled, crashing the tool handler instead of returning a user-friendly error.

Origin: The grep/filter parameters were introduced in #39771 (2026-03-20) by Pavel Feldman when trace CLI handlers were extracted into separate files.

SebTardif added 2 commits May 9, 2026 06:03
MCP tools that accept grep/filter parameters pass them directly to
new RegExp() without validation. An invalid pattern crashes the tool
with an unhandled exception. Wrap in try-catch and throw a clear
error message for invalid patterns.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
Preserves the RegExp constructor's error message (e.g. 'Invalid
regular expression: /[a-/: Unterminated character class') so users
can understand why their pattern is invalid.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
Copy link
Copy Markdown
Member

@pavelfeldman pavelfeldman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move it to where we validate params using zod.

Move regex validation from inline try-catch in handlers to Zod
.refine() on the inputSchema. This validates the pattern during
parameter parsing before the handler runs.

Revert inline validation in trace CLI tools (traceActions,
traceRequests) since those use commander, not Zod.

Also add the same validation to the CLI daemon's network command
schema.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
@SebTardif
Copy link
Copy Markdown
Contributor Author

Moved the validation to Zod .refine() on the filter field in both network.ts and cli-daemon/commands.ts. Invalid regex is now caught during param parsing before the handler runs.

Reverted the inline try-catch in trace CLI tools (traceActions, traceRequests) since those use commander, not Zod.

inputSchema: z.object({
static: z.boolean().default(false).describe('Whether to include successful static resources like images, fonts, scripts, etc. Defaults to false.'),
filter: z.string().optional().describe('Only return requests whose URL matches this regexp (e.g. "/api/.*user").'),
filter: z.string().optional().refine(v => { if (!v) return true; try { new RegExp(v); return true; } catch { return false; } }, { message: 'Invalid regular expression' }).describe('Only return requests whose URL matches this regexp (e.g. "/api/.*user").'),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised linter is not yelling at you. Let's kick it into isRegexString next to isRegExp?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this typically would warrant a test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted isRegexString into @isomorphic/rtti next to isRegExp. The Zod schema now uses .refine(v => !v || isRegexString(v)).

Also reverted the cli-daemon and trace CLI changes since validation is handled at the tool schema level.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test in tests/mcp/network.spec.ts that verifies invalid regex filter ([invalid() returns an error. All 14 network tests pass.

Add isRegexString to @isomorphic/rtti next to isRegExp. Use it in
the network tool's Zod schema to validate regex filter strings.

Revert cli-daemon commands.ts and trace CLI tools to original since
validation is handled at the tool schema level.

Add test that verifies invalid regex filter is rejected.

Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 9, 2026

Test results for "MCP"

5 failed
❌ [firefox] › mcp/cli-devtools.spec.ts:217 › video-start-stop @mcp-windows-latest-firefox
❌ [firefox] › mcp/cli-devtools.spec.ts:231 › video-chapter @mcp-windows-latest-firefox
❌ [msedge] › mcp/annotate.spec.ts:330 › should annotate when context has no fixed viewport @mcp-windows-latest-msedge
❌ [msedge] › mcp/annotate.spec.ts:367 › should cancel browser_annotate when the MCP request is aborted @mcp-windows-latest-msedge
❌ [msedge] › mcp/annotate.spec.ts:398 › should cancel browser_annotate when the MCP client disconnects @mcp-windows-latest-msedge

7052 passed, 1068 skipped


Merge workflow run.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 9, 2026

Test results for "tests 1"

3 flaky ⚠️ [installation tests] › playwright-test-package-managers.spec.ts:19 › npm: @playwright/test should work `@package-installations-windows-latest`
⚠️ [chromium-library] › library/video.spec.ts:719 › screencast › should work with video+trace `@ubuntu-22.04-chromium-tip-of-tree`
⚠️ [firefox-page] › page/page-emulate-media.spec.ts:144 › should keep reduced motion and color emulation after reload `@firefox-ubuntu-22.04-node20`

41703 passed, 850 skipped


Merge workflow run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants