Skip to content

Commit 2195258

Browse files
fix(lab): rewrite raw POSIX path regex without ambiguous alternation
Code scanning flagged RAW_POSIX_PATH_RE as polynomial ReDoS (alert #1): the `(?:\/|[^/\0\r\n]+)+` alternation can repartition a run of path segments combinatorially on client-supplied event fields. Rewrite the segment run as an unambiguous `[^/]+(?:\/+[^/]+)*\/*` sequence so the match stays linear. The exec-side sibling regex was already rewritten on dev; this closes the remaining flagged pattern. Co-Authored-By: Epinephrine <[email protected]>
1 parent 5cdd97e commit 2195258

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎src/lab/events/limits.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const FORBIDDEN_EXACT_KEYS = new Set([
3232
]);
3333

3434
const RAW_POSIX_PATH_RE =
35-
/(?:^|[^A-Za-z0-9._~/])\/(?:(?=$|[^A-Za-z0-9._~/])|(?!\/)(?![ \t\r\n])(?:\/|[^/\0\r\n]+)+\/?(?=$|[^A-Za-z0-9._~/]))/u;
35+
/(?:^|[^A-Za-z0-9._~/])\/(?:(?=$|[^A-Za-z0-9._~/])|(?!\/)(?![ \t\r\n])[^/\0\r\n]+(?:\/+[^/\0\r\n]+)*\/*(?=$|[^A-Za-z0-9._~/]))/u;
3636
const ASCII_URL_WHITESPACE_RE = /[\t\r\n]/g;
3737
const FILE_URI_RE = /(?:^|[^A-Za-z0-9+.-])file:/i;
3838

‎tests/lab/lab-post-merge-hardening.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,30 @@ test("event privacy admission rejects raw filesystem path bypass forms", () => {
224224
}
225225
});
226226

227+
test("event privacy admission stays linear on pathological path strings", () => {
228+
// RAW_POSIX_PATH_RE once alternated `\/` with `[^/]+` under a shared `+`; long segment runs
229+
// were repartitioned combinatorially. Keep these just under the 4 KiB field cap so a
230+
// backtracking regression surfaces as a timeout rather than a wrong verdict.
231+
const deep = `cwd=/${"a/".repeat(2000)}`;
232+
try {
233+
enforceEventStructureLimits({ detail: deep });
234+
throw new Error("expected raw_path rejection for a long segment chain");
235+
} catch (err) {
236+
expect((err as { code?: string }).code).toBe("raw_path");
237+
}
238+
239+
const dense = `cwd=/${"a//".repeat(1300)}`;
240+
try {
241+
enforceEventStructureLimits({ detail: dense });
242+
throw new Error("expected raw_path rejection for a slash-dense chain");
243+
} catch (err) {
244+
expect((err as { code?: string }).code).toBe("raw_path");
245+
}
246+
247+
const notAPath = `https://example.com/${"a/".repeat(2000)}`;
248+
expect(() => enforceEventStructureLimits({ detail: notAPath })).not.toThrow();
249+
});
250+
227251
test("invalid JSON contract artifacts classify as artifact_mismatch", () => {
228252
const home = tempHome();
229253
const artifactsDir = join(home, "artifacts");

0 commit comments

Comments
 (0)