OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security
TypeScript ai-securityapplication-securityclicode-scanningcodexcodex-securitycybersecuritydevsecopsnodejsnpmopenaisdksecuritytypescriptvulnerability-scanning
fix(import): preserve literal CSV paths, candidate IDs, and notes (#1786)
Michael D'Angelo50e6de4555af
diff --git a/plugins/codex-security/mcp-app/src/artifact-contracts.ts b/plugins/codex-security/mcp-app/src/artifact-contracts.tsindex dc068a9bd..5ef65a7e3 100644--- a/plugins/codex-security/mcp-app/src/artifact-contracts.ts+++ b/plugins/codex-security/mcp-app/src/artifact-contracts.ts@@ -35,7 +35,7 @@ const scanId = zconst repositoryPath = pattern(z.string().min(1).max(4096),- "^(?:\\./)?(?!/)(?![A-Za-z]:)(?!.*\\\\)(?!.*(?:^|/)\\.\\.?(/|$))(?!.*//)(?!.*[\\u0000-\\u001f\\u007f])(?!.*\\/$).+$",+ "^(?:\\./)?(?!/)(?![A-Za-z]:)(?![\\s\\S]*\\\\)(?![\\s\\S]*(?:^|/)\\.\\.?(/|$))(?![\\s\\S]*//)(?![\\s\\S]*[\\u0000-\\u001f\\u007f])(?![\\s\\S]*\\/$)[\\s\\S]+$",);const candidateId = zdiff --git a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts b/plugins/codex-security/mcp-app/src/artifact-scan-draft.tsindex a598dad64..d8d2c52b6 100644--- a/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts+++ b/plugins/codex-security/mcp-app/src/artifact-scan-draft.ts@@ -1751,7 +1751,7 @@ export async function getCodexSecurityCompletedScan(function safeRelativePath(value: string, context: string): string {const parts = value.split("/");if (- value.trim().length === 0 ||+ value.length === 0 ||Buffer.from(value, "utf8").toString("utf8") !== value ||value === "." ||value.startsWith("/") ||diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_foundation.ts b/plugins/codex-security/mcp-app/tests/test_artifact_foundation.tsindex ed9976bd4..47e517a1f 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_foundation.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_foundation.ts@@ -1,4 +1,5 @@import * as z from "zod/v4";+import { Ajv2020 } from "ajv/dist/2020.js";import { readJson } from "./support/json.ts";import assert from "node:assert/strict";import { spawn } from "node:child_process";@@ -77,11 +78,24 @@ async function testSchemaSourceOfTruth() {path: common.repositoryPath,candidateId: common.candidateId,});+ const exported = await readJson(+ path.join(+ import.meta.dirname,+ "../../schemas/definitions/artifact-common.schema.json",+ ),+ );+ const validateExportedPath = new Ajv2020({ strict: false }).compile(+ exported.$defs.repositoryPath,+ );for (const repositoryPath of ["src/index.ts","./src/index.ts","scope with spaces/café.ts",".hidden/config.ts",+ "\u2028",+ "\u2029",+ "src/a\u2028b.py",+ "src/a\u2029b.py",]) {assert.equal(validator.safeParse({ path: repositoryPath, candidateId: "candidate-1" })@@ -89,6 +103,7 @@ async function testSchemaSourceOfTruth() {true,repositoryPath,);+ assert.equal(validateExportedPath(repositoryPath), true, repositoryPath);}for (const repositoryPath of ["/etc/passwd",@@ -98,6 +113,16 @@ async function testSchemaSourceOfTruth() {"src//outside.ts","C:\\outside.ts","src/\0outside.ts",+ ...["\u2028", "\u2029"].flatMap((separator) => [+ `${separator}/../outside.ts`,+ `${separator}/./outside.ts`,+ `${separator}\\outside.ts`,+ `${separator}//outside.ts`,+ `${separator}/`,+ ...["\0", "\t", "\n", "\r", "\u007f"].map(+ (control) => `${separator}${control}outside.ts`,+ ),+ ]),]) {assert.equal(validator.safeParse({ path: repositoryPath, candidateId: "candidate-1" })@@ -105,6 +130,7 @@ async function testSchemaSourceOfTruth() {false,repositoryPath,);+ assert.equal(validateExportedPath(repositoryPath), false, repositoryPath);}for (const candidateId of [".",diff --git a/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.ts b/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.tsindex 21b8138d2..bea88fc75 100644--- a/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.ts+++ b/plugins/codex-security/mcp-app/tests/test_artifact_scan_draft.ts@@ -2513,6 +2513,41 @@ try {...incompleteCodeEvidence,code: "extract_archive_entry(untrusted_entry, output_path)",};+ for (const literalPath of ["\u2028", "\u2029", " ", "\u00A0", "\uFEFF"]) {+ const literalRoot = path.join(+ root,+ `literal-path-${literalPath.codePointAt(0)}`,+ );+ await mkdir(literalRoot);+ const { context: literalContext } = draftFixture(literalRoot, "standard");+ const literalFinding = {+ ...finding,+ locations: [{ ...finding.locations[0], path: literalPath }],+ codeEvidence: [{ ...completeCodeEvidence, path: literalPath }],+ };+ await recordCodexSecurityScanDraft(literalContext, {+ ...input,+ findings: [literalFinding],+ });+ assert.equal(+ (await readJson(literalRoot, "findings.json")).findings[0].locations[0]+ .path,+ literalPath,+ );+ await recordCodexSecurityScanDraft(literalContext, {+ ...input,+ findings: [{ ...literalFinding, summary: "Updated finding summary." }],+ });+ const reloaded = draftApi.parseCanonicalScanDraft({+ scanId,+ manifest: await readJson(literalRoot, "scan-manifest.json"),+ findings: await readJson(literalRoot, "findings.json"),+ coverage: await readJson(literalRoot, "coverage.json"),+ });+ assert.equal(reloaded.findings[0].summary, "Updated finding summary.");+ assert.equal(reloaded.findings[0].locations[0].path, literalPath);+ assert.equal(reloaded.findings[0].codeEvidence[0].path, literalPath);+ }const evidencedFinding = {...finding,codeEvidence: [completeCodeEvidence],diff --git a/plugins/codex-security/schemas/definitions/artifact-common.schema.json b/plugins/codex-security/schemas/definitions/artifact-common.schema.jsonindex c3b6dc32f..9a5f1ee8b 100644--- a/plugins/codex-security/schemas/definitions/artifact-common.schema.json+++ b/plugins/codex-security/schemas/definitions/artifact-common.schema.json@@ -12,7 +12,7 @@"type": "string","minLength": 1,"maxLength": 4096,- "pattern": "^(?:\\./)?(?!/)(?![A-Za-z]:)(?!.*\\\\)(?!.*(?:^|/)\\.\\.?(/|$))(?!.*//)(?!.*[\\u0000-\\u001f\\u007f])(?!.*\\/$).+$"+ "pattern": "^(?:\\./)?(?!/)(?![A-Za-z]:)(?![\\s\\S]*\\\\)(?![\\s\\S]*(?:^|/)\\.\\.?(/|$))(?![\\s\\S]*//)(?![\\s\\S]*[\\u0000-\\u001f\\u007f])(?![\\s\\S]*\\/$)[\\s\\S]+$"},"candidateId": {"type": "string",diff --git a/sdk/typescript/src/findings-import.ts b/sdk/typescript/src/findings-import.tsindex ade7a18fc..f6e15cf94 100644--- a/sdk/typescript/src/findings-import.ts+++ b/sdk/typescript/src/findings-import.ts@@ -30,7 +30,10 @@ const csvFindingRowSchema = z.string().optional().transform((value) =>- value === undefined || value.trim() === "" ? undefined : value,+ value === undefined ||+ (value.trim() === "" && !value.includes("\uFEFF"))+ ? undefined+ : value,),title: requiredCsvText("title"),summary: requiredCsvText("summary"),@@ -54,7 +57,9 @@ const csvFindingRowSchema = z.transform((value) => (value === "" ? undefined : value)),note: z.string()- .transform((value) => (value.trim() === "" ? undefined : value)),+ .transform((value) =>+ value.trim() === "" && !value.includes("\uFEFF") ? undefined : value,+ ),remediation: requiredCsvText("remediation"),path: z.string().refine(safeFindingPath, {error: "has an invalid path",@@ -341,7 +346,7 @@ function validCsvLine(value: string): boolean {function safeFindingPath(value: string): boolean {if (- value.trim().length === 0 ||+ value.length === 0 ||value === "." ||isAbsolute(value) ||/^[A-Za-z]:/u.test(value) ||diff --git a/sdk/typescript/tests-ts/findings-import-literal-fields.test.ts b/sdk/typescript/tests-ts/findings-import-literal-fields.test.tsnew file mode 100644index 000000000..df34b3d7b--- /dev/null+++ b/sdk/typescript/tests-ts/findings-import-literal-fields.test.ts@@ -0,0 +1,141 @@+import { expect, test } from "bun:test";+import {+ bindImportedFindings,+ parseImportedFindings,+} from "../src/findings-import.js";+import { PLUGIN_ROOT } from "./plugin-root.js";++function csvSource(fields: Record<string, string> = {}): string {+ const row = {+ occurrence_id: "occ_000000000000000000000001",+ finding_id: "csf_000000000000000000000001",+ title: "Synthetic finding",+ summary: "Reported summary",+ severity: "high",+ confidence: "high",+ status: "open",+ close_reason: "",+ note: "",+ remediation: "Validate the input.",+ path: "source.ts",+ start_line: "1",+ end_line: "",+ ...fields,+ };+ return (+ [Object.keys(row), Object.values(row)]+ .map((cells) =>+ cells.map((cell) => `"${cell.replaceAll('"', '""')}"`).join(","),+ )+ .join("\r\n") + "\r\n"+ );+}++test.each([" ", " ", "\u00A0", "\u2003", "\u2028", "\u2029", "\uFEFF"])(+ "preserves the literal CSV path %j",+ async (path) => {+ const [finding] = await parseImportedFindings(+ csvSource({ path }),+ "csv",+ PLUGIN_ROOT,+ );+ expect(finding!.locations[0]!.path).toBe(path);+ },+);++test.each([+ "",+ ".",+ "..",+ "../source.ts",+ "dir/../source.ts",+ "/source.ts",+ "C:source.ts",+ "C:/source.ts",+ "dir\\source.ts",+ "\t",+ "\n",+ "\0",+])("still rejects an unsafe or empty CSV path %j", async (path) => {+ await expect(+ parseImportedFindings(csvSource({ path }), "csv", PLUGIN_ROOT),+ ).rejects.toThrow("has an invalid path");+});++test.each([+ [undefined, undefined],+ ["", undefined],+ [" \t\r\n\u00A0", undefined],+ ["candidate-1", "candidate-1"],+ [" candidate-1 ", " candidate-1 "],+ ["\u0085", "\u0085"],+ ["\u001C\u001D\u001E\u001F", "\u001C\u001D\u001E\u001F"],+ ["\uFEFF", "\uFEFF"],+ [" \t\uFEFF \t", " \t\uFEFF \t"],+ ["\uFEFF\uFEFF", "\uFEFF\uFEFF"],+] as const)("preserves optional candidate ID %j", async (value, expected) => {+ const source = csvSource(value === undefined ? {} : { candidate_id: value });+ for (const bom of ["", "\uFEFF"]) {+ const parsed = await parseImportedFindings(+ bom + source,+ "csv",+ PLUGIN_ROOT,+ );+ const bound = bindImportedFindings(parsed, "csv", "scan", "target");+ for (const finding of [parsed[0]!, bound[0]!]) {+ expect(finding.extensions?.["candidateId"]).toBe(expected);+ }+ }+});++test.each([+ ["", undefined],+ [" \t\r\n\u00A0", undefined],+ [" ordinary λ ", " ordinary λ "],+ ["\uFEFF", "\uFEFF"],+ [" \t\uFEFF \t", " \t\uFEFF \t"],+ ["\u0085", "\u0085"],+ ["\u001C\u001D\u001E\u001F", "\u001C\u001D\u001E\u001F"],+ ["'\u0085=formula", "\u0085=formula"],+ ["'\uFEFF=formula", "'\uFEFF=formula"],+] as const)("preserves original decoded note %j", async (note, expected) => {+ for (const closeReason of [+ "",+ "already_fixed",+ "false_positive",+ "wont_fix",+ ]) {+ const parsed = parseImportedFindings(+ "\uFEFF" ++ csvSource({+ note,+ status: closeReason === "" ? "open" : "closed",+ close_reason: closeReason,+ }),+ "csv",+ PLUGIN_ROOT,+ );+ if (+ expected === undefined &&+ ["false_positive", "wont_fix"].includes(closeReason)+ ) {+ await expect(parsed).rejects.toThrow(+ "requires a note for its close_reason",+ );+ continue;+ }+ const [finding] = bindImportedFindings(+ await parsed,+ "csv",+ "scan",+ "target",+ );+ if (expected === undefined) {+ expect(finding!.validation!.summary).not.toContain("Source note:");+ } else {+ expect(finding!.validation!.summary).toEndWith(+ `Source note: ${expected}`,+ );+ }+ }+});diff --git a/sdk/typescript/tests-ts/findings-import.test.ts b/sdk/typescript/tests-ts/findings-import.test.tsindex 57918f9ec..af2ed576c 100644--- a/sdk/typescript/tests-ts/findings-import.test.ts+++ b/sdk/typescript/tests-ts/findings-import.test.ts@@ -27,13 +27,14 @@ async function sourceDocument(): Promise<FindingsDocument> {);}-function exportCsv(findings: Finding[]): string {+function exportCsv(findings: Finding[], coverage = {}): string {return execFileSync(PYTHON,["-c",- "import json, sys; sys.path.insert(0, sys.argv[1]); from finalize_scan_contract import build_csv_projection; sys.stdout.buffer.write(build_csv_projection(json.load(sys.stdin.buffer), {}))",+ "import json, sys; sys.path.insert(0, sys.argv[1]); from finalize_scan_contract import build_csv_projection; sys.stdout.buffer.write(build_csv_projection(json.load(sys.stdin.buffer), json.loads(sys.argv[2])))",join(PLUGIN_ROOT, "scripts"),+ JSON.stringify(coverage),],{cwd: join(PLUGIN_ROOT, "scripts"),@@ -177,6 +178,30 @@ describe("findings import formats", () => {expect(imported!.title).toBe(title);});+ test("round-trips literal whitespace paths and FEFF candidate IDs through the exporter", async () => {+ const {+ findings: [finding],+ } = await sourceDocument();+ for (const path of [" ", "\u00A0", "\u2028", "\u2029", "\uFEFF"]) {+ const [imported] = await parseImportedFindings(+ exportCsv(+ [+ {+ ...finding!,+ locations: [{ path, startLine: 1, endLine: 1 }],+ extensions: { candidateId: "\uFEFF" },+ },+ ],+ { mode: "deep_repository" },+ ),+ "csv",+ PLUGIN_ROOT,+ );+ expect(imported!.locations).toEqual([{ path, startLine: 1, endLine: 1 }]);+ expect(imported!.extensions?.["candidateId"]).toBe("\uFEFF");+ }+ });+test("accepts full findings documents and findings-service payloads", async () => {const document = await sourceDocument();document.findings[0]!.summary =diff --git a/sdk/typescript/tests-ts/import-scan.test.ts b/sdk/typescript/tests-ts/import-scan.test.tsindex 33a278d8c..a4e753f8b 100644--- a/sdk/typescript/tests-ts/import-scan.test.ts+++ b/sdk/typescript/tests-ts/import-scan.test.ts@@ -324,6 +324,49 @@ test("CSV import seals and reloads non-whitespace byte order marks in required texpect(await readFile(context.options.sourcePath, "utf8")).toBe(source);});+test.each(["\u00A0", "\u2028", "\u2029", "src/a\u2028b.py", "src/a\u2029b.py"])(+ "CSV import retains literal fields without replaying source decisions for %j",+ async (path) => {+ const context = await fixture();+ const source =+ "\uFEFF" ++ context.source+ .replaceAll('"title"', '"candidate_id","title"')+ .replaceAll(+ '"Reported archive extraction issue"',+ '"\uFEFF","Reported archive extraction issue"',+ )+ .replaceAll('"open","",""', '"closed","false_positive"," \uFEFF "')+ .replaceAll('"src/extract.ts"', `"${path}"`);+ await writeFile(context.options.sourcePath, source);+ const result = completed(+ await importScan(context.options, context.dependencies),+ );+ const loaded = await loadContract(result.scanDir, {+ pluginRoot: PLUGIN_ROOT,+ });+ for (const finding of loaded.findings.findings) {+ expect(finding.locations[0]!.path).toBe(path);+ expect(finding.extensions?.["candidateId"]).toBe("\uFEFF");+ expect(finding.validation).toMatchObject({+ method: "Source-reported CSV import metadata",+ status: "closed",+ summary: expect.stringContaining(+ "Source close reason: false_positive\nSource note: \uFEFF ",+ ),+ });+ }+ const decisions = await runCommand(context.python, [+ "-c",+ "import sqlite3, sys; c = sqlite3.connect(sys.argv[1]); print(c.execute('SELECT COUNT(*) FROM finding_decisions').fetchone()[0], c.execute('SELECT COUNT(*) FROM finding_triage').fetchone()[0])",+ join(context.stateDirectory, "workbench.sqlite3"),+ ]);+ expect(decisions.status, decisions.stderr).toBe(0);+ expect(decisions.stdout.trim()).toBe("0 0");+ expect(await readFile(context.options.sourcePath, "utf8")).toBe(source);+ },+);+test.each(["csv", "json"] as const)("empty %s imports preserve source integrity without claiming an analyzed surface",async (format) => {