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

feat(imports): submit selected scanner findings to the private inbox (#2177)

Michael D'Angeloc26898519353
diff --git a/docs/scanner-imports/schema-guide.md b/docs/scanner-imports/schema-guide.md
index 81f494e11..3bbda2319 100644
--- a/docs/scanner-imports/schema-guide.md
+++ b/docs/scanner-imports/schema-guide.md
@@ -7,10 +7,17 @@ mean and how to interpret the result.
## Choose the record and source key
-A source record is identified by its destination repository, stored provider,
-source key and source finding ID. Use the same source key for each export of the
-same stream, such as an organization, project and branch. A date or run ID in
-the key creates a new set of records on each run.
+Private Cloud imports identify a record by its importing owner, provider, source
+key and vendor occurrence ID. Use a stable key for the vendor project or
+repository where those occurrence IDs are unique. Changing the assessment
+target preserves the record’s identity. The preview shows the format, size and
+digest of the report the CLI will upload. The receipt identifies a private job.
+Explicit assessment and publication produce a shared finding.
+
+The repository publisher identifies a record by destination repository,
+provider, source key and source finding ID. Use the same source key for each
+export of the same stream, such as an organization, project and branch. A
+date or run ID in the key creates a new set of records on each run.
The reader derives the source finding ID from the export. The differences matter
when a finding moves, an advisory affects several packages, or the export format
@@ -64,12 +71,12 @@ readers also apply the Cloud size limit to the evidence they return.
repository path from the preview. Each record includes
`sourceKey`, `sourceFindingId`, `version`, `evidence` and `assessment`.
-**Cloud.** Each item includes a `source_finding_id` and an `expected_version`.
+**Repository publisher.** Each item includes a `source_finding_id` and an `expected_version`.
Check the per-item result for conflicts. The receipt's source-report,
observation and canonical-finding IDs identify Cloud records. Read the source
report for the imported evidence.
-Cloud allows up to 256 KiB and 20 levels of nesting per finding. The publisher
+The repository publisher allows up to 256 KiB and 20 levels of nesting per finding. The publisher
batches up to 100 items and 5 MiB per request. Local file imports do not apply
these Cloud limits.
@@ -93,12 +100,17 @@ request before the response was lost.
Preview the original export with `--dry-run --format json` using the same
build, state directory, Cloud account, endpoint and import settings.
2. Check `accountId`, `destination` and `resumed` in the preview. When `resumed`
- is true, confirm the command to continue with the saved request IDs, bodies
- and expected versions. When it is false, review the fresh submission before
+ is true, confirm the command to continue the saved submission. The
+ repository publisher also preserves the original expected versions. When it is false, review the fresh submission before
confirming. If a repository reset retires the old submission and preparation
returns an error, run `--dry-run` again and review the new destination and
environment before confirming.
+For a failed or cancelled private job, a fresh preview identifies the previous
+job and confirmation starts a new attempt. A lost response from an active job
+reuses its original request IDs. After an inbox reset, review the new format,
+size and digest before confirming.
+
With the SDK, repeat the preparation call that created the submission, using
the same options. For `prepareExternalPublication()`, supply the original
export file. For `prepareNormalizedExternalPublication()`, supply the original
diff --git a/docs/scanner-imports/snyk.md b/docs/scanner-imports/snyk.md
index dfcb7da62..0a4ee3296 100644
--- a/docs/scanner-imports/snyk.md
+++ b/docs/scanner-imports/snyk.md
@@ -104,6 +104,10 @@ each format a separate key. Changes in collection context such as a plugin
scan's `issueCount` can update a stored snapshot even if the occurrence
stays the same. [Imported evidence and identity](schema-guide.md)
+For native tests of a public GitHub URL, the reader checks the repository in
+the report's `path` against the selected destination. Any `#ref` stays in the
+saved source evidence.
+
Known source Git URLs must match a comparable configured local remote when
one is available, or the selected Cloud repository. Snyk Code, Container,
IaC, SBOM results and hosted backlog responses use other formats.
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.md
index 3622cb1c6..f9979b479 100644
--- a/sdk/typescript/docs/cli.md
+++ b/sdk/typescript/docs/cli.md
@@ -1407,6 +1407,32 @@ The [Wiz guide](https://github.com/openai/codex-security/blob/main/docs/scanner-
SAST findings, repository secrets, or IaC findings. The format reference below
describes the fields to preserve when preparing those exports.
+#### Review the Cloud destination
+
+The preview shows which Cloud destination will receive the selected records.
+When the deployment advertises private import for the scanner, the CLI puts a
+report in the signed-in account’s **private findings inbox**. The preview
+identifies its format and byte count. For complete Endor findings and Wiz
+vulnerability JSON exports, the CLI sends the original bytes when Cloud
+advertises the format and every record matches the reviewed selection. Other
+selections and in-memory collectors use `security.findings.v1`. The CLI
+attaches the selected repository and connector as a suggested assessment
+target. Review the selection, exclusions, source key and destination before
+confirming.
+
+The job receipt contains the file and job identifiers and record counts. To
+share a finding, open it in Cloud, assess it against a pinned repository
+revision, and explicitly publish a reportable assessment. An import can
+finish with record errors. Check the error and unprocessed counts in the
+receipt.
+
+Repeat the same command to recover an interrupted private import. The CLI
+checks the report digest, reuses its saved request IDs, and reads an accepted
+job even when new uploads are disabled. Resetting the inbox requires a new
+preview and confirmation. Deployments and scanners using the repository
+publisher continue to show the repository destination and per-finding receipts.
+An in-flight repository publication resumes on its original protocol.
+
#### Select findings and a destination
Acquire selected findings through an authorized Wiz connection and save the
diff --git a/sdk/typescript/scripts/package-dist-files.mjs b/sdk/typescript/scripts/package-dist-files.mjs
index 0afbb6b64..0fa8bfe84 100644
--- a/sdk/typescript/scripts/package-dist-files.mjs
+++ b/sdk/typescript/scripts/package-dist-files.mjs
@@ -40,6 +40,7 @@ const distModules = [
"native-json-findings",
"opengrep-findings",
"external-findings-publish",
+ "private-finding-import",
"external-import-contract",
"external-import-models",
"external-imports-feature",
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index b9093dad8..1f4b4dee5 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -703,9 +703,16 @@ function renderExternalPublicationSummary(
result: ExternalPublicationResult,
): string {
return [
- `Import ${result.status}.`,
+ result.privateJob
+ ? `Private inbox import ${result.privateJob.status}. Job: ${diagnosticValue(result.privateJob.id)}`
+ : `Import ${result.status}.`,
`Submission totals — Created: ${result.counts.created} Updated: ${result.counts.updated} Unchanged: ${result.counts.unchanged} Failed: ${result.counts.error}`,
- `Excluded: ${result.excluded.length} Awaiting acknowledgement: ${result.unacknowledged} Verified: ${result.verified}`,
+ result.privateJob
+ ? `Excluded: ${result.excluded.length} Unprocessed: ${result.privateJob.counts.unprocessed}`
+ : `Excluded: ${result.excluded.length} Awaiting acknowledgement: ${result.unacknowledged} Verified: ${result.verified}`,
+ ...(result.privateJob?.error
+ ? [diagnosticValue(result.privateJob.error.message)]
+ : []),
...result.failures.map(
(item) =>
`Failed ${diagnosticValue(item.source_finding_id)} (${item.code}): ${diagnosticValue(item.message)}`,
@@ -721,9 +728,11 @@ function renderExternalPublicationSummary(
? []
: [
"Imported evidence remains not assessed by Codex.",
- result.cloudUrl
- ? `View Cloud repository: ${diagnosticValue(result.cloudUrl)}`
- : "Open the repository in Codex Security Cloud.",
+ result.privateJob
+ ? "Open the private inbox in Codex Security Cloud to assess a finding and choose whether to publish it."
+ : result.cloudUrl
+ ? `View Cloud repository: ${diagnosticValue(result.cloudUrl)}`
+ : "Open the repository in Codex Security Cloud.",
]),
"",
].join("\n");
@@ -3790,7 +3799,7 @@ export async function main(
const showPreview = () => {
showScannerReport(preview);
writeDiagnostic(
- `Cloud API: ${diagnosticValue(preview.cloudApiUrl)}\nAccount: ${diagnosticValue(preview.accountId)}\nDestination: ${diagnosticValue(preview.destination.url)} (${diagnosticValue(preview.destination.id)})\nEnvironment: ${[...new Set(preview.requests.map((request) => request.repository.environment_id))].map(diagnosticValue).join(", ")}\nSource: ${preview.source.provider} / ${diagnosticValue(preview.source.source_key)}\nRead: ${preview.read} Ready: ${preview.findings.length} Excluded: ${preview.excluded.length}${preview.resumed ? "\nResuming the saved submission." : ""}\n`,
+ `Cloud API: ${diagnosticValue(preview.cloudApiUrl)}\nAccount: ${diagnosticValue(preview.accountId)}\nDestination: ${diagnosticValue(preview.destination.url)} (${diagnosticValue(preview.destination.id)})\n${preview.privateImport ? `Visibility: Private inbox\nReport: ${preview.privateImport.format} (${preview.privateImport.bytes} bytes)\nReport SHA-256: ${preview.privateImport.sha256}${preview.privateImport.retryOfJobId ? `\nRetrying private job: ${diagnosticValue(preview.privateImport.retryOfJobId)}` : ""}` : `Environment: ${[...new Set(preview.requests.map((request) => request.repository.environment_id))].map(diagnosticValue).join(", ")}`}\nSource: ${preview.source.provider} / ${diagnosticValue(preview.source.source_key)}\nRead: ${preview.read} Ready: ${preview.findings.length} Excluded: ${preview.excluded.length}${preview.resumed ? "\nResuming the saved submission." : ""}\n`,
);
if (preview.cloudUrl)
writeDiagnostic(
@@ -4225,7 +4234,7 @@ export async function main(
`Cloud account: ${diagnosticValue(cloud.accountId)} on ${diagnosticValue(cloud.cloudApiUrl)}`,
);
note(
- `Cloud repository: ${diagnosticValue(cloud.destination.url)} · Environments: ${[...environments].map(diagnosticValue).join(", ")}`,
+ `Cloud repository: ${diagnosticValue(cloud.destination.url)} · ${cloud.privateImport ? `Private inbox · ${cloud.privateImport.format} (${cloud.privateImport.bytes} bytes)` : `Environments: ${[...environments].map(diagnosticValue).join(", ")}`}`,
);
note(
`Cloud: ${cloud.findings.length} ready, ${cloud.excluded.length} excluded.`,
diff --git a/sdk/typescript/src/external-findings-publish.ts b/sdk/typescript/src/external-findings-publish.ts
index 78498d6e2..c695b1ca9 100644
--- a/sdk/typescript/src/external-findings-publish.ts
+++ b/sdk/typescript/src/external-findings-publish.ts
@@ -13,6 +13,14 @@ import { createRequire } from "node:module";
import { join } from "node:path";
import { setTimeout as delay } from "node:timers/promises";
import { isBoxedPrimitive, isNativeError, isProxy } from "node:util/types";
+import {
+ privateImportClient,
+ selectPrivateReport,
+ type PrivateImportFormat,
+ readPrivateCheckpoint,
+ unsuccessfulJob,
+ type PrivateImportJob,
+} from "./private-finding-import.js";
import { readCloudCredentials } from "./cloud-publish.js";
import {
cloudBaseUrl,
@@ -134,6 +142,8 @@ export interface ExternalPublicationResult {
verified: number;
error?: string;
savedSubmission?: string;
+ /** Private jobs expose aggregate outcomes before assessment or publication. */
+ privateJob?: PrivateImportJob;
}
/** Preserve accepted work in CLI JSON output when publication cannot finish. */
@@ -173,6 +183,13 @@ export interface ExternalPublicationPreview extends ExternalFindings {
source: FindingImportRequest["source"];
resumed: boolean;
requests: FindingImportRequest[];
+ privateImport?: {
+ format: PrivateImportFormat;
+ contextVersion: string;
+ sha256: string;
+ bytes: number;
+ retryOfJobId?: string;
+ };
}
export interface PreparedExternalPublication {
@@ -469,6 +486,7 @@ export async function prepareExternalPublication(
parsed.publication ?? normalizedPublicationInput(parsed.findings),
{ ...settings, provider: EXTERNAL_FINDINGS_PROVIDERS[settings.provider] },
dependencies,
+ path,
);
}
@@ -816,6 +834,7 @@ async function preparePublicationInput(
input: PublicationSelection,
options: NormalizedExternalPublicationOptions,
dependencies: ExternalPublicationDependencies,
+ nativePath?: string,
): Promise<PreparedExternalPublication> {
const { inspectorProjectMap } = options;
const progress = (event: ExternalPublicationProgress): void =>
@@ -862,6 +881,7 @@ async function preparePublicationInput(
const credentials = await (
dependencies.credentials ?? (() => readCloudCredentials(environment))
)();
+ let privateProviders: Set<string> | undefined;
async function request(
endpoint: string,
body?: FindingImportRequest,
@@ -896,6 +916,15 @@ async function preparePublicationInput(
`Cloud import failed (HTTP ${response.status}). ${payload?.error?.message ?? response.statusText}${retryAfter ? ` Retry-After: ${retryAfter}.` : ""}`,
);
}
+ if (endpoint.startsWith("/repositories?")) {
+ const advertised = response.headers.get(
+ "Codex-Security-Private-Import-Providers",
+ );
+ if (advertised !== null)
+ privateProviders = new Set(
+ advertised.split(",").map((provider) => provider.trim()),
+ );
+ }
return response.json();
}
const destinations: ImportRepository[] = [];
@@ -1797,6 +1826,203 @@ async function preparePublicationInput(
checkRepositories(submissionFindings(saved.requests), report);
}
+ // Existing repository submissions keep their original request bodies and receipts.
+ const privatePath = join(state, `${key}.private-import.json`);
+ const privateSaved = await readOptionalFile(privatePath);
+ if (
+ !saved &&
+ (privateProviders?.has(source.provider) || privateSaved !== undefined)
+ ) {
+ const client = privateImportClient(
+ apiBaseUrl,
+ credentials,
+ dependencies.fetch ?? globalThis.fetch,
+ dependencies.signal,
+ );
+ const context = await client.context();
+ if (source.source_key.trim() !== source.source_key)
+ throw new CodexSecurityError(
+ "A private source key cannot have leading or trailing whitespace.",
+ );
+ const report = await selectPrivateReport(
+ parsed,
+ source.provider,
+ nativePath,
+ context,
+ privateSaved,
+ );
+ const binding = {
+ format: report.format,
+ apiBaseUrl,
+ accountId: credentials.account_id,
+ source,
+ repository: {
+ id: destination.id,
+ repo_connector_id: destination.repo_connector_id,
+ },
+ sha256: report.digest,
+ contextVersion: context.context_version,
+ };
+ function checkpointForContext(data: string | undefined) {
+ return data === undefined
+ ? undefined
+ : readPrivateCheckpoint(data, binding);
+ }
+ const previous = checkpointForContext(privateSaved);
+ function requireAccess(
+ value: typeof context,
+ savedFile: boolean,
+ savedJob: boolean,
+ ) {
+ if (
+ !value.can_read ||
+ (!savedJob &&
+ ((!savedFile && !value.can_upload) ||
+ !value.can_create_import_jobs ||
+ !value.formats.includes(report.format)))
+ )
+ throw new CodexSecurityError(
+ "Private findings import is unavailable for this account or report format.",
+ );
+ }
+ requireAccess(context, !!previous?.fileId, !!previous?.jobId);
+ const retryOfJobId =
+ unsuccessfulJob(previous?.receipt) &&
+ context.can_upload &&
+ context.can_create_import_jobs &&
+ context.formats.includes(report.format)
+ ? previous?.jobId
+ : undefined;
+ const privateImport = {
+ format: report.format,
+ contextVersion: binding.contextVersion,
+ sha256: report.digest,
+ bytes: Buffer.byteLength(report.bytes),
+ ...(retryOfJobId ? { retryOfJobId } : {}),
+ };
+ const preview: ExternalPublicationPreview = {
+ ...parsed,
+ cloudApiUrl: `${apiBaseUrl}/finding-imports`,
+ accountId: credentials.account_id,
+ destination,
+ source,
+ resumed: previous !== undefined && !retryOfJobId,
+ requests: [],
+ privateImport,
+ };
+ return {
+ preview: structuredClone(preview),
+ publish: () =>
+ underLocks(lockKeys, async () => {
+ const current = await client.context();
+ if (current.context_version !== binding.contextVersion)
+ throw new CodexSecurityError(
+ "The private inbox changed. Run the command again to review the new destination.",
+ );
+ const stored = checkpointForContext(
+ await readOptionalFile(privatePath),
+ );
+ // Only a preview of this terminal job can start a new attempt.
+ const retrying =
+ retryOfJobId !== undefined && stored?.receipt?.id === retryOfJobId;
+ const active = retrying ? undefined : stored;
+ requireAccess(current, !!active?.fileId, !!active?.jobId);
+ if (await selectedCheckpoint())
+ throw new CodexSecurityError(
+ "A repository submission is pending. Review its saved request before continuing.",
+ );
+ const checkpoint = active ?? {
+ ...binding,
+ uploadRequestId: randomUUID(),
+ jobRequestId: randomUUID(),
+ fileId: undefined,
+ jobId: undefined,
+ receipt: undefined,
+ previousReceipt: retrying ? stored?.receipt : undefined,
+ };
+ const result: ExternalPublicationResult = {
+ cloudApiUrl: preview.cloudApiUrl,
+ status: "interrupted",
+ read: parsed.read,
+ ready: parsed.findings.length,
+ report: parsed.report,
+ excluded: parsed.excluded,
+ receipts: [],
+ counts: { created: 0, updated: 0, unchanged: 0, error: 0 },
+ failures: [],
+ unacknowledged: parsed.findings.length,
+ verified: 0,
+ };
+ function acknowledge(job: PrivateImportJob) {
+ const { created, updated, unchanged, error } = job.counts;
+ const processed = created + updated + unchanged + error;
+ if (
+ job.counts.discovered !== processed + job.counts.unprocessed ||
+ job.counts.discovered > result.ready ||
+ (job.status === "completed" &&
+ (processed !== result.ready ||
+ job.counts.total !== result.ready))
+ )
+ throw new CodexSecurityError(
+ "Cloud returned counts that do not match the selected report.",
+ );
+ result.privateJob = job;
+ result.counts = { created, updated, unchanged, error };
+ result.unacknowledged = result.ready - processed;
+ result.status =
+ job.status === "completed" && !error && !parsed.excluded.length
+ ? "complete"
+ : "partial";
+ if (job.error) result.error = job.error.message;
+ else delete result.error;
+ }
+ try {
+ const savedReceipt =
+ checkpoint.receipt ?? checkpoint.previousReceipt;
+ if (savedReceipt) acknowledge(savedReceipt);
+ if (!active) await writeAtomicJson(privatePath, checkpoint);
+ if (!checkpoint.fileId) {
+ progress({ phase: "uploading", completed: 0 });
+ checkpoint.fileId = await client.upload({
+ requestId: checkpoint.uploadRequestId,
+ contextVersion: binding.contextVersion,
+ ...report,
+ recovering: active !== undefined,
+ });
+ await writeAtomicJson(privatePath, checkpoint);
+ }
+ progress({ phase: "verifying", completed: 0 });
+ const job = await client.job(
+ {
+ requestId: checkpoint.jobRequestId,
+ contextVersion: binding.contextVersion,
+ fileId: checkpoint.fileId,
+ format: report.format,
+ source,
+ repository: binding.repository,
+ jobId: checkpoint.jobId,
+ },
+ async (id) => {
+ checkpoint.jobId = id;
+ await writeAtomicJson(privatePath, checkpoint);
+ },
+ );
+ acknowledge(job);
+ checkpoint.jobId = job.id;
+ checkpoint.receipt = job;
+ await writeAtomicJson(privatePath, checkpoint);
+ return result;
+ } catch (error) {
+ result.status = "interrupted";
+ result.error =
+ error instanceof Error ? error.message : String(error);
+ result.savedSubmission = privatePath;
+ throw new ExternalPublicationError(result.error, result, error);
+ }
+ }),
+ };
+ }
+
let requests = saved?.requests;
if (!requests) {
let preparedCount = 0;
@@ -2017,6 +2243,10 @@ async function preparePublicationInput(
throw interruptedPreview(message);
}
if (selected) throw new CodexSecurityError(message);
+ if ((await readOptionalFile(privatePath)) !== undefined)
+ throw new CodexSecurityError(
+ "A private import already owns this selection. Review its saved job before continuing.",
+ );
if ((await readOptionalFile(pendingPath)) !== undefined)
throw occupiedCheckpoint();
}
diff --git a/sdk/typescript/src/private-finding-import.ts b/sdk/typescript/src/private-finding-import.ts
new file mode 100644
index 000000000..96f79c20c
--- /dev/null
+++ b/sdk/typescript/src/private-finding-import.ts
@@ -0,0 +1,521 @@
+import { hash } from "node:crypto";
+import { constants } from "node:fs";
+import { open } from "node:fs/promises";
+import { setTimeout as delay } from "node:timers/promises";
+import { z } from "zod";
+import { CodexSecurityError } from "./errors.js";
+import type { ExternalFinding, ExternalFindings } from "./external-findings.js";
+import type { ImportSource } from "./external-import-models.js";
+import { canonicalJson, isRecord } from "./record.js";
+
+const structuredFormat = "security.findings.v1";
+const privateFormat = z.enum([
+ structuredFormat,
+ "endor.findings_json.v1",
+ "wiz.vulnerability_json.v1",
+]);
+export type PrivateImportFormat = z.infer<typeof privateFormat>;
+const fileId = z.string().regex(/^aifile_[0-9a-f]{32}$/u);
+const contextSchema = z.object({
+ object: z.literal("security.finding_import_context"),
+ context_version: z.string().min(1),
+ can_upload: z.boolean(),
+ can_create_import_jobs: z.boolean(),
+ can_read: z.boolean(),
+ formats: z.array(z.string()),
+ limits: z.object({
+ max_direct_file_bytes: z.int().positive(),
+ max_record_bytes: z.int().positive(),
+ }),
+});
+const fileSchema = z.object({
+ id: fileId,
+ object: z.literal("security.finding_import_file"),
+ purpose: z.literal("report"),
+ sha256: z.string(),
+ bytes: z.int().positive(),
+});
+const jobSchema = z.object({
+ object: z.literal("security.finding_import_job"),
+ id: z.string().regex(/^import_[0-9a-f]{32}$/u),
+ request_id: z.uuid(),
+ source: z.object({ provider: z.string(), source_key: z.string() }),
+ input: z.object({
+ type: z.literal("file"),
+ file_id: fileId,
+ format: privateFormat,
+ }),
+ default_targets: z.array(
+ z.object({
+ type: z.literal("repository"),
+ repo_id: z.string(),
+ repo_connector_id: z.string(),
+ }),
+ ),
+ status: z.enum([
+ "queued",
+ "in_progress",
+ "cancelling",
+ "completed",
+ "failed",
+ "cancelled",
+ ]),
+ counts: z.object({
+ total: z.int().nonnegative().nullable(),
+ discovered: z.int().nonnegative(),
+ created: z.int().nonnegative(),
+ updated: z.int().nonnegative(),
+ unchanged: z.int().nonnegative(),
+ error: z.int().nonnegative(),
+ unprocessed: z.int().nonnegative(),
+ }),
+ poll_after_seconds: z.int().positive().nullable(),
+ error: z.object({ message: z.string() }).nullable(),
+});
+
+export type PrivateImportContext = z.infer<typeof contextSchema>;
+export type PrivateImportJob = z.infer<typeof jobSchema>;
+
+export function privateReport(
+ findings: ExternalFinding[],
+ limits: PrivateImportContext["limits"],
+) {
+ if (findings.length > 10_000)
+ throw new CodexSecurityError(
+ "A structured report accepts up to 10,000 findings. Split this selection.",
+ );
+ const records = findings.map(({ source_finding_id, evidence }) => {
+ if (source_finding_id !== source_finding_id.trim())
+ throw new CodexSecurityError(
+ "A private source finding ID cannot have leading or trailing whitespace.",
+ );
+ const record = {
+ source_finding_id,
+ evidence: {
+ ...evidence,
+ severity: evidence.severity === "unknown" ? null : evidence.severity,
+ locations: evidence.locations?.map(({ path, line }, index) => ({
+ path,
+ start_line: line,
+ ...(index === 0 && evidence.details?.code?.end_line != null
+ ? { end_line: evidence.details.code.end_line }
+ : {}),
+ })),
+ },
+ };
+ if (Buffer.byteLength(canonicalJson(record)) > limits.max_record_bytes)
+ throw new CodexSecurityError(
+ `Finding ${JSON.stringify(source_finding_id)} exceeds the private import record limit.`,
+ );
+ return record;
+ });
+ const bytes = canonicalJson({ format: structuredFormat, findings: records });
+ if (Buffer.byteLength(bytes) > limits.max_direct_file_bytes)
+ throw new CodexSecurityError(
+ "The selected report exceeds the Cloud direct-upload limit. Split this selection.",
+ );
+ return {
+ format: structuredFormat as PrivateImportFormat,
+ bytes,
+ digest: hash("sha256", bytes),
+ };
+}
+
+const endorExport = z.strictObject({
+ list: z.strictObject({
+ objects: z.array(z.unknown()),
+ response: z
+ .strictObject({
+ next_page_id: z.literal(["", null]).optional(),
+ next_page_token: z.literal(["", "0", 0, null]).optional(),
+ total_size: z.union([z.string(), z.number()]).optional(),
+ })
+ .optional(),
+ }),
+});
+const wizExport = z.strictObject({
+ vulnerabilityFindings: z.strictObject({
+ nodes: z.array(z.unknown()),
+ pageInfo: z
+ .strictObject({
+ hasNextPage: z.literal(false).optional(),
+ hasPreviousPage: z.literal(false).optional(),
+ startCursor: z.string().nullable().optional(),
+ endCursor: z.string().nullable().optional(),
+ })
+ .optional(),
+ }),
+});
+
+// Only complete, known envelopes can be sent byte for byte. Unreviewed sibling
+// collections, metadata or partial pages keep the normalized projection.
+function nativeRecords(
+ payload: unknown,
+ provider: string,
+): unknown[] | undefined {
+ if (provider === "wiz" && Array.isArray(payload)) return payload;
+ if (!isRecord(payload)) return;
+ if (provider === "endor") {
+ if (isRecord(payload["spec"])) return [payload];
+ return endorExport.safeParse(payload).data?.list.objects;
+ }
+ if (typeof payload["id"] === "string") return [payload];
+ const data =
+ Object.keys(payload).length === 1 && "data" in payload
+ ? payload["data"]
+ : payload;
+ return wizExport.safeParse(data).data?.vulnerabilityFindings.nodes;
+}
+
+export async function selectPrivateReport(
+ selection: ExternalFindings,
+ provider: string,
+ path: string | undefined,
+ context: PrivateImportContext,
+ saved: string | undefined,
+): Promise<ReturnType<typeof privateReport>> {
+ const checkpoint =
+ saved === undefined ? undefined : checkpointSchema.parse(JSON.parse(saved));
+ const originalFormat =
+ checkpoint?.contextVersion === context.context_version
+ ? checkpoint.format
+ : undefined;
+ const format =
+ provider === "endor"
+ ? "endor.findings_json.v1"
+ : provider === "wiz"
+ ? "wiz.vulnerability_json.v1"
+ : undefined;
+ if (
+ format &&
+ path &&
+ selection.excluded.length === 0 &&
+ selection.read <= 10_000 &&
+ selection.findings.length === selection.read &&
+ (originalFormat
+ ? originalFormat === format
+ : context.formats.includes(format))
+ ) {
+ const handle = await open(
+ path,
+ constants.O_RDONLY | (constants.O_NONBLOCK ?? 0),
+ );
+ let bytes: string | undefined;
+ try {
+ const info = await handle.stat();
+ if (
+ info.isFile() &&
+ info.size > 0 &&
+ info.size <= context.limits.max_direct_file_bytes
+ ) {
+ const buffer = Buffer.alloc(info.size + 1);
+ let count = 0;
+ while (count < buffer.length) {
+ const { bytesRead } = await handle.read(
+ buffer,
+ count,
+ buffer.length - count,
+ );
+ if (bytesRead === 0) break;
+ count += bytesRead;
+ }
+ if (count === info.size) {
+ try {
+ bytes = new TextDecoder("utf-8", {
+ fatal: true,
+ ignoreBOM: true,
+ }).decode(buffer.subarray(0, count));
+ } catch {
+ /* Binary exports use the normalized selection. */
+ }
+ }
+ }
+ } finally {
+ await handle.close();
+ }
+ if (bytes !== undefined) {
+ let payload: unknown;
+ try {
+ payload = JSON.parse(bytes.replace(/^\uFEFF/u, ""));
+ } catch {
+ /* JSONL and other exports use the normalized selection. */
+ }
+ const records = nativeRecords(payload, provider);
+ if (
+ records &&
+ records.length === selection.findings.length &&
+ records.every((raw, index) => {
+ const reviewed = selection.findings[index]!;
+ if (!isRecord(raw)) return false;
+ const record = canonicalJson(raw);
+ return (
+ raw[provider === "endor" ? "uuid" : "id"] ===
+ reviewed.source_finding_id &&
+ reviewed.source_finding_id === reviewed.source_finding_id.trim() &&
+ reviewed.evidence.source_data != null &&
+ record === canonicalJson(reviewed.evidence.source_data) &&
+ Buffer.byteLength(record) <= context.limits.max_record_bytes
+ );
+ })
+ )
+ return { format, bytes, digest: hash("sha256", bytes) };
+ }
+ }
+ if (originalFormat && originalFormat !== structuredFormat)
+ throw new CodexSecurityError(
+ "The original native report is required to resume this private import.",
+ );
+ return privateReport(selection.findings, context.limits);
+}
+
+const checkpointSchema = z.object({
+ format: privateFormat.default(structuredFormat),
+ apiBaseUrl: z.string(),
+ accountId: z.string(),
+ contextVersion: z.string(),
+ sha256: z.string(),
+ source: z.object({ provider: z.string(), source_key: z.string() }),
+ repository: z.object({ id: z.string(), repo_connector_id: z.string() }),
+ uploadRequestId: z.uuid(),
+ jobRequestId: z.uuid(),
+ fileId: fileId.optional(),
+ jobId: jobSchema.shape.id.optional(),
+ receipt: jobSchema
+ .refine((job) => job.status === "completed" || unsuccessfulJob(job))
+ .optional(),
+ previousReceipt: jobSchema.refine(unsuccessfulJob).optional(),
+});
+
+export function unsuccessfulJob(job: PrivateImportJob | undefined) {
+ return job?.status === "failed" || job?.status === "cancelled";
+}
+
+export function readPrivateCheckpoint(
+ serialized: string,
+ expected: {
+ format: PrivateImportFormat;
+ apiBaseUrl: string;
+ accountId: string;
+ contextVersion: string;
+ sha256: string;
+ source: ImportSource;
+ repository: { id: string; repo_connector_id: string };
+ },
+) {
+ const checkpoint = checkpointSchema.parse(JSON.parse(serialized));
+ const {
+ uploadRequestId,
+ jobRequestId,
+ fileId,
+ jobId,
+ receipt,
+ previousReceipt,
+ contextVersion,
+ format,
+ sha256,
+ ...scope
+ } = checkpoint;
+ const {
+ contextVersion: currentContext,
+ format: selectedFormat,
+ sha256: selectedDigest,
+ ...selectedScope
+ } = expected;
+ const sameContext = contextVersion === currentContext;
+ if (
+ canonicalJson(scope) !== canonicalJson(selectedScope) ||
+ (sameContext && (format !== selectedFormat || sha256 !== selectedDigest)) ||
+ (jobId && !fileId) ||
+ [receipt, previousReceipt].some(
+ (job) =>
+ job &&
+ (job.input.format !== format ||
+ canonicalJson(job.source) !== canonicalJson(scope.source) ||
+ job.default_targets.length !== 1 ||
+ job.default_targets[0]?.repo_id !== scope.repository.id ||
+ job.default_targets[0]?.repo_connector_id !==
+ scope.repository.repo_connector_id),
+ ) ||
+ (receipt &&
+ (receipt.id !== jobId ||
+ receipt.request_id !== jobRequestId ||
+ receipt.input.file_id !== fileId)) ||
+ uploadRequestId === jobRequestId
+ )
+ throw new CodexSecurityError(
+ "Saved private import does not match this account, repository or report.",
+ );
+ return sameContext ? checkpoint : undefined;
+}
+
+export class PrivateImportRequestError extends CodexSecurityError {
+ constructor(
+ readonly status: number,
+ readonly code: string | undefined,
+ message: string,
+ ) {
+ super(message);
+ }
+}
+
+export function privateImportClient(
+ base: string,
+ credentials: { access_token: string; account_id: string },
+ fetch: (url: string, options: RequestInit) => Promise<Response>,
+ signal?: AbortSignal,
+) {
+ async function request(path: string, body?: FormData | string) {
+ const timeout = AbortSignal.timeout(body === undefined ? 30_000 : 60_000);
+ const response = await fetch(`${base}/finding-imports/${path}`, {
+ method: body === undefined ? "GET" : "POST",
+ body,
+ redirect: "error",
+ headers: {
+ Authorization: `Bearer ${credentials.access_token}`,
+ "ChatGPT-Account-ID": credentials.account_id,
+ Accept: "application/json",
+ ...(typeof body === "string"
+ ? { "Content-Type": "application/json" }
+ : {}),
+ },
+ signal: signal ? AbortSignal.any([signal, timeout]) : timeout,
+ });
+ if (!response.ok) {
+ const payload = await response.json().catch(() => undefined);
+ const error = z
+ .object({
+ error: z.object({ code: z.string(), message: z.string().optional() }),
+ })
+ .safeParse(payload);
+ const detail = error.data?.error;
+ throw new PrivateImportRequestError(
+ response.status,
+ detail?.code,
+ `Private import failed (HTTP ${response.status}): ${detail?.message ?? detail?.code ?? response.statusText}`,
+ );
+ }
+ return response;
+ }
+ return {
+ async context() {
+ return contextSchema.parse(await (await request("context")).json());
+ },
+ async upload(input: {
+ requestId: string;
+ contextVersion: string;
+ bytes: string;
+ digest: string;
+ recovering: boolean;
+ }) {
+ let response: Response | undefined;
+ if (input.recovering) {
+ try {
+ response = await request(`file-requests/${input.requestId}`);
+ } catch (error) {
+ if (
+ !(error instanceof PrivateImportRequestError) ||
+ (error.status !== 404 &&
+ !(error.status === 409 && error.code === "upload_retryable"))
+ )
+ throw error;
+ }
+ }
+ if (!response) {
+ const form = new FormData();
+ form.set("request_id", input.requestId);
+ form.set("context_version", input.contextVersion);
+ form.set("purpose", "report");
+ form.set(
+ "file",
+ new Blob([input.bytes], { type: "application/json" }),
+ "findings.json",
+ );
+ response = await request("files", form);
+ }
+ const file = fileSchema.parse(await response.json());
+ if (
+ file.sha256 !== input.digest ||
+ file.bytes !== Buffer.byteLength(input.bytes)
+ )
+ throw new CodexSecurityError(
+ "The uploaded file does not match the reviewed report.",
+ );
+ return file.id;
+ },
+ async job(
+ input: {
+ requestId: string;
+ contextVersion: string;
+ fileId: string;
+ format: PrivateImportFormat;
+ source: ImportSource;
+ repository: { id: string; repo_connector_id: string };
+ jobId?: string;
+ },
+ accepted: (id: string) => Promise<void>,
+ ) {
+ let id = input.jobId;
+ let response = id
+ ? await request(`jobs/${id}`)
+ : await request(
+ "jobs",
+ JSON.stringify({
+ request_id: input.requestId,
+ context_version: input.contextVersion,
+ source: input.source,
+ input: {
+ type: "file",
+ file_id: input.fileId,
+ format: input.format,
+ },
+ default_targets: [
+ {
+ type: "repository",
+ repo_id: input.repository.id,
+ repo_connector_id: input.repository.repo_connector_id,
+ },
+ ],
+ }),
+ );
+ for (;;) {
+ const job = jobSchema.parse(await response.json());
+ if (
+ (id !== undefined && job.id !== id) ||
+ job.request_id !== input.requestId ||
+ job.input.file_id !== input.fileId ||
+ job.input.format !== input.format ||
+ job.source.provider !== input.source.provider ||
+ job.source.source_key !== input.source.source_key ||
+ job.default_targets.length !== 1 ||
+ job.default_targets[0]?.repo_id !== input.repository.id ||
+ job.default_targets[0]?.repo_connector_id !==
+ input.repository.repo_connector_id
+ )
+ throw new CodexSecurityError(
+ "Cloud returned a job for a different private import.",
+ );
+ if (
+ job.status === "completed" ||
+ job.status === "failed" ||
+ job.status === "cancelled"
+ )
+ return job;
+ if (!id) {
+ id = job.id;
+ await accepted(id);
+ }
+ const retry = response.headers.get("Retry-After");
+ const seconds = retry === null ? 0 : Number(retry);
+ await delay(
+ Math.max(
+ Number.isFinite(seconds) ? seconds : 0,
+ job.poll_after_seconds ?? 1,
+ ) * 1000,
+ undefined,
+ { signal },
+ );
+ response = await request(`jobs/${id}`);
+ }
+ },
+ };
+}
diff --git a/sdk/typescript/src/snyk-findings.ts b/sdk/typescript/src/snyk-findings.ts
index 9360e1660..a9b1b17f9 100644
--- a/sdk/typescript/src/snyk-findings.ts
+++ b/sdk/typescript/src/snyk-findings.ts
@@ -212,6 +212,24 @@ export function snykRepositoryUrls(
: repository,
];
}
+ const project = source["project"];
+ if (
+ isRecord(project) &&
+ typeof project["ok"] === "boolean" &&
+ text(project["packageManager"]) &&
+ isRecord(source["vulnerability"])
+ ) {
+ // A remote Snyk test puts its GitHub URL and optional ref in the same
+ // path field that local scans use for a directory or npm package name.
+ const repository = text(project["path"])?.split("#", 1)[0];
+ if (
+ repository &&
+ /^https?:\/\/github\.com(?::\d+)?\/[^/?#\\\s]+\/[^/?#\\\s]+\/?$/iu.test(
+ repository,
+ )
+ )
+ return [repository];
+ }
// Generic normalized evidence may use these field names for opaque metadata.
const document = source["document"];
const driver = source["driver"];
diff --git a/sdk/typescript/src/sonar-import.ts b/sdk/typescript/src/sonar-import.ts
index 7888609af..71ad71f96 100644
--- a/sdk/typescript/src/sonar-import.ts
+++ b/sdk/typescript/src/sonar-import.ts
@@ -68,6 +68,7 @@ function cloudReviewDigest(
url: preview.destination.url,
reset: preview.destination.reset_marker,
source: preview.source,
+ privateImport: preview.privateImport,
items: preview.requests.flatMap((request) =>
request.items.map((item) => [
item.source_finding_id,
diff --git a/sdk/typescript/tests-ts/external-findings-publish.test.ts b/sdk/typescript/tests-ts/external-findings-publish.test.ts
index a4f4a9cff..ecf21f9d6 100644
--- a/sdk/typescript/tests-ts/external-findings-publish.test.ts
+++ b/sdk/typescript/tests-ts/external-findings-publish.test.ts
@@ -11,6 +11,7 @@ import {
} from "./support/opengrep-import-fixtures.js";
import {
normalizeSnykFindingsExport,
+ normalizeSnykJson,
normalizeSnykSarif,
snykRepositoryUrls,
} from "../src/snyk-findings.js";
@@ -60,6 +61,10 @@ import {
normalizeGitHubCodeScanningAlerts,
readGitHubFindings,
} from "../src/github-findings.js";
+import {
+ privateReport,
+ selectPrivateReport,
+} from "../src/private-finding-import.js";
import { readVendorFindings } from "../src/wiz-findings.js";
import {
cloudRepositoryUrlKey,
@@ -6125,6 +6130,32 @@ for (const [provider, report, normalize, dryRunTitle] of [
);
}
+test("native Snyk remote tests bind file and normalized imports to the reported GitHub repository", async () => {
+ const input = snykJson();
+ input.path = "https://github.com/other/project#release/v1";
+ const f = await fixture(input);
+ const selected = { ...options, provider: "snyk" as const };
+ await expect(
+ prepareExternalPublication(f.file, selected, f.deps),
+ ).rejects.toThrow("does not match the selected Cloud repository");
+ await expect(
+ prepareNormalizedExternalPublication(
+ normalizeSnykJson(input),
+ selected,
+ f.deps,
+ ),
+ ).rejects.toThrow("does not match the selected Cloud repository");
+ expect(f.posts).toHaveLength(0);
+
+ input.path = "https://github.com/example/project.git#release/v1";
+ await writeFile(f.file, JSON.stringify(input));
+ const prepared = await prepareExternalPublication(f.file, selected, f.deps);
+ expect((await prepared.publish()).verified).toBe(1);
+ expect([...f.reports.values()][0]!.evidence.source_data).toMatchObject({
+ project: { path: input.path },
+ });
+});
+
test("Snyk source repository binding is rechecked for a fresh import after completion", async () => {
const input: any = snykSarif();
input.runs[0].versionControlProvenance = [
@@ -7528,3 +7559,416 @@ test("the GitHub reader resumes a released boxed score whose Buffer fit the Clou
expect(completed.receipts.map(({ id }) => id)).toEqual([request.request_id]);
expect(f.posts.map((body) => hash("sha256", body))).toEqual([bodyHash]);
});
+
+test.each([
+ { provider: "snyk", format: "security.findings.v1", raw: snykSarif() },
+ {
+ provider: "endor",
+ format: "endor.findings_json.v1",
+ raw: { list: { objects: [endorFinding()] } },
+ },
+ {
+ provider: "wiz",
+ format: "wiz.vulnerability_json.v1",
+ raw: [
+ {
+ id: "occurrence-1",
+ name: "CVE-2099-0001",
+ detailedName: "example-package",
+ vendorSeverity: "HIGH",
+ vulnerableAsset: { id: "asset-1" },
+ },
+ ],
+ },
+] as const)(
+ "a signed-in $provider private import resumes lost upload and job responses",
+ async ({ provider, format, raw }) => {
+ const f = await fixture(raw);
+ const originalBytes = await readFile(f.file, "utf8");
+ const settings = { ...options, provider };
+ const repositoryPreview = await prepareExternalPublication(
+ f.file,
+ settings,
+ f.deps,
+ );
+ f.state.environmentId = null;
+ let contextVersion = "generation-a";
+ let formats = [
+ "security.findings.v1",
+ "endor.findings_json.v1",
+ "wiz.vulnerability_json.v1",
+ ];
+ let expectedFormat: typeof format = format;
+ let loseResponses = true;
+ let unavailable = false;
+ let terminal: "completed" | "failed" | "cancelled" = "completed";
+ let advertised = true;
+ let canCreate = true;
+ let fileRequest: string | undefined;
+ let fileHash: string | undefined;
+ let fileBytes = 0;
+ let jobRequest: string | undefined;
+ const fileId = "aifile_" + "a".repeat(32);
+ let jobId = "import_" + "b".repeat(32);
+ const calls: string[] = [];
+ const fileMetadata = () => ({
+ object: "security.finding_import_file",
+ id: fileId,
+ purpose: "report",
+ sha256: fileHash,
+ bytes: fileBytes,
+ });
+ const job = (status: string) => ({
+ object: "security.finding_import_job",
+ id: jobId,
+ request_id: jobRequest,
+ source: { provider, source_key: settings.sourceKey },
+ input: { type: "file", file_id: fileId, format: expectedFormat },
+ default_targets: [
+ {
+ type: "repository",
+ repo_id: options.repository,
+ repo_connector_id: "github",
+ },
+ ],
+ status,
+ counts: {
+ total: 1,
+ discovered: 1,
+ created: status === "completed" ? 1 : 0,
+ updated: 0,
+ unchanged: 0,
+ error: 0,
+ unprocessed: status === "completed" ? 0 : 1,
+ },
+ poll_after_seconds: null,
+ error: null,
+ });
+ const deps = {
+ ...f.deps,
+ fetch: async (url: string, init: RequestInit) => {
+ const path = new URL(url).pathname.replace("/backend-api/aardvark", "");
+ expect(new Headers(init.headers).get("Authorization")).toBe(
+ "Bearer synthetic-token",
+ );
+ expect(new Headers(init.headers).get("ChatGPT-Account-ID")).toBe(
+ "synthetic-account",
+ );
+ expect(init.redirect).toBe("error");
+ calls.push(`${init.method} ${path}`);
+ if (path === "/external/repositories") {
+ const response = await f.deps.fetch(url, init);
+ return new Response(response.body, {
+ headers: advertised
+ ? {
+ "Codex-Security-Private-Import-Providers":
+ "snyk,github,endor,wiz",
+ }
+ : {},
+ });
+ }
+ if (path === "/finding-imports/context")
+ return Response.json({
+ object: "security.finding_import_context",
+ context_version: contextVersion,
+ can_upload: canCreate,
+ can_create_import_jobs: canCreate,
+ can_read: true,
+ formats,
+ limits: {
+ max_direct_file_bytes: 32 * 1024 * 1024,
+ max_record_bytes: 1024 * 1024,
+ },
+ });
+ if (path === "/finding-imports/files") {
+ const form = init.body as FormData;
+ expect(form.get("context_version")).toBe(contextVersion);
+ expect(form.get("purpose")).toBe("report");
+ fileRequest = String(form.get("request_id"));
+ const bytes = await (form.get("file") as Blob).text();
+ if (expectedFormat === "security.findings.v1") {
+ const doc = JSON.parse(bytes);
+ expect(doc.format).toBe(expectedFormat);
+ expect(doc.findings[0].source_finding_id).toBeTruthy();
+ expect(doc.findings[0].evidence.source_data).toBeTruthy();
+ } else {
+ expect(bytes).toBe(originalBytes);
+ }
+ fileHash = hash("sha256", bytes);
+ fileBytes = Buffer.byteLength(bytes);
+ if (loseResponses) throw new Error("lost upload response");
+ return Response.json(fileMetadata());
+ }
+ if (path === `/finding-imports/file-requests/${fileRequest}`)
+ return Response.json(fileMetadata());
+ if (path === "/finding-imports/jobs") {
+ const body = JSON.parse(String(init.body));
+ expect(body.request_id).not.toBe(fileRequest);
+ expect(body.input).toEqual({
+ type: "file",
+ file_id: fileId,
+ format: expectedFormat,
+ });
+ expect(body.default_targets).toEqual([
+ {
+ type: "repository",
+ repo_id: options.repository,
+ repo_connector_id: "github",
+ },
+ ]);
+ if (!loseResponses) {
+ expect(body.request_id).not.toBe(jobRequest);
+ jobRequest = body.request_id;
+ jobId =
+ terminal === "completed"
+ ? "import_" + "d".repeat(32)
+ : "import_" + "c".repeat(32);
+ return Response.json(job(terminal));
+ }
+ if (!jobRequest) {
+ jobRequest = body.request_id;
+ throw new Error("lost job response");
+ }
+ expect(body.request_id).toBe(jobRequest);
+ return Response.json(job("queued"), {
+ headers: { "Retry-After": "0" },
+ });
+ }
+ if (path === `/finding-imports/jobs/${jobId}`)
+ return unavailable
+ ? new Response(null, { status: 503 })
+ : Response.json(job(terminal));
+ throw new Error("Unexpected route: " + path);
+ },
+ };
+ const prepare = () => prepareExternalPublication(f.file, settings, deps);
+ const original = await prepare();
+ expect(original.preview.privateImport?.sha256).toBeTruthy();
+ expect(original.preview.privateImport?.format).toBe(format);
+ const cli = createCliTest(main);
+ const dryRun = [...f.command, "--dry-run"];
+ dryRun[dryRun.indexOf("--provider") + 1] = provider;
+ dryRun[dryRun.indexOf("--format") + 1] = "toon";
+ expect(
+ await cli.runCli(dryRun, { ...f.cliDeps, cloudFetch: deps.fetch }),
+ ).toBe(0);
+ expect(cli.stderr.text()).toContain(
+ `Report: ${format} (${original.preview.privateImport?.bytes} bytes)`,
+ );
+ expect(calls.filter((call) => call.startsWith("POST"))).toEqual([]);
+ contextVersion = "generation-b";
+ await expect(original.publish()).rejects.toThrow("private inbox changed");
+ canCreate = false;
+ await expect(prepare()).rejects.toThrow("unavailable");
+ canCreate = true;
+ await expect((await prepare()).publish()).rejects.toThrow(
+ "lost upload response",
+ );
+ await expect(repositoryPreview.publish()).rejects.toThrow(
+ "A private import already owns this selection",
+ );
+ expect(f.posts).toHaveLength(0);
+ advertised = false;
+ await expect((await prepare()).publish()).rejects.toThrow(
+ "lost job response",
+ );
+ const completed = await (await prepare()).publish();
+ expect(completed.privateJob?.counts.created).toBe(1);
+ expect(completed.receipts).toEqual([]);
+ expect(completed.verified).toBe(0);
+ expect(
+ calls.filter((call) => call === "POST /finding-imports/files"),
+ ).toHaveLength(1);
+ canCreate = false;
+ expect((await (await prepare()).publish()).privateJob?.id).toBe(jobId);
+ expect(calls.at(-1)).toBe(`GET /finding-imports/jobs/${jobId}`);
+ unavailable = true;
+ const acknowledged = {
+ result: {
+ status: "interrupted",
+ counts: { created: 1 },
+ unacknowledged: 0,
+ privateJob: { id: jobId },
+ },
+ };
+ await expect((await prepare()).publish()).rejects.toMatchObject(
+ acknowledged,
+ );
+ unavailable = false;
+
+ // The same context retains its native wire representation even if discovery changes.
+ formats = ["security.findings.v1"];
+ expect((await prepare()).preview.privateImport?.format).toBe(format);
+ contextVersion = "generation-c";
+ canCreate = true;
+ loseResponses = false;
+ expectedFormat = "security.findings.v1";
+ terminal = provider === "wiz" ? "cancelled" : "failed";
+ const fresh = await prepare();
+ expect(fresh.preview.resumed).toBe(false);
+ expect(fresh.preview.privateImport?.format).toBe(expectedFormat);
+ expect((await fresh.publish()).privateJob?.status).toBe(terminal);
+ const failedJob = jobId;
+ const retry = await prepare();
+ const secondPreview = await prepare();
+ expect(retry.preview.resumed).toBe(false);
+ expect(retry.preview.privateImport?.retryOfJobId).toBe(failedJob);
+
+ // A terminal response still acknowledges the report if saving its receipt fails.
+ terminal = "completed";
+ const rename = fs.rename;
+ const fault = spyOn(fs, "rename").mockImplementation(async (from, to) => {
+ if (
+ String(to).endsWith(".private-import.json") &&
+ JSON.parse(await readFile(from, "utf8")).receipt?.status === "completed"
+ )
+ throw new Error("Checkpoint interrupted");
+ return rename(from, to);
+ });
+ try {
+ await expect(retry.publish()).rejects.toMatchObject({
+ result: {
+ status: "interrupted",
+ counts: { created: 1 },
+ unacknowledged: 0,
+ privateJob: { status: "completed" },
+ },
+ });
+ } finally {
+ fault.mockRestore();
+ }
+ // A second preview of the failed job replays the already-started replacement.
+ loseResponses = true;
+ const uploads = calls.filter(
+ (call) => call === "POST /finding-imports/files",
+ ).length;
+ expect((await secondPreview.publish()).privateJob?.id).not.toBe(failedJob);
+ expect(
+ calls.filter((call) => call === "POST /finding-imports/files"),
+ ).toHaveLength(uploads);
+ },
+);
+
+test("a private preview recognizes a later repository checkpoint under its historical filename", async () => {
+ const f = await fixture();
+ const privatePreview = await prepareNormalizedExternalPublication(
+ scoredFindings(2.5),
+ options,
+ {
+ ...f.deps,
+ fetch: async (url, init) => {
+ expect(init.method).toBe("GET");
+ if (new URL(url).pathname.endsWith("/repositories")) {
+ const response = await f.deps.fetch(url, init);
+ return new Response(response.body, {
+ headers: { "Codex-Security-Private-Import-Providers": "wiz" },
+ });
+ }
+ expect(new URL(url).pathname).toEndWith("/finding-imports/context");
+ return Response.json({
+ object: "security.finding_import_context",
+ context_version: "generation-a",
+ can_read: true,
+ can_upload: true,
+ can_create_import_jobs: true,
+ formats: ["security.findings.v1"],
+ limits: { max_direct_file_bytes: 32768, max_record_bytes: 8192 },
+ });
+ },
+ },
+ );
+ const repository = await prepareNormalizedExternalPublication(
+ scoredFindings(new Number(2.5)),
+ options,
+ f.deps,
+ );
+ f.state.throttle = true;
+ await expect(repository.publish()).rejects.toThrow("Slow down");
+ await expect(privatePreview.publish()).rejects.toThrow(
+ "A repository submission is pending",
+ );
+ expect(f.posts).toHaveLength(1);
+});
+
+test("a private structured report retains occurrence IDs, unknown severity and source lines", () => {
+ const input = [
+ {
+ source_finding_id: "original-occurrence",
+ evidence: {
+ title: "Synthetic code finding",
+ severity: "unknown" as const,
+ locations: [
+ { path: "src/sample.ts", line: 7 },
+ { path: "src/other.ts", line: 10 },
+ ],
+ details: { kind: "sast" as const, code: { end_line: 9 } },
+ source_data: { occurrence: "original-occurrence" },
+ },
+ },
+ ];
+ const limits = { max_record_bytes: 4096, max_direct_file_bytes: 8192 };
+ const report = privateReport(input, limits);
+ const record = JSON.parse(report.bytes).findings[0];
+ expect(record.source_finding_id).toBe(input[0]!.source_finding_id);
+ expect(record.evidence.severity).toBeNull();
+ expect(record.evidence.locations).toEqual([
+ { path: "src/sample.ts", start_line: 7, end_line: 9 },
+ { path: "src/other.ts", start_line: 10 },
+ ]);
+ expect(record.evidence.source_data).toEqual(input[0]!.evidence.source_data);
+ expect(privateReport(input, limits).digest).toBe(report.digest);
+ expect(() =>
+ privateReport(input, { ...limits, max_record_bytes: 32 }),
+ ).toThrow("record limit");
+});
+
+test("filtered, unsupported and changed files never upload an unreviewed native record", async () => {
+ const raw = endorFinding();
+ const f = await fixture(raw);
+ const selection = normalizeEndorFindings(raw);
+ const context = {
+ object: "security.finding_import_context" as const,
+ context_version: "v1",
+ can_upload: true,
+ can_create_import_jobs: true,
+ can_read: true,
+ formats: ["security.findings.v1", "endor.findings_json.v1"],
+ limits: {
+ max_direct_file_bytes: 32 * 1024 * 1024,
+ max_record_bytes: 1024 * 1024,
+ },
+ };
+ const select = (findings = selection, formats = context.formats) =>
+ selectPrivateReport(
+ findings,
+ "endor",
+ f.file,
+ { ...context, formats },
+ undefined,
+ );
+ expect((await select()).format).toBe("endor.findings_json.v1");
+ expect((await select(selection, ["security.findings.v1"])).format).toBe(
+ "security.findings.v1",
+ );
+ expect(
+ (
+ await select({
+ ...selection,
+ read: 2,
+ excluded: [
+ { position: 2, source_finding_id: null, reason: "excluded" },
+ ],
+ })
+ ).format,
+ ).toBe("security.findings.v1");
+ await writeFile(
+ f.file,
+ JSON.stringify({ list: { objects: [raw] }, unreviewed: "omitted" }),
+ );
+ expect((await select()).bytes).not.toContain("unreviewed");
+ await writeFile(
+ f.file,
+ JSON.stringify({ ...raw, meta: { ...raw.meta, name: "unreviewed" } }),
+ );
+ expect((await select()).bytes).not.toContain("unreviewed");
+});
diff --git a/sdk/typescript/tests-ts/snyk-findings-local.test.ts b/sdk/typescript/tests-ts/snyk-findings-local.test.ts
index 95fcaa45b..5a56ad116 100644
--- a/sdk/typescript/tests-ts/snyk-findings-local.test.ts
+++ b/sdk/typescript/tests-ts/snyk-findings-local.test.ts
@@ -66,7 +66,7 @@ test.each(["plugin", "JSON", "SARIF"] as const)(
},
);
-test.each(["plugin", "SARIF"] as const)(
+test.each(["plugin", "JSON", "SARIF"] as const)(
"Snyk %s repository provenance cannot be imported into a different local checkout",
async (format) => {
await using fixture = await localReportFixture(
@@ -78,15 +78,20 @@ test.each(["plugin", "SARIF"] as const)(
const input =
format === "plugin"
? snykPluginExport()
- : {
- ...sarif,
- runs: sarif.runs.map((run) => ({
- ...run,
- versionControlProvenance: [
- { repositoryUri: "https://github.com/example/project" },
- ],
- })),
- };
+ : format === "JSON"
+ ? {
+ ...snykJson(),
+ path: "https://github.com/example/project#release",
+ }
+ : {
+ ...sarif,
+ runs: sarif.runs.map((run) => ({
+ ...run,
+ versionControlProvenance: [
+ { repositoryUri: "https://github.com/example/project" },
+ ],
+ })),
+ };
await fixture.writeJson(input);
await expect(fixture.prepare()).rejects.toThrow(/repository/iu);
expect(await fixture.read()).toEqual([]);