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
5 changes: 5 additions & 0 deletions .changeset/tools-start-message-redos.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rock-js/tools': patch
---

Strip trailing dots from spinner messages without a regular expression, fixing a CodeQL polynomial ReDoS alert.
23 changes: 23 additions & 0 deletions packages/tools/src/lib/__tests__/prompts.test.ts
Original file line number Diff line number Diff line change
@@ -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}...`);
});
7 changes: 5 additions & 2 deletions packages/tools/src/lib/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading