-
Notifications
You must be signed in to change notification settings - Fork 5.1k
feat(mcp): support wildcard port in allowedOrigins and blockedOrigins #38944
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
base: main
Are you sure you want to change the base?
feat(mcp): support wildcard port in allowedOrigins and blockedOrigins #38944
Conversation
Add support for wildcard port syntax like `http://localhost:*` in the network.allowedOrigins and network.blockedOrigins config options. This allows restricting browser requests to a specific protocol and hostname while accepting any port number. Previously, patterns like `http://localhost:*` would fail to parse as a valid URL, causing the code to fall through to legacy host-only mode and generate an invalid glob pattern like `*://http://localhost:*/**`. The fix detects wildcard port patterns and generates the correct glob pattern `http://localhost:*/**` which properly matches any port.
7608161 to
6f842bc
Compare
|
@microsoft-github-policy-service agree company="Cyera" |
| const wildcardPortMatch = originOrHost.match(/^(https?):\/\/([^/:]+):\*$/); | ||
| if (wildcardPortMatch) { | ||
| const [, protocol, hostname] = wildcardPortMatch; | ||
| return `${protocol}://${hostname}:*/**`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to capture separate groups:
const wildcardPortMatch = originOrHost.match(/^(https?:\/\/[^/:]+):\*$/);
if (wildcardPortMatch)
return `${wildcardPortMatch[1]}:*/**`;| * List of origins to allow the browser to request. Default is to allow all. Origins matching both `allowedOrigins` and `blockedOrigins` will be blocked. | ||
| * | ||
| * Supported formats: | ||
| * - Full origin: `https://example.com` - matches only that origin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add port number to the example origin
| * | ||
| * Supported formats: | ||
| * - Full origin: `https://example.com` - matches only that origin | ||
| * - Hostname only: `example.com` - matches any protocol on that host |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this line as it is for legacy mode.
Summary
Add support for wildcard port syntax like
http://localhost:*in thenetwork.allowedOriginsandnetwork.blockedOriginsconfig options. This allows restricting browser requests to a specific protocol and hostname while accepting any port number.Fixes microsoft/playwright-mcp#1337
Changes
packages/playwright/src/mcp/browser/context.ts: UpdateoriginOrHostGlob()to detect wildcard port patterns and generate the correct glob patternpackages/playwright/src/mcp/config.d.ts: Document supported origin formats in JSDoctests/mcp/request-blocking.spec.ts: Add 4 tests for wildcard port functionalityProblem
Previously, patterns like
http://localhost:*would fail to parse as a valid URL, causing the code to fall through to legacy host-only mode and generate an invalid glob pattern like*://http://localhost:*/**which never matches anything.Solution
Detect wildcard port patterns with regex before attempting URL parsing:
http://localhost:**://http://localhost:*/**❌http://localhost:*/**✅https://example.com:**://https://example.com:*/**❌https://example.com:*/**✅http://localhost:8000http://localhost:8000/**localhost*://localhost/**Use Case
This is useful when you want to: