diff --git a/.changeset/tools-start-message-redos.md b/.changeset/tools-start-message-redos.md new file mode 100644 index 000000000..55e1b1470 --- /dev/null +++ b/.changeset/tools-start-message-redos.md @@ -0,0 +1,5 @@ +--- +'@rock-js/tools': patch +--- + +Strip trailing dots from spinner messages without a regular expression, fixing a CodeQL polynomial ReDoS alert. diff --git a/packages/tools/src/lib/__tests__/prompts.test.ts b/packages/tools/src/lib/__tests__/prompts.test.ts new file mode 100644 index 000000000..d5fea23bf --- /dev/null +++ b/packages/tools/src/lib/__tests__/prompts.test.ts @@ -0,0 +1,23 @@ +import { expect, test } from 'vitest'; +import { formatStartMessage } from '../prompts.js'; + +test.each([ + ['Building the app', 'Building the app...'], + ['Building the app.', 'Building the app...'], + ['Building the app...', 'Building the app...'], + ['v1.2 build', 'v1.2 build...'], + ['...', '...'], + ['', '...'], +])('formatStartMessage(%j) is %j', (text, expected) => { + expect(formatStartMessage(text)).toBe(expected); +}); + +test('formatStartMessage keeps undefined', () => { + expect(formatStartMessage(undefined)).toBeUndefined(); +}); + +// `/\.+$/` backtracks quadratically on this input (~13s), past the test timeout. +test('formatStartMessage handles long runs of dots', () => { + const text = `${'.'.repeat(200_000)}x`; + expect(formatStartMessage(text)).toBe(`${text}...`); +}); diff --git a/packages/tools/src/lib/prompts.ts b/packages/tools/src/lib/prompts.ts index d1bc9efbe..b810bacc1 100644 --- a/packages/tools/src/lib/prompts.ts +++ b/packages/tools/src/lib/prompts.ts @@ -135,8 +135,11 @@ export function formatStartMessage( return undefined; } - const messageWithoutDots = text.replace(/\.+$/, ''); - return `${messageWithoutDots}...`; + let end = text.length; + while (end > 0 && text[end - 1] === '.') { + end--; + } + return `${text.slice(0, end)}...`; } export function cancelPromptAndExit(message?: string): never {