From 67fc99e8e27961dad96b0808dcc48b5c91059ea8 Mon Sep 17 00:00:00 2001 From: Thavamani Murugan Date: Sun, 26 Apr 2026 09:20:14 +0530 Subject: [PATCH] fix(glob): throw descriptive error for unbalanced braces in glob patterns Fixes: https://github.com/microsoft/playwright/issues/40422 --- packages/isomorphic/urlMatch.ts | 6 ++++++ tests/page/interception.spec.ts | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/packages/isomorphic/urlMatch.ts b/packages/isomorphic/urlMatch.ts index cdd7f0aab0a61..1b48e42ca34e4 100644 --- a/packages/isomorphic/urlMatch.ts +++ b/packages/isomorphic/urlMatch.ts @@ -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; @@ -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(''); } diff --git a/tests/page/interception.spec.ts b/tests/page/interception.spec.ts index e96e126f00b97..0db1e4c510948 100644 --- a/tests/page/interception.spec.ts +++ b/tests/page/interception.spec.ts @@ -164,6 +164,14 @@ it('should work with glob', async () => { expect(urlMatches(undefined, `${prefix}:blank`, `${prefix}:*`)).toBeTruthy(); expect(urlMatches(undefined, `not${prefix}:blank`, `${prefix}:*`)).toBeFalsy(); } + + // Unbalanced braces must throw early with a descriptive error. + expect(() => globToRegexPattern('{foo')).toThrow(`Invalid glob pattern "{foo": unmatched '{'`); + expect(() => globToRegexPattern('}foo')).toThrow(`Invalid glob pattern "}foo": unmatched '}'`); + expect(() => globToRegexPattern('**/*.png?{')).toThrow(`Invalid glob pattern "**/*.png?{": unmatched '{'`); + expect(() => globToRegexPattern('https://example.com/{a')).toThrow(`Invalid glob pattern "https://example.com/{a": unmatched '{'`); + expect(() => globToRegexPattern('{a,b')).toThrow(`Invalid glob pattern "{a,b": unmatched '{'`); + expect(() => globToRegexPattern('{a,{b}}')).toThrow(`Invalid glob pattern "{a,{b}}": nested '{' is not supported`); }); it('should intercept by glob', async function({ page, server, isAndroid }) {