Skip to content

Commit a22ff98

Browse files
authored
fix(security): bound test path classification (#1262)
1 parent 13f7749 commit a22ff98

2 files changed

Lines changed: 52 additions & 6 deletions

File tree

‎src/clawsweeper-change-detection.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,22 @@ export function sqliteSchemaChangeFromPullFilesForTest(options: {
195195
}
196196

197197
function isProductionSourcePath(path: string): boolean {
198-
return Boolean(
199-
path &&
200-
!isDocsPath(path) &&
201-
!/(^|\/)(?:__tests__|examples?|fixtures?|test|tests)(?:\/|$)/i.test(path) &&
202-
!/\.(?:spec|test)\.[^/]+$/i.test(path),
203-
);
198+
if (!path || isDocsPath(path)) return false;
199+
const segments = path.toLowerCase().split("/");
200+
if (
201+
segments.some((segment) =>
202+
["__tests__", "example", "examples", "fixture", "fixtures", "test", "tests"].includes(
203+
segment,
204+
),
205+
)
206+
) {
207+
return false;
208+
}
209+
const basename = segments.at(-1) ?? "";
210+
return ![".spec.", ".test."].some((marker) => {
211+
const markerIndex = basename.indexOf(marker);
212+
return markerIndex >= 0 && markerIndex + marker.length < basename.length;
213+
});
204214
}
205215

206216
function isLikelySqliteSchemaPath(path: string): boolean {

‎test/pr-surface-policy.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import assert from "node:assert/strict";
22
import { readFileSync } from "node:fs";
3+
import { performance } from "node:perf_hooks";
34
import test from "node:test";
45

56
import {
@@ -444,6 +445,41 @@ test("SQLite schema detector finds production table changes and ignores non-sche
444445
});
445446
});
446447

448+
test("production path classification preserves test segment and basename boundaries", () => {
449+
const cases = [
450+
["test/schema.sql", false],
451+
["src/__TESTS__/schema.sql", false],
452+
["examples/schema.sql", false],
453+
["src/cache/store.spec.ts", false],
454+
["src/cache/store.TEST.TS", false],
455+
["src/spec/schema.sql", true],
456+
["src/test-fixtures/schema.sql", true],
457+
["src/cache/store.spec.", true],
458+
["src/cache/store.spec.unit.spec.", false],
459+
] as const;
460+
461+
for (const [filename, expectedChange] of cases) {
462+
const detection = sqliteSchemaChangeFromPullFilesForTest({
463+
pullFiles: [{ filename, patch: "@@\n+CREATE TABLE example (id TEXT);" }],
464+
});
465+
466+
assert.equal(detection.change, expectedChange, filename);
467+
}
468+
});
469+
470+
test("production path classification is bounded for repeated test markers", () => {
471+
const filename = `${".spec.".repeat(20_000)}/schema.sql`;
472+
const startedAt = performance.now();
473+
const detection = sqliteSchemaChangeFromPullFilesForTest({
474+
pullFiles: [{ filename, patch: "@@\n+CREATE TABLE example (id TEXT);" }],
475+
});
476+
const elapsedMs = performance.now() - startedAt;
477+
478+
assert.equal(detection.change, true);
479+
assert.equal(detection.files.length, 1);
480+
assert.ok(elapsedMs < 250, `classification took ${elapsedMs.toFixed(1)}ms`);
481+
});
482+
447483
test("SQLite schema warning is visible for table changes and absent otherwise", () => {
448484
const reportBody = `
449485

0 commit comments

Comments
 (0)