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(sdk): reject hosted receipts for changed sealed files (#2105)

Michael D'Angelo4fdd71b0d00b
diff --git a/sdk/typescript/src/contract.ts b/sdk/typescript/src/contract.ts
index 9f7c985f2..3fcd03ba4 100644
--- a/sdk/typescript/src/contract.ts
+++ b/sdk/typescript/src/contract.ts
@@ -64,6 +64,12 @@ export interface LoadedContract {
review: NonNullable<ScanManifest["scan"]["review"]>;
}
+// Hosted receipts compare reread files with the bytes that passed validation.
+export const validatedFileDigests = new WeakMap<
+ ScanManifest,
+ ReadonlyMap<string, string>
+>();
+
export class ContractSchemaError extends ContractValidationError {}
export const SEALED_DEFERRED_PATH_RULE = "invalidSealedDeferredPath";
@@ -94,12 +100,12 @@ export async function loadContractWithScanDirectory(
);
const scanRoot = await requireScanRoot(scanDirectory, options.signal);
const scanDir = scanRoot.path;
- const documentDigests = new Map<string, string>();
+ const fileDigests = new Map<string, string>();
const payloads: Record<string, unknown> = {};
payloads["scan-manifest.json"] = await readScanJson(
scanDir,
"scan-manifest.json",
- documentDigests,
+ fileDigests,
options.signal,
scanRoot,
);
@@ -114,7 +120,7 @@ export async function loadContractWithScanDirectory(
payloads[filename] = await readScanJson(
scanDir,
filename as keyof typeof DOCUMENTS,
- documentDigests,
+ fileDigests,
options.signal,
scanRoot,
);
@@ -196,7 +202,7 @@ export async function loadContractWithScanDirectory(
manifest,
findings,
coverage,
- documentDigests,
+ fileDigests,
options.signal,
scanRoot,
);
@@ -209,6 +215,7 @@ export async function loadContractWithScanDirectory(
);
}
await verifyScanRoot(scanRoot, options.signal);
+ validatedFileDigests.set(manifest, fileDigests);
return {
contract: { manifest, findings, review, mode },
scanDirectory: scanRoot.path,
@@ -510,7 +517,7 @@ async function validateSeal(
manifest: ScanManifest,
findings: FindingsDocument,
coverage: CoverageDocument | undefined,
- documentDigests: ReadonlyMap<string, string>,
+ fileDigests: Map<string, string>,
signal?: AbortSignal,
expectedRoot?: ScanRoot,
): Promise<void> {
@@ -536,7 +543,7 @@ async function validateSeal(
artifactPaths.add(normalized);
artifactCollisionKeys.add(collisionKey);
const digest =
- documentDigests.get(normalized) ??
+ fileDigests.get(normalized) ??
(await sha256ScanFile(
scanDir,
normalized,
@@ -549,6 +556,7 @@ async function validateSeal(
`${context}: sealed artifact changed or is missing.`,
);
}
+ fileDigests.set(artifact.path, digest);
}
const requireSealedReference = (reference: unknown, context: string) => {
@@ -823,7 +831,7 @@ export function safeScopePath(value: string): string {
async function readScanJson(
scanDir: string,
relativePath: keyof typeof DOCUMENTS,
- documentDigests: Map<string, string>,
+ fileDigests: Map<string, string>,
signal?: AbortSignal,
expectedRoot?: ScanRoot,
): Promise<Record<string, unknown>> {
@@ -836,7 +844,7 @@ async function readScanJson(
);
try {
const bytes = await file.readFile({ signal });
- documentDigests.set(relativePath, hash("sha256", bytes));
+ fileDigests.set(relativePath, hash("sha256", bytes));
return parseJson(join(scanDir, relativePath), bytes);
} catch (error) {
throwIfAborted(signal);
diff --git a/sdk/typescript/src/hosted-scan.ts b/sdk/typescript/src/hosted-scan.ts
index f8a21120e..b0007f584 100644
--- a/sdk/typescript/src/hosted-scan.ts
+++ b/sdk/typescript/src/hosted-scan.ts
@@ -1,6 +1,10 @@
-import { createHash } from "node:crypto";
+import { hash } from "node:crypto";
import { isDeepStrictEqual } from "node:util";
-import { readScanFile, safeScopePath } from "./contract.js";
+import {
+ readScanFile,
+ safeScopePath,
+ validatedFileDigests,
+} from "./contract.js";
import { isRecord } from "./record.js";
import { VERSION } from "./version.js";
import {
@@ -287,11 +291,12 @@ export async function runHostedScan(
)
throw new Error("Completed artifacts do not match the requested scope.");
const sources = result.manifest.scan["preservedSources"];
+ const sealedDigests = validatedFileDigests.get(result.manifest)!;
const names = new Set([
"scan-manifest.json",
"findings.json",
"report.md",
- ...result.manifest.scan.artifacts.map((artifact) => artifact.path),
+ ...result.manifest.scan.artifacts.map(({ path }) => path),
...(isRecord(sources) ? Object.keys(sources) : []),
]);
if (result.ownerSuggestionsPath !== undefined)
@@ -301,11 +306,20 @@ export async function runHostedScan(
const contents = await readScanFile(outputDirectory, path, path);
return {
path,
- sha256: createHash("sha256").update(contents).digest("hex"),
- bytes: Buffer.byteLength(contents),
+ sha256: hash("sha256", contents),
+ bytes: contents.byteLength,
};
}),
);
+ // Report a seal mismatch after the concurrent reads have closed their files.
+ for (const { path, sha256 } of artifacts) {
+ const expected = sealedDigests.get(path);
+ if (expected !== undefined && expected !== sha256) {
+ throw new Error(
+ `The sealed file ${path} changed before the hosted receipt was created.`,
+ );
+ }
+ }
return {
...base(),
status: "completed" as const,
diff --git a/sdk/typescript/tests-ts/hosted-scan.test.ts b/sdk/typescript/tests-ts/hosted-scan.test.ts
index 4c6746975..fd5805d9e 100644
--- a/sdk/typescript/tests-ts/hosted-scan.test.ts
+++ b/sdk/typescript/tests-ts/hosted-scan.test.ts
@@ -1,8 +1,15 @@
import { execFileSync, spawn } from "node:child_process";
import { createInterface } from "node:readline";
-import { createHash } from "node:crypto";
+import { hash } from "node:crypto";
import { PassThrough, Writable } from "node:stream";
-import { readFile, realpath, stat, symlink, unlink } from "node:fs/promises";
+import {
+ appendFile,
+ readFile,
+ realpath,
+ stat,
+ symlink,
+ unlink,
+} from "node:fs/promises";
import { tmpdir } from "node:os";
import { delimiter, dirname, isAbsolute, join } from "node:path";
import { afterEach, expect, mock, spyOn, test } from "bun:test";
@@ -151,7 +158,10 @@ async function fixture(
};
}
-async function writeDraft(request: ScanExecutionRequest) {
+async function writeDraft(
+ request: ScanExecutionRequest,
+ receiptRefs: string[] = [],
+) {
const directory = request.runtime.outputDirectory;
const reviewState = {
inventoryStrategy:
@@ -161,7 +171,7 @@ async function writeDraft(request: ScanExecutionRequest) {
id: "fixture",
label: "Synthetic scope fixture",
disposition: "no_issue_found",
- receiptRefs: [],
+ receiptRefs,
notes: "Synthetic test only.",
},
],
@@ -185,6 +195,7 @@ async function writeDraft(request: ScanExecutionRequest) {
};
for (const [name, document] of Object.entries(documents))
await writeJson(join(directory, name), document);
+ return reviewState;
}
function receipt(
@@ -379,9 +390,7 @@ test.each(sealedScopes)(
if (normalized.length > 1) expect(report).not.toContain("service-c");
for (const artifact of result.artifacts) {
const data = await readFile(join(result.outputDirectory, artifact.path));
- expect(artifact.sha256).toBe(
- createHash("sha256").update(data).digest("hex"),
- );
+ expect(artifact.sha256).toBe(hash("sha256", data));
expect(artifact.bytes).toBe(data.byteLength);
}
const artifactPaths = result.artifacts.map((artifact) => artifact.path);
@@ -669,6 +678,81 @@ test("sealed review with follow-up diagnostics remains completed", async () => {
);
});
+test.each(["scan-manifest.json", "findings.json", "artifacts/evidence.txt"])(
+ "changed bytes in %s cannot produce a completed hosted receipt",
+ async (path) => {
+ const original = CodexSecurity.prototype.run;
+ using _swap = spyOn(CodexSecurity.prototype, "run").mockImplementation(
+ async function (this: CodexSecurity, ...args) {
+ const validated = await original.apply(this, args);
+ await appendFile(join(validated.scanDir, path), "\n");
+ return validated;
+ },
+ );
+ const result = await runHostedScan(await fixture(), {
+ executor: {
+ async run(request) {
+ const evidence = "artifacts/evidence.txt";
+ writeSource(
+ request.runtime.outputDirectory,
+ evidence,
+ "scan evidence",
+ );
+ await writeDraft(request, [evidence]);
+ return receipt(request);
+ },
+ },
+ });
+ expect(result.status).toBe("failed");
+ expect(result.artifacts).toEqual([]);
+ expect(result).toHaveProperty(
+ "error",
+ `The sealed file ${path} changed before the hosted receipt was created.`,
+ );
+ },
+);
+
+test("hosted receipts hash MCP checkpoints as written", async () => {
+ const { saveScanDraftCheckpoint } = await import(
+ new URL(
+ "../../../plugins/codex-security/mcp-app/src/artifact-scan-draft.ts",
+ import.meta.url,
+ ).href
+ );
+ const input = await fixture();
+ const result = await runHostedScan(input, {
+ executor: {
+ async run(request) {
+ const reviewState = await writeDraft(request);
+ await saveScanDraftCheckpoint(
+ {
+ root: request.runtime.outputDirectory,
+ repoRoot: request.repository,
+ layout: "scan",
+ },
+ { scanId: request.scanId, findings: [], reviewState },
+ );
+ return receipt(request);
+ },
+ },
+ });
+ expect(result.status).toBe("completed");
+ const manifest = await readJson<{
+ scan: { preservedSources: Record<string, string> };
+ }>(join(input.outputDirectory, "scan-manifest.json"));
+ const checkpoint = result.artifacts.find(
+ ({ path, sha256 }) =>
+ path.startsWith("checkpoints/") &&
+ Object.hasOwn(manifest.scan.preservedSources, path) &&
+ sha256 !== manifest.scan.preservedSources[path],
+ );
+ expect(checkpoint).toBeDefined();
+ const contents = await readFile(
+ join(input.outputDirectory, checkpoint!.path),
+ );
+ expect(checkpoint!.sha256).toBe(hash("sha256", contents));
+});
+
test("a changed checkout cannot produce accepted completed output", async () => {
const input = await fixture();
const result = await runHostedScan(input, {