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(plugin): Preserve recoverable root-cause evidence references (#604)

ianw-oai924d7bfe4437
diff --git a/sdk/typescript/_bundled_plugin/scripts/finalize_scan_contract.py b/sdk/typescript/_bundled_plugin/scripts/finalize_scan_contract.py
index dbb5eedc1..a9daf4d75 100644
--- a/sdk/typescript/_bundled_plugin/scripts/finalize_scan_contract.py
+++ b/sdk/typescript/_bundled_plugin/scripts/finalize_scan_contract.py
@@ -1619,6 +1619,7 @@ def _legacy_sealed_findings_for_validation(findings: dict[str, Any]) -> dict[str
and evidence["id"]
}
for section_name, list_fields in (
+ ("rootCause", ("evidenceRefs", "evidence_refs")),
("root_cause", ("evidenceRefs", "evidence_refs")),
(
"validation",
diff --git a/sdk/typescript/tests-ts/plugin-finding-detail-contract.test.ts b/sdk/typescript/tests-ts/plugin-finding-detail-contract.test.ts
index 063a82086..07e23f24b 100644
--- a/sdk/typescript/tests-ts/plugin-finding-detail-contract.test.ts
+++ b/sdk/typescript/tests-ts/plugin-finding-detail-contract.test.ts
@@ -916,12 +916,22 @@ describe("bundled plugin finding detail contracts", () => {
"findings = json.loads((plugin / 'examples' / 'completed-scan' / 'findings.json').read_text())",
"finding = findings['findings'][0]",
"finding['code_evidence'] = [{'id': 'legacy-source', 'code': 'legacy_source()'}]",
+ "finding['rootCause'] = {'summary': 'Canonical root cause.', 'evidenceRefs': ['legacy-source', 'missing-source']}",
"finding['validation'] = {'evidenceRefs': [None, '', 42, 'legacy-source', 'missing-source']}",
"finding['attackPath'] = {'dataflow': {'evidence_refs': [None, '', 42, 'legacy-source', 'missing-source']}}",
+ "original = json.dumps(findings, sort_keys=True)",
"finalizer = runpy.run_path(str(plugin / 'scripts' / 'finalize_scan_contract.py'))",
"compatible = finalizer['_legacy_sealed_findings_for_validation'](findings)",
"finalizer['_validate_finding'](compatible['findings'][0], 'findings[0]')",
- "print(json.dumps({'originalValidation': finding['validation']['evidenceRefs'], 'compatibleValidation': compatible['findings'][0]['validation']['evidenceRefs'], 'originalDataflow': finding['attackPath']['dataflow']['evidence_refs'], 'compatibleDataflow': compatible['findings'][0]['attackPath']['dataflow']['evidence_refs']}))",
+ "print(json.dumps({",
+ " 'originalUnchanged': json.dumps(findings, sort_keys=True) == original,",
+ " 'originalRootCause': finding['rootCause'],",
+ " 'compatibleRootCause': compatible['findings'][0]['rootCause'],",
+ " 'originalValidation': finding['validation']['evidenceRefs'],",
+ " 'compatibleValidation': compatible['findings'][0]['validation']['evidenceRefs'],",
+ " 'originalDataflow': finding['attackPath']['dataflow']['evidence_refs'],",
+ " 'compatibleDataflow': compatible['findings'][0]['attackPath']['dataflow']['evidence_refs'],",
+ "}))",
].join("\n");
const result = Bun.spawnSync(
[python!, "-I", "-B", "-c", script, PLUGIN_ROOT],
@@ -930,6 +940,15 @@ describe("bundled plugin finding detail contracts", () => {
expect(result.exitCode, new TextDecoder().decode(result.stderr)).toBe(0);
expect(JSON.parse(new TextDecoder().decode(result.stdout))).toEqual({
+ originalUnchanged: true,
+ originalRootCause: {
+ summary: "Canonical root cause.",
+ evidenceRefs: ["legacy-source", "missing-source"],
+ },
+ compatibleRootCause: {
+ summary: "Canonical root cause.",
+ evidenceRefs: ["legacy-source"],
+ },
originalValidation: [null, "", 42, "legacy-source", "missing-source"],
compatibleValidation: ["legacy-source"],
originalDataflow: [null, "", 42, "legacy-source", "missing-source"],
diff --git a/sdk/typescript/tests-ts/scan-recovery.test.ts b/sdk/typescript/tests-ts/scan-recovery.test.ts
index 59466b894..c731e9f94 100644
--- a/sdk/typescript/tests-ts/scan-recovery.test.ts
+++ b/sdk/typescript/tests-ts/scan-recovery.test.ts
@@ -684,6 +684,81 @@ describe("malformed scan artifact recovery", () => {
);
});
+ test.each(
+ (["rootCause", "root_cause"] as const).flatMap((section) =>
+ (["evidenceRefs", "evidence_refs"] as const).flatMap((refsKey) =>
+ [true, false].map((includeEvidence) => ({
+ section,
+ refsKey,
+ includeEvidence,
+ })),
+ ),
+ ),
+ )(
+ "recovers $section refs $refsKey with evidence catalog: $includeEvidence",
+ async ({ section, refsKey, includeEvidence }) => {
+ const fixture = await startDraftScan();
+ const path = join(fixture.scanDir, "findings.json");
+ const document = await readJson<FindingsDocument>(path);
+ const finding = document.findings[0]!;
+ if (includeEvidence) {
+ finding.codeEvidence = [
+ {
+ id: "canonical-source",
+ label: "Synthetic source",
+ path: "src/extract.py",
+ startLine: 41,
+ code: "canonical_source()",
+ explanation: "Synthetic evidence for reference recovery.",
+ },
+ ];
+ finding["code_evidence"] = [
+ { id: "legacy-source", code: "legacy_source()" },
+ ];
+ }
+ finding[section] = {
+ summary: "The destination is not constrained to the extraction root.",
+ [refsKey]: ["legacy-source", "src/extract.py:41", "canonical-source"],
+ };
+ await writeJson(path, document);
+ const original = await readFile(path, "utf8");
+
+ const strict = spawnSync(
+ fixture.python,
+ [
+ "-I",
+ "-B",
+ join(PLUGIN_ROOT, "scripts", "finalize_scan_contract.py"),
+ "--scan-dir",
+ fixture.scanDir,
+ ],
+ { encoding: "utf8" },
+ );
+ expect(strict.status).not.toBe(0);
+ expect(strict.stderr).toContain("unknown code-evidence ids");
+ expect(await readFile(path, "utf8")).toBe(original);
+
+ const completed = await completeScan(fixture);
+
+ expect(completed.progress.status).toBe("complete");
+ expect(completed.findingCount).toBe(1);
+ expect(completed.warnings).toEqual([
+ "Recovered finding 1: normalized legacy finding details.",
+ ]);
+ const recovered = (await readJson<FindingsDocument>(path)).findings[0]!;
+ expect(recovered[section]).toEqual({
+ summary: "The destination is not constrained to the extraction root.",
+ [refsKey]: includeEvidence ? ["legacy-source", "canonical-source"] : [],
+ });
+ expect(recovered.codeEvidence).toEqual(finding.codeEvidence);
+ expect(recovered["code_evidence"]).toEqual(finding["code_evidence"]);
+ const coverage = await readJson<CoverageDocument>(
+ join(fixture.scanDir, "coverage.json"),
+ );
+ expect(coverage.completeness).toBe("complete");
+ },
+ );
+
test("preserves recovery warnings across prepared scan completion", async () => {
const fixture = await startDraftScan();
const path = join(fixture.scanDir, "findings.json");
@@ -788,10 +863,27 @@ describe("malformed scan artifact recovery", () => {
unsafeLocation.locations[0]!.path = "../outside.py";
const missingIdentity = structuredClone(valid);
delete (missingIdentity as Partial<Finding>).identity;
+ const invalidEvidenceId = structuredClone(valid);
+ invalidEvidenceId.identity.anchor = "invalid-evidence-id";
+ invalidEvidenceId.codeEvidence = [
+ {
+ id: "src/extract.py:41",
+ label: "Synthetic source",
+ path: "src/extract.py",
+ startLine: 41,
+ code: "synthetic_source()",
+ explanation: "The evidence identifier is intentionally malformed.",
+ },
+ ];
+ invalidEvidenceId["rootCause"] = {
+ summary: "Synthetic root cause.",
+ evidenceRefs: ["src/extract.py:41"],
+ };
document.findings.push(
missingSummary,
unsafeLocation,
missingIdentity,
+ invalidEvidenceId,
structuredClone(valid),
null,
);
@@ -801,7 +893,7 @@ describe("malformed scan artifact recovery", () => {
expect(completed.progress.status).toBe("complete");
expect(completed.findingCount).toBe(1);
- expect(completed.warnings).toHaveLength(5);
+ expect(completed.warnings).toHaveLength(6);
expect(
completed.warnings.every((warning) =>
warning.startsWith("Skipped malformed finding"),
@@ -811,6 +903,7 @@ describe("malformed scan artifact recovery", () => {
"summary",
"safe repository-relative",
"identity",
+ "codeEvidence[0].id",
"duplicate logical finding",
"expected an object",
]) {
@@ -826,7 +919,7 @@ describe("malformed scan artifact recovery", () => {
expect((coverage.surfaces as CoverageSurface[])[0]?.disposition).toBe(
"needs_follow_up",
);
- expect(coverage.deferred).toHaveLength(4);
+ expect(coverage.deferred).toHaveLength(5);
});
test.each([