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(artifacts): preserve import and export representations (#1510)

mldangelo-oai2ded063ed4e3
diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.py
index 12218d7cb..1a94345fc 100644
--- a/plugins/codex-security/scripts/finalize_scan_contract.py
+++ b/plugins/codex-security/scripts/finalize_scan_contract.py
@@ -2565,9 +2565,23 @@ def _artifact_record(
}
-def _coverage_receipt_refs(coverage: dict[str, Any]) -> list[str]:
- refs = {ref for surface in coverage["surfaces"] for ref in surface.get("receiptRefs", [])}
- return sorted(refs)
+def _supplementary_artifact_refs(scan: dict[str, Any], coverage: dict[str, Any]) -> dict[str, str]:
+ refs = {
+ ref: "coverage receipt"
+ for surface in coverage["surfaces"]
+ for ref in surface.get("receiptRefs", [])
+ }
+ extensions = scan.get("extensions")
+ imported = extensions.get("import") if isinstance(extensions, dict) else None
+ if isinstance(imported, dict) and "sourceRef" in imported:
+ context = "manifest.scan.extensions.import"
+ ref = _require_portable_relative_path(
+ _require_str(imported, "sourceRef", context), f"{context}.sourceRef"
+ )
+ if not ref.startswith("artifacts/"):
+ raise ContractError(f"{context}.sourceRef: expected a file under artifacts/")
+ refs.setdefault(ref, "import source")
+ return {ref: refs[ref] for ref in sorted(refs)}
def _validate_sealed_coverage_receipts(scan: dict[str, Any], coverage: dict[str, Any]) -> None:
@@ -2575,9 +2589,9 @@ def _validate_sealed_coverage_receipts(scan: dict[str, Any], coverage: dict[str,
_require_portable_relative_path(artifact["path"], "sealed artifact path")
for artifact in scan["artifacts"]
}
- for ref in _coverage_receipt_refs(coverage):
+ for ref, context in _supplementary_artifact_refs(scan, coverage).items():
if ref not in artifact_paths:
- raise ContractError(f"coverage receipt is missing from sealed artifacts: {ref}")
+ raise ContractError(f"{context} is missing from sealed artifacts: {ref}")
def _validate_existing_seal(
@@ -3001,7 +3015,7 @@ def _prepare_scan_finalization(
_artifact_record(scan_dir, "coverage.json", "application/json", coverage_bytes),
*[
_artifact_record(scan_dir, ref, "application/octet-stream")
- for ref in _coverage_receipt_refs(coverage)
+ for ref in _supplementary_artifact_refs(scan, coverage)
],
]
_validate_manifest(manifest)
diff --git a/plugins/codex-security/tests/test_finalize_scan_contract.py b/plugins/codex-security/tests/test_finalize_scan_contract.py
index 66366bd04..2b99427b2 100644
--- a/plugins/codex-security/tests/test_finalize_scan_contract.py
+++ b/plugins/codex-security/tests/test_finalize_scan_contract.py
@@ -1941,6 +1941,97 @@ The extraction root is not enforced.
with self.assertRaisesRegex(FINALIZER.ContractError, "sealed artifact changed"):
FINALIZER.finalize_scan(self.scan_dir)
+ def test_seals_import_source_without_claiming_coverage(self) -> None:
+ for with_surface in (False, True):
+ for format in ("csv", "json"):
+ with self.subTest(with_surface=with_surface, format=format):
+ source_ref = f"artifacts/import/source.{format}"
+ source = b"synthetic imported source\n"
+ self.manifest["scan"]["extensions"] = {
+ "import": {"format": format, "sourceRef": source_ref}
+ }
+ self.findings["findings"] = []
+ self.coverage["completeness"] = "unknown"
+ self.coverage["surfaces"] = (
+ [
+ {
+ "id": "import",
+ "label": "import",
+ "disposition": "reported",
+ "receiptRefs": [source_ref],
+ }
+ ]
+ if with_surface
+ else []
+ )
+ self.write_scan()
+ source_path = self.scan_dir / source_ref
+ source_path.parent.mkdir(parents=True, exist_ok=True)
+ source_path.write_bytes(source)
+ FINALIZER.finalize_scan(self.scan_dir)
+ manifest = self.read_json("scan-manifest.json")
+ records = [
+ record
+ for record in manifest["scan"]["artifacts"]
+ if record["path"] == source_ref
+ ]
+ self.assertEqual(
+ records,
+ [
+ {
+ "path": source_ref,
+ "sha256": hashlib.sha256(source).hexdigest(),
+ "mediaType": "application/octet-stream",
+ }
+ ],
+ )
+ self.assertEqual(
+ self.read_json("coverage.json")["surfaces"], self.coverage["surfaces"]
+ )
+ sealed = (self.scan_dir / "scan-manifest.json").read_bytes()
+ FINALIZER.finalize_scan(self.scan_dir)
+ self.assertEqual((self.scan_dir / "scan-manifest.json").read_bytes(), sealed)
+ source_path.write_bytes(b"changed\n")
+ with self.assertRaisesRegex(FINALIZER.ContractError, "sealed artifact changed"):
+ FINALIZER.finalize_scan(self.scan_dir)
+ source_path.unlink()
+ with self.assertRaises(FINALIZER.ContractError):
+ FINALIZER.build_findings_export(self.scan_dir, "json")
+ source_path.write_bytes(source)
+ manifest["scan"]["artifacts"].remove(records[0])
+ self.write_json("scan-manifest.json", manifest)
+ with self.assertRaisesRegex(
+ FINALIZER.ContractError, "missing from sealed artifacts"
+ ):
+ FINALIZER.finalize_scan(self.scan_dir)
+
+ def test_rejects_missing_or_unsafe_import_source(self) -> None:
+ self.findings["findings"] = []
+ self.coverage["completeness"] = "unknown"
+ self.coverage["surfaces"] = []
+ for source_ref in ("artifacts/import/missing.json", "../source.json", "findings.json"):
+ with self.subTest(source_ref=source_ref):
+ self.manifest["scan"]["extensions"] = {"import": {"sourceRef": source_ref}}
+ self.write_scan()
+ with self.assertRaises(FINALIZER.ContractError):
+ FINALIZER.finalize_scan(self.scan_dir)
+ self.assertNotIn("artifacts", self.read_json("scan-manifest.json")["scan"])
+
+ def test_rejects_symlink_import_source(self) -> None:
+ source_ref = "artifacts/import/source.json"
+ self.manifest["scan"]["extensions"] = {"import": {"sourceRef": source_ref}}
+ self.findings["findings"] = []
+ self.coverage["completeness"] = "unknown"
+ self.coverage["surfaces"] = []
+ self.write_scan()
+ source_path = self.scan_dir / source_ref
+ source_path.parent.mkdir(parents=True)
+ target = source_path.with_name("retained.json")
+ target.write_bytes(b"{}\n")
+ source_path.symlink_to(target)
+ with self.assertRaisesRegex(FINALIZER.ContractError, "non-symlink"):
+ FINALIZER.finalize_scan(self.scan_dir)
+
def test_verifies_legacy_aliased_receipt_ref(self) -> None:
receipt_ref = "artifacts/02_discovery/work_ledger.jsonl"
legacy_ref = "artifacts/02_discovery/./work_ledger.jsonl"
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index e8ad0fc10..132ba6c57 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -1737,6 +1737,7 @@ export async function main(
let exitCode = 0;
let frameworkExit: number | undefined;
const frameworkCapture = captureOutput();
+ let rawExportOutput = false;
let streamedLogs: Awaited<ReturnType<typeof readSavedScanLogs>> | undefined;
let renderedHistory: string | undefined;
let renderedPublication: string | undefined;
@@ -4733,6 +4734,7 @@ export async function main(
},
),
async run({ args, options }) {
+ rawExportOutput = options.output === "-";
try {
const currentDirectory = dependencies.currentDirectory();
if (args.scanDir !== undefined && options.scan !== undefined) {
@@ -6116,6 +6118,7 @@ export async function main(
return 2;
}
}
+ if (rawExportOutput) return exitCode;
if (
frameworkOutput.length === 0 &&
streamedLogs === undefined &&
diff --git a/sdk/typescript/src/cloud-publish.ts b/sdk/typescript/src/cloud-publish.ts
index e07c02ec3..49eb09c27 100644
--- a/sdk/typescript/src/cloud-publish.ts
+++ b/sdk/typescript/src/cloud-publish.ts
@@ -4,7 +4,11 @@ import { join } from "node:path";
import { z } from "incur";
import { parse as parseToml } from "smol-toml";
import { loadContract, sha256Text as sha256 } from "./contract.js";
-import { AuthenticationRequiredError, CodexSecurityError } from "./errors.js";
+import {
+ AuthenticationRequiredError,
+ CodexSecurityError,
+ errorMessage,
+} from "./errors.js";
import type { Finding, ScanManifest } from "./models.js";
import {
CSV_TARGET_ID,
@@ -82,14 +86,22 @@ export async function publishFindingsCsvToCloud(
dependencies.signal?.throwIfAborted();
let source: string;
try {
- source = await readFile(csvPath, "utf8");
+ source = new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
+ await readFile(csvPath),
+ );
} catch (error) {
- throw new CodexSecurityError("Could not read findings CSV.", {
- cause: error,
- });
+ throw new CodexSecurityError(
+ `Could not read findings CSV. ${errorMessage(error)}`,
+ { cause: error },
+ );
}
dependencies.signal?.throwIfAborted();
const rows = parseFindingsCsv(source);
+ if (rows.length === 0) {
+ throw new CodexSecurityError(
+ "Findings CSV must contain at least one finding.",
+ );
+ }
const digest = sha256(source);
const scanId = `scan_csv_${sha256(
["codex-security-csv-import/v1", VERSION, source].join("\0"),
diff --git a/sdk/typescript/src/contract.ts b/sdk/typescript/src/contract.ts
index aeca99e3a..3c40f830d 100644
--- a/sdk/typescript/src/contract.ts
+++ b/sdk/typescript/src/contract.ts
@@ -603,22 +603,33 @@ async function validateSeal(
}
}
+ const requireSealedReference = (reference: unknown, context: string) => {
+ throwIfAborted(signal);
+ if (typeof reference !== "string") {
+ throw new ContractValidationError(`${context}: expected a string.`);
+ }
+ const normalized = portableRelativePath(reference, context.toLowerCase());
+ if (!normalized.startsWith("artifacts/")) {
+ throw new ContractValidationError(
+ `${context} must be under artifacts/: ${reference}`,
+ );
+ }
+ if (!artifactPaths.has(normalized)) {
+ throw new ContractValidationError(
+ `${context} is missing from sealed artifacts: ${reference}`,
+ );
+ }
+ };
for (const surface of coverage.surfaces) {
for (const receipt of surface.receiptRefs) {
- throwIfAborted(signal);
- const normalized = portableRelativePath(receipt, "coverage receipt");
- if (!normalized.startsWith("artifacts/")) {
- throw new ContractValidationError(
- `Coverage receipt must be under artifacts/: ${receipt}`,
- );
- }
- if (!artifactPaths.has(normalized)) {
- throw new ContractValidationError(
- `Coverage receipt is missing from sealed artifacts: ${receipt}`,
- );
- }
+ requireSealedReference(receipt, "Coverage receipt");
}
}
+ const extensions = scan["extensions"];
+ const imported = isRecord(extensions) ? extensions["import"] : undefined;
+ if (isRecord(imported) && "sourceRef" in imported) {
+ requireSealedReference(imported["sourceRef"], "Import source");
+ }
for (const [index, finding] of findings.findings.entries()) {
const writeup = finding.writeup;
diff --git a/sdk/typescript/src/custom-validation.ts b/sdk/typescript/src/custom-validation.ts
index 33d073713..41087b2db 100644
--- a/sdk/typescript/src/custom-validation.ts
+++ b/sdk/typescript/src/custom-validation.ts
@@ -397,6 +397,12 @@ export async function runCustomValidation(options: {
finding.validation = {
...validation,
summary: validation.evidence.join("\n"),
+ counterEvidence: validation.counterevidence_or_proof_gap
+ ? [validation.counterevidence_or_proof_gap]
+ : [],
+ limitations: validation.remaining_uncertainty
+ ? [validation.remaining_uncertainty]
+ : [],
};
finding.confidence = {
level: validation.confidence,
diff --git a/sdk/typescript/src/findings-import.ts b/sdk/typescript/src/findings-import.ts
index 078b43242..7e369dc47 100644
--- a/sdk/typescript/src/findings-import.ts
+++ b/sdk/typescript/src/findings-import.ts
@@ -247,11 +247,6 @@ export function parseFindingsCsv(source: string): CsvFindingRow[] {
`Findings CSV must use the Codex Security export columns: ${REQUIRED_CSV_COLUMNS.join(", ")} (candidate_id is optional).`,
);
}
- if (rows.length === 0) {
- throw new CodexSecurityError(
- "Findings CSV must contain at least one finding.",
- );
- }
const occurrenceIds = new Set<string>();
return rows.map((record, index) => {
diff --git a/sdk/typescript/src/import-scan.ts b/sdk/typescript/src/import-scan.ts
index 0fda27655..611fca277 100644
--- a/sdk/typescript/src/import-scan.ts
+++ b/sdk/typescript/src/import-scan.ts
@@ -136,7 +136,7 @@ export async function importScan(
signal,
);
const findings = await parseImportedFindings(
- source.toString("utf8"),
+ new TextDecoder("utf-8", { fatal: true }).decode(source),
options.format,
pluginRoot,
);
@@ -270,15 +270,17 @@ export async function importScan(
"coverage.json": {
completeness: "unknown",
inventoryStrategy: "custom",
- surfaces: [
- {
- id: "import",
- label: basename(inputPath),
- disposition: "reported",
- receiptRefs: [sourceRef],
- notes: IMPORT_DESCRIPTION,
- },
- ],
+ surfaces: findings.length
+ ? [
+ {
+ id: "import",
+ label: basename(inputPath),
+ disposition: "reported",
+ receiptRefs: [sourceRef],
+ notes: IMPORT_DESCRIPTION,
+ },
+ ]
+ : [],
explicitExclusions: [],
deferred: [],
},
diff --git a/sdk/typescript/src/knowledge-base.ts b/sdk/typescript/src/knowledge-base.ts
index 307b7cac6..4d924b3a5 100644
--- a/sdk/typescript/src/knowledge-base.ts
+++ b/sdk/typescript/src/knowledge-base.ts
@@ -312,7 +312,7 @@ function extractDocx(path: string, bytes: Uint8Array): string {
xml
.replace(/<\/(?:\w+:)?p\s*>/gu, "\n")
.replace(/<(?:\w+:)?(?:br|cr)\b[^>]*>/gu, "\n")
- .replace(/<(?:\w+:)?tab\b[^>]*\/>/gu, "\t")
+ .replace(/<(?:\w+:)?tab\b[^>]*>/gu, "\t")
.replace(/<[^>]+>/gu, ""),
);
} catch (error) {
diff --git a/sdk/typescript/tests-ts/cli-cloud-publish.test.ts b/sdk/typescript/tests-ts/cli-cloud-publish.test.ts
index ca5be5362..c1ae2c887 100644
--- a/sdk/typescript/tests-ts/cli-cloud-publish.test.ts
+++ b/sdk/typescript/tests-ts/cli-cloud-publish.test.ts
@@ -4,6 +4,7 @@ import {
cp,
mkdir,
mkdtemp,
+ readFile,
realpath,
rm,
symlink,
@@ -82,6 +83,39 @@ describe("publish scan to Cloud", () => {
expect(stdout.text()).toContain("Findings CSV");
});
+ test("preserves native CSV read and decode failures in CLI output", async () => {
+ const root = await temporaryDirectory();
+ const invalid = join(root, "invalid.csv");
+ await writeFile(invalid, Buffer.from([0xff]));
+ const deps = dependencies({
+ onConfig: throwing("unexpected model setup"),
+ onWorkbench: throwing("unexpected scan lookup"),
+ });
+ const fetchMock = mock(rejecting("unexpected request"));
+ deps.cloudFetch = fetchMock;
+ for (const path of [invalid, join(root, "missing.csv")]) {
+ let nativeError: Error | undefined;
+ try {
+ new TextDecoder("utf-8", { fatal: true }).decode(await readFile(path));
+ } catch (error) {
+ nativeError = error as Error;
+ }
+ expect(nativeError).toBeInstanceOf(Error);
+ const { stdout, stderr, runCli } = createCliTest(main);
+ expect(
+ await runCli(
+ ["publish", "scan", "--to", "cloud", "--csv", path, "--dry-run"],
+ deps,
+ ),
+ ).toBe(2);
+ expect(stdout.text()).toBe("");
+ expect(stderr.text()).toContain(
+ `Could not read findings CSV. ${nativeError!.message}`,
+ );
+ }
+ expect(fetchMock).not.toHaveBeenCalled();
+ });
+
test("passes --dry-run through when publishing a findings CSV", async () => {
const deps = dependencies({
currentDirectory: "/workspace/repository",
diff --git a/sdk/typescript/tests-ts/cli-export.test.ts b/sdk/typescript/tests-ts/cli-export.test.ts
index 1c9d815e0..76835950e 100644
--- a/sdk/typescript/tests-ts/cli-export.test.ts
+++ b/sdk/typescript/tests-ts/cli-export.test.ts
@@ -74,23 +74,97 @@ describe("CLI", () => {
});
test("exports findings to stdout without initializing Codex", async () => {
- for (const [format, expected] of [
- ["csv", "occurrence_id,finding_id\n"],
- ["json", '{"documentType":"codex-security.findings"}\n'],
- ["sarif", '{"version":"2.1.0"}\n'],
- ] as const) {
- const { stdout, stderr, runCli } = createCliTest(main);
+ const dataHome = await temporaryDirectory("export-skills-");
+ const previousDataHome = process.env["XDG_DATA_HOME"];
+ process.env["XDG_DATA_HOME"] = dataHome;
+ try {
+ const skillPath = join(dataHome, "skills", "codex-security-export");
+ await mkdir(join(dataHome, "incur"), { recursive: true });
+ await mkdir(skillPath, { recursive: true });
+ await writeFile(
+ join(skillPath, "SKILL.md"),
+ "Previously installed skill.",
+ );
+ await writeFile(
+ join(dataHome, "incur", "codex-security.json"),
+ JSON.stringify({
+ hash: "previous-command-hash",
+ skills: ["codex-security-export"],
+ paths: [skillPath],
+ }),
+ );
+ for (const [format, expected] of [
+ ["csv", "occurrence_id,finding_id\n"],
+ ["json", '{"documentType":"codex-security.findings"}\n'],
+ ["sarif", '{"version":"2.1.0"}\n'],
+ ["md", "# Synthetic threat model\n"],
+ ] as const) {
+ for (const metadata of [[], ["--full-output"], ["--token-count"]]) {
+ const { stdout, stderr, runCli } = createCliTest(main);
- const deps = dependencies();
- deps.createSecurity = mustNotInitializeCodex;
- expect(
- await runCli(
- ["export", "scan", "--export-format", format, "--output", "-"],
- deps,
- ),
- ).toBe(0);
- expect(stdout.text()).toBe(expected);
- expect(stderr.text()).toBe("");
+ const deps = dependencies();
+ deps.createSecurity = mustNotInitializeCodex;
+ deps.exportFindings = async () => Buffer.from(expected);
+ expect(
+ await runCli(
+ [
+ "export",
+ "scan",
+ "--export-format",
+ format,
+ "--output",
+ "-",
+ ...(format === "md" ? ["--artifact", "threat-model"] : []),
+ ...metadata,
+ ],
+ deps,
+ ),
+ ).toBe(0);
+ expect(stdout.text()).toBe(expected);
+ expect(stderr.text()).toBe("");
+ }
+ }
+ const failure = dependencies();
+ failure.exportFindings = async () => {
+ throw new Error("EACCES: synthetic export denied");
+ };
+ failure.runWorkbench = async () => {
+ throw new Error("Synthetic setup read failure");
+ };
+ for (const [args, diagnostic] of [
+ [
+ ["export", "scan", "--output", "-"],
+ "EACCES: synthetic export denied",
+ ],
+ [
+ ["export", "scan", "--export-format", "md", "--output", "-"],
+ "Findings exports support",
+ ],
+ [["export", "--output", "-"], "Synthetic setup read failure"],
+ [
+ ["export", "scan", "--export-format", "invalid", "--output", "-"],
+ "Invalid option:",
+ ],
+ ] as const) {
+ const failed = createCliTest(main);
+ expect(await failed.runCli([...args], failure)).toBe(2);
+ expect(failed.stdout.text()).toBe("");
+ expect(failed.stderr.text()).toContain(diagnostic);
+ }
+ for (const metadata of ["--help", "--schema"]) {
+ const result = createCliTest(main);
+ expect(
+ await result.runCli(
+ ["export", "scan", "--output", "-", metadata],
+ failure,
+ ),
+ ).toBe(0);
+ expect(result.stdout.text()).not.toBe("");
+ }
+ } finally {
+ if (previousDataHome === undefined) delete process.env["XDG_DATA_HOME"];
+ else process.env["XDG_DATA_HOME"] = previousDataHome;
+ await rm(dataHome, { recursive: true, force: true });
}
});
diff --git a/sdk/typescript/tests-ts/cloud-publish.test.ts b/sdk/typescript/tests-ts/cloud-publish.test.ts
index ba4d2c1a7..9a12be5b2 100644
--- a/sdk/typescript/tests-ts/cloud-publish.test.ts
+++ b/sdk/typescript/tests-ts/cloud-publish.test.ts
@@ -1,11 +1,13 @@
import { setFindingIdentity, sha256 } from "./support/finding-identity.js";
+import { VERSION } from "../src/version.js";
import type { FindingsDocument, ScanManifest } from "../src/models.js";
import { responding } from "./support/responses.js";
import { once } from "node:events";
import { rejecting } from "./support/errors.js";
import { chmod, mkdir, readFile, rm, writeFile } from "node:fs/promises";
+import * as fs from "node:fs/promises";
import { join } from "node:path";
-import { afterEach, describe, expect, test, mock } from "bun:test";
+import { afterEach, describe, expect, test, mock, spyOn } from "bun:test";
import {
publishFindingsCsvToCloud,
publishScanToCloud,
@@ -122,6 +124,79 @@ async function addSecondFinding(scan: string): Promise<void> {
}
describe("Cloud publication", () => {
+ test("rejects invalid CSV UTF-8 before credentials or upload", async () => {
+ const path = await csvFixture();
+ const { environment } = await fixture();
+ const [prefix, suffix] = `${csvHeader}\n${csvRow}\n`.split(
+ "including nested entries",
+ );
+ const fetchMock = mock(rejecting("unexpected request"));
+ const reading = spyOn(fs, "readFile");
+ try {
+ for (const bytes of [Buffer.from([0xff]), Buffer.from([0xe2, 0x82])]) {
+ let decodeError: Error | undefined;
+ try {
+ new TextDecoder("utf-8", { fatal: true }).decode(bytes);
+ } catch (error) {
+ decodeError = error as Error;
+ }
+ expect(decodeError).toBeInstanceOf(TypeError);
+ await writeFile(
+ path,
+ Buffer.concat([Buffer.from(prefix!), bytes, Buffer.from(suffix!)]),
+ );
+ await expect(
+ publishFindingsCsvToCloud(path, { environment, fetch: fetchMock }),
+ ).rejects.toMatchObject({
+ name: "CodexSecurityError",
+ message: `Could not read findings CSV. ${decodeError!.message}`,
+ cause: expect.objectContaining({ message: decodeError!.message }),
+ });
+ }
+ expect(reading.mock.calls.map(([file]) => file)).toEqual([path, path]);
+ expect(fetchMock).not.toHaveBeenCalled();
+ } finally {
+ reading.mockRestore();
+ }
+ });
+
+ test("preserves the filesystem cause when the selected CSV is missing", async () => {
+ const path = join(await temporaryDirectory(), "missing.csv");
+ const fetchMock = mock(rejecting("unexpected request"));
+ await expect(
+ publishFindingsCsvToCloud(path, { dryRun: true, fetch: fetchMock }),
+ ).rejects.toMatchObject({
+ name: "CodexSecurityError",
+ message: expect.stringContaining("ENOENT"),
+ cause: expect.objectContaining({ code: "ENOENT" }),
+ });
+ expect(fetchMock).not.toHaveBeenCalled();
+ });
+
+ test("preserves valid Unicode and BOM-bearing CSV source identities", async () => {
+ const path = await csvFixture();
+ const fetchMock = mock(rejecting("unexpected request"));
+ for (const marker of ["�", "😀"]) {
+ for (const bom of ["", "\uFEFF"]) {
+ const source = `${bom}${csvHeader}\n${csvRow.replace("including nested entries", marker)}\n`;
+ await writeFile(path, source);
+ const result = await publishFindingsCsvToCloud(path, {
+ dryRun: true,
+ fetch: fetchMock,
+ });
+ expect(result.findings?.[0]?.title).toBe(
+ `Unsafe archive extraction, ${marker}`,
+ );
+ expect(result.scanId).toBe(
+ `scan_csv_${sha256(
+ ["codex-security-csv-import/v1", VERSION, source].join("\0"),
+ ).slice(0, 24)}`,
+ );
+ }
+ }
+ expect(fetchMock).not.toHaveBeenCalled();
+ });
+
test("previews a validated findings export CSV without credentials or network access", async () => {
const path = await csvFixture();
const fetchMock = mock(rejecting("unexpected request"));
diff --git a/sdk/typescript/tests-ts/custom-validation.test.ts b/sdk/typescript/tests-ts/custom-validation.test.ts
index ad6c377e7..db3de27d5 100644
--- a/sdk/typescript/tests-ts/custom-validation.test.ts
+++ b/sdk/typescript/tests-ts/custom-validation.test.ts
@@ -200,6 +200,12 @@ describe("custom validation", () => {
"not_applicable",
"deferred",
);
+ const counterEvidence =
+ "The protected caller checks a separate precondition.";
+ const limitations = "The alternate configuration was not exercised.";
+ output.validations[0]!.validation.counterevidence_or_proof_gap =
+ counterEvidence;
+ output.validations[0]!.validation.remaining_uncertainty = limitations;
output.validations[0]!.severity = {
level: "medium",
rationale: "Requires an uncommon configuration.",
@@ -252,7 +258,11 @@ describe("custom validation", () => {
severity: output.validations[0]!.severity,
confidence: { level: "medium" },
attackPath: { impact: output.validations[0]!.impact },
- validation: { disposition: "reportable" },
+ validation: {
+ disposition: "reportable",
+ counterEvidence: [counterEvidence],
+ limitations: [limitations],
+ },
});
const coverage = await json<CoverageDocument>(
join(f.scanDir, "coverage.json"),
@@ -346,6 +356,10 @@ describe("custom validation", () => {
);
expect(saved.findings).toHaveLength(3);
for (const [index, finding] of saved.findings.entries()) {
+ expect(finding.validation).toMatchObject({
+ counterEvidence: [],
+ limitations: [],
+ });
expect(finding.identity).toEqual(f.findings.findings[index]!.identity);
expect(finding.locations).toEqual(f.findings.findings[index]!.locations);
expect(finding.attackPath).toEqual({
@@ -621,6 +635,10 @@ describe("custom validation", () => {
const output = result(
scenario === "dismissed" ? "suppressed" : "reportable",
);
+ output.validations[0]!.validation.counterevidence_or_proof_gap =
+ "Synthetic counterevidence from validation.";
+ output.validations[0]!.validation.remaining_uncertainty =
+ "Synthetic limitation from validation.";
if (scenario === "incomplete") {
output.status = "incomplete";
output.reason =
@@ -735,6 +753,11 @@ describe("custom validation", () => {
}
const completed = await pending;
if (scenario === "standard") {
+ const report = await readFile(join(scanDir, "report.md"), "utf8");
+ expect(report).toContain(
+ "Synthetic counterevidence from validation.",
+ );
+ expect(report).toContain("Synthetic limitation from validation.");
expect(
activities.filter(
({ description }) => description === validationActivity,
diff --git a/sdk/typescript/tests-ts/findings-import.test.ts b/sdk/typescript/tests-ts/findings-import.test.ts
index 37f5f5f87..541dc6fcb 100644
--- a/sdk/typescript/tests-ts/findings-import.test.ts
+++ b/sdk/typescript/tests-ts/findings-import.test.ts
@@ -44,6 +44,18 @@ function exportCsv(findings: Finding[]): string {
}
describe("findings import formats", () => {
+ test("round-trips an empty exported CSV", async () => {
+ expect(
+ await parseImportedFindings(exportCsv([]), "csv", PLUGIN_ROOT),
+ ).toEqual([]);
+ await expect(
+ parseImportedFindings("", "csv", PLUGIN_ROOT),
+ ).rejects.toThrow();
+ await expect(
+ parseImportedFindings("title,summary\n", "csv", PLUGIN_ROOT),
+ ).rejects.toThrow();
+ });
+
test("retains distinct occurrences of a shared finding in CSV and JSON", async () => {
const {
findings: [finding],
diff --git a/sdk/typescript/tests-ts/import-scan.test.ts b/sdk/typescript/tests-ts/import-scan.test.ts
index 93f5ea901..23d3375f6 100644
--- a/sdk/typescript/tests-ts/import-scan.test.ts
+++ b/sdk/typescript/tests-ts/import-scan.test.ts
@@ -32,7 +32,7 @@ const { temporaryDirectories: roots, cleanup } = createApiTestFixtures();
afterEach(cleanup);
const description =
- 'An imported report with a comma, a "quoted value", and Unicode: café.\n\n' +
+ 'An imported report with a comma, a "quoted value", and Unicode: café �.\n\n' +
"The full second paragraph must survive importing and indexing.\n" +
"A final line describes the reported impact.";
const sourceOccurrenceIds = [
@@ -268,6 +268,9 @@ test.each(["csv", "json"] as const)(
expect(result.manifest.scan.target.kind).toBe("directory_snapshot");
expect(result.manifest.scan.scope.runtimeStatus).toBe("imported");
expect(result.coverage.completeness).toBe("unknown");
+ expect(result.coverage.surfaces).toMatchObject([
+ { disposition: "reported" },
+ ]);
expect(result.turnResult["imported"]).toBe(true);
expect(result.threadId).toBe("");
expect(await readFile(result.reportPath, "utf8")).toContain(
@@ -285,10 +288,10 @@ test.each(["csv", "json"] as const)(
import: { format, sourceRef, findingCount: 2 },
});
expect(
- result.manifest.scan.artifacts.some(
+ result.manifest.scan.artifacts.filter(
(artifact) => artifact.path === sourceRef,
),
- ).toBe(true);
+ ).toHaveLength(1);
expect(await readFile(join(result.scanDir, sourceRef), "utf8")).toBe(
context.source,
);
@@ -318,6 +321,93 @@ test.each(["csv", "json"] as const)(
},
);
+test.each(["csv", "json"] as const)(
+ "empty %s imports preserve source integrity without claiming an analyzed surface",
+ async (format) => {
+ const context = await fixture(format);
+ const source =
+ format === "csv"
+ ? `${csvColumns.join(",")}\n`
+ : JSON.stringify({ findings: [] });
+ await writeFile(context.options.sourcePath, source);
+ const result = completed(
+ await importScan(context.options, context.dependencies),
+ );
+ expect(result.manifest.scan.status).toBe("completed");
+ expect(result.findings.findings).toEqual([]);
+ expect(result.coverage).toMatchObject({
+ completeness: "unknown",
+ surfaces: [],
+ });
+ const sourceRef = `artifacts/import/source.${format}`;
+ expect(result.manifest.scan["extensions"]).toMatchObject({
+ import: { format, sourceRef, findingCount: 0 },
+ });
+ expect(
+ result.manifest.scan.artifacts.filter(
+ (artifact) => artifact.path === sourceRef,
+ ),
+ ).toHaveLength(1);
+ const retainedSource = join(result.scanDir, sourceRef);
+ expect(await readFile(retainedSource, "utf8")).toBe(source);
+ expect((await storedScans(context))[0]).toMatchObject({
+ status: "complete",
+ occurrence_count: 0,
+ });
+ const manifestPath = join(result.scanDir, "scan-manifest.json");
+ const manifestText = await readFile(manifestPath, "utf8");
+ await writeFile(
+ manifestPath,
+ JSON.stringify({
+ ...result.manifest,
+ scan: {
+ ...result.manifest.scan,
+ artifacts: result.manifest.scan.artifacts.filter(
+ (artifact) => artifact.path !== sourceRef,
+ ),
+ },
+ }),
+ );
+ await expect(
+ loadContract(result.scanDir, { pluginRoot: PLUGIN_ROOT }),
+ ).rejects.toThrow("Import source is missing from sealed artifacts");
+ await writeFile(manifestPath, manifestText);
+ await writeFile(retainedSource, `${source}\n`);
+ await expect(
+ loadContract(result.scanDir, { pluginRoot: PLUGIN_ROOT }),
+ ).rejects.toThrow("sealed artifact changed");
+ await writeFile(retainedSource, source);
+ await loadContract(result.scanDir, { pluginRoot: PLUGIN_ROOT });
+ await rm(retainedSource);
+ await expect(
+ loadContract(result.scanDir, { pluginRoot: PLUGIN_ROOT }),
+ ).rejects.toThrow("expected a file inside the scan directory");
+ },
+);
+
+test.each(["csv", "json"] as const)(
+ "%s import rejects invalid UTF-8 before persistence and preserves valid text",
+ async (format) => {
+ const context = await fixture(format);
+ const source = Buffer.from(`\uFEFF${context.source}`);
+ await writeFile(context.options.sourcePath, source);
+ const workbench = mock(runWorkbench);
+ const dependencies = { ...context.dependencies, runWorkbench: workbench };
+ expect(
+ await importScan({ ...context.options, dryRun: true }, dependencies),
+ ).toMatchObject({ findingCount: 2 });
+ source[source.indexOf(Buffer.from("café"))] = 0xff;
+ await writeFile(context.options.sourcePath, source);
+ for (const dryRun of [false, true]) {
+ await expect(
+ importScan({ ...context.options, dryRun }, dependencies),
+ ).rejects.toThrow(TypeError);
+ }
+ expect(workbench).not.toHaveBeenCalled();
+ expect(await readFile(context.options.sourcePath)).toEqual(source);
+ },
+);
+
test.each(["csv", "json"] as const)(
"%s import rejects a symlinked source before persistence",
async (format) => {
diff --git a/sdk/typescript/tests-ts/knowledge-base.test.ts b/sdk/typescript/tests-ts/knowledge-base.test.ts
index 9786f6419..dccf92152 100644
--- a/sdk/typescript/tests-ts/knowledge-base.test.ts
+++ b/sdk/typescript/tests-ts/knowledge-base.test.ts
@@ -41,7 +41,7 @@ function docx(
): Uint8Array {
return zipSync({
"word/document.xml": strToU8(
- `<?xml version="1.0"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r><w:t>${text}</w:t></w:r>${secondLine === undefined ? "" : `${breakElement}<w:r><w:t>${secondLine}</w:t></w:r>`}</w:p></w:body></w:document>`,
+ `<?xml version="1.0"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><w:p><w:r><w:t>${text}</w:t>${secondLine === undefined ? "" : `${breakElement}<w:t>${secondLine}</w:t>`}</w:r></w:p></w:body></w:document>`,
),
});
}
@@ -482,6 +482,14 @@ describe("scan knowledge bases", () => {
join(root, "carriage-return.docx"),
docx("Authentication", "Review sessions", "<w:cr/>"),
);
+ await writeFile(
+ join(root, "paired-tab.docx"),
+ docx("Role", "Capability", "<w:tab></w:tab>"),
+ );
+ await writeFile(
+ join(root, "self-closing-tab.docx"),
+ docx("User", "Permission", "<w:tab/>"),
+ );
const knowledgeBase = await prepareKnowledgeBase([root]);
temporaryDirectories.track(knowledgeBase.path);
@@ -491,6 +499,8 @@ describe("scan knowledge bases", () => {
expect(documents).toContain("SSRF & IDOR\nReview authentication\n");
expect(documents).toContain("Authorization\nReview permissions\n");
expect(documents).toContain("Authentication\nReview sessions\n");
+ expect(documents).toContain("Role\tCapability\n");
+ expect(documents).toContain("User\tPermission\n");
});
test.each([