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(contract): align sealed scan reader compatibility (#1175)

Manas Choksib1fd40279c22
diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.py
index 9d6ec4f52..e1479238b 100644
--- a/plugins/codex-security/scripts/finalize_scan_contract.py
+++ b/plugins/codex-security/scripts/finalize_scan_contract.py
@@ -706,7 +706,7 @@ def _read_scan_local_json_with_metadata(
raw = handle.read()
metadata = os.fstat(handle.fileno())
try:
- payload = _loads_json(raw.decode("utf-8"))
+ payload = _loads_json(raw.decode("utf-8-sig"))
except (UnicodeDecodeError, ValueError) as exc:
raise ContractError(f"{context}: invalid JSON: {exc}") from exc
if not isinstance(payload, dict):
@@ -887,7 +887,7 @@ def _write_scan_local_json(scan_dir: Path, relative_path: str, payload: Any) ->
def _validate_remote(remote: str, context: str) -> None:
parsed = urlsplit(remote)
- if not parsed.scheme or not parsed.netloc:
+ if "\\" in remote or not parsed.scheme or not parsed.netloc:
raise ContractError(f"{context}: expected a sanitized canonical absolute URL")
if parsed.username or parsed.password or parsed.query or parsed.fragment:
raise ContractError(
diff --git a/sdk/typescript/src/contract.ts b/sdk/typescript/src/contract.ts
index 1d2780454..ccfbc264a 100644
--- a/sdk/typescript/src/contract.ts
+++ b/sdk/typescript/src/contract.ts
@@ -234,6 +234,7 @@ export function normalizePersistedFindings(payload: unknown): unknown {
}
for (const [sectionName, listFields] of [
+ ["rootCause", ["evidenceRefs", "evidence_refs"]],
["root_cause", ["evidenceRefs", "evidence_refs"]],
[
"validation",
@@ -432,8 +433,31 @@ function validateCanonicalContract(
}
}
+ const findingIds = new Set<string>();
for (const [findingIndex, finding] of findings.findings.entries()) {
const context = `findings.findings[${findingIndex}]`;
+ if (findingIds.has(finding.findingId)) {
+ throw new ContractValidationError(`${context}: duplicate finding id.`);
+ }
+ findingIds.add(finding.findingId);
+ for (const [field, value] of [
+ ["title", finding.title],
+ ["summary", finding.summary],
+ ["remediation", finding.remediation],
+ ["confidence.rationale", finding.confidence.rationale],
+ ["taxonomy.category", finding.taxonomy.category],
+ ["provenance.source", finding.provenance.source],
+ ...(finding.severity.score === undefined
+ ? []
+ : [["severity.scoringSystem", finding.severity.scoringSystem]]),
+ ]) {
+ // Match the producer's Python str.strip without changing saved text.
+ if (/^[\p{White_Space}\u001c-\u001f]*$/u.test(value ?? "")) {
+ throw new ContractValidationError(
+ `${context}.${field}: expected a non-empty string.`,
+ );
+ }
+ }
for (const [locationIndex, location] of finding.locations.entries()) {
const locationContext = `${context}.locations[${locationIndex}]`;
try {
@@ -444,6 +468,11 @@ function validateCanonicalContract(
{ cause: error },
);
}
+ if ((location.endLine ?? location.startLine) < location.startLine) {
+ throw new ContractValidationError(
+ `${locationContext}.endLine: expected an integer >= startLine.`,
+ );
+ }
}
const fingerprint = `codex-security/v1:sha256:${hash(
diff --git a/sdk/typescript/tests-ts/contract.test.ts b/sdk/typescript/tests-ts/contract.test.ts
index 49bda1ef4..56c7e17c7 100644
--- a/sdk/typescript/tests-ts/contract.test.ts
+++ b/sdk/typescript/tests-ts/contract.test.ts
@@ -53,6 +53,16 @@ async function reseal(scanDir: string): Promise<void> {
await writeJson(manifestPath, manifest);
}
+function pythonExport(scanDir: string) {
+ return runPython(process.env["PYTHON"] ?? Bun.which("python3") ?? "python", [
+ join(PLUGIN_ROOT, "scripts", "finalize_scan_contract.py"),
+ "--scan-dir",
+ scanDir,
+ "--export-format",
+ "json",
+ ]);
+}
+
function expectation(
target: NormalizedTarget,
mode: "standard" | "deep" = "standard",
@@ -438,6 +448,123 @@ describe("canonical scan contract", () => {
expect(await readJson(findingsPath)).toEqual(findings);
});
+ test.each(["rootCause", "root_cause"])(
+ "reads legacy %s evidence references in both readers without rewriting the seal",
+ async (section) => {
+ const scanDir = await copyExample();
+ const findingsPath = join(scanDir, "findings.json");
+ const findings = await readJson(findingsPath);
+ findings["findings"][0]["code_evidence"] = [
+ { id: "synthetic-evidence", code: "handler()" },
+ ];
+ findings["findings"][0][section] = {
+ summary: "Synthetic root cause.",
+ evidenceRefs: [null, "synthetic-evidence", 3],
+ };
+ await writeJson(findingsPath, findings);
+ await reseal(scanDir);
+ const bytes = await readFile(findingsPath);
+ const exported = pythonExport(scanDir);
+ expect(exported.exitCode, new TextDecoder().decode(exported.stderr)).toBe(
+ 0,
+ );
+ const loaded = await loadContract(scanDir, { pluginRoot: PLUGIN_ROOT });
+ expect(loaded.findings.findings[0]?.[section as "rootCause"]).toEqual({
+ summary: "Synthetic root cause.",
+ evidenceRefs: ["synthetic-evidence"],
+ });
+ expect(await readFile(findingsPath)).toEqual(bytes);
+ },
+ );
+
+ test("rejects duplicate sealed findings in both readers", async () => {
+ const scanDir = await copyExample();
+ const findingsPath = join(scanDir, "findings.json");
+ const findings = await readJson(findingsPath);
+ findings["findings"].push(structuredClone(findings["findings"][0]));
+ await writeJson(findingsPath, findings);
+ await reseal(scanDir);
+ expect(pythonExport(scanDir).exitCode).not.toBe(0);
+ await expect(
+ loadContract(scanDir, { pluginRoot: PLUGIN_ROOT }),
+ ).rejects.toThrow("duplicate finding");
+ });
+
+ test.each([
+ ["title", " \t\n", false],
+ ["summary", " \t\n", false],
+ ["remediation", " \t\n", false],
+ ["confidence.rationale", " \t\n", false],
+ ["taxonomy.category", " \t\n", false],
+ ["provenance.source", " \t\n", false],
+ ["severity.scoringSystem", " \t\n", false],
+ ["title", "\u0085\u001c", false],
+ ["title", "\ufeff", true],
+ ["title", " Synthetic required text. \n", true],
+ ] as const)(
+ "agrees with Python on required finding text %s %j",
+ async (field, text, valid) => {
+ const scanDir = await copyExample();
+ const findingsPath = join(scanDir, "findings.json");
+ const findings = await readJson(findingsPath);
+ const finding = findings["findings"][0];
+ finding["severity"]["score"] = 5;
+ finding["severity"]["scoringSystem"] = "synthetic";
+ const parts = field.split(".");
+ const object = parts.length === 1 ? finding : finding[parts[0]!];
+ object[parts.at(-1)!] = text;
+ await writeJson(findingsPath, findings);
+ await reseal(scanDir);
+ const exported = pythonExport(scanDir);
+ expect(exported.exitCode === 0).toBe(valid);
+ const loaded = loadContract(scanDir, { pluginRoot: PLUGIN_ROOT });
+ if (valid) {
+ await expect(loaded).resolves.toBeDefined();
+ expect(JSON.parse(new TextDecoder().decode(exported.stdout))).toEqual(
+ findings,
+ );
+ } else {
+ await expect(loaded).rejects.toThrow("non-empty string");
+ }
+ expect(await readJson(findingsPath)).toEqual(findings);
+ },
+ );
+
+ test.each(["scan-manifest.json", "findings.json", "coverage.json"])(
+ "reads a UTF-8 BOM in sealed %s without changing its bytes",
+ async (filename) => {
+ const scanDir = await copyExample();
+ const path = join(scanDir, filename);
+ const bytes = Buffer.concat([
+ Buffer.from([0xef, 0xbb, 0xbf]),
+ await readFile(path),
+ ]);
+ await writeFile(path, bytes);
+ if (filename !== "scan-manifest.json") await reseal(scanDir);
+ const exported = pythonExport(scanDir);
+ expect(exported.exitCode, new TextDecoder().decode(exported.stderr)).toBe(
+ 0,
+ );
+ await expect(
+ loadContract(scanDir, { pluginRoot: PLUGIN_ROOT }),
+ ).resolves.toBeDefined();
+ expect(await readFile(path)).toEqual(bytes);
+ },
+ );
+
+ test("rejects noncanonical remote backslashes in both readers", async () => {
+ const scanDir = await copyExample();
+ const manifestPath = join(scanDir, "scan-manifest.json");
+ const manifest = await readJson(manifestPath);
+ manifest["scan"]["target"]["remote"] =
+ "https://example.test/synthetic/repo\\name";
+ await writeJson(manifestPath, manifest);
+ expect(pythonExport(scanDir).exitCode).not.toBe(0);
+ await expect(
+ loadContract(scanDir, { pluginRoot: PLUGIN_ROOT }),
+ ).rejects.toThrow("canonical absolute URL");
+ });
+
test("loads an empty legacy root cause without changing the artifact", async () => {
const scanDir = await copyExample();
const findingsPath = join(scanDir, "findings.json");
@@ -731,6 +858,29 @@ describe("canonical scan contract", () => {
).resolves.toBeDefined();
});
+ test.each([-1, 0])(
+ "agrees with Python on a finding end-line offset of %i",
+ async (offset) => {
+ const scanDir = await copyExample();
+ const findingsPath = join(scanDir, "findings.json");
+ const findings = await readJson(findingsPath);
+ const location = findings["findings"][0]["locations"][0];
+ location["endLine"] = location["startLine"] + offset;
+ await writeJson(findingsPath, findings);
+ await reseal(scanDir);
+
+ expect(pythonExport(scanDir).exitCode === 0).toBe(offset === 0);
+ const loaded = loadContract(scanDir, { pluginRoot: PLUGIN_ROOT });
+ if (offset === 0) {
+ await expect(loaded).resolves.toBeDefined();
+ } else {
+ await expect(loaded).rejects.toThrow(
+ "findings.findings[0].locations[0].endLine: expected an integer >= startLine.",
+ );
+ }
+ },
+ );
+
test("rejects trailing-dot aliases for sealed artifacts", async () => {
const scanDir = await copyExample();
const manifestPath = join(scanDir, "scan-manifest.json");