Skip to content

Commit 75d0f91

Browse files
committed
fix(cli): match a dash-led id in linear time
looksLikeAnId's single regex backtracked quadratically on a long dash token that ends in a non-id character (CodeQL js/polynomial-redos on the v0.9.43 release). Two linear checks with the same meaning replace it.
1 parent 06ddf2f commit 75d0f91

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎packages/sim-cli/src/runtime/build.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,24 @@ describe('parse-error guidance for a value typed where a flag belongs', () => {
10591059
expect(stderr).toContain('Example: sim move <id> --workspace <id>')
10601060
})
10611061

1062+
it('points an id that opens with a dash past the option parser', () => {
1063+
expect(stderrOf(move(), ['-Ab3xYz', '--workspace', 'ws_1'])).toContain(
1064+
'Example: sim move -- -Ab3xYz'
1065+
)
1066+
})
1067+
1068+
it('treats a lowercase dash token as a misspelt flag, not an id', () => {
1069+
const stderr = stderrOf(move(), ['-abc', '--workspace', 'ws_1'])
1070+
expect(stderr).not.toContain('Example: sim move --')
1071+
expect(stderr).toContain('Options for sim move: -w, --workspace <id>')
1072+
})
1073+
1074+
it('answers a long dash token that is neither flag nor id in linear time', () => {
1075+
const started = performance.now()
1076+
stderrOf(move(), [`-${'0'.repeat(100_000)}!`, '--workspace', 'ws_1'])
1077+
expect(performance.now() - started).toBeLessThan(1000)
1078+
})
1079+
10621080
it('names no flag when two required flags are missing', () => {
10631081
const create = new Command('create')
10641082
.requiredOption('--name <value>')

‎packages/sim-cli/src/runtime/build.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ function commandPath(command: Command): string {
8585
*/
8686
function looksLikeAnId(token: string): boolean {
8787
return (
88-
token.length > 2 &&
89-
!token.startsWith('--') &&
90-
/^-[A-Za-z0-9_-]*[A-Z0-9][A-Za-z0-9_-]*$/.test(token)
88+
token.length > 2 && !token.startsWith('--') && /^-[\w-]+$/.test(token) && /[A-Z0-9]/.test(token)
9189
)
9290
}
9391

0 commit comments

Comments
 (0)