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
feat(publish): upload findings from CSV (#645)
Kyle Brownf08cbba704e0
diff --git a/examples/findings.csv b/examples/findings.csvnew file mode 100644index 000000000..ac0566982--- /dev/null+++ b/examples/findings.csv@@ -0,0 +1 @@+occurrence_id,finding_id,title,summary,severity,confidence,status,close_reason,note,remediation,path,start_line,end_linediff --git a/sdk/typescript/README.md b/sdk/typescript/README.mdindex 271ee9b3d..d5905a377 100644--- a/sdk/typescript/README.md+++ b/sdk/typescript/README.md@@ -295,6 +295,7 @@ npx @openai/codex-security export /path/outside/repository/results --export-formnpx @openai/codex-security export /path/outside/repository/results --export-format json --output /path/outside/repository/findings.jsonnpx @openai/codex-security publish scan /path/outside/repository/results --to linear --linear-team TEAM_IDnpx @openai/codex-security publish scan --to linear --linear-team TEAM_ID+npx @openai/codex-security publish scan --to cloud --csv /path/outside/repository/findings.csvnpx @openai/codex-security validate /path/outside/repository/findings.json "Possible SQL injection in src/query.ts:42"npx @openai/codex-security validate "Possible SQL injection" --effort highnpx @openai/codex-security verify-fix --linear-issue SEC-123 --json@@ -730,7 +731,7 @@ and scans stopped at their configured cost limit do not start another turn.invocation and defaults to `1`. Results remain under `--output-dir`; rerun thesame command to resume.-### Publish multiple completed scans to Cloud+### Publish findings to CloudChoose completed scans from your local history:@@ -772,6 +773,27 @@ publication, even when an `auth.json` file exists, because that file may bestale and belong to a different account. The CLI sends one scan at a time,with each scan's findings and metadata in a separate request.+To publish findings from a CSV instead of a completed scan, pass the CSV+created by `codex-security export --export-format csv`:++```bash+npx @openai/codex-security publish scan --to cloud \+ --csv /path/outside/repository/findings.csv+```++The repository includes a header-only+[findings CSV template](https://github.com/openai/codex-security/blob/main/examples/findings.csv)+that you can copy and fill in before publishing.++The CSV must have the export columns `occurrence_id`, `finding_id`, `title`,+`summary`, `severity`, `confidence`, `status`, `close_reason`, `note`,+`remediation`, `path`, `start_line`, and `end_line`. Deep-scan exports may also+include `candidate_id`. Use `--dry-run --json` to validate the CSV and inspect+the normalized findings locally without a login or network request.++`--csv` is only supported with `--to cloud` and cannot be combined with a scan+directory, `--scan-dir`, or `--scan`.+For artifacts outside local history, use repeated `--scan-dir PATH` instead.A single positional directory is still accepted and can be combined with`--scan-dir`. Do not combine directory inputs with `--scan`. Each directorydiff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.tsindex 9da4da0b4..79bebe6fa 100644--- a/sdk/typescript/src/cli.ts+++ b/sdk/typescript/src/cli.ts@@ -56,6 +56,7 @@ import {} from "./api.js";import { accountStatus } from "./auth.js";import {+ publishFindingsCsvToCloud,publishScanToCloud,type CloudPublicationResult,} from "./cloud-publish.js";@@ -254,6 +255,7 @@ const VALUE_OPTIONS = new Set(["--max-time-hours","--max-attempts","--export-format",+ "--csv","--output","--source-root","--format",@@ -1062,6 +1064,7 @@ interface CliDependencies {Partial<Pick<BulkScanPrompt, "checkbox">>;checkScanPublication?: typeof checkScanPublication;publishScan?: typeof publishScan;+ publishFindingsCsvToCloud?: typeof publishFindingsCsvToCloud;publishScanToCloud?: typeof publishScanToCloud;confirmPatchReview?: (question: string) => Promise<boolean>;patchEditor?: (@@ -1998,9 +2001,9 @@ export async function main(}};const publication = Cli.create("publish", {- description: "Publish completed Codex Security scan findings.",+ description: "Publish Codex Security findings.",}).command("scan", {- description: "Publish every finding from a completed scan to Linear.",+ description: "Publish findings from a completed scan or CSV.",destructive: true,mcp: false,args: z.object({@@ -2032,7 +2035,10 @@ export async function main(dryRun: z.boolean().default(false)- .describe("Preview the findings without creating Linear issues."),+ .describe("Preview the findings without publishing them."),+ csv: optionValue("--csv")+ .optional()+ .describe("Findings CSV to publish instead of a completed scan."),skipExisting: z.boolean().default(false)@@ -2103,6 +2109,10 @@ export async function main(};try {const currentDirectory = dependencies.currentDirectory();+ const csvPath =+ options.csv === undefined+ ? undefined+ : resolveCliPath(currentDirectory, options.csv);const directories = [...new Set([@@ -2121,6 +2131,21 @@ export async function main("Use --scan or scan directory inputs, not both.",);}+ if (+ csvPath !== undefined &&+ (args.scanDir !== undefined ||+ options.scan.length > 0 ||+ options.scanDir.length > 0)+ ) {+ throw new CodexSecurityError(+ "Use --csv or scan directory and ID inputs, not both.",+ );+ }+ if (csvPath !== undefined && options.to !== "cloud") {+ throw new CodexSecurityError(+ "--csv is only supported with --to cloud.",+ );+ }if (new Set(options.scan).size > 1 && options.to !== "cloud") {throw new CodexSecurityError("Multiple scans are only supported with --to cloud.",@@ -2153,6 +2178,16 @@ export async function main(dependencies.addSignalListener("SIGTERM", onTerminate);observingSignals = true;}+ if (csvPath !== undefined) {+ const result = await (+ dependencies.publishFindingsCsvToCloud ?? publishFindingsCsvToCloud+ )(csvPath, {+ environment: dependencies.environment,+ dryRun: options.dryRun,+ signal: controller.signal,+ });+ return { ...result };+ }const selectedScans: { scanDir: string; scanId?: string }[] =directories.map((scanDir) => ({ scanDir }));for (const requestedId of new Set(options.scan)) {diff --git a/sdk/typescript/src/cloud-publish.ts b/sdk/typescript/src/cloud-publish.tsindex eddc7eb99..3f0a6ce63 100644--- a/sdk/typescript/src/cloud-publish.ts+++ b/sdk/typescript/src/cloud-publish.ts@@ -1,11 +1,13 @@+import { createHash } from "node:crypto";import { existsSync } from "node:fs";import { readFile } from "node:fs/promises";-import { join } from "node:path";+import { isAbsolute, join, posix } from "node:path";import { z } from "incur";+import Papa from "papaparse";import { parse as parseToml } from "smol-toml";import { loadContract } from "./contract.js";import { AuthenticationRequiredError, CodexSecurityError } from "./errors.js";-import type { Finding } from "./models.js";+import type { Finding, ScanManifest } from "./models.js";import {bundledPluginRoot,codexSecurityCredentialAllowsAmbientImport,@@ -13,9 +15,11 @@ import {codexSecurityHasStoredFileCredentials,expandHome,} from "./runtime.js";+import { VERSION } from "./version.js";const CLOUD_PUBLISH_URL ="https://chatgpt.com/backend-api/aardvark/cli/findings";+const CSV_TARGET_ID = "codex-security-csv-import";const CHATGPT_LOGIN_REQUIRED ="Cloud publication requires a ChatGPT login already available to Codex Security. Run a scan or sign in with ChatGPT using Codex file credential storage, then retry.";@@ -42,13 +46,107 @@ export interface CloudPublicationResult {findings?: Finding[];}+interface CloudPublicationDependencies {+ environment?: NodeJS.ProcessEnv;+ fetch?: (url: string, options: RequestInit) => Promise<Response>;+ signal?: AbortSignal;+ dryRun?: boolean;+}++const EXPORTED_CSV_ESCAPE = /^'(?:[\t\r\n]|\s*[=+\-@=+-@])/u;+const csvFindingRowSchema = z+ .object({+ occurrence_id: z.string().regex(/^occ_[a-f0-9]{24}$/u, {+ error: "has an invalid occurrence_id",+ }),+ finding_id: z.string().regex(/^csf_[a-f0-9]{24}$/u, {+ error: "has an invalid finding_id",+ }),+ candidate_id: z+ .string()+ .optional()+ .transform((value) =>+ value === undefined || value.trim() === "" ? undefined : value,+ ),+ title: requiredCsvText("title"),+ summary: requiredCsvText("summary"),+ severity: z.enum(["critical", "high", "medium", "low", "informational"], {+ error: "has an invalid severity",+ }),+ confidence: z.enum(["high", "medium", "low"], {+ error: "has an invalid confidence",+ }),+ status: z.enum(["open", "closed"], {+ error: "has an invalid status",+ }),+ close_reason: z+ .union(+ [+ z.literal(""),+ z.enum(["already_fixed", "wont_fix", "false_positive"]),+ ],+ { error: "has an invalid close_reason" },+ )+ .transform((value) => (value === "" ? undefined : value)),+ note: z+ .string()+ .transform((value) => (value.trim() === "" ? undefined : value)),+ remediation: requiredCsvText("remediation"),+ path: z.string().refine(safeFindingPath, {+ error: "has an invalid path",+ }),+ start_line: z+ .string()+ .refine(validCsvLine, { error: "has an invalid start_line" })+ .transform(Number),+ end_line: z+ .string()+ .refine((value) => value === "" || validCsvLine(value), {+ error: "has an invalid end_line",+ })+ .transform((value) => (value === "" ? undefined : Number(value))),+ })+ .superRefine((row, context) => {+ if (+ (row.status === "open" && row.close_reason !== undefined) ||+ (row.status === "closed" && row.close_reason === undefined)+ ) {+ context.addIssue({+ code: "custom",+ path: ["close_reason"],+ message: "has an invalid close_reason",+ });+ }+ if (+ row.status === "closed" &&+ (row.close_reason === "false_positive" ||+ row.close_reason === "wont_fix") &&+ row.note === undefined+ ) {+ context.addIssue({+ code: "custom",+ path: ["note"],+ message: "requires a note for its close_reason",+ });+ }+ if (row.end_line !== undefined && row.end_line < row.start_line) {+ context.addIssue({+ code: "custom",+ path: ["end_line"],+ message: "has end_line before start_line",+ });+ }+ });+type CsvFindingRow = z.infer<typeof csvFindingRowSchema>;+type CsvColumn = keyof typeof csvFindingRowSchema.shape;+const CSV_COLUMNS = Object.keys(csvFindingRowSchema.shape) as CsvColumn[];+const REQUIRED_CSV_COLUMNS = CSV_COLUMNS.filter(+ (column) => !csvFindingRowSchema.shape[column].isOptional(),+);+export async function publishScanToCloud(scanDirectory: string,- dependencies: {- environment?: NodeJS.ProcessEnv;- fetch?: (url: string, options: RequestInit) => Promise<Response>;- signal?: AbortSignal;- dryRun?: boolean;+ dependencies: CloudPublicationDependencies & {expectedScanId?: string;} = {},): Promise<CloudPublicationResult> {@@ -62,14 +160,270 @@ export async function publishScanToCloud("The completed scan has no findings to publish.",);}+ return publishCloudPayload(manifest.scan, findings.findings, dependencies);+}++export async function publishFindingsCsvToCloud(+ csvPath: string,+ dependencies: CloudPublicationDependencies = {},+): Promise<CloudPublicationResult> {+ dependencies.signal?.throwIfAborted();+ let source: string;+ try {+ source = await readFile(csvPath, "utf8");+ } catch (error) {+ throw new CodexSecurityError("Could not read findings CSV.", {+ cause: error,+ });+ }+ dependencies.signal?.throwIfAborted();+ const rows = parseFindingsCsv(source);+ const digest = sha256(source);+ const scanId = `scan_csv_${sha256(+ ["codex-security-csv-import/v1", VERSION, source].join("\0"),+ ).slice(0, 24)}`;+ const findings = rows.map((row) => csvRowFinding(row, scanId));+ const timestamp = "1970-01-01T00:00:00.000Z";+ const findingsDocument = JSON.stringify({+ documentType: "codex-security.findings",+ schemaVersion: "1.0",+ scanId,+ findings,+ });+ const coverageDocument = JSON.stringify({+ documentType: "codex-security.coverage",+ schemaVersion: "1.0",+ scanId,+ mode: "repository",+ completeness: "unknown",+ inventoryStrategy: "custom",+ includePaths: ["."],+ excludePaths: [],+ surfaces: findings.map((finding) => ({+ id: finding.occurrenceId,+ label: finding.title,+ disposition: "reported",+ receiptRefs: [],+ })),+ explicitExclusions: [],+ deferred: [],+ });+ const scan: ScanManifest["scan"] = {+ id: scanId,+ producer: { name: "codex-security-cli", version: VERSION },+ status: "completed",+ startedAt: timestamp,+ completedAt: timestamp,+ sealedAt: timestamp,+ target: {+ kind: "directory_snapshot",+ targetId: CSV_TARGET_ID,+ displayName: "findings.csv",+ snapshotDigest: `codex-security-snapshot/v1:sha256:${digest}`,+ },+ scope: {+ includePaths: ["."],+ excludePaths: [],+ summary: "Findings imported from a Codex Security CSV export.",+ },+ coverageRef: "coverage.json",+ findingsRef: "findings.json",+ artifacts: [+ {+ path: "findings.json",+ sha256: sha256(findingsDocument),+ mediaType: "application/json",+ },+ {+ path: "coverage.json",+ sha256: sha256(coverageDocument),+ mediaType: "application/json",+ },+ {+ path: "findings.csv",+ sha256: digest,+ mediaType: "text/csv",+ },+ ],+ };+ return publishCloudPayload(scan, findings, dependencies);+}++function parseFindingsCsv(source: string): CsvFindingRow[] {+ const {+ data: rows,+ errors,+ meta,+ } = Papa.parse<Record<string, string>>(source, {+ header: true,+ delimiter: ",",+ skipEmptyLines: "greedy",+ transform: decodeExportedCsvCell,+ });+ const fieldMismatch = errors.find(+ (error) => error.code === "TooFewFields" || error.code === "TooManyFields",+ );+ if (fieldMismatch?.row !== undefined) {+ throw csvRowError(fieldMismatch.row + 2, "must match the header columns");+ }+ if (errors.length > 0) {+ throw new CodexSecurityError(+ `Findings CSV could not be parsed: ${errors[0]!.message}`,+ );+ }+ const headers = meta.fields;+ const allowed = new Set<string>(CSV_COLUMNS);+ if (+ headers === undefined ||+ !REQUIRED_CSV_COLUMNS.every((name) => headers.includes(name)) ||+ headers.some((name) => !allowed.has(name)) ||+ new Set(headers).size !== headers.length+ ) {+ throw new CodexSecurityError(+ `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 findingIds = new Set<string>();+ const occurrenceIds = new Set<string>();+ return rows.map((record, index) => {+ const rowNumber = index + 2;+ const parsed = csvFindingRowSchema.safeParse(record);+ if (!parsed.success) {+ 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;+ });+}++function decodeExportedCsvCell(value: string): string {+ return EXPORTED_CSV_ESCAPE.test(value) ? value.slice(1) : value;+}++function csvRowFinding(row: CsvFindingRow, scanId: string): Finding {+ const ruleId = "import.csv";+ const anchor = row.finding_id;+ const fingerprint = `codex-security/v1:sha256:${sha256(+ ["codex-security/v1", CSV_TARGET_ID, ruleId, anchor, ""].join("\0"),+ )}`;+ const findingId = `csf_${sha256(fingerprint).slice(0, 24)}`;+ const occurrenceId = `occ_${sha256([scanId, fingerprint].join("\0")).slice(+ 0,+ 24,+ )}`;+ return {+ findingId,+ occurrenceId,+ ruleId,+ identity: { anchor },+ fingerprints: {+ algorithm: "codex-security/v1",+ primary: fingerprint,+ },+ title: row.title,+ summary: row.summary,+ severity: { level: row.severity },+ confidence: {+ level: row.confidence,+ rationale: "Imported from a Codex Security findings CSV.",+ },+ taxonomy: { category: "imported", cwe: [] },+ locations: [+ {+ path: row.path,+ startLine: row.start_line,+ ...(row.end_line === undefined ? {} : { endLine: row.end_line }),+ },+ ],+ remediation: row.remediation,+ validation: {+ method: "Source-reported CSV import metadata",+ status: row.status,+ summary: [+ `Source finding ID: ${row.finding_id}`,+ `Source occurrence ID: ${row.occurrence_id}`,+ `Source status: ${row.status}`,+ ...(row.close_reason === undefined+ ? []+ : [`Source close reason: ${row.close_reason}`]),+ ...(row.note === undefined ? [] : [`Source note: ${row.note}`]),+ ].join("\n"),+ },+ provenance: { source: "csv_import" },+ extensions: {+ ...(row.candidate_id === undefined+ ? {}+ : { candidateId: row.candidate_id }),+ },+ };+}++function requiredCsvText(column: string) {+ return z.string().refine((value) => value.trim().length > 0, {+ error: `requires ${column}`,+ });+}++function validCsvLine(value: string): boolean {+ if (!/^[1-9]\d*$/u.test(value)) return false;+ const line = Number(value);+ return Number.isSafeInteger(line);+}++function safeFindingPath(value: string): boolean {+ if (+ value.trim().length === 0 ||+ value === "." ||+ isAbsolute(value) ||+ /^[A-Za-z]:/u.test(value) ||+ value.includes("\\") ||+ /[\u0000-\u001F]/u.test(value) ||+ value.split("/").includes("..")+ ) {+ return false;+ }+ const normalized = posix.normalize(value).replace(/\/+$/u, "");+ return normalized !== "." && !normalized.startsWith("../");+}++function csvRowError(rowNumber: number, detail: string): CodexSecurityError {+ return new CodexSecurityError(`Findings CSV row ${rowNumber} ${detail}.`);+}++function sha256(value: string): string {+ return createHash("sha256").update(value, "utf8").digest("hex");+}++async function publishCloudPayload(+ scan: ScanManifest["scan"],+ findings: Finding[],+ dependencies: CloudPublicationDependencies,+): Promise<CloudPublicationResult> {+ if (findings.length === 0) {+ throw new CodexSecurityError("There are no findings to publish.");+ }dependencies.signal?.throwIfAborted();if (dependencies.dryRun) {return {- scanId: manifest.scan.id,+ scanId: scan.id,findingIds: [],- findingCount: findings.findings.length,+ findingCount: findings.length,dryRun: true,- findings: findings.findings,+ findings,};}const credentials = await readCloudCredentials(@@ -94,8 +448,8 @@ export async function publishScanToCloud(},body: JSON.stringify({schemaVersion: "1.0",- scan: manifest.scan,- findings: findings.findings,+ scan,+ findings,}),redirect: "error",signal,@@ -129,8 +483,8 @@ export async function publishScanToCloud(if ((response.status !== 200 && response.status !== 201) ||!receipt.success ||- receipt.data.finding_count !== findings.findings.length ||- receipt.data.finding_ids.length !== findings.findings.length ||+ receipt.data.finding_count !== findings.length ||+ receipt.data.finding_ids.length !== findings.length ||new Set(receipt.data.finding_ids).size !== receipt.data.finding_ids.length) {throw new CodexSecurityError(@@ -138,7 +492,7 @@ export async function publishScanToCloud();}return {- scanId: manifest.scan.id,+ scanId: scan.id,findingIds: receipt.data.finding_ids,findingCount: receipt.data.finding_count,};diff --git a/sdk/typescript/tests-ts/cli-cloud-publish.test.ts b/sdk/typescript/tests-ts/cli-cloud-publish.test.tsindex be01e8732..803c5ad93 100644--- a/sdk/typescript/tests-ts/cli-cloud-publish.test.ts+++ b/sdk/typescript/tests-ts/cli-cloud-publish.test.ts@@ -57,6 +57,100 @@ async function savedScansFixture() {}describe("publish scan to Cloud", () => {+ test("documents the findings CSV option", async () => {+ const stdout = capture();+ expect(+ await main(+ ["publish", "scan", "--help"],+ stdout.stream,+ capture().stream,+ dependencies(),+ ),+ ).toBe(0);+ expect(stdout.text()).toContain("--csv <string>");+ expect(stdout.text()).toContain("Findings CSV");+ });++ test("passes --dry-run through when publishing a findings CSV", async () => {+ const deps = dependencies({+ currentDirectory: "/workspace/repository",+ onWorkbench: () => {+ throw new Error("unexpected scan lookup");+ },+ });+ let publishedPath = "";+ deps.publishFindingsCsvToCloud = async (path, options) => {+ publishedPath = path;+ expect(options?.dryRun).toBe(true);+ expect(options?.signal).toBeInstanceOf(AbortSignal);+ return { ...receipt, dryRun: true };+ };+ const stdout = capture();+ const stderr = capture();+ expect(+ await main(+ [+ "publish",+ "scan",+ "--to",+ "cloud",+ "--csv",+ "inputs/findings.csv",+ "--dry-run",+ "--json",+ ],+ stdout.stream,+ stderr.stream,+ deps,+ ),+ ).toBe(0);+ expect(publishedPath).toBe(+ resolve("/workspace/repository", "inputs/findings.csv"),+ );+ expect(JSON.parse(stdout.text())).toMatchObject({+ scanId: "scan-1",+ findingCount: 1,+ dryRun: true,+ });+ expect(stderr.text()).toBe("");+ });++ test.each([+ [+ "a saved scan",+ ["--scan", "11111111", "--to", "cloud", "--csv", "findings.csv"],+ "Use --csv or scan directory and ID inputs",+ ],+ [+ "a scan directory",+ ["scan", "--to", "cloud", "--csv", "findings.csv"],+ "Use --csv or scan directory and ID inputs",+ ],+ [+ "Linear",+ ["--to", "linear", "--linear-team", "team", "--csv", "findings.csv"],+ "--csv is only supported with --to cloud",+ ],+ ])("rejects combining --csv with %s", async (_name, args, message) => {+ const deps = dependencies();+ let publications = 0;+ deps.publishFindingsCsvToCloud = async () => {+ publications++;+ return receipt;+ };+ const stderr = capture();+ expect(+ await main(+ ["publish", "scan", ...args],+ capture().stream,+ stderr.stream,+ deps,+ ),+ ).toBe(2);+ expect(publications).toBe(0);+ expect(stderr.text()).toContain(message);+ });+test("resolves IDs, prefixes, and latest before publishing and deduplicates aliases", async () => {const { scans, deps, workbenchCalls } = await savedScansFixture();const [first, second] = scans;diff --git a/sdk/typescript/tests-ts/cloud-publish.test.ts b/sdk/typescript/tests-ts/cloud-publish.test.tsindex 35290fd84..200b97613 100644--- a/sdk/typescript/tests-ts/cloud-publish.test.ts+++ b/sdk/typescript/tests-ts/cloud-publish.test.ts@@ -11,7 +11,10 @@ import {import { tmpdir } from "node:os";import { join } from "node:path";import { afterEach, describe, expect, test } from "bun:test";-import { publishScanToCloud } from "../src/cloud-publish.js";+import {+ publishFindingsCsvToCloud,+ publishScanToCloud,+} from "../src/cloud-publish.js";import {codexSecurityCredentialHome,setCodexSecurityCredentialLogout,@@ -33,6 +36,44 @@ const receipt = {finding_ids: ["finding-1"],finding_count: 1,};+const csvHeader = [+ "occurrence_id",+ "finding_id",+ "title",+ "summary",+ "severity",+ "confidence",+ "status",+ "close_reason",+ "note",+ "remediation",+ "path",+ "start_line",+ "end_line",+].join(",");+const csvRow = [+ "occ_e79cb19591e696572a1c22be",+ "csf_852f90d6e1177502ff113d4a",+ '"Unsafe archive extraction, including nested entries"',+ "An attacker-controlled path reaches a filesystem write.",+ "high",+ "high",+ "open",+ "",+ "",+ "Normalize and validate destinations.",+ "src/extract.py",+ "41",+ "44",+].join(",");++async function csvFixture(contents = `${csvHeader}\n${csvRow}\n`) {+ const root = await mkdtemp(join(tmpdir(), "codex-security-cloud-csv-"));+ directories.push(root);+ const path = join(root, "findings.csv");+ await writeFile(path, contents);+ return path;+}afterEach(async () => {await Promise.all(@@ -120,6 +161,187 @@ async function addSecondFinding(scan: string): Promise<void> {}describe("Cloud publication", () => {+ test("previews a validated findings export CSV without credentials or network access", async () => {+ const path = await csvFixture();+ let requests = 0;+ const result = await publishFindingsCsvToCloud(path, {+ dryRun: true,+ fetch: async () => {+ requests++;+ throw new Error("unexpected request");+ },+ });+ expect(result).toMatchObject({+ scanId: expect.stringMatching(/^scan_csv_[a-f0-9]{24}$/),+ findingIds: [],+ findingCount: 1,+ dryRun: true,+ findings: [+ {+ findingId: expect.stringMatching(/^csf_[a-f0-9]{24}$/),+ occurrenceId: expect.stringMatching(/^occ_[a-f0-9]{24}$/),+ title: "Unsafe archive extraction, including nested entries",+ severity: { level: "high" },+ confidence: { level: "high" },+ locations: [{ path: "src/extract.py", startLine: 41, endLine: 44 }],+ validation: {+ method: "Source-reported CSV import metadata",+ status: "open",+ summary:+ "Source finding ID: csf_852f90d6e1177502ff113d4a\nSource occurrence ID: occ_e79cb19591e696572a1c22be\nSource status: open",+ },+ provenance: { source: "csv_import" },+ },+ ],+ });+ expect(requests).toBe(0);+ });++ test("posts CSV findings with generated scan provenance", async () => {+ const path = await csvFixture();+ const { environment } = await fixture();+ let payload: Record<string, unknown> | undefined;+ const result = await publishFindingsCsvToCloud(path, {+ environment,+ fetch: async (_url, options) => {+ payload = JSON.parse(String(options.body));+ return Response.json(receipt, { status: 201 });+ },+ });+ expect(result).toEqual({+ scanId: result.scanId,+ findingIds: ["finding-1"],+ findingCount: 1,+ });+ expect(result.scanId).toMatch(/^scan_csv_[a-f0-9]{24}$/);+ expect(payload).toMatchObject({+ schemaVersion: "1.0",+ scan: {+ id: result.scanId,+ producer: { name: "codex-security-cli" },+ status: "completed",+ startedAt: "1970-01-01T00:00:00.000Z",+ target: {+ kind: "directory_snapshot",+ displayName: "findings.csv",+ },+ },+ findings: [+ {+ remediation: "Normalize and validate destinations.",+ provenance: { source: "csv_import" },+ },+ ],+ });+ const [finding] = payload!["findings"] as Array<{+ findingId: string;+ occurrenceId: string;+ }>;+ expect(finding!.findingId).toMatch(/^csf_[a-f0-9]{24}$/);+ expect(finding!.occurrenceId).toMatch(/^occ_[a-f0-9]{24}$/);+ });++ 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`,+ );+ const result = await publishFindingsCsvToCloud(path, { dryRun: true });+ expect(result.findings?.[0]?.extensions).toEqual({+ candidateId: "candidate-001",+ });+ });++ test("preserves source IDs and triage fields in finding validation", async () => {+ const path = await csvFixture(+ `${csvHeader}\n${csvRow.replace(",open,,,", ',closed,wont_fix,"Accepted risk.",')}\n`,+ );+ const result = await publishFindingsCsvToCloud(path, { dryRun: true });+ expect(result.findings?.[0]?.validation).toEqual({+ method: "Source-reported CSV import metadata",+ status: "closed",+ summary:+ "Source finding ID: csf_852f90d6e1177502ff113d4a\nSource occurrence ID: occ_e79cb19591e696572a1c22be\nSource status: closed\nSource close reason: wont_fix\nSource note: Accepted risk.",+ });+ expect(result.findings?.[0]?.provenance).toEqual({+ source: "csv_import",+ });+ });++ test("decodes exported CSV cells without stripping literal apostrophes", async () => {+ const path = await csvFixture(+ `${csvHeader}\n${csvRow+ .replace(+ '"Unsafe archive extraction, including nested entries"',+ '"\'\tTabbed title"',+ )+ .replace(+ "An attacker-controlled path reaches a filesystem write.",+ "'Literal apostrophe.",+ )+ .replace(+ "Normalize and validate destinations.",+ "' @owner should remediate.",+ )+ .replace("src/extract.py", "'-generated/extract.py")}\n`,+ );+ const result = await publishFindingsCsvToCloud(path, { dryRun: true });+ expect(result.findings?.[0]).toMatchObject({+ title: "\tTabbed title",+ summary: "'Literal apostrophe.",+ remediation: " @owner should remediate.",+ locations: [{ path: "-generated/extract.py" }],+ });+ });++ test.each([+ ["missing required header", csvHeader.replace("summary,", "")],+ ["unknown header", `${csvHeader},unknown`],+ ["no findings", csvHeader],+ ["wrong column count", `${csvHeader}\n${csvRow},extra`],+ [+ "invalid finding ID",+ `${csvHeader}\n${csvRow.replace("csf_852f90d6e1177502ff113d4a", "finding-1")}`,+ ],+ [+ "empty title",+ `${csvHeader}\n${csvRow.replace('"Unsafe archive extraction, including nested entries"', '""')}`,+ ],+ [+ "invalid severity",+ `${csvHeader}\n${csvRow.replace(",high,high,", ",urgent,high,")}`,+ ],+ [+ "invalid close reason",+ `${csvHeader}\n${csvRow.replace(",open,,,", ",closed,deferred,,")}`,+ ],+ [+ "missing required close note",+ `${csvHeader}\n${csvRow.replace(",open,,,", ",closed,false_positive,,")}`,+ ],+ [+ "unsafe path",+ `${csvHeader}\n${csvRow.replace("src/extract.py", "../escape.py")}`,+ ],+ [+ "invalid line range",+ `${csvHeader}\n${csvRow.replace(",41,44", ",45,44")}`,+ ],+ ["duplicate finding", `${csvHeader}\n${csvRow}\n${csvRow}`],+ ])("rejects a CSV with %s before authentication", async (_name, source) => {+ const path = await csvFixture(`${source}\n`);+ let requests = 0;+ await expect(+ publishFindingsCsvToCloud(path, {+ environment: {},+ fetch: async () => {+ requests++;+ throw new Error("unexpected request");+ },+ }),+ ).rejects.toThrow(/Findings CSV/);+ expect(requests).toBe(0);+ });+test.each([false, true])("rejects artifacts from another scan before upload or preview (dryRun=%s)",async (dryRun) => {