Skip to content

Commit e88b50f

Browse files
thymikeeclaude
andauthored
fix: clear the polynomial-redos class across main (#1546)
* fix: clear the polynomial-redos class across main Three sites of the same CodeQL js/polynomial-redos family: - packages/replay-test session-test-artifacts/-discovery slugs trimmed edge dashes with /^-+|-+$/g, which backtracks polynomially on long dash runs built from caller-supplied paths (alerts #27/#28). Replaced with a shared linear trimEdgeDashes. - src/replay/target-identity.ts's target-v1 annotation line regex had the \s+(.*) ambiguity (the shape flagged as alert #29 on the #1536 copy). Anchored the payload group on \S so the split point is unique; the only caller matches against trimmed lines, so behavior is unchanged. Adversarial regression test on the slug path (100k-char dash run, sub-second); the annotation-regex adversarial case is covered on the #1536 package copy and the frozen replay-compat corpus passes here unchanged. Co-Authored-By: Claude <[email protected]> * test: make the redos regression fail against the retired regex form The edge-run input matched the old /^-+|-+$/g in one pass; the quadratic case is an interior run (each dash restarts a -+$ attempt that fails at the trailing byte). The slug pipeline collapses runs before trimming, so the test targets trimEdgeDashes directly and asserts the input comes back byte-identical. Co-Authored-By: Claude <[email protected]> * fix: drop the import the test rewrite orphaned Co-Authored-By: Claude <[email protected]> * test: pin the all-dash fallback identifiers Artifact slug falls back to 'test', invocation id to 'suite', and a session-name slug that trims to nothing is omitted without a dangling separator. Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
1 parent ef66dcd commit e88b50f

4 files changed

Lines changed: 53 additions & 15 deletions

File tree

‎packages/replay-test/src/internal/__tests__/session-test-discovery.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { test } from 'vitest';
22
import assert from 'node:assert/strict';
33
import { AppError } from '@agent-device/kernel/errors';
4-
import { discoverReplayTestEntries } from '../session-test-discovery.ts';
4+
import {
5+
buildReplayTestInvocationId,
6+
buildReplayTestSessionName,
7+
discoverReplayTestEntries,
8+
} from '../session-test-discovery.ts';
9+
import { buildReplayTestArtifactSlug, trimEdgeDashes } from '../session-test-artifacts.ts';
510
import type { ReplayTestManifest, ReplayTestSource } from '../session-test-types.ts';
611

712
// Scheduler-owned discovery policy (#1478 P3b): which sources a --platform filter runs, which
@@ -75,3 +80,27 @@ test('a suite that matched nothing after filtering is rejected', () => {
7580
(error: unknown) => error instanceof AppError && /No replay tests matched/.test(error.message),
7681
);
7782
});
83+
84+
test('edge-dash trimming stays linear on an interior dash run (CodeQL js/polynomial-redos)', () => {
85+
// The slug pipeline collapses character runs before trimming, so only a
86+
// direct call can carry a long interior run — which is exactly the shape
87+
// the retired /^-+|-+$/g form re-scans quadratically (each interior dash
88+
// restarts a -+$ attempt that fails at the trailing x). 100k dashes take
89+
// seconds there and must stay well under a second here, with the input
90+
// returned byte-identical since nothing sits at the edges.
91+
const interiorRun = `x${'-'.repeat(100_000)}x`;
92+
const startedAt = performance.now();
93+
const trimmed = trimEdgeDashes(interiorRun);
94+
assert.ok(performance.now() - startedAt < 1000);
95+
assert.equal(trimmed, interiorRun);
96+
});
97+
98+
test('all-dash inputs land on the documented fallbacks instead of empty identifiers', () => {
99+
assert.equal(buildReplayTestArtifactSlug('/tmp/####', '/tmp'), 'test');
100+
assert.equal(buildReplayTestInvocationId('----'), 'suite');
101+
// A session-name slug that trims to nothing is omitted entirely — no dangling separator.
102+
assert.equal(
103+
buildReplayTestSessionName('s', 'suite1', '/tmp/----.ad', 0),
104+
's:test:suite1:1:attempt-1',
105+
);
106+
});

‎packages/replay-test/src/internal/session-test-artifacts.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ export function buildReplayTestArtifactSlug(filePath: string, cwd?: string): str
2323
? path.basename(filePath)
2424
: relativePath;
2525
return (
26-
value
27-
.toLowerCase()
28-
.replace(/[\\/]+/g, '__')
29-
.replace(/[^a-z0-9._-]+/g, '-')
30-
.replace(/^-+|-+$/g, '') || 'test'
26+
trimEdgeDashes(
27+
value
28+
.toLowerCase()
29+
.replace(/[\\/]+/g, '__')
30+
.replace(/[^a-z0-9._-]+/g, '-'),
31+
) || 'test'
3132
);
3233
}
3334

@@ -135,3 +136,16 @@ function isExistingFile(filePath: string): boolean {
135136
return false;
136137
}
137138
}
139+
140+
/**
141+
* Linear-time edge trim. The regex form (`/^-+|-+$/g`) backtracks
142+
* polynomially on long dash runs (CodeQL js/polynomial-redos #27/#28), and
143+
* these slugs are built from caller-supplied file paths.
144+
*/
145+
export function trimEdgeDashes(value: string): string {
146+
let start = 0;
147+
let end = value.length;
148+
while (start < end && value[start] === '-') start += 1;
149+
while (end > start && value[end - 1] === '-') end -= 1;
150+
return value.slice(start, end);
151+
}

‎packages/replay-test/src/internal/session-test-discovery.ts‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import path from 'node:path';
2+
import { trimEdgeDashes } from './session-test-artifacts.ts';
23
import { AppError } from '@agent-device/kernel/errors';
34
import { isApplePlatform, type PlatformSelector } from '@agent-device/kernel/device';
45
import type {
@@ -89,20 +90,14 @@ export function buildReplayTestSessionName(
8990
attemptIndex = 0,
9091
): string {
9192
const baseName = path.basename(filePath, path.extname(filePath));
92-
const slug = baseName
93-
.toLowerCase()
94-
.replace(/[^a-z0-9]+/g, '-')
95-
.replace(/^-+|-+$/g, '');
93+
const slug = trimEdgeDashes(baseName.toLowerCase().replace(/[^a-z0-9]+/g, '-'));
9694
const testNumber = caseIndex + 1;
9795
return `${sessionName}:test:${suiteInvocationId}:${testNumber}${slug ? `-${slug}` : ''}:attempt-${attemptIndex + 1}`;
9896
}
9997

10098
export function buildReplayTestInvocationId(requestId?: string): string {
10199
const raw = requestId?.trim() || `${process.pid}-${Date.now().toString(36)}`;
102-
const normalized = raw
103-
.toLowerCase()
104-
.replace(/[^a-z0-9]+/g, '-')
105-
.replace(/^-+|-+$/g, '');
100+
const normalized = trimEdgeDashes(raw.toLowerCase().replace(/[^a-z0-9]+/g, '-'));
106101
return normalized || 'suite';
107102
}
108103

‎src/replay/target-identity.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { AppError } from '@agent-device/kernel/errors';
1313
const TARGET_ANNOTATION_TAG = 'agent-device:target-v1';
1414
// Captures the rest of the line verbatim: a line claiming the tag with a
1515
// garbage payload is a malformed v1 annotation, never an ordinary comment.
16-
const TARGET_ANNOTATION_LINE_RE = /^#\s*agent-device:target-v(\d+)(?:\s+(.*))?$/;
16+
const TARGET_ANNOTATION_LINE_RE = /^#\s*agent-device:target-v(\d+)(?:\s+(\S.*))?$/;
1717

1818
export const TARGET_ANNOTATION_MAX_FIELD_BYTES = 256;
1919
export const TARGET_ANNOTATION_MAX_PAYLOAD_BYTES = 4096;

0 commit comments

Comments
 (0)