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): identify invalid committed receipt lines (#2180)

Dipesh Babue5d5f2c7d498
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.md
index 8fc67e24f..b3130b583 100644
--- a/sdk/typescript/docs/cli.md
+++ b/sdk/typescript/docs/cli.md
@@ -1253,6 +1253,11 @@ 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`.
+
New attempts use `recovery-checkouts/<id>/attempt-<n>`. Recovery defaults to four
workers and one recovery/new attempt per repository. A resume connection failure
stops only that repository for this invocation. Failed recovery checkouts remain;
diff --git a/sdk/typescript/src/multiscan.ts b/sdk/typescript/src/multiscan.ts
index 0344ffab7..274436b99 100644
--- a/sdk/typescript/src/multiscan.ts
+++ b/sdk/typescript/src/multiscan.ts
@@ -1057,20 +1057,21 @@ async function readReceipts(
throw error;
}
const end = contents.lastIndexOf(10) + 1;
- if (contents.at(-1) !== 10) {
- const partial = contents.subarray(end);
- if (preserveInterrupted) {
- await writeFile(`${path}.interrupted-${randomUUID()}`, partial, {
- flag: "wx",
- mode: 0o600,
- });
- }
- await truncate(path, end);
- }
const lines = contents.subarray(0, end).toString("utf8").split("\n");
const receipts = new Map<string, MultiscanHistory>();
- for (const line of lines.filter(Boolean)) {
- const saved = JSON.parse(line);
+ for (const [index, line] of lines.entries()) {
+ if (!line) continue;
+ let saved;
+ try {
+ saved = JSON.parse(line);
+ if (typeof saved?.id !== "string")
+ throw new Error("Expected a receipt with a string id");
+ } 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.`,
+ { cause },
+ );
+ }
if (saved.status === "completed_with_incomplete_coverage")
saved.status = "completed";
const receipt = saved as MultiscanReceipt;
@@ -1082,6 +1083,16 @@ async function readReceipts(
scan: receipt.knowledgeBaseFailure ? previous?.scan : receipt,
});
}
+ if (contents.at(-1) !== 10) {
+ const partial = contents.subarray(end);
+ if (preserveInterrupted) {
+ await writeFile(`${path}.interrupted-${randomUUID()}`, partial, {
+ flag: "wx",
+ mode: 0o600,
+ });
+ }
+ await truncate(path, end);
+ }
return receipts;
}
diff --git a/sdk/typescript/tests-ts/multiscan-recovery.test.ts b/sdk/typescript/tests-ts/multiscan-recovery.test.ts
index 2706414e6..ad50e01f0 100644
--- a/sdk/typescript/tests-ts/multiscan-recovery.test.ts
+++ b/sdk/typescript/tests-ts/multiscan-recovery.test.ts
@@ -509,6 +509,54 @@ describe("multiscan", () => {
]);
});
+ test.each([
+ ["malformed middle record", "{", true],
+ ["malformed final record", "{", false],
+ ["non-object", "null", false],
+ ["missing ID", "{}", false],
+ ["non-string ID", '{"id":42}', false],
+ ] as const)(
+ "CLI identifies the %s and preserves committed history",
+ async (_name, bad, middle) => {
+ const { paths } = await repositoryFixture("receipt");
+ const args = [
+ "bulk-scan",
+ paths.input,
+ "--output-dir",
+ paths.output,
+ "--json",
+ ];
+ const deps = dependencies();
+ const run = mock(completeRun);
+ const configured: Parameters<typeof main>[3] = {
+ ...deps,
+ createSecurity: (config) => ({ ...deps.createSecurity(config), run }),
+ };
+ expect(await createCliTest(main).runCli(args, configured)).toBe(0);
+ const ledger = join(paths.output, "results.jsonl");
+ const valid = await readFile(ledger, "utf8");
+ const damaged = Buffer.concat([
+ Buffer.from(`${valid}\n${bad}\n`),
+ ...(middle ? [Buffer.from(valid), Buffer.from([0xe2, 0x82])] : []),
+ ]);
+ await writeFile(ledger, damaged);
+ for (const flags of [[], ["--recover"]]) {
+ const cli = createCliTest(main);
+ expect(await cli.runCli([...args, ...flags], configured)).toBe(2);
+ expect(cli.stderr.text()).toContain(`${ledger}:3:`);
+ expect(cli.stderr.text()).toContain("Restore a complete");
+ expect(cli.stderr.text()).toContain("new --output-dir");
+ expect(await readFile(ledger)).toEqual(damaged);
+ expect(
+ (await readdir(paths.output)).filter((name) =>
+ name.startsWith("results.jsonl.interrupted-"),
+ ),
+ ).toEqual([]);
+ }
+ expect(run).toHaveBeenCalledTimes(1);
+ },
+ );
+
const interruptedUtf8Cases: [string, number, boolean][] = [
["é", 1, false],
["中", 1, false],