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(csv): preserve finding occurrences and literal apostrophes (#1247)
mldangelo-oaibcda23b2a1b9
diff --git a/plugins/codex-security/scripts/finalize_scan_contract.py b/plugins/codex-security/scripts/finalize_scan_contract.pyindex 51259b0a4..9d6ec4f52 100644--- a/plugins/codex-security/scripts/finalize_scan_contract.py+++ b/plugins/codex-security/scripts/finalize_scan_contract.py@@ -2699,7 +2699,7 @@ def write_sarif_projection(def csv_cell(value: Any) -> Any:if isinstance(value, str) and (- value.startswith(("\t", "\r", "\n"))+ value.startswith(("'", "\t", "\r", "\n"))or value.lstrip().startswith(("=", "+", "-", "@", "=", "+", "-", "@"))):return f"'{value}"diff --git a/sdk/typescript/README.md b/sdk/typescript/README.mdindex 255e08ad6..4ef0de962 100644--- a/sdk/typescript/README.md+++ b/sdk/typescript/README.md@@ -1889,6 +1889,11 @@ cancels the export. `export --help` lists the CLI options.JSON preserves the sealed findings document. CSV marks findings as open,omits local triage state, and cannot go to stdout when JSON output is requested.+CSV escapes spreadsheet formula prefixes and literal leading apostrophes with+an extra apostrophe; import removes that escape. Older CSV exports cannot+distinguish some literal apostrophes from escapes. Use the JSON export when+recovering those values from an older scan. Distinct CSV occurrence IDs are+retained even when their finding IDs match, including when publishing CSV.For CI, save output outside the checkout and set a severity threshold:diff --git a/sdk/typescript/src/findings-import.ts b/sdk/typescript/src/findings-import.tsindex 4dcfb6fce..c5b639752 100644--- a/sdk/typescript/src/findings-import.ts+++ b/sdk/typescript/src/findings-import.ts@@ -10,7 +10,9 @@ import type { Finding, FindingsDocument } from "./models.js";export const CSV_TARGET_ID = "codex-security-csv-import";export type FindingsImportFormat = "csv" | "json";-const EXPORTED_CSV_ESCAPE = /^'(?:[\t\r\n]|\s*[=+\-@=+-@])/u;+// Match the Python exporter's str.lstrip(), including its extra C0 separators.+const EXPORTED_CSV_ESCAPE =+ /^'(?:['\t\r\n]|[\p{White_Space}\u001c-\u001f]*[=+\-@=+-@])/u;const csvFindingRowSchema = z.object({occurrence_id: z.string().regex(/^occ_[a-f0-9]{24}$/u, {@@ -254,7 +256,6 @@ export function parseFindingsCsv(source: string): CsvFindingRow[] {);}- const findingIds = new Set<string>();const occurrenceIds = new Set<string>();return rows.map((record, index) => {const rowNumber = index + 2;@@ -263,13 +264,9 @@ export function parseFindingsCsv(source: string): CsvFindingRow[] {throw csvRowError(rowNumber, parsed.error.issues[0]!.message);}const row = parsed.data;- if (findingIds.has(row.finding_id)) {- throw csvRowError(rowNumber, "has a duplicate finding_id");- }if (occurrenceIds.has(row.occurrence_id)) {throw csvRowError(rowNumber, "has a duplicate occurrence_id");}- findingIds.add(row.finding_id);occurrenceIds.add(row.occurrence_id);return row;});@@ -281,7 +278,7 @@ function decodeExportedCsvCell(value: string): string {export function csvRowFinding(row: CsvFindingRow, scanId: string): Finding {const ruleId = "import.csv";- const anchor = row.finding_id;+ const anchor = row.occurrence_id;const fingerprint = `codex-security/v1:sha256:${sha256(["codex-security/v1", CSV_TARGET_ID, ruleId, anchor, ""].join("\0"),)}`;diff --git a/sdk/typescript/tests-ts/cloud-publish.test.ts b/sdk/typescript/tests-ts/cloud-publish.test.tsindex 91ad0777b..fa034278f 100644--- a/sdk/typescript/tests-ts/cloud-publish.test.ts+++ b/sdk/typescript/tests-ts/cloud-publish.test.ts@@ -214,6 +214,24 @@ describe("Cloud publication", () => {expect(finding!.occurrenceId).toMatch(/^occ_[a-f0-9]{24}$/);});+ test("keeps separate CSV occurrences with the same source finding ID", async () => {+ const second = csvRow.replace(+ "occ_e79cb19591e696572a1c22be",+ "occ_111111111111111111111111",+ );+ const path = await csvFixture(`${csvHeader}\n${csvRow}\n${second}\n`);+ const result = await publishFindingsCsvToCloud(path, { dryRun: true });+ expect(result.findingCount).toBe(2);+ expect(+ new Set(result.findings!.map((finding) => finding.occurrenceId)).size,+ ).toBe(2);+ expect(+ new Set(result.findings!.map((finding) => finding.findingId)).size,+ ).toBe(2);+ const repeated = await publishFindingsCsvToCloud(path, { dryRun: true });+ expect(repeated.findings).toEqual(result.findings);+ });+test("accepts the candidate_id column from a Deep scan export", async () => {const path = await csvFixture(`${csvHeader.replace("finding_id,", "finding_id,candidate_id,")}\n${csvRow.replace("csf_852f90d6e1177502ff113d4a,", "csf_852f90d6e1177502ff113d4a,candidate-001,")}\n`,diff --git a/sdk/typescript/tests-ts/findings-import.test.ts b/sdk/typescript/tests-ts/findings-import.test.tsindex 6c4ba695f..c62f8fc59 100644--- a/sdk/typescript/tests-ts/findings-import.test.ts+++ b/sdk/typescript/tests-ts/findings-import.test.ts@@ -7,6 +7,7 @@ import {} from "../src/findings-import.js";import type { Finding, FindingsDocument } from "../src/models.js";import { PLUGIN_ROOT } from "./plugin-root.js";+import { PYTHON } from "./support/security-policy.js";const BYTE_ORDER_MARK = "\uFEFF";const CSV_SOURCE =@@ -25,7 +26,121 @@ async function sourceDocument(): Promise<FindingsDocument> {);}+function exportCsv(findings: Finding[]): 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), {}))",+ join(PLUGIN_ROOT, "scripts"),+ ],+ {+ cwd: join(PLUGIN_ROOT, "scripts"),+ input: JSON.stringify({ findings }),+ encoding: "utf8",+ },+ );+}+describe("findings import formats", () => {+ test("retains distinct occurrences of a shared finding in CSV and JSON", async () => {+ const {+ findings: [finding],+ } = await sourceDocument();+ const rows = [+ finding!,+ { ...finding!, occurrenceId: "occ_111111111111111111111111" },+ ];+ for (const format of ["csv", "json"] as const) {+ const source =+ format === "csv" ? exportCsv(rows) : JSON.stringify({ findings: rows });+ const parsed = await parseImportedFindings(source, format, PLUGIN_ROOT);+ expect(+ parsed.map(({ findingId, occurrenceId }) => [findingId, occurrenceId]),+ ).toEqual(+ rows.map(({ findingId, occurrenceId }) => [findingId, occurrenceId]),+ );+ expect(+ new Set(+ bindImportedFindings(parsed, format, "scan", "target").map(+ (row) => row.occurrenceId,+ ),+ ).size,+ ).toBe(2);+ }+ await expect(+ parseImportedFindings(+ exportCsv([finding!, finding!]),+ "csv",+ PLUGIN_ROOT,+ ),+ ).rejects.toThrow("duplicate occurrence_id");+ });++ test("round-trips literal apostrophes and spreadsheet escapes through the exporter", async () => {+ const {+ findings: [finding],+ } = await sourceDocument();+ const values = [+ "'--no-verify' skips hooks",+ "''=literal",+ "'Literal",+ "ordinary λ",+ "=formula",+ "\u0085=formula",+ "\u001c=formula",+ "\u001d=formula",+ "\u001e=formula",+ "\u001f=formula",+ "\uFEFF=formula",+ " +formula",+ "-option",+ "@owner",+ "\tTabbed",+ "\nNewline",+ " =wide",+ " +wide",+ "-wide",+ "@wide",+ ];+ for (const value of values) {+ const source = {+ ...finding!,+ title: value,+ summary: value,+ remediation: value,+ locations: [+ {+ path: value.replace(/[\u0000-\u001F]/gu, "") + ".ts",+ startLine: 1,+ },+ ],+ };+ const [imported] = await parseImportedFindings(+ exportCsv([source]),+ "csv",+ PLUGIN_ROOT,+ );+ expect(imported).toMatchObject({+ title: source.title,+ summary: source.summary,+ remediation: source.remediation,+ locations: source.locations,+ });+ }+ });++ test("retains a literal apostrophe before a non-whitespace byte order mark", async () => {+ const title = "'\uFEFF=formula";+ const [imported] = await parseImportedFindings(+ CSV_SOURCE.replace("Reported CSV import issue", title),+ "csv",+ PLUGIN_ROOT,+ );++ expect(imported!.title).toBe(title);+ });+test("accepts full findings documents and findings-service payloads", async () => {const document = await sourceDocument();document.findings[0]!.summary =@@ -146,3 +261,4 @@ describe("findings import formats", () => {).rejects.toThrow("duplicate occurrenceId");});});+import { execFileSync } from "node:child_process";