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(deep-scan): align worker artifact validation with publication (#2315)

Michael D'Angelob5e541b6542c
diff --git a/plugins/codex-security/mcp-app/artifact-writer-main.ts b/plugins/codex-security/mcp-app/artifact-writer-main.ts
index 847941419..ec1fb1ea9 100644
--- a/plugins/codex-security/mcp-app/artifact-writer-main.ts
+++ b/plugins/codex-security/mcp-app/artifact-writer-main.ts
@@ -41,15 +41,15 @@ export async function createCodexSecurityArtifactWriterServer(
// Older coordinators sent only the requested paths, with lexical scope matching.
const includePaths = isRecord(scopeInput) ? scopeInput.paths : scopeInput;
const filePaths = isRecord(scopeInput) ? scopeInput.filePaths : undefined;
- for (const paths of [includePaths, filePaths]) {
- if (
- paths !== undefined &&
- (!Array.isArray(paths) || paths.some((path) => typeof path !== "string"))
- ) {
- throw new Error(
- "The coordinator-bound include paths must be an array of paths.",
- );
- }
+ const isPathList = (paths: unknown): paths is string[] =>
+ Array.isArray(paths) && paths.every((path) => typeof path === "string");
+ if (
+ (scopeInput !== undefined && !isPathList(includePaths)) ||
+ (filePaths !== undefined && !isPathList(filePaths))
+ ) {
+ throw new Error(
+ "The coordinator-bound include paths must be an array of paths.",
+ );
}
const pluginRoot = environment.CODEX_SECURITY_PLUGIN_ROOT
? environment.CODEX_SECURITY_PLUGIN_ROOT
@@ -57,8 +57,7 @@ export async function createCodexSecurityArtifactWriterServer(
const pythonCommand = environment.CODEX_SECURITY_PYTHON_COMMAND
? environment.CODEX_SECURITY_PYTHON_COMMAND
: undefined;
- // Preserve the asynchronous context boundary before server construction.
- const context: ArtifactContext = await (async () => ({
+ const context: ArtifactContext = {
root: await canonicalDirectory(root, "Codex Security worker artifact root"),
repoRoot: await canonicalDirectory(
repoRoot,
@@ -80,7 +79,7 @@ export async function createCodexSecurityArtifactWriterServer(
...defined("filePaths", filePaths as string[] | undefined),
...defined("pluginRoot", pluginRoot),
...defined("pythonCommand", pythonCommand),
- }))();
+ };
const server = new McpServer(
{ name: "codex-security-artifacts", version: MCP_APP_VERSION },
{
diff --git a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts b/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
index 37a48bea4..0145b72be 100644
--- a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
+++ b/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts
@@ -545,13 +545,17 @@ export async function retainRecordedReceiptOwnership(
context: ArtifactContext,
input: JsonObject,
): Promise<void> {
- if (await hasScanDraftCheckpoint(context, input)) return;
const coverage = input.reviewState ?? input.coverage;
if (!isObject(coverage) || !Array.isArray(coverage.surfaces)) return;
- for (const surface of coverage.surfaces) {
- if (isObject(surface) && isObject(surface.provenance))
- delete surface.provenance.scanReceiptRefs;
- }
+ const owners = coverage.surfaces.flatMap((surface) =>
+ isObject(surface) &&
+ isObject(surface.provenance) &&
+ "scanReceiptRefs" in surface.provenance
+ ? [surface.provenance]
+ : [],
+ );
+ if (!owners.length || (await hasScanDraftCheckpoint(context, input))) return;
+ for (const owner of owners) delete owner.scanReceiptRefs;
}
export function workerCoverageReceiptLocation(
@@ -562,13 +566,14 @@ export function workerCoverageReceiptLocation(
const components = coverageReceiptComponents(ref);
if (context.layout !== "worker") return { context, components };
const workerPrefix = `artifacts/deep_discovery/workers/${basename(dirname(context.root))}/`;
+ const normalized = components.join("/");
const shared =
isObject(surface.provenance) &&
(surface.provenance.scanReceiptRefs as string[] | undefined)?.includes(ref);
if (
shared ||
- ref.startsWith(`${workerPrefix}output/`) ||
- ref.startsWith(`${workerPrefix}attempts/`)
+ normalized.startsWith(`${workerPrefix}output/`) ||
+ normalized.startsWith(`${workerPrefix}attempts/`)
) {
return {
context: {
diff --git a/plugins/codex-security/mcp-app/src/coverage-publication.ts b/plugins/codex-security/mcp-app/src/coverage-publication.ts
index 48a1f9424..0a6b22136 100644
--- a/plugins/codex-security/mcp-app/src/coverage-publication.ts
+++ b/plugins/codex-security/mcp-app/src/coverage-publication.ts
@@ -39,7 +39,7 @@ function relativeComponents(
if (
/^[\p{White_Space}\x1c-\x1f]*$/u.test(value) ||
(!allowDot && normalized.length === 0) ||
- /^\p{L}:/u.test(value) ||
+ /^[A-Za-z]:/u.test(value) ||
/[\\\x00-\x1f]/u.test(value) ||
value.startsWith("/") ||
parts.includes("..")
@@ -67,6 +67,7 @@ export function validateRetainedCoverage(coverage: JsonObject): void {
"surfaces",
"explicitExclusions",
"deferred",
+ "resolvedDeferred",
"openQuestions",
] as const)
requirePublicationJson(coverage[field], `coverage.${field}`);
diff --git a/plugins/codex-security/mcp-app/src/deep-scan/artifact-validation.ts b/plugins/codex-security/mcp-app/src/deep-scan/artifact-validation.ts
index cea90105c..97f6d8889 100644
--- a/plugins/codex-security/mcp-app/src/deep-scan/artifact-validation.ts
+++ b/plugins/codex-security/mcp-app/src/deep-scan/artifact-validation.ts
@@ -1,12 +1,9 @@
import { createHash } from "node:crypto";
-import { dirname, posix, relative, sep } from "node:path";
+import { dirname, join, posix, relative, sep } from "node:path";
import { asRecord } from "../record.js";
import type { JsonObject } from "../types.js";
import { artifactSourcePath, type ArtifactContext } from "../artifact-io.js";
-import {
- coverageReceiptComponents,
- validateRetainedCoverage,
-} from "../coverage-publication.js";
+import { validateRetainedCoverage } from "../coverage-publication.js";
import {
parsePersistedScanDraft,
scanDraftInputSchema,
@@ -90,19 +87,41 @@ export async function validateDiscoveryArtifacts(
"Standard scan worker wrote only a checkpoint; its audit is not complete.",
);
validateRetainedCoverage(result.reviewState);
+ // Check the worker's own task IDs before qualifying active and closed tasks.
+ const closures = result.reviewState.resolvedDeferred ?? [];
+ if (closures.length) {
+ const active = new Set(
+ result.reviewState.deferred.flatMap((row) => [row.id, row.candidateId]),
+ );
+ const closed = new Set<string>();
+ for (const { id } of closures) {
+ if (active.has(id))
+ throw new Error(
+ `Standard scan worker deferred work is still active: ${id}`,
+ );
+ if (closed.has(id))
+ throw new Error(
+ `Standard scan worker repeats resolved deferred id: ${id}`,
+ );
+ closed.add(id);
+ }
+ }
const receiptPaths = new Map<string, string>();
for (const row of result.reviewState.surfaces as JsonObject[]) {
for (const ref of (row.receiptRefs as string[] | undefined) ?? []) {
- const components = coverageReceiptComponents(ref);
- const normalized = components.join("/");
+ const location = workerCoverageReceiptLocation(context, row, ref);
+ const normalized = relative(
+ artifacts.workersRoot,
+ join(location.context.root, ...location.components),
+ );
const key = normalized.toLowerCase();
const previous = receiptPaths.get(key);
- if (previous !== undefined && previous !== normalized)
+ if (previous === normalized) continue;
+ if (previous !== undefined)
throw new Error(
`Standard scan worker coverage receipt: duplicate artifact path: ${previous} and ${normalized}`,
);
receiptPaths.set(key, normalized);
- const location = workerCoverageReceiptLocation(context, row, ref);
await artifactSourcePath(
location.context,
location.components,
@@ -160,7 +179,7 @@ export function scanCoverage(
if (!surfaceIds.has(surface.id)) surfaceIds.set(surface.id, id);
const shared = asRecord(surface.provenance)?.scanReceiptRefs as
string[] | undefined;
- const scanRefs = new Set(shared?.map((ref) => posix.normalize(ref)));
+ const scanRefs = new Set(shared);
return {
...project(surface),
id,
@@ -169,7 +188,7 @@ export function scanCoverage(
const ref = posix.normalize(receipt);
return ref.startsWith(archivePrefix) ||
ref.startsWith(`${artifactPrefix}/`) ||
- scanRefs.has(ref)
+ scanRefs.has(receipt)
? ref
: `${artifactPrefix}/${ref}`;
},
@@ -231,8 +250,9 @@ export function addScanCoverage(
const rows = coverage[field];
if (!Array.isArray(rows)) continue;
const retained = previous[field];
- if (Array.isArray(retained)) retained.push(...rows);
- else Object.assign(previous, { [field]: rows });
+ if (Array.isArray(retained)) {
+ for (const row of rows) retained.push(row);
+ } else Object.assign(previous, { [field]: rows });
}
}
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.ts
index a55a42bdd..33dfbb35e 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_artifact_validation.ts
@@ -681,6 +681,10 @@ async function testCoveragePublicationParity(root: string) {
"artifacts/review.md",
"artifacts/./review.md",
"artifacts//review.md",
+ workerReceiptRef(output, "artifacts/review.md").replace(
+ "/workers/",
+ "/./workers//",
+ ),
]) {
const result = structuredClone(original);
result.reviewState.surfaces[0].receiptRefs = [ref];
@@ -688,7 +692,7 @@ async function testCoveragePublicationParity(root: string) {
{
id: "review",
reason: "Review another route.",
- paths: [".", "./src//api.ts"],
+ paths: [".", "./src//api.ts", "é:helper.ts"],
surfaceIds: ["api"],
receiptRefs: { external: "opaque metadata" },
},
@@ -712,6 +716,9 @@ async function testCoveragePublicationParity(root: string) {
{ ...worker, result: accepted },
worker.artifacts.scanDir,
);
+ assert.deepEqual(coverage.surfaces[0].receiptRefs, [
+ workerReceiptRef(output, "artifacts/review.md"),
+ ]);
assert.deepEqual(coverage.deferred[0].receiptRefs, {
external: "opaque metadata",
});
@@ -755,6 +762,41 @@ async function testCoveragePublicationParity(root: string) {
error: /well-formed Unicode/,
},
];
+ const closure = { id: "route", reason: "Reviewed route." };
+ cases.push(
+ {
+ patch: (result) => {
+ result.reviewState.resolvedDeferred = [closure, closure];
+ },
+ error: /repeats resolved deferred id/,
+ },
+ {
+ patch: (result) => {
+ result.reviewState.deferred = [
+ { id: "route", reason: "Still pending." },
+ ];
+ result.reviewState.resolvedDeferred = [closure];
+ },
+ error: /deferred work is still active/,
+ },
+ {
+ patch: (result) => {
+ result.reviewState.deferred = [
+ { id: "other", candidateId: "route", reason: "Needs evidence." },
+ ];
+ result.reviewState.resolvedDeferred = [closure];
+ },
+ error: /deferred work is still active/,
+ },
+ {
+ patch: (result) => {
+ result.reviewState.resolvedDeferred = [
+ { ...closure, reason: "Invalid \ud800 text." },
+ ];
+ },
+ error: /well-formed Unicode/,
+ },
+ );
for (const ref of ["artifacts/conİn$", "artifacts/conın$"]) {
cases.push({
patch: (result) => {
@@ -776,15 +818,20 @@ async function testCoveragePublicationParity(root: string) {
path.join(output, "artifacts/Review.md"),
"Other case-sensitive evidence.\n",
);
- cases.push({
- patch: (result) => {
- result.reviewState.surfaces[0].receiptRefs = [
- "artifacts/review.md",
- "artifacts/Review.md",
- ];
- },
- error: /duplicate artifact path/,
- });
+ for (const second of [
+ "artifacts/Review.md",
+ workerReceiptRef(output, "artifacts/Review.md"),
+ ]) {
+ cases.push({
+ patch: (result) => {
+ result.reviewState.surfaces[0].receiptRefs = [
+ "artifacts/review.md",
+ second,
+ ];
+ },
+ error: /duplicate artifact path/,
+ });
+ }
for (const { patch, error } of cases) {
const result = structuredClone(original);
patch(result);
diff --git a/plugins/codex-security/mcp-app/tests/test_deep_scan_scope.ts b/plugins/codex-security/mcp-app/tests/test_deep_scan_scope.ts
index 144fa66da..8310805bf 100644
--- a/plugins/codex-security/mcp-app/tests/test_deep_scan_scope.ts
+++ b/plugins/codex-security/mcp-app/tests/test_deep_scan_scope.ts
@@ -170,6 +170,27 @@ for (const legacy of [false, true]) {
});
}
+test("a supplied scope file must name its paths before the worker can accept drafts", async (t) => {
+ const { importSource } = await import("./import-module.ts");
+ const { createCodexSecurityArtifactWriterServer } = await importSource(
+ "../artifact-writer-main.ts",
+ { absWorkingDir: import.meta.dirname },
+ );
+ const f = await fixture(t, "worker");
+ const scopeFile = join(f.root, "../missing-paths.json");
+ await writeFile(scopeFile, JSON.stringify({ filePaths: ["api/source.py"] }));
+ await assert.rejects(
+ createCodexSecurityArtifactWriterServer({
+ CODEX_SECURITY_ARTIFACT_ROOT: f.root,
+ CODEX_SECURITY_REPO_ROOT: f.root,
+ CODEX_SECURITY_SCAN_ID: f.context.scanId,
+ CODEX_SECURITY_SCOPE: ".",
+ CODEX_SECURITY_INCLUDE_PATHS_FILE: scopeFile,
+ }),
+ /coordinator-bound include paths/,
+ );
+});
+
test("worker scope keeps a finding with any matching location and supports exact files", async (t) => {
const f = await fixture(t, "worker");
Object.assign(f.context, {