Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(bulk-scan): reject invalid saved attempt numbers (#2292)

Michael D'Angelo0b755454b0c1
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.md
index 1aac4a88b..d191056fe 100644
--- a/sdk/typescript/docs/cli.md
+++ b/sdk/typescript/docs/cli.md
@@ -1275,10 +1275,12 @@ failed or interrupted attempt:
- An unavailable, failed, or canceled scan starts a new attempt at the pinned
revision, preserving old artifacts and checkouts.
-If a nonempty committed line in `results.jsonl` contains invalid JSON or lacks
-a string task ID, the command reports its line number and leaves the ledger untouched.
-Restore a complete ledger backup that includes every committed attempt, or start
-a separate campaign in a new `--output-dir`.
+Each nonempty committed line in `results.jsonl` must be valid JSON with a string
+task ID and a positive safe integer attempt number. If a line fails these checks,
+the command reports its line number and preserves the ledger. Restore a complete
+ledger backup that includes every committed attempt, or start a separate campaign
+in a new `--output-dir`. If a repository exhausts its attempt numbers, starting
+a fresh attempt requires a new campaign.
New attempts use `recovery-checkouts/<id>/attempt-<n>`. Recovery defaults to four
workers and one recovery/new attempt per repository. A resume connection failure
diff --git a/sdk/typescript/src/multiscan.ts b/sdk/typescript/src/multiscan.ts
index 274436b99..a6f2ad2c5 100644
--- a/sdk/typescript/src/multiscan.ts
+++ b/sdk/typescript/src/multiscan.ts
@@ -63,6 +63,8 @@ const LOCK_HEARTBEAT_MS = 5_000;
const WINDOWS_DEVICE_PATH_NAME =
/^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\.|$)/iu;
+class AttemptNumberExhaustedError extends Error {}
+
type MultiscanTask = ReturnType<typeof parseInventory>[number];
interface MultiscanReceipt extends MultiscanTask {
@@ -349,11 +351,7 @@ async function runCampaign(
continue;
}
if (artifactsPresent) {
- if (
- receipt.status === "completed" &&
- Number.isSafeInteger(receipt.attempt) &&
- receipt.attempt > 0
- ) {
+ if (receipt.status === "completed") {
try {
if (receipt.cleanupPending === "checkout") {
await rm(join(output, "checkouts", task.id), {
@@ -418,6 +416,13 @@ async function runCampaign(
const knowledge = knowledgeByMode[task.mode];
const history = receipts.get(task.id.toLowerCase());
let maxAttempt = history?.maxAttempt ?? 0;
+ const nextAttempt = () => {
+ if (maxAttempt === Number.MAX_SAFE_INTEGER)
+ throw new AttemptNumberExhaustedError(
+ `No more attempt numbers are available for ${task.id}. Start a separate campaign in a new --output-dir.`,
+ );
+ return ++maxAttempt;
+ };
let recoveryAttempt = history?.scan?.attempt ?? 0;
const artifactRoot = join(output, "artifacts", task.id);
if (options.recoverScan !== undefined) {
@@ -433,7 +438,7 @@ async function runCampaign(
for (let retry = 0; retry < options.maxAttempts; retry += 1) {
options.signal?.throwIfAborted();
let attempt = maxAttempt;
- if (options.recoverScan === undefined) attempt = ++maxAttempt;
+ if (options.recoverScan === undefined) attempt = nextAttempt();
else if (retry === 0) attempt = recoveryAttempt;
let scanDir = join(artifactRoot, `attempt-${attempt}`);
let checkout: string | undefined;
@@ -495,7 +500,7 @@ async function runCampaign(
}
const scanSettings = options.scanOptionsByMode?.[task.mode];
if (result === undefined) {
- if (options.recoverScan !== undefined) attempt = ++maxAttempt;
+ if (options.recoverScan !== undefined) attempt = nextAttempt();
scanDir = join(artifactRoot, `attempt-${attempt}`);
notifyProgress(options, {
repository: task.id,
@@ -591,6 +596,7 @@ async function runCampaign(
if (result.manifest.scan.status !== "completed")
throw new Error(`Multiscan scan ${result.manifest.scan.status}.`);
} catch (error) {
+ if (error instanceof AttemptNumberExhaustedError) throw error;
if (options.signal?.aborted === true) options.signal.throwIfAborted();
if (error instanceof ScanCostLimitExceededError) {
cost = error.cost;
@@ -1066,6 +1072,8 @@ async function readReceipts(
saved = JSON.parse(line);
if (typeof saved?.id !== "string")
throw new Error("Expected a receipt with a string id");
+ if (!Number.isSafeInteger(saved.attempt) || saved.attempt < 1)
+ throw new Error("Expected a positive integer receipt attempt");
} catch (cause) {
throw new Error(
`${path}:${index + 1}: ${errorMessage(cause)}. Restore a complete ledger backup that includes every committed attempt, or start a separate campaign in a new --output-dir.`,
diff --git a/sdk/typescript/tests-ts/multiscan-checkout.test.ts b/sdk/typescript/tests-ts/multiscan-checkout.test.ts
index e68c2fe46..1a8bf8d3c 100644
--- a/sdk/typescript/tests-ts/multiscan-checkout.test.ts
+++ b/sdk/typescript/tests-ts/multiscan-checkout.test.ts
@@ -545,11 +545,9 @@ describe("multiscan", () => {
})}\n`,
);
- expect(await runMultiscan(configured)).toMatchObject({
- completed: 1,
- failed: 0,
- skipped: 1,
- });
+ await expect(runMultiscan(configured)).rejects.toThrow(
+ "Expected a positive integer receipt attempt",
+ );
expect(scans).toHaveBeenCalledTimes(1);
expect(await readFile(preserved, "utf8")).toBe("preserved\n");
});
diff --git a/sdk/typescript/tests-ts/multiscan-recovery.test.ts b/sdk/typescript/tests-ts/multiscan-recovery.test.ts
index ad50e01f0..ed4668d75 100644
--- a/sdk/typescript/tests-ts/multiscan-recovery.test.ts
+++ b/sdk/typescript/tests-ts/multiscan-recovery.test.ts
@@ -515,6 +515,10 @@ describe("multiscan", () => {
["non-object", "null", false],
["missing ID", "{}", false],
["non-string ID", '{"id":42}', false],
+ ["missing attempt", '{"id":"receipt"}', false],
+ ["string attempt", '{"id":"receipt","attempt":"1"}', false],
+ ["zero attempt", '{"id":"receipt","attempt":0}', false],
+ ["fractional attempt", '{"id":"receipt","attempt":1.5}', true],
] as const)(
"CLI identifies the %s and preserves committed history",
async (_name, bad, middle) => {
@@ -557,6 +561,42 @@ describe("multiscan", () => {
},
);
+ test.each(["rerun", "recover receipt", "recover directory"])(
+ "preserves exhausted attempt numbers on %s",
+ async (mode) => {
+ const { paths } = await repositoryFixture("repo");
+ const setup = options(paths, client(rejecting("Initial failure")), {
+ maxAttempts: 1,
+ });
+ const { resultsPath: ledger } = await runMultiscan(setup);
+ const root = join(paths.output, "artifacts", "repo");
+ const last = join(root, `attempt-${Number.MAX_SAFE_INTEGER}`);
+ if (mode === "recover directory") {
+ await mkdir(last);
+ } else {
+ const [receipt] = await results(ledger);
+ await writeFile(
+ ledger,
+ JSON.stringify({
+ ...receipt,
+ attempt: Number.MAX_SAFE_INTEGER,
+ outputDir: last,
+ }) + "\n",
+ );
+ }
+ const saved = await readFile(ledger, "utf8");
+ const directories = await readdir(root);
+ const run = mock(completeRun);
+ const configured = options(paths, client(run), {
+ ...(mode === "rerun" ? {} : { recoverScan: async () => undefined }),
+ });
+ await expect(runMultiscan(configured)).rejects.toThrow("attempt numbers");
+ expect(run).not.toHaveBeenCalled();
+ expect(await readFile(ledger, "utf8")).toBe(saved);
+ expect(await readdir(root)).toEqual(directories);
+ },
+ );
+
const interruptedUtf8Cases: [string, number, boolean][] = [
["é", 1, false],
["中", 1, false],
@@ -1838,11 +1878,12 @@ describe("multiscan", () => {
const checkout = join(paths.output, "checkouts", "invalid-recovery");
await mkdir(checkout, { recursive: true, mode: 0o700 });
await writeFile(join(checkout, "retained.txt"), "Original checkout.");
+ const nextAttempt = newerAttempt ? Number.MAX_SAFE_INTEGER : 2;
const nextOutput = join(
paths.output,
"artifacts",
"invalid-recovery",
- "attempt-2",
+ `attempt-${nextAttempt}`,
);
if (newerAttempt) await completedScan(nextOutput);
const recoverScan = mock(async (path: string) => ({
@@ -1870,7 +1911,7 @@ describe("multiscan", () => {
);
expect((await results(summary.resultsPath)).at(-1)).toMatchObject({
status: "completed",
- attempt: 2,
+ attempt: nextAttempt,
});
},
);