Skip to content
Closed
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 @@ -56,10 +56,14 @@ export function globToRegexPattern(glob: string): string {

switch (c) {
case '{':
if (inGroup)
throw new Error(`Invalid glob pattern "${glob}": nested '{' is not supported`);
inGroup = true;
tokens.push('(');
break;
case '}':
if (!inGroup)
throw new Error(`Invalid glob pattern "${glob}": unmatched '}'`);
inGroup = false;
tokens.push(')');
break;
Expand All @@ -74,6 +78,8 @@ export function globToRegexPattern(glob: string): string {
tokens.push(escapedChars.has(c) ? '\\' + c : c);
}
}
if (inGroup)
throw new Error(`Invalid glob pattern "${glob}": unmatched '{'`);
tokens.push('$');
return tokens.join('');
}
Expand Down
4 changes: 3 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 @@ -834,6 +834,8 @@ export class RouteHandler {
this._baseURL = baseURL;
this._times = times;
this.url = url;
if (isString(url))
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.

_updateInterceptionPatterns is called next line at both constructions sites and it does the glob validation on the server, do we still need to duplicate the logic here?

resolveGlobToRegexPattern(baseURL, url);
this.handler = handler;
this._savedZone = platform.zones.current().pop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import fs from 'fs';
import path from 'path';

import { deserializeURLMatch, urlMatches } from '@isomorphic/urlMatch';
import { deserializeURLMatch, resolveGlobToRegexPattern, urlMatches } from '@isomorphic/urlMatch';
import { createGuid } from '@utils/crypto';
import { BrowserContext } from '../browserContext';
import { CDPSessionDispatcher } from './cdpSessionDispatcher';
Expand Down Expand Up @@ -329,6 +329,11 @@ export class BrowserContextDispatcher extends Dispatcher<BrowserContext, channel
}

async setWebSocketInterceptionPatterns(params: channels.PageSetWebSocketInterceptionPatternsParams, progress: Progress): Promise<void> {
const baseURL = this._context._options.baseURL;
for (const pattern of params.patterns) {
if (pattern.glob)
resolveGlobToRegexPattern(baseURL, pattern.glob, true);
}
this._webSocketInterceptionPatterns = params.patterns;
if (params.patterns.length && !this._routeWebSocketInitScript)
this._routeWebSocketInitScript = await WebSocketRouteDispatcher.install(progress, this.connection, this._context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { deserializeURLMatch, urlMatches } from '@isomorphic/urlMatch';
import { deserializeURLMatch, resolveGlobToRegexPattern, urlMatches } from '@isomorphic/urlMatch';
import { Page, Worker } from '../page';
import { Dispatcher } from './dispatcher';
import { parseError, serializeError } from '../errors';
Expand Down Expand Up @@ -219,6 +219,11 @@ export class PageDispatcher extends Dispatcher<Page, channels.PageChannel, Brows
}

async setWebSocketInterceptionPatterns(params: channels.PageSetWebSocketInterceptionPatternsParams, progress: Progress): Promise<void> {
const baseURL = this._page.browserContext._options.baseURL;
for (const pattern of params.patterns) {
if (pattern.glob)
resolveGlobToRegexPattern(baseURL, pattern.glob, true);
}
this._webSocketInterceptionPatterns = params.patterns;
if (params.patterns.length && !this._routeWebSocketInitScript)
this._routeWebSocketInitScript = await WebSocketRouteDispatcher.install(progress, this.connection, this._page);
Expand Down
6 changes: 6 additions & 0 deletions tests/page/page-route.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ it('should not support ? in glob pattern', async ({ page, server }) => {
expect(await page.content()).toContain('index123hello');
});

it('should throw for unbalanced braces in glob pattern', async ({ page }) => {
await expect(page.route('http://*/foo{', () => {})).rejects.toThrow(`Invalid glob pattern "http://*/foo{": unmatched '{'`);
await expect(page.route('}foo', () => {})).rejects.toThrow(`Invalid glob pattern "}foo": unmatched '}'`);
await expect(page.route('**/img.{png,{gif}}', () => {})).rejects.toThrow(`Invalid glob pattern "**/img.{png,{gif}}": nested '{' is not supported`);
});

it('should work when POST is redirected with 302', async ({ page, server }) => {
server.setRedirect('/rredirect', '/empty.html');
await page.goto(server.EMPTY_PAGE);
Expand Down
Loading