Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(utils): fix polynomial backtracking in ansiRegex (#40762)
Sebastien Tardife8fa99c59540
diff --git a/packages/isomorphic/stringUtils.ts b/packages/isomorphic/stringUtils.tsindex 6e8a911507..65dc22051f 100644--- a/packages/isomorphic/stringUtils.ts+++ b/packages/isomorphic/stringUtils.ts@@ -189,7 +189,10 @@ export function parseRegex(regex: string): RegExp {return new RegExp(source, flags);}-export const ansiRegex = new RegExp('([\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~])))', 'g');+// Semicolons removed from [[\]()#;?] to avoid polynomial backtracking+// when both that group and (?:;...)* can match runs of semicolons.+// \d{1,4} relaxed to \d{0,4} so empty params (e.g. ESC[;H) still match.+export const ansiRegex = new RegExp('([\\u001B\\u009B][[\\]()#?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{0,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~])))', 'g');export function stripAnsiEscapes(str: string): string {return str.replace(ansiRegex, '');}diff --git a/tests/config/utils.ts b/tests/config/utils.tsindex 95d42a17a3..f39363a61f 100644--- a/tests/config/utils.ts+++ b/tests/config/utils.ts@@ -231,7 +231,7 @@ export function unshift(snapshot: string): string {return lines.filter(t => t.trim()).map(line => line.substring(whitespacePrefixLength)).join('\n');}-const ansiRegex = new RegExp('[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))', 'g');+const ansiRegex = new RegExp('[\\u001B\\u009B][[\\]()#?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{0,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))', 'g');export function stripAnsi(str: string): string {return str.replace(ansiRegex, '');}diff --git a/tests/library/snapshot-renderer.spec.ts b/tests/library/snapshot-renderer.spec.tsindex 0dd0d7900c..f512a6933b 100644--- a/tests/library/snapshot-renderer.spec.ts+++ b/tests/library/snapshot-renderer.spec.ts@@ -17,6 +17,7 @@import { test, expect } from '@playwright/test';import { SnapshotRenderer } from '../../packages/isomorphic/trace/snapshotRenderer';import { LRUCache } from '../../packages/isomorphic/lruCache';+import { stripAnsiEscapes } from '../../packages/isomorphic/stringUtils';import type { FrameSnapshot } from '../../packages/trace/src/snapshot';function makeSnapshot(overrides: Partial<FrameSnapshot> = {}): FrameSnapshot {@@ -111,3 +112,21 @@ test('snapshot renderer handles case-insensitive iframe tag names', () => {expect(html).toContain('__playwright_srcdoc__');expect(html).toContain('__playwright_src__');});++test('stripAnsiEscapes should not exhibit polynomial backtracking', () => {+ // \x1b[ + 50000 semicolons + non-terminal character.+ // Before the fix this took >3 seconds due to O(n^2) backtracking.+ const payload = '\x1b[' + ';'.repeat(50000) + '!';+ const result = stripAnsiEscapes(payload);+ // The ESC[ prefix is not a complete ANSI sequence, so it stays in the output.+ expect(result).toContain('!');+});++test('stripAnsiEscapes handles common ANSI sequences after fix', () => {+ expect(stripAnsiEscapes('\x1b[31mred\x1b[0m')).toBe('red');+ expect(stripAnsiEscapes('\x1b[0;31mbold red\x1b[0m')).toBe('bold red');+ expect(stripAnsiEscapes('\x1b[;H')).toBe('');+ expect(stripAnsiEscapes('\x1b[2J')).toBe('');+ expect(stripAnsiEscapes('\x1b[38;2;255;0;0mcolored\x1b[0m')).toBe('colored');+ expect(stripAnsiEscapes('hello\x1b[32mworld\x1b[0m!')).toBe('helloworld!');+});