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
6 changes: 6 additions & 0 deletions packages/isomorphic/urlMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ export function globToRegexPattern(glob: string): string {

switch (c) {
case '{':
if (inGroup)
throw new Error(`Invalid glob pattern ${JSON.stringify(glob)}: nested '{' is not supported`);
inGroup = true;
tokens.push('(');
break;
case '}':
if (!inGroup)
throw new Error(`Invalid glob pattern ${JSON.stringify(glob)}: unmatched '}'`);
inGroup = false;
tokens.push(')');
break;
Expand All @@ -82,6 +86,8 @@ export function globToRegexPattern(glob: string): string {
tokens.push(escapedChars.has(c) ? '\\' + c : c);
}
}
if (inGroup)
throw new Error(`Invalid glob pattern ${JSON.stringify(glob)}: unmatched '{'`);
tokens.push('$');
return tokens.join('');
}
Expand Down
10 changes: 9 additions & 1 deletion packages/playwright-core/src/client/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { assert } from '@isomorphic/assert';
import { headersObjectToArray } from '@isomorphic/headers';
import { serializeURLMatch, urlMatches } from '@isomorphic/urlMatch';
import { resolveGlobToRegexPattern, serializeURLMatch, urlMatches } from '@isomorphic/urlMatch';
import { LongStandingScope, ManualPromise } from '@isomorphic/manualPromise';
import { MultiMap } from '@isomorphic/multimap';
import { isString } from '@isomorphic/rtti';
Expand Down Expand Up @@ -591,6 +591,10 @@ export class WebSocketRouteHandler {
this._baseURL = baseURL;
this.url = url;
this.handler = handler;
// Eagerly validate string globs so that invalid patterns throw at the call site
// (e.g. page.routeWebSocket()) rather than silently failing later.
if (typeof url === 'string')
resolveGlobToRegexPattern(baseURL, url, true);
}

static prepareInterceptionPatterns(handlers: WebSocketRouteHandler[]) {
Expand Down Expand Up @@ -836,6 +840,10 @@ export class RouteHandler {
this.url = url;
this.handler = handler;
this._savedZone = platform.zones.current().pop();
// Eagerly validate string globs so that invalid patterns throw at the call site
// (e.g. page.route()) rather than silently aborting requests later.
if (typeof url === 'string')
resolveGlobToRegexPattern(baseURL, url);
}

static prepareInterceptionPatterns(handlers: RouteHandler[]) {
Expand Down
16 changes: 16 additions & 0 deletions tests/page/interception.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ it('should work with glob', async () => {
}
});

it('should throw on unbalanced glob braces', async () => {
expect(() => globToRegexPattern('{foo')).toThrow(`Invalid glob pattern "{foo": unmatched '{'`);
expect(() => globToRegexPattern('}foo')).toThrow(`Invalid glob pattern "}foo": unmatched '}'`);
expect(() => globToRegexPattern('http://*/foo{')).toThrow(`unmatched '{'`);
expect(() => globToRegexPattern('**/*.png?{')).toThrow(`unmatched '{'`);
expect(() => globToRegexPattern('https://example.com/{a')).toThrow(`unmatched '{'`);
expect(() => globToRegexPattern('{{foo}')).toThrow(`nested '{' is not supported`);
// Escaped braces remain literal and must not throw.
expect(() => globToRegexPattern('\\{foo')).not.toThrow();
expect(() => globToRegexPattern('foo\\}')).not.toThrow();
});

it('should throw on page.route with invalid glob', async ({ page }) => {
await expect(page.route('http://*/foo{', route => route.continue())).rejects.toThrow(`unmatched '{'`);
});

it('should intercept by glob', async function({ page, server, isAndroid }) {
it.skip(isAndroid);

Expand Down
Loading