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): publish hosted scanner findings to Cloud (#2085)

Michael D'Angelo48549a524d77
diff --git a/docs/scanner-imports/schema-guide.md b/docs/scanner-imports/schema-guide.md
index 5ddf4de99..158fb6608 100644
--- a/docs/scanner-imports/schema-guide.md
+++ b/docs/scanner-imports/schema-guide.md
@@ -119,6 +119,24 @@ export file. For `prepareNormalizedExternalPublication()`, supply the original
`ExternalFindings`. Check `preview.resumed` before calling `publish()`.
Use the same SDK version throughout a submission when possible.
+Older SDK checkpoints saved the destination repository ID without the catalog
+URL. After a repository rename, retry those requests with the original
+normalized findings and report metadata, omitting supplemental source
+repository declarations. A new declaration needs a matching current catalog
+entry or a URL recorded when the request was approved.
+
+If a hosted Semgrep or Snyk request is pending, rerun the original input to
+finish it before submitting updated evidence for any of the same findings.
+Selections with distinct scanner-issued IDs can proceed independently on the
+same deployment, account, repository, connector, provider and source key.
+When an older checkpoint establishes only the source, finish that request
+before submitting another selection for that source. Use the SDK build that
+created the request and include the original report context and caller-added
+fields. Older SDKs included some fields in the checkpoint filename without
+retaining them in the saved request. If the publisher cannot identify the
+deployment of an older request, rerun it on its original deployment. An
+identified repository reset retires the request and requires fresh approval.
+
After an SDK upgrade, retry with the original input. An older SDK could approve
different bytes from those its JSON export contained. When the SDK verifies
the old selection and request bytes, it resumes with the original request IDs
diff --git a/docs/scanner-imports/semgrep.md b/docs/scanner-imports/semgrep.md
index 174e76878..c3add8c39 100644
--- a/docs/scanner-imports/semgrep.md
+++ b/docs/scanner-imports/semgrep.md
@@ -192,6 +192,23 @@ native finding. If unrelated SDK metadata uses the same v1 fields, move it to
another key such as `source_data.ticket`, or submit those rows in an ordinary
normalized batch without the Platform report marker or scope.
+To publish the selection to Cloud, use the same file and source key:
+
+```sh
+codex-security publish findings selected.json --to cloud --provider semgrep \
+ --repository https://github.com/example/project --source-key example/project/release/platform --dry-run
+```
+
+The publisher checks the original project's repository against the authorized destination, including
+Supply Chain findings with no typed repository field. After reviewing the
+preview, repeat without `--dry-run`.
+
+New marked approvals that include caller-managed rows record the collection
+scope for retry. On older approvals, the publisher checks the repository and
+any scope retained by the original native records. A caller-managed row can
+lack the original collection scope. Changing supplemental display metadata on
+a retry cannot change the approved request bytes, account or destination.
+
## Collect a Platform project and branch
With a Web API token in `SEMGREP_APP_TOKEN`, collect all findings returned for a
@@ -229,13 +246,20 @@ report=$(mktemp ./semgrep-backlog.XXXXXX) &&
--provider semgrep --source-key example/project/topic --dry-run --format json
```
+To preview this saved backlog in Cloud, keep its source key and choose the authorized repository.
+
+```sh
+codex-security publish findings "$report" --to cloud --provider semgrep \
+ --repository https://github.com/example/project --source-key example/project/topic --dry-run
+```
+
Use `--semgrep-product sca` for Supply Chain or `ai_sast` for AI-powered
detection. The default is `sast`. Use `--source-repository-url` on collection
-when the project has no repository URL. In that case the report records the mapping in
+when the project has no repository URL. The report records that mapping in
`sourceRepositoryUrl`, and stored finding evidence retains a null native project URL.
-For SSH projects, the report keeps the original address in `nativeProjectUrl` alongside the resolved HTTP(S) address in `project.url`, and records any supplied mapping separately.
-Save the complete output from a successful
-command; the local importer checks the recorded finding count and repository/ref
+For SSH projects, the report stores the original address in `nativeProjectUrl` and the resolved HTTP(S) address in `project.url`.
+It records any supplied mapping separately. Save the complete output from a successful
+command. The importer checks the recorded finding count and repository/ref
claims. It retains each original finding as JSON text so large IDs survive replay. The reader also accepts saved v1 project exports with native object rows and decimal type witnesses.
To import a collected backlog with the TypeScript SDK, pass the normalized
diff --git a/docs/scanner-imports/snyk.md b/docs/scanner-imports/snyk.md
index 1dc3e4b2a..16d32b003 100644
--- a/docs/scanner-imports/snyk.md
+++ b/docs/scanner-imports/snyk.md
@@ -56,8 +56,11 @@ To select a subset in the SDK, filter the normalized report’s `findings`
array. Keep the report’s `snykRestContext` and each selected finding’s
`source_finding_id` and `evidence.source_data`. Every hosted row must retain
the native issue and its generated ID. The verified report context can supply
-the monitored project for a row that retains only its original issue. For a v1
-row, keep `source_data.issue` and the original `source_data.numericFields`
+the monitored project for a row that retains only its original issue. The Cloud
+preview includes the verified project and any captured target in the evidence it
+will publish.
+
+For a v1 row, keep `source_data.issue` and the original `source_data.numericFields`
when present. If you trim the metadata, keep its matching entries for every
`issue`, `project`, and `target` object you retain. V1 uses those entries to
distinguish exact numbers from strings with the same spelling. For a v2 row,
@@ -79,7 +82,8 @@ remotes. For a checkout with only filesystem remotes or none, the selected
directory is the import destination.
For private source-control hosts, use the same repository URL as the checkout.
SDK callers that have independently verified a GitHub Enterprise or GitLab
-repository can pass its canonical HTTPS URL and platform to the local importer.
+repository can pass its canonical HTTPS URL and platform to local or Cloud
+preparation. Cloud checks that source against the selected repository.
This allows that repository's standard SSH clone URL and `.git` suffix. For
example, a GitLab repository uses
`sourceRepositories: [{ url: sourceRepositoryUrl, platform: "gitlab" }]`
@@ -87,6 +91,22 @@ in the SDK dependencies. Use `platform: "github"` for GitHub Enterprise.
When reading the saved file directly, pass the same verified sources as the
third argument to `readSnykFindings(path, "local", sourceRepositories)`.
+To preview the saved issues in Cloud, use the same source key and an authorized repository:
+
+```sh
+codex-security publish findings "$report" --to cloud --provider snyk \
+ --source-key ORG_ID/PROJECT_ID --repository https://github.com/example/project --dry-run
+```
+
+Review the preview and repeat without `--dry-run` to publish. The original Snyk
+project must match the selected Cloud repository. If Cloud accepted a request
+but the response was lost, repeat the same command to resume the saved request.
+
+For a new normalized SDK import, keep the original Snyk issue and project
+evidence alongside its generated finding ID. If you build a projection without
+that evidence, supply your own stable finding ID and the source repository URL.
+An already approved, pending request resumes with its saved evidence.
+
## Quick start
[Enable external imports](README.md#preview-a-report). For a first local import,
diff --git a/plugins/codex-security/skills/publish-findings/SKILL.md b/plugins/codex-security/skills/publish-findings/SKILL.md
index 23be6607e..d8b90c414 100644
--- a/plugins/codex-security/skills/publish-findings/SKILL.md
+++ b/plugins/codex-security/skills/publish-findings/SKILL.md
@@ -74,7 +74,7 @@ Use a stable Wiz tenant/finding namespace as `--source-key`; a project filter do
## Snyk findings
-For an existing monitored Open Source or Code project, follow the [Snyk Issues API import guide](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/snyk.md#existing-issues-from-snyk). With a read-authorized `SNYK_TOKEN`, run `codex-security import snyk PROJECT_ID --snyk-org ORG_ID --source-repository-url URL --format json`. Add `--snyk-product code` for a Code project and `--snyk-api-origin` for a regional deployment. Save the complete successful JSON output, preview with `publish findings --to local --provider snyk`, then confirm the local import.
+For an existing monitored Open Source or Code project, follow the [Snyk Issues API import guide](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/snyk.md#existing-issues-from-snyk). With a read-authorized `SNYK_TOKEN`, run `codex-security import snyk PROJECT_ID --snyk-org ORG_ID --source-repository-url URL --format json`. Add `--snyk-product code` for a Code project and `--snyk-api-origin` for a regional deployment. Save the complete successful JSON output, preview with `publish findings --provider snyk` and the requested `--to local` or `--to cloud` destination, then confirm the import.
Prefer the [official Snyk Codex plugin](https://github.com/snyk/studio-recipes/tree/main/plugins/codex) when available. Ask its `snyk-findings-export` skill for **SCA only from the repository’s Git root** and save the `snyk-findings-export/v1` bundle. It scans the current workspace through local MCP; it cannot list existing platform projects or retrieve earlier findings. Do not substitute a fresh scan for a request to read a platform backlog. Authentication, relevant Snyk access and the project's package tooling are required.
@@ -96,7 +96,7 @@ For a selection of existing Code or Supply Chain findings in Codex, use the sign
Save a private JSON capture outside the checkout with `format: "semgrep-guardian-selection/v1"`, the verified `sourceRepositoryUrl`, the MCP `request` (tool `name` and `arguments`), the exact selected string `findingIds`, and `structuredContent: { findings: [...] }` containing the original selected tool rows. Match IDs exactly and keep the tool's repository, ref and evidence. Guardian returns a repository name, so confirm its mapping to the selected checkout URL. Use the [Guardian preview command](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/semgrep.md#import-selected-guardian-findings) and confirm the selection locally. Guardian handles sign-in for this workflow. Guardian and Web API IDs remain separate even when the strings match.
-For a whole project and ref, use the [Web API collector](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/semgrep.md#collect-a-platform-project-and-branch) with its read-capable token. If the user supplies [saved Web API responses](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/semgrep.md#existing-platform-findings), use the IDs from those records.
+For a whole project and ref, use the [Web API collector](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/semgrep.md#collect-a-platform-project-and-branch) with its read-capable token. If the user supplies [saved Web API responses](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/semgrep.md#existing-platform-findings), use the IDs from those records. Preview Web API findings against the requested local or Cloud repository.
Import the native report with `--provider semgrep`, a stable organization/project/branch `--source-key`, and `--source-repository-url` identifying the actual source repository. Use `--to local --repository /path/to/project` for local storage or `--to cloud --repository URL_OR_ID` for Cloud. The source URL is checked against configured local Git remotes or the Cloud destination. Preview with `--dry-run --format json` before confirming the import.
diff --git a/sdk/typescript/README.md b/sdk/typescript/README.md
index edd993ca3..3c80a057e 100644
--- a/sdk/typescript/README.md
+++ b/sdk/typescript/README.md
@@ -709,7 +709,7 @@ Use `normalizeSelectedSemgrepPlatformFindings` for selected SAST findings from s
Semgrep Web API responses. See the [Semgrep SDK example](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/semgrep.md#import-selected-semgrep-platform-sast-findings-with-the-sdk).
Use `selectSemgrepPlatformFindings` to [save a selection for the local CLI](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/semgrep.md#existing-platform-findings).
Use `collectSemgrepPlatformFindings` to [collect one project at an explicit ref](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/semgrep.md#collect-a-platform-project-and-branch) for a single product (default `sast`).
-Use `import snyk` to [collect existing Open Source or Code issues](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/snyk.md#existing-issues-from-snyk) for local import.
+Use `import snyk` to [collect existing Open Source or Code issues](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/snyk.md#existing-issues-from-snyk) for local import or Cloud publication.
For a private repository with a verified SCM, pass its URL and `platform: "gitlab"` or `"github"` in `sourceRepositories` to match standard SSH clone URLs.
This does not affect scan publishing or the existing GitHub alert collector.
diff --git a/sdk/typescript/docs/cli.md b/sdk/typescript/docs/cli.md
index 45712e17d..773585a57 100644
--- a/sdk/typescript/docs/cli.md
+++ b/sdk/typescript/docs/cli.md
@@ -1364,7 +1364,7 @@ Use `publish findings FILE --provider semgrep --to local` for a
or a [saved Semgrep Web API selection](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/semgrep.md#existing-platform-findings).
Guardian uses its existing sign-in. Its imported IDs are separate from Web API IDs.
To [collect a full Semgrep project and branch](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/semgrep.md#collect-a-platform-project-and-branch), use
-`import semgrep PROJECT --semgrep-deployment SLUG --semgrep-ref REF [--semgrep-product sast|sca|ai_sast]`.
+`import semgrep PROJECT --semgrep-deployment SLUG --semgrep-ref REF [--semgrep-product sast|sca|ai_sast]`. Both saved formats support local and Cloud import.
For completed scans exported in the Codex Security JSON or CSV format, use
[scan import](#import-findings-as-a-saved-scan).
@@ -1459,7 +1459,7 @@ available, links to the destination repository in Cloud.
For an existing monitored Snyk project, use
`codex-security import snyk PROJECT_ID --snyk-org ORG_ID --source-repository-url URL --format json`
-and save its output for `publish findings --to local --provider snyk`. Set
+and save its output for `publish findings --provider snyk` with `--to local` or `--to cloud`. Set
`SNYK_TOKEN` with read access. The default product is Open Source. Select
`--snyk-product code` for Code and `--snyk-api-origin` for a regional API
(default `https://api.snyk.io`). See the [hosted import example](https://github.com/openai/codex-security/blob/main/docs/scanner-imports/snyk.md#existing-issues-from-snyk).
diff --git a/sdk/typescript/src/cli.ts b/sdk/typescript/src/cli.ts
index 92bc12c3e..5fcb31cd2 100644
--- a/sdk/typescript/src/cli.ts
+++ b/sdk/typescript/src/cli.ts
@@ -4215,7 +4215,7 @@ export async function main(
imports.command("snyk", {
description:
"Read Snyk Open Source or Code issues for one monitored project.",
- hint: "Requires SNYK_TOKEN. Save complete --format json output and import it with publish findings --to local --provider snyk.",
+ hint: "Requires SNYK_TOKEN. Save the complete --format json output. Use publish findings --provider snyk with --to local or --to cloud.",
destructive: false,
mcp: false,
args: z.object({
@@ -4276,7 +4276,7 @@ export async function main(
imports.command("semgrep", {
description:
"Read a full Semgrep Platform project and ref through the Web API.",
- hint: "Requires SEMGREP_APP_TOKEN with Web API access. Import the saved --format json output or a selected Guardian MCP capture with publish findings --to local --provider semgrep.",
+ hint: "Requires SEMGREP_APP_TOKEN with Web API access. Import the saved --format json output with publish findings --provider semgrep and --to local or --to cloud. Use --to local for a selected Guardian MCP capture.",
destructive: false,
mcp: false,
args: z.object({
diff --git a/sdk/typescript/src/external-findings-publish.ts b/sdk/typescript/src/external-findings-publish.ts
index 1640cfcfb..ea693fe80 100644
--- a/sdk/typescript/src/external-findings-publish.ts
+++ b/sdk/typescript/src/external-findings-publish.ts
@@ -33,11 +33,6 @@ import {
cloudFindingsUrl,
DEFAULT_CLOUD_BASE_URL,
} from "./cloud-endpoint.js";
-import {
- isSemgrepPlatformEvidence,
- semgrepPlatformReportRepository,
-} from "./semgrep-platform-findings.js";
-import { isSnykRestFinding, isSnykRestReport } from "./snyk-rest-findings.js";
import { CodexSecurityError, errorMessage } from "./errors.js";
import { requireExternalImports } from "./external-imports-feature.js";
import {
@@ -48,6 +43,7 @@ import {
import {
EXTERNAL_FINDINGS_PROVIDERS,
readExternalFindings,
+ semgrepSarifRepositories,
type ExternalFindingsReaderOptions,
} from "./external-findings-read.js";
import {
@@ -60,7 +56,41 @@ import { wizCsvRepositoryUrl } from "./wiz-csv.js";
import { wizFindingRepository } from "./wiz-findings.js";
import { previousReaderSelection } from "./reader-selection.js";
import { dependabotRepository } from "./github-dependabot-findings.js";
-import { snykRepositoryUrls } from "./snyk-findings.js";
+import {
+ normalizeSemgrepFindings,
+ readSemgrepReport,
+} from "./semgrep-findings.js";
+import {
+ normalizeSnykFile,
+ readSnykReport,
+ snykRepositoryUrls,
+} from "./snyk-findings.js";
+import {
+ type SnykIssuesFindings,
+ isSnykRestFinding,
+ isSnykRestFormat,
+ snykRestCheckpointFindingId,
+ snykRestImportRepository,
+ snykRestRepositoryMatches,
+} from "./snyk-rest-findings.js";
+import {
+ hasRetainedSemgrepScope,
+ isSemgrepPlatformEvidence,
+ isSemgrepPlatformFormat,
+ semgrepPlatformFormat,
+ parseSemgrepResponse,
+ semgrepLegacyRepositoryWitnesses,
+ semgrepNativeProjectUrl,
+ semgrepPlatformFindingBinding,
+ semgrepPlatformRepositoryReference,
+ semgrepPlatformReportScope,
+} from "./semgrep-platform-findings.js";
+import { semgrepV1Id } from "./semgrep-platform-v1.js";
+import {
+ hostedRepositoryUrlKey,
+ normalizeHostedGitRepositoryUrl,
+} from "./hosted-git-repository.js";
+import { hostedRepositoryUrl } from "./hosted-scanner-http.js";
import { endorRepositoryUrls } from "./endor-findings.js";
import {
blackDuckReportRepositories,
@@ -71,6 +101,8 @@ import { veracodeReportRepositories } from "./veracode-findings.js";
import { inspectorReportRepository } from "./inspector-findings.js";
import { sonarRepositoryUrls } from "./sonar-findings.js";
import {
+ snapshotSourceRepositories,
+ type ExternalSourceRepository,
snapshotImportOptions,
cloneImportPreview,
nativeErrorStackGetter,
@@ -80,6 +112,7 @@ import {
type ExternalPublicationInput,
} from "./external-findings.js";
import {
+ canonicalNumber,
cloudRepositoryUrlKey,
CloudEvidenceDepthError,
validateImportRequest,
@@ -120,6 +153,8 @@ export interface ExternalPublicationOptions extends Omit<
/** Optional transport, account, cancellation, and progress hooks. */
export interface ExternalPublicationDependencies {
+ /** Independently verified source repositories for hosted Semgrep or Snyk evidence. */
+ sourceRepositories?: readonly ExternalSourceRepository[];
environment?: NodeJS.ProcessEnv;
fetch?: (url: string, options: RequestInit) => Promise<Response>;
credentials?: () => Promise<{ access_token: string; account_id: string }>;
@@ -175,6 +210,8 @@ interface SavedSubmission {
requests: FindingImportRequest[];
receipts?: FindingImportReceipt[];
inputProjection?: {
+ retainedHostedSelection?: true;
+ semgrepPartialScopeDigest?: string;
identityFormat?: 2;
inputDigest: string;
savedEvidenceDigest: string;
@@ -185,6 +222,9 @@ interface SavedSubmission {
legacyApproval?: true;
// The older canonical file was checked under its lock for a second approval.
canonicalChecked?: true;
+ /** Catalog URL when this source selection was approved, before any repository rename. */
+ approvedRepositoryUrl?: string;
+ approvedRepositoryPlatform?: "other";
inspectorProjectMapRequired?: true;
}
@@ -487,8 +527,32 @@ async function withImportLocks<T>(
}
}
-// A caller's serializer may deliberately omit an ID accessor. Inspect stored
-// IDs here and check the actual exported rows after normalization.
+type ResolvedPublicationInput = PublicationSelection & {
+ recheckFileRepository?: (
+ approvedUrl: string,
+ platform?: "other",
+ ) => Promise<unknown>;
+};
+
+type PreparePublicationInput =
+ | ResolvedPublicationInput
+ | ((destination: ImportRepository) => Promise<ResolvedPublicationInput>);
+
+function hostedSourceReferences(
+ readerSources: readonly ExternalSourceRepository[],
+ url?: string,
+ sourcePlatform?: string,
+): readonly ExternalSourceRepository[] {
+ // The catalog supplies a GitHub identity only when the reader has no SCM mapping.
+ const sources =
+ readerSources.length || !url
+ ? readerSources
+ : [{ url, platform: "github" as const }];
+ return sourcePlatform === "other"
+ ? sources.filter(({ platform }) => platform !== "github")
+ : sources;
+}
+
function storedFindingId(finding: ExternalFinding): string {
const seen = new Set<object>();
for (
@@ -511,38 +575,19 @@ function storedFindingId(finding: ExternalFinding): string {
return "";
}
-function rejectLocalScannerReports(
- input: ExternalFindings,
- provider: string,
-): void {
- if (!input || (typeof input !== "object" && typeof input !== "function"))
- return;
- const stored = (row: ExternalFinding) => ({
- source_finding_id: storedFindingId(row),
- evidence: row.evidence,
- });
- const isPlatform = (row: ExternalFinding) => {
- if (!row) return false;
- const finding = stored(row);
- return (
- /^semgrep-guardian:[0-9a-f]{64}$/u.test(finding.source_finding_id) ||
- ownData(ownData(finding.evidence, "source_data"), "format") ===
- guardianSelectionFormat ||
- isSemgrepPlatformEvidence(finding, provider)
- );
- };
- const hasPlatformRows = (rows: ExternalFinding[]) =>
- Array.isArray(rows) && rows.some(isPlatform);
+function rejectGuardianSelection(input: ExternalFindings): void {
+ const isGuardian = (row: ExternalFinding) =>
+ row &&
+ (/^semgrep-guardian:[0-9a-f]{64}$/u.test(storedFindingId(row)) ||
+ ownData(ownData(row.evidence, "source_data"), "format") ===
+ guardianSelectionFormat);
if (
ownData(ownData(input, "report"), "format") === guardianSelectionFormat ||
- semgrepPlatformReportRepository(input, provider, isPlatform) ||
- hasPlatformRows(input.findings)
+ (Array.isArray(input?.findings) && input.findings.some(isGuardian))
)
throw new CodexSecurityError(
- "Semgrep Platform findings require a local import.",
+ "Semgrep Guardian findings require a local import.",
);
- if (isSnykRestReport(input, (row) => row && isSnykRestFinding(stored(row))))
- throw new CodexSecurityError("Snyk REST findings require a local import.");
}
export async function prepareExternalPublication(
@@ -552,13 +597,87 @@ export async function prepareExternalPublication(
): Promise<PreparedExternalPublication> {
requireExternalImports(dependencies.environment);
const settings = snapshotImportOptions(options);
- const parsed = await readExternalFindings(path, settings);
- rejectLocalScannerReports(parsed.findings, settings.provider);
+ const readerSources = snapshotSourceRepositories(
+ dependencies.sourceRepositories,
+ );
+ const provider = EXTERNAL_FINDINGS_PROVIDERS[settings.provider];
+ let input: PreparePublicationInput;
+ if (settings.provider === "semgrep" || settings.provider === "snyk") {
+ const semgrep = settings.provider === "semgrep";
+ const report = await (semgrep
+ ? readSemgrepReport(path)
+ : readSnykReport(path));
+ const normalize = async (url?: string, platform?: "other") => {
+ const references = hostedSourceReferences(
+ readerSources,
+ url,
+ semgrep ? platform : undefined,
+ );
+ const findings = semgrep
+ ? normalizeSemgrepFindings(
+ report,
+ {
+ sourceRepositoryUrl: settings.sourceRepositoryUrl,
+ sourceRepositories: references,
+ },
+ "cloud",
+ )
+ : await normalizeSnykFile(report, "cloud", references);
+ return normalizedPublicationInput(findings, provider);
+ };
+ const hosted =
+ isRecord(report) &&
+ (semgrep
+ ? isSemgrepPlatformFormat(report["format"])
+ : isSnykRestFormat(report["format"]));
+ input = hosted
+ ? async (destination) => {
+ try {
+ const projection = await normalize(destination.url);
+ if (!semgrep) return projection;
+ return {
+ ...projection,
+ recheckFileRepository: async (approvedUrl, platform) => {
+ if (platform === "other")
+ await normalize(approvedUrl, platform);
+ },
+ };
+ } catch (error) {
+ const project = report["project"];
+ const candidate =
+ report["sourceRepositoryUrl"] ??
+ settings.sourceRepositoryUrl ??
+ (semgrep && isRecord(project) ? project["url"] : undefined);
+ if (typeof candidate !== "string" || candidate === destination.url)
+ throw error;
+ // A renamed repository can require its old spelling to identify the saved request.
+ // Only an approved checkpoint can later authorize that spelling.
+ const projection = await normalize(
+ normalizeHostedGitRepositoryUrl(candidate, destination.url, true),
+ );
+ return {
+ ...projection,
+ recheckFileRepository: (approvedUrl, platform) => {
+ if (approvedUrl === destination.url) throw error;
+ return normalize(approvedUrl, platform);
+ },
+ };
+ }
+ }
+ : await normalize();
+ } else {
+ const parsed = await readExternalFindings(path, settings);
+ rejectGuardianSelection(parsed.findings);
+ input =
+ parsed.publication ??
+ normalizedPublicationInput(parsed.findings, provider);
+ }
return preparePublicationInput(
- parsed.publication ??
- normalizedPublicationInput(parsed.findings, settings.provider),
- { ...settings, provider: EXTERNAL_FINDINGS_PROVIDERS[settings.provider] },
+ input,
+ { ...settings, provider },
dependencies,
+ readerSources,
+ settings.sourceRepositoryUrl,
path,
);
}
@@ -571,12 +690,206 @@ export async function prepareNormalizedExternalPublication(
): Promise<PreparedExternalPublication> {
requireExternalImports(dependencies.environment);
const settings = snapshotImportOptions(options);
- rejectLocalScannerReports(input, settings.provider);
+ rejectGuardianSelection(input);
return preparePublicationInput(
normalizedPublicationInput(input, settings.provider),
settings,
dependencies,
+ snapshotSourceRepositories(dependencies.sourceRepositories),
+ );
+}
+
+function cloudSourceKey(
+ url: string,
+ platform: ExternalSourceRepository["platform"],
+) {
+ return hostedRepositoryUrlKey(
+ url,
+ platform === "github",
+ platform === "gitlab",
+ "cloud",
+ );
+}
+
+/** Verify every source in a hosted selection, including SDK inputs containing typed-only rows. */
+function hostedRepositoryMatcher(
+ input: ExternalFindings & Partial<SnykIssuesFindings>,
+ provider: NormalizedExternalPublicationOptions["provider"],
+ declaredUrl: string | undefined,
+ destination: Pick<ImportRepository, "url">,
+ readerSources: readonly ExternalSourceRepository[],
+ allowOpaqueLegacyRows = false,
+): { github: boolean; matches: (url: string) => boolean } | undefined {
+ const references = hostedSourceReferences(
+ readerSources,
+ destination.url,
+ input.sourceRepositoryPlatform,
+ );
+ if (provider === "snyk") {
+ const declaredSource =
+ input.sourceRepositoryUrl !== undefined ||
+ input.sourceRepositoryPlatform !== undefined ||
+ declaredUrl !== undefined ||
+ readerSources.length > 0;
+ const binding = snykRestImportRepository(
+ input,
+ provider,
+ [
+ ...readerSources.map(({ url }) => url),
+ ...(declaredUrl === undefined ? [] : [declaredUrl]),
+ ],
+ references,
+ undefined,
+ allowOpaqueLegacyRows,
+ declaredSource ? destination.url : undefined,
+ );
+ if (binding)
+ return {
+ github: binding.platform === "github",
+ matches: (url) => snykRestRepositoryMatches(binding, url),
+ };
+ }
+ if (provider !== "semgrep") return;
+
+ const declared =
+ input.sourceRepositoryUrl === undefined
+ ? undefined
+ : hostedRepositoryUrl(
+ input.sourceRepositoryUrl,
+ "Semgrep source repository",
+ );
+ const reportScope = semgrepPlatformReportScope(input, provider, references);
+ const hasNativeSource =
+ reportScope !== undefined ||
+ input.findings.some((finding) =>
+ isSemgrepPlatformEvidence(finding, provider),
+ );
+ const claims = new Set<string>();
+ let original: string | undefined;
+ const nativeRows = [];
+ for (const finding of input.findings) {
+ const typed = finding.evidence.details?.repository?.url;
+ if (typed) claims.add(typed);
+ const data = finding.evidence.source_data;
+ const sarif = semgrepSarifRepositories(finding, hasNativeSource);
+ for (const { url } of sarif)
+ claims.add(hostedRepositoryUrl(url, "Semgrep source repository"));
+ const project = data?.["project"];
+ if (
+ !allowOpaqueLegacyRows ||
+ !isSemgrepPlatformEvidence(finding, provider) ||
+ (isRecord(project) &&
+ project["id"] != null &&
+ project["name"] != null &&
+ data?.["deployment"] != null &&
+ data["ref"] != null &&
+ data["product"] != null &&
+ ((isRecord(data["finding"]) && data["finding"]["id"] != null) ||
+ typeof data["semgrepPlatformFindingJson"] === "string"))
+ ) {
+ nativeRows.push({ finding, sarif });
+ continue;
+ }
+ // Released SDKs could save partial native records. Retain any source still
+ // identifiable on that row while validating complete siblings under the new mapping.
+ if (!data) continue;
+ if (reportScope?.projectId !== undefined) {
+ const id = isRecord(project) ? project["id"] : undefined;
+ if (
+ (id != null &&
+ id !== reportScope.projectId &&
+ semgrepV1Id(id) !== reportScope.projectId) ||
+ (["deployment", "ref", "product"] as const).some(
+ (field) => data[field] != null && data[field] !== reportScope[field],
+ )
+ )
+ throw new CodexSecurityError(
+ "The retained Semgrep finding does not match the supplied report scope.",
+ );
+ }
+ const rows = [data["finding"]];
+ const raw = data["semgrepPlatformFindingJson"];
+ if (typeof raw === "string") {
+ try {
+ rows.push(parseSemgrepResponse(raw));
+ } catch {
+ // An unreadable historical row contributes no URL. Check the remaining claims.
+ }
+ }
+ const nativeUrls = [isRecord(project) ? project["url"] : undefined];
+ if (data["format"] === semgrepPlatformFormat)
+ nativeUrls.push(data["nativeProjectUrl"]);
+ for (const row of rows) {
+ if (!isRecord(row)) continue;
+ if (
+ reportScope?.ref !== undefined &&
+ row["ref"] !== undefined &&
+ row["ref"] !== reportScope.ref
+ )
+ throw new CodexSecurityError(
+ "The retained Semgrep finding does not match the supplied report scope.",
+ );
+ const repository = row["repository"];
+ if (isRecord(repository)) nativeUrls.push(repository["url"]);
+ }
+ const urls = nativeUrls.map((url) =>
+ semgrepNativeProjectUrl(url, references),
+ );
+ for (const url of [...urls, data["sourceRepositoryUrl"]]) {
+ if (typeof url !== "string") continue;
+ original ??= url;
+ claims.add(url);
+ }
+ }
+ const witnesses = semgrepLegacyRepositoryWitnesses(
+ nativeRows.map(({ finding }) => finding),
+ provider,
+ references,
+ "cloud",
);
+ for (const { finding, sarif } of nativeRows) {
+ const native = semgrepPlatformFindingBinding(
+ finding,
+ provider,
+ references,
+ witnesses,
+ reportScope,
+ [{ url: projectedBlackDuckRepositoryUrl(finding, "cloud") }, ...sarif],
+ "cloud",
+ allowOpaqueLegacyRows,
+ )?.url;
+ if (native) {
+ original ??= native;
+ claims.add(native);
+ }
+ }
+ const source =
+ original ??
+ declared ??
+ declaredUrl ??
+ (readerSources.length || input.sourceRepositoryPlatform !== undefined
+ ? destination.url
+ : undefined);
+ if (source === undefined) return;
+ const { platform } = semgrepPlatformRepositoryReference(source, references);
+ const expected = cloudSourceKey(source, platform);
+ const matches = (url: string) => cloudSourceKey(url, platform) === expected;
+ if (declared !== undefined) claims.add(declared);
+ if (declaredUrl !== undefined) claims.add(declaredUrl);
+ for (const reader of readerSources)
+ claims.add(
+ normalizeHostedGitRepositoryUrl(
+ reader.url,
+ source,
+ reader.platform !== undefined,
+ ),
+ );
+ for (const url of claims)
+ if (!matches(hostedRepositoryUrl(url, "Semgrep source repository")))
+ throw new CodexSecurityError(
+ "The finding does not match the original Semgrep project repository.",
+ );
+ return { github: platform === "github", matches };
}
interface PublicationSelection extends ExternalPublicationInput {
@@ -757,8 +1070,8 @@ function historicalPublication(
}
function normalizedPublicationInput(
- input: ExternalFindings,
- provider: string,
+ input: ExternalFindings & Partial<SnykIssuesFindings>,
+ provider: NormalizedExternalPublicationOptions["provider"],
): PublicationSelection {
const nativeDiagnostics = new Map<number, Error>();
let native: ExternalFindings | undefined;
@@ -790,6 +1103,43 @@ function normalizedPublicationInput(
freshError = error;
current = { read: native.read, findings: [], excluded: [] };
}
+ // Repository and format declarations can be non-enumerable SDK getters.
+ for (const key of [
+ "sourceFormat",
+ "semgrepPlatformScope",
+ "snykRestContext",
+ ...(provider === "semgrep" || provider === "snyk"
+ ? ["sourceRepositoryUrl", "sourceRepositoryPlatform"]
+ : []),
+ ]) {
+ if (
+ !(key in current) &&
+ !Object.prototype.propertyIsEnumerable.call(input, key) &&
+ key in input
+ )
+ Object.assign(current, {
+ [key]: structuredClone(Reflect.get(input, key)),
+ });
+ }
+ const parsed: ExternalFindings & Partial<SnykIssuesFindings> = current;
+ if (parsed.snykRestContext !== undefined) {
+ if (provider !== "snyk")
+ throw new CodexSecurityError(
+ "Snyk REST evidence requires the snyk provider.",
+ );
+ if (!isSnykRestFormat(parsed.snykRestContext?.["format"]))
+ throw new CodexSecurityError(
+ "Expected a versioned Snyk REST report context.",
+ );
+ }
+ const markedSemgrep =
+ "sourceFormat" in parsed && isSemgrepPlatformFormat(parsed.sourceFormat);
+ if (markedSemgrep && provider !== "semgrep")
+ throw new CodexSecurityError(
+ "Semgrep Platform evidence requires the semgrep provider.",
+ );
+ if (markedSemgrep || "semgrepPlatformScope" in parsed)
+ semgrepPlatformReportScope(parsed, provider);
if (native && nativeDiagnostics.size) {
const historical = native.report!["errors"] as Error[];
const exported = current.report?.["errors"];
@@ -833,7 +1183,7 @@ function normalizedPublicationInput(
: undefined;
if (reader) native = reader.snapshot;
const original = historicalPublication(native);
- if (original) rejectLocalScannerReports(original.parsed, provider);
+ if (original) rejectGuardianSelection(original.parsed);
try {
validatePublicationFindings(current, validateExternalEvidence);
} catch (error) {
@@ -846,7 +1196,7 @@ function normalizedPublicationInput(
// Only the old selection can be reviewed when fresh validation failed.
current = { read: original.parsed.read, findings: [], excluded: [] };
}
- rejectLocalScannerReports(current, provider);
+ rejectGuardianSelection(current);
const identity = publicationIdentity(current);
return {
current,
@@ -907,10 +1257,132 @@ function verifiableLegacyMetadata(
return verifiable;
}
+// Prefer the current native evidence for new publications. The original SDK input
+// remains a candidate so an approved request can resume under its exact digest.
+function* hostedPublicationProjections(
+ input: ResolvedPublicationInput,
+ provider: "semgrep" | "snyk",
+ destination: ImportRepository,
+ readerSources: readonly ExternalSourceRepository[],
+): Generator<ResolvedPublicationInput> {
+ const current: ExternalFindings & Partial<SnykIssuesFindings> = input.current;
+ if (provider === "snyk") {
+ const first = current.findings.find(isSnykRestFinding);
+ if (!first && current.snykRestContext === undefined) return;
+ // A previous publisher may have approved the exact raw selection.
+ yield input;
+ const originalUrl =
+ current.snykRestContext?.["sourceRepositoryUrl"] ??
+ current.sourceRepositoryUrl ??
+ first?.evidence.source_data?.["sourceRepositoryUrl"];
+ // The original spelling reconstructs a lookup key after a catalog rename.
+ // Admission validates the input against the saved approval or live destination.
+ const retained = structuredClone(current);
+ try {
+ snykRestImportRepository(
+ retained,
+ provider,
+ [],
+ hostedSourceReferences(
+ readerSources,
+ typeof originalUrl === "string" ? originalUrl : destination.url,
+ current.sourceRepositoryPlatform,
+ ),
+ (finding, source) => {
+ finding.evidence.source_data = source;
+ },
+ );
+ } catch (error) {
+ // A headerless input can still identify an already-approved opaque request.
+ if (current.snykRestContext !== undefined) throw error;
+ return;
+ }
+ yield {
+ ...input,
+ current: retained,
+ // The raw input above owns its historical native checkpoint.
+ original: undefined,
+ currentIdentity: publicationIdentity(retained),
+ };
+ return;
+ }
+ const originalRow = (finding: ExternalFindings["findings"][number]) =>
+ isRecord(finding.evidence.source_data?.["finding"]) &&
+ isSemgrepPlatformEvidence(finding, "semgrep");
+ const currentBulkRow = (finding: ExternalFindings["findings"][number]) =>
+ finding.evidence.source_data?.["format"] === semgrepPlatformFormat;
+ const first =
+ current.findings.find(originalRow) ?? current.findings.find(currentBulkRow);
+ if (!first) return;
+ const reportScope = semgrepPlatformReportScope(current, "semgrep");
+ const retained = first.evidence.source_data;
+ const project = retained?.["project"];
+ const retainedUrl =
+ current.sourceRepositoryUrl ??
+ retained?.["sourceRepositoryUrl"] ??
+ (isRecord(project) ? project["url"] : undefined);
+ // A renamed repository may need the original spelling to identify its checkpoint.
+ // Preparation validates the unmodified report against the authenticated approval.
+ for (const url of new Set([destination.url, retainedUrl])) {
+ if (typeof url !== "string") continue;
+ const references = hostedSourceReferences(
+ readerSources,
+ url,
+ current.sourceRepositoryPlatform,
+ );
+ try {
+ const witnesses = semgrepLegacyRepositoryWitnesses(
+ current.findings,
+ "semgrep",
+ references,
+ "cloud",
+ );
+ const findings = current.findings.map((finding) => {
+ const original = originalRow(finding);
+ if (!original && !currentBulkRow(finding)) return finding;
+ const binding = semgrepPlatformFindingBinding(
+ finding,
+ "semgrep",
+ references,
+ witnesses,
+ reportScope,
+ undefined,
+ "cloud",
+ );
+ // The authenticated repository can make an offline backlog row selectable.
+ return binding?.sourceData &&
+ (original || binding.sourceData["format"] !== semgrepPlatformFormat)
+ ? {
+ ...finding,
+ evidence: {
+ ...finding.evidence,
+ ...binding.migration,
+ source_data: binding.sourceData,
+ },
+ }
+ : finding;
+ });
+ const upgraded = { ...current, findings };
+ yield {
+ ...input,
+ current: upgraded,
+ original: undefined,
+ currentIdentity: publicationIdentity(upgraded),
+ };
+ } catch {
+ // The released SDK also accepted partial original records; keep the raw
+ // identity as a fallback so its exact saved request remains recoverable.
+ }
+ }
+ yield input;
+}
+
async function preparePublicationInput(
- input: PublicationSelection,
+ supplied: PreparePublicationInput,
options: NormalizedExternalPublicationOptions,
dependencies: ExternalPublicationDependencies,
+ readerSources: readonly ExternalSourceRepository[],
+ declaredUrl?: string,
nativePath?: string,
): Promise<PreparedExternalPublication> {
const { inspectorProjectMap } = options;
@@ -926,28 +1398,26 @@ async function preparePublicationInput(
);
}
progress({ phase: "reading", completed: 0 });
- let {
- current: parsed,
- legacy: legacyParsed,
- currentIdentity,
- legacyIdentity,
- } = input;
- function noFindings(): never {
+ function noFindings(parsed: ExternalFindings): never {
const diagnostics = reportForJson(parsed.report)?.["errors"];
throw new CodexSecurityError(
`No supported findings are ready to publish. ${parsed.excluded.map((item) => `Item ${item.position}: ${item.reason}`).join(" ")}${Array.isArray(diagnostics) && diagnostics.length ? ` Scanner diagnostics: ${JSON.stringify(diagnostics)}` : ""}`,
);
}
- if (
- !parsed.findings.length &&
- !input.original?.parsed.findings.length &&
- input.potentialIds !== null &&
- !input.potentialIds?.length &&
- !input.uncertainFindingIds?.length &&
- !input.unprovenOccurrenceCount &&
- !input.unprovenIdPrefixes?.length
- )
- noFindings();
+ function checkSelection(input: PublicationSelection) {
+ const parsed = input.current;
+ if (
+ !parsed.findings.length &&
+ !input.original?.parsed.findings.length &&
+ input.potentialIds !== null &&
+ !input.potentialIds?.length &&
+ !input.uncertainFindingIds?.length &&
+ !input.unprovenOccurrenceCount &&
+ !input.unprovenIdPrefixes?.length
+ )
+ noFindings(parsed);
+ }
+ if (typeof supplied !== "function") checkSelection(supplied);
progress({ phase: "discovering", completed: 0 });
const environment = dependencies.environment ?? process.env;
requireExternalImports(environment);
@@ -1004,40 +1474,48 @@ async function preparePublicationInput(
}
return response.json();
}
- const destinations: ImportRepository[] = [];
- let page: string | null = null;
- const seenPages = new Set<string>();
- do {
- const result = validateRepositories(
- await request(
- `/repositories?limit=20&repository=${encodeURIComponent(options.repository)}${page ? `&page=${encodeURIComponent(page)}` : ""}`,
- ),
- );
- destinations.push(
- ...result.data.filter(
- (item) =>
- item.id === options.repository ||
- cloudRepositoryUrlKey(item.url) ===
- cloudRepositoryUrlKey(options.repository),
- ),
- );
- page = result.next;
- if (
- result.has_more !== (page !== null) ||
- (page !== null && seenPages.has(page))
- )
+ async function discoverDestination(): Promise<ImportRepository> {
+ const destinations: ImportRepository[] = [];
+ let page: string | null = null;
+ const seenPages = new Set<string>();
+ do {
+ const result = validateRepositories(
+ await request(
+ `/repositories?limit=20&repository=${encodeURIComponent(options.repository)}${page ? `&page=${encodeURIComponent(page)}` : ""}`,
+ ),
+ );
+ destinations.push(
+ ...result.data.filter(
+ (item) =>
+ item.id === options.repository ||
+ cloudRepositoryUrlKey(item.url) ===
+ cloudRepositoryUrlKey(options.repository),
+ ),
+ );
+ page = result.next;
+ if (
+ result.has_more !== (page !== null) ||
+ (page !== null && seenPages.has(page))
+ )
+ throw new CodexSecurityError(
+ "Cloud returned invalid repository pagination.",
+ );
+ if (page !== null) seenPages.add(page);
+ } while (page !== null);
+ if (destinations.length !== 1)
throw new CodexSecurityError(
- "Cloud returned invalid repository pagination.",
+ destinations.length
+ ? "The repository matches multiple connectors. Select an unambiguous Cloud repository."
+ : "The destination repository is not available to this Cloud account. Copy its repository URL or ID from Codex Security Cloud and check that you are signed into the intended account.",
);
- if (page !== null) seenPages.add(page);
- } while (page !== null);
- if (destinations.length !== 1)
- throw new CodexSecurityError(
- destinations.length
- ? "The repository matches multiple connectors. Select an unambiguous Cloud repository."
- : "The destination repository is not available to this Cloud account. Copy its repository URL or ID from Codex Security Cloud and check that you are signed into the intended account.",
- );
- const destination = destinations[0]!;
+ return destinations[0]!;
+ }
+ const destination = await discoverDestination();
+ const deferred = typeof supplied === "function";
+ const resolved = deferred ? await supplied(destination) : supplied;
+ if (deferred) checkSelection(resolved);
+ const report: ExternalFindings & Partial<SnykIssuesFindings> =
+ resolved.current;
const source = { provider: options.provider, source_key: options.sourceKey };
const importPrefix = source.provider === "sonar" ? "/v2" : "";
const sourceReportsPath = importPrefix + sourcePath(destination);
@@ -1062,6 +1540,48 @@ async function preparePublicationInput(
),
);
}
+ const hosted = options.provider === "snyk" || options.provider === "semgrep";
+ // Different selections can share native occurrences. Serialize their checkpoint
+ // creation under the authenticated source and release before calling Cloud.
+ const sourceLock =
+ "source-" + hash("sha256", canonicalJson(checkpointIdentity));
+ const withSourceAdmission = (operation: () => Promise<void>) =>
+ hosted ? underLocks([sourceLock], operation) : operation();
+ let projection = resolved;
+ let capturedPending: string | undefined;
+ const projectionKeys = new Set<string>();
+ if (options.provider === "semgrep" || options.provider === "snyk") {
+ for (const candidate of hostedPublicationProjections(
+ resolved,
+ options.provider,
+ destination,
+ readerSources,
+ )) {
+ const candidateKey = scopeKey(candidate.currentIdentity);
+ if (projectionKeys.has(candidateKey)) continue;
+ // New imports use the first reconstructed projection; an exact pending key
+ // takes precedence, including an earlier SDK's unmodified original input.
+ capturedPending = await readOptionalFile(
+ join(state, candidateKey + ".pending.json"),
+ );
+ if (projection === resolved || capturedPending !== undefined)
+ projection = candidate;
+ projectionKeys.add(candidateKey);
+ if (capturedPending !== undefined) break;
+ }
+ }
+ const input = projection;
+ let {
+ current: parsed,
+ legacy: legacyParsed,
+ currentIdentity,
+ legacyIdentity,
+ recheckFileRepository,
+ } = input;
+ const scopedSemgrepReport =
+ options.provider === "semgrep" &&
+ "sourceFormat" in parsed &&
+ isSemgrepPlatformFormat(parsed.sourceFormat);
const original = input.original;
const originalParsed = original?.parsed ?? parsed;
const originalIdentity = original?.identity ?? currentIdentity;
@@ -1125,6 +1645,225 @@ async function preparePublicationInput(
)
);
}
+ const originalHostedRetry =
+ "This source has a pending hosted Cloud request. Resume it with the SDK build that created it and the original input before publishing another selection.";
+ const unknownHostedRetry =
+ "A pending hosted request matches this provider and repository, but its deployment could not be established. Resume it with the SDK build that created it, on the original deployment, using the full original input and metadata.";
+ function originalHostedPending(
+ content: SavedSubmission,
+ filenameKey: string,
+ ) {
+ const projection = content.inputProjection;
+ if (
+ (options.provider !== "semgrep" && options.provider !== "snyk") ||
+ content.accountId !== credentials.account_id ||
+ !projection ||
+ !content.requests?.length ||
+ !content.requests.every(
+ ({ repository, source: savedSource }) =>
+ repository.id === destination.id &&
+ repository.repo_connector_id === destination.repo_connector_id &&
+ canonicalJson(savedSource) === canonicalJson(source),
+ )
+ )
+ return false;
+ const findings = submissionFindings(content.requests);
+ if (
+ projection.savedEvidenceDigest !== hash("sha256", canonicalJson(findings))
+ )
+ return false;
+ const projections = [findings];
+ // Historical filenames include caller fields discarded by request validation.
+ // Reuse them by stable ID with the evidence and order of the approved request.
+ const metadata = new Map(
+ legacyParsed.findings
+ .filter((row) =>
+ Object.keys(row).some(
+ (key) => key !== "evidence" && key !== "source_finding_id",
+ ),
+ )
+ .map((row) => [row.source_finding_id, row]),
+ );
+ if (findings.some((row) => metadata.has(row.source_finding_id)))
+ projections.push(
+ findings.map((row) => ({
+ ...metadata.get(row.source_finding_id),
+ ...row,
+ })),
+ );
+ const identities: unknown[] = [...projections];
+ if (parsed.report !== undefined) {
+ const report = reportForJson(parsed.report);
+ identities.push(
+ ...projections.map((rows) => ({ findings: rows, report })),
+ );
+ }
+ // Caller metadata on a normalized finding is part of the filename but not the
+ // request body. An exact retry still supplies that original identity.
+ if (filenameKey === scopeKey(legacyIdentity))
+ identities.unshift(legacyIdentity);
+ const identity = identities.find(
+ (value) => filenameKey === scopeKey(value),
+ );
+ const digest = (value: unknown) => hash("sha256", canonicalJson(value));
+ const semgrep = options.provider === "semgrep";
+ const isHosted = (row: (typeof findings)[number]) =>
+ semgrep
+ ? isSemgrepPlatformEvidence(row, "semgrep")
+ : isSnykRestFinding(row);
+ const scannerId = (row: (typeof findings)[number]) =>
+ isHosted({
+ ...row,
+ evidence: { ...row.evidence, source_data: undefined },
+ });
+ const hasNativeEvidence = (row: (typeof findings)[number]) => {
+ const data = row.evidence.source_data;
+ // A discriminator alone cannot identify a native occurrence.
+ return (
+ data &&
+ isHosted({
+ source_finding_id: "",
+ evidence: {
+ ...row.evidence,
+ source_data: { ...data, format: undefined },
+ },
+ })
+ );
+ };
+ const references = hostedSourceReferences(
+ readerSources,
+ content.approvedRepositoryUrl ?? destination.url,
+ content.approvedRepositoryPlatform,
+ );
+ const occurrenceIds = (rows: typeof findings) => {
+ const ids = new Set(rows.map((row) => row.source_finding_id));
+ let native = false;
+ let unresolved = false;
+ for (const row of rows) {
+ if (!hasNativeEvidence(row)) continue;
+ native = true;
+ try {
+ const id = semgrep
+ ? semgrepPlatformFindingBinding(
+ { ...row, source_finding_id: "" },
+ "semgrep",
+ references,
+ undefined,
+ undefined,
+ undefined,
+ "cloud",
+ )?.nativeId
+ : snykRestCheckpointFindingId(row, references);
+ if (id) {
+ ids.add(id);
+ continue;
+ }
+ } catch {
+ // An earlier SDK may have accepted a partial native projection.
+ }
+ if (!scannerId(row)) unresolved = true;
+ }
+ return { ids, native, unresolved };
+ };
+ const previous = occurrenceIds(findings);
+ const requested = occurrenceIds(parsed.findings);
+ const hostedSelection =
+ projection.retainedHostedSelection ||
+ scopedSemgrepReport ||
+ previous.native ||
+ requested.native;
+ // Submitted and native IDs share one namespace even when one side drops its raw evidence.
+ const couldOverlap =
+ (hostedSelection &&
+ Array.from(requested.ids).some((id) => previous.ids.has(id))) ||
+ (previous.native &&
+ requested.native &&
+ (previous.unresolved || requested.unresolved));
+ if (content.approvedRepositoryUrl && !projection.retainedHostedSelection) {
+ if (!couldOverlap) return false;
+ if (identity)
+ return projection.inputDigest === digest(identity) && "verified";
+ // The filename includes metadata omitted from the saved request. A reproducible
+ // original digest under another filename proves this is another deployment.
+ return (
+ !identities.some((value) => digest(value) === projection.inputDigest) &&
+ "unidentified"
+ );
+ }
+ if (identity && projection.inputDigest !== digest(identity))
+ return (
+ (couldOverlap || !previous.native || !requested.native) && "verified"
+ );
+
+ if (!couldOverlap) return false;
+ if (!identity) {
+ // If the saved selections reconstruct an old input digest but not its scoped
+ // filename, the pending request belongs to another deployment. The older
+ // publisher included its repository comparison flags in the digest.
+ const repositories = new Set([destination.url]);
+ for (const { evidence } of findings) {
+ const data = evidence.source_data;
+ const project = data?.["project"];
+ const retained =
+ (semgrep && isRecord(project) ? project["url"] : undefined) ??
+ data?.["sourceRepositoryUrl"];
+ if (typeof retained === "string") repositories.add(retained);
+ }
+ const paths = semgrep ? [null, true] : [false, true];
+ const suffixes = semgrep ? [false] : [false, true];
+ for (const candidate of identities) {
+ if (digest(candidate) === projection.inputDigest) return false;
+ for (const strict of paths) {
+ for (const suffix of suffixes) {
+ for (const repository of repositories) {
+ const url = hostedRepositoryUrlKey(repository, !strict, suffix);
+ if (
+ digest([candidate, [url, strict, suffix]]) ===
+ projection.inputDigest
+ )
+ return false;
+ }
+ }
+ }
+ }
+ return "unidentified";
+ }
+ return "verified";
+ }
+ function wasReset(content: SavedSubmission) {
+ return content.requests.some(
+ ({ repository }) => repository.reset_marker !== destination.reset_marker,
+ );
+ }
+ async function confirmReset() {
+ // A newer pending request may have been written while the catalog response
+ // was in flight. Confirm the reset after observing this immutable request.
+ const confirmed = await discoverDestination();
+ if (
+ confirmed.id !== destination.id ||
+ confirmed.repo_connector_id !== destination.repo_connector_id ||
+ confirmed.reset_marker !== destination.reset_marker
+ )
+ throw new CodexSecurityError(
+ "The repository changed while preparing this publication. Review the destination again.",
+ );
+ }
+ function suppliedSemgrepScopeDigest() {
+ const scope = semgrepPlatformReportScope(report, options.provider);
+ return hash(
+ "sha256",
+ canonicalJson(
+ scope?.format
+ ? [
+ scope.deployment,
+ canonicalNumber(scope.projectId),
+ scope.ref,
+ scope.product,
+ ]
+ : null,
+ ),
+ );
+ }
async function retireIfReset(
content: SavedSubmission,
retire: () => Promise<void>,
@@ -1134,6 +1873,7 @@ async function preparePublicationInput(
(batch) => batch.repository.reset_marker !== destination.reset_marker,
)
) {
+ if (hosted) await confirmReset();
await retire();
throw new CloudImportError(
409,
@@ -1160,6 +1900,7 @@ async function preparePublicationInput(
function validateSaved(
record: SavedSubmission | null | undefined,
legacy = false,
+ filenameKey = key,
): SavedSubmission {
const content = readSaved(record);
const saved = submissionFindings(content.requests);
@@ -1171,10 +1912,18 @@ async function preparePublicationInput(
(!sameFindingSelection(saved, expected) ||
projection.inputDigest !== (legacy ? legacyInputDigest : inputDigest) ||
projection.savedEvidenceDigest !== hash("sha256", wire))
- )
+ ) {
+ const status = originalHostedPending(content, filenameKey);
+ if (status === "unidentified")
+ throw new CodexSecurityError(unknownHostedRetry);
+ if (status) {
+ if (wasReset(content)) return content;
+ throw new CodexSecurityError(originalHostedRetry);
+ }
throw new Error(
"Saved import evidence does not match the selected input.",
);
+ }
// Older file readers can have a different, explicitly bound projection.
// A corrected numeric checkpoint must carry the selected evidence verbatim.
const exactWire =
@@ -1193,7 +1942,16 @@ async function preparePublicationInput(
!verifiableLegacyMetadata(originalParsed, true)
)
throw unmatchedPreviousNumbers();
- rejectLocalScannerReports({ ...parsed, findings: saved }, options.provider);
+ rejectGuardianSelection({ ...parsed, findings: saved });
+ const approvedScope = content.inputProjection?.semgrepPartialScopeDigest;
+ if (
+ approvedScope !== undefined &&
+ !wasReset(content) &&
+ approvedScope !== suppliedSemgrepScopeDigest()
+ )
+ throw new CodexSecurityError(
+ "The supplied Semgrep report scope does not match the approved request.",
+ );
return content;
}
@@ -1228,9 +1986,10 @@ async function preparePublicationInput(
!verifiableLegacyMetadata(originalParsed, false)
)
throw unmatchedPreviousNumbers();
- rejectLocalScannerReports({ ...parsed, findings: saved }, options.provider);
+ rejectGuardianSelection({ ...parsed, findings: saved });
content.legacyApproval = true;
content.inputProjection = {
+ ...content.inputProjection,
identityFormat: 2,
inputDigest,
savedEvidenceDigest: wireDigest,
@@ -1327,6 +2086,7 @@ async function preparePublicationInput(
const content = validateSaved(
serialized === undefined ? JSON.parse(captured) : prior,
true,
+ legacyKey,
);
if (!sameFindingSelection(legacyParsed.findings, parsed.findings))
throw new CodexSecurityError(
@@ -1353,14 +2113,16 @@ async function preparePublicationInput(
}
recovered = content;
if (serialized === undefined) return;
- if (!rebindProjection) {
- await rename(legacyPath, canonicalPath);
- } else {
- // Bind the new input projection to the original immutable evidence,
- // so later inventory reordering still resumes these exact requests.
- await writeAtomicJson(canonicalPath, content);
- await rm(legacyPath);
- }
+ await withSourceAdmission(async () => {
+ if (!rebindProjection) {
+ await rename(legacyPath, canonicalPath);
+ } else {
+ // Bind the new input projection to the original immutable evidence,
+ // so later inventory reordering still resumes these exact requests.
+ await writeAtomicJson(canonicalPath, content);
+ await rm(legacyPath);
+ }
+ });
});
}
}
@@ -1586,7 +2348,8 @@ async function preparePublicationInput(
contents ??
(selected?.fileKey === key && !known.has(fileKey)
? await readOptionalRegularFile(path)
- : await readOptionalFile(path));
+ : await readOptionalFile(path)) ??
+ (preparing && fileKey === key ? capturedPending : undefined);
if (serialized === undefined) {
if (!known.has(fileKey)) incomplete = true;
return selected;
@@ -1607,6 +2370,35 @@ async function preparePublicationInput(
const ownerKey = tagged ? tag : fileKey;
const historical = !tagged && fileKey === originalKey && fileKey !== key;
if (ownerKey !== key && !historical) {
+ if (hosted && record?.inputProjection) {
+ let status;
+ try {
+ status = originalHostedPending(record, ownerKey);
+ } catch {
+ // An unrelated or unreadable checkpoint cannot identify this source.
+ }
+ if (status === "unidentified")
+ throw new CodexSecurityError(unknownHostedRetry);
+ if (status) {
+ if (wasReset(record)) {
+ if (!preparing)
+ throw new CodexSecurityError(
+ "The pending request and this preview have different repository reset markers. Review the destination again.",
+ );
+ await retireIfReset(record, () =>
+ underLocks([fileKey], async () => {
+ const bytes = await readOptionalFile(path);
+ if (
+ bytes !== undefined &&
+ sameSubmission(JSON.parse(bytes), record)
+ )
+ await rm(path);
+ }),
+ );
+ }
+ throw new CodexSecurityError(originalHostedRetry);
+ }
+ }
if (
record &&
(!tagged || cannotCaptureOriginal || possibleBufferExport) &&
@@ -1666,7 +2458,7 @@ async function preparePublicationInput(
for (const fileKey of known)
selected = await readCandidate(fileKey, selected);
// A known file that disappeared can be checked against its completed receipt.
- if (!selected && knownFileKey !== undefined) return undefined;
+ if (!selected && knownFileKey !== undefined && !hosted) return undefined;
const hint = await lookupFileKey();
if (hint && !known.has(hint)) {
const contents = await readOptionalRegularFile(
@@ -1791,7 +2583,7 @@ async function preparePublicationInput(
}
const saved = selected?.content ?? recovered;
if (historicalOnly && !saved) throw unmatchedPreviousNumbers();
- if (!parsed.findings.length) noFindings();
+ if (!parsed.findings.length) noFindings(parsed);
if (saved) {
await retireIfReset(saved, () =>
underLocks(lockKeys, async () => {
@@ -1809,14 +2601,136 @@ async function preparePublicationInput(
"Supply the verified Inspector project binding again to resume.",
);
+ // The findings-only checkpoint key lets projected and native callers resume one immutable
+ // request. Supplemental report claims are outside that digest and must still match its source.
+ const recheckOriginal =
+ options.provider === "semgrep" &&
+ projection !== resolved &&
+ saved?.approvedRepositoryPlatform === "other";
+ // Native Semgrep and OpenGrep files include their required source URL in every
+ // saved finding. An exact replay by repository ID survives a catalog rename.
+ const recordedFileSource =
+ saved &&
+ !saved.approvedRepositoryUrl &&
+ options.provider === "semgrep" &&
+ !deferred &&
+ declaredUrl !== undefined &&
+ saved.requests.every(({ items }) =>
+ items.every(
+ ({ evidence }) => evidence.details?.repository?.url === declaredUrl,
+ ),
+ );
+ const declaredSource =
+ recheckOriginal ||
+ readerSources.length > 0 ||
+ (declaredUrl !== undefined && !recordedFileSource) ||
+ report.sourceRepositoryUrl !== undefined ||
+ report.sourceRepositoryPlatform !== undefined ||
+ report.snykRestContext !== undefined;
+ let legacyTypedUrl: string | undefined;
+ let suppliedReport = report;
+ const sourceFor = (url: string, platform?: "other") => {
+ try {
+ return hostedRepositoryMatcher(
+ platform
+ ? { ...suppliedReport, sourceRepositoryPlatform: platform }
+ : suppliedReport,
+ options.provider,
+ declaredUrl,
+ { url },
+ readerSources,
+ saved !== undefined,
+ );
+ } catch (error) {
+ if (!recheckOriginal || readerSources.length) throw error;
+ // An older strict approval did not distinguish GitLab from another case-sensitive SCM.
+ throw new CodexSecurityError(
+ `${error instanceof Error ? error.message : String(error)} Resume with the original Semgrep input and SCM repository mapping, or the SDK build that created this request.`,
+ { cause: error },
+ );
+ }
+ };
+ if (hosted && saved && declaredSource) {
+ // The released publisher checked typed repository URLs against its authenticated catalog.
+ // Opaque original records could name a different project and cannot establish a rename.
+ const findings = submissionFindings(saved.requests);
+ if (!saved.approvedRepositoryUrl)
+ legacyTypedUrl = findings.find(
+ ({ evidence }) => evidence.details?.repository?.url,
+ )?.evidence.details?.repository?.url;
+ // Supplemental claims must agree with each verifiable row from the immutable request.
+ suppliedReport = {
+ ...report,
+ // The v1 upgrade may drop a redundant mapping using the catalog's GitHub casing.
+ findings: recheckOriginal ? [...report.findings, ...findings] : findings,
+ };
+ }
+ // The released request retained its typed finding URL, but only the authenticated
+ // catalog or a previously recorded approval can bind a new source declaration.
+ const approvedUrl = saved?.approvedRepositoryUrl ?? destination.url;
+ if (
+ hosted &&
+ readerSources.some(({ platform, url }) => {
+ const source = normalizeHostedGitRepositoryUrl(
+ url,
+ approvedUrl,
+ platform !== undefined,
+ );
+ return (
+ cloudSourceKey(source, platform) !==
+ cloudSourceKey(approvedUrl, platform)
+ );
+ })
+ )
+ throw new CodexSecurityError(
+ "The reader's source repository does not match the selected Cloud repository.",
+ );
+ await recheckFileRepository?.(approvedUrl, saved?.approvedRepositoryPlatform);
+ const hostedSource =
+ hosted && (!saved || declaredSource)
+ ? sourceFor(approvedUrl, saved?.approvedRepositoryPlatform)
+ : undefined;
+ const matchesHostedRepository = hostedSource?.matches;
+ function requireApprovedSource(url: string, platform?: "other") {
+ const binding =
+ platform && hostedSource?.github
+ ? sourceFor(url, platform)
+ : hostedSource;
+ if (binding && !binding.matches(url))
+ throw new CodexSecurityError(
+ "The source project does not match the selected Cloud repository.",
+ );
+ }
+ requireApprovedSource(approvedUrl);
+ if (legacyTypedUrl) requireApprovedSource(legacyTypedUrl);
+
function checkRepositories(
findings: ExternalFindings["findings"],
report?: ExternalFindings["report"],
) {
+ // A catalog casing or spelling change keeps the original source comparison target.
+ const approved = hosted ? saved?.approvedRepositoryUrl : undefined;
+ const sameCatalog =
+ approved !== undefined &&
+ cloudSourceKey(approved, "github") ===
+ cloudSourceKey(destination.url, "github");
+ const comparisonUrl = sameCatalog ? approved : destination.url;
+ const key = sameCatalog
+ ? (url: string) =>
+ cloudSourceKey(
+ url,
+ saved?.approvedRepositoryPlatform === "other"
+ ? undefined
+ : "github",
+ )
+ : cloudRepositoryUrlKey;
function requireDestination(id: string, url: string | null | undefined) {
if (
url &&
- cloudRepositoryUrlKey(url) !== cloudRepositoryUrlKey(destination.url)
+ (matchesHostedRepository
+ ? !matchesHostedRepository(url) ||
+ !matchesHostedRepository(comparisonUrl)
+ : key(url) !== key(comparisonUrl))
)
throw new CodexSecurityError(
`Finding ${JSON.stringify(id)} belongs to ${JSON.stringify(url)}, which does not match the selected Cloud repository ${JSON.stringify(destination.url)}. Verify the source repository mapping before publishing.`,
@@ -1899,6 +2813,21 @@ async function preparePublicationInput(
for (const url of urls) requireDestination(id, url);
}
}
+ if (!saved) {
+ for (const finding of parsed.findings) {
+ if (
+ options.provider !== "semgrep" &&
+ isSemgrepPlatformEvidence(finding, options.provider)
+ )
+ throw new CodexSecurityError(
+ "Semgrep Platform evidence requires the semgrep provider.",
+ );
+ if (options.provider !== "snyk" && isSnykRestFinding(finding))
+ throw new CodexSecurityError(
+ "Snyk REST evidence requires the snyk provider.",
+ );
+ }
+ }
checkRepositories(parsed.findings, parsed.report);
// A retry sends its saved projection, which may differ from today's reader.
if (saved) {
@@ -1911,6 +2840,38 @@ async function preparePublicationInput(
checkRepositories(submissionFindings(saved.requests), report);
}
+ // An older, genuinely unbound checkpoint first acquires a URL on a matching preview.
+ // Retain it at the original path so its bare caller can still recover the same request.
+ if (saved && !saved.approvedRepositoryUrl && hostedSource) {
+ await underLocks(lockKeys, async () => {
+ const serialized = await readOptionalFile(pendingPath);
+ if (serialized === undefined) return;
+ const current = validateSaved(JSON.parse(serialized));
+ if (!sameSubmission(current, saved))
+ throw new CodexSecurityError(
+ "Another publication prepared this input. Review the saved request again.",
+ );
+ if (current.approvedRepositoryUrl) {
+ requireApprovedSource(
+ current.approvedRepositoryUrl,
+ current.approvedRepositoryPlatform,
+ );
+ } else {
+ // Adding the catalog URL cannot erase an older SDK's same-selection hold.
+ if (
+ current.inputProjection &&
+ originalHostedPending(current, current.wireKey ?? fileKey) ===
+ "verified"
+ )
+ current.inputProjection.retainedHostedSelection = true;
+ current.approvedRepositoryUrl = approvedUrl;
+ await writeAtomicJson(pendingPath, current);
+ }
+ saved.inputProjection = current.inputProjection;
+ saved.approvedRepositoryUrl = current.approvedRepositoryUrl;
+ saved.approvedRepositoryPlatform = current.approvedRepositoryPlatform;
+ });
+ }
// Existing repository submissions keep their original request bodies and receipts.
const privatePath = join(state, `${key}.private-import.json`);
const privateSaved = await readOptionalFile(privatePath);
@@ -2200,6 +3161,15 @@ async function preparePublicationInput(
}
if (current) requests.push(validateImportRequest(current));
}
+ const bindSemgrepScope =
+ !saved &&
+ options.provider === "semgrep" &&
+ report.findings.some(
+ (finding) =>
+ !isSemgrepPlatformEvidence(finding, "semgrep") &&
+ (scopedSemgrepReport ||
+ hasRetainedSemgrepScope(finding.evidence.source_data?.["finding"])),
+ );
const submission: SavedSubmission = {
accountId: credentials.account_id,
requests,
@@ -2208,10 +3178,25 @@ async function preparePublicationInput(
wireKey: saved?.wireKey ?? (fileKey === key ? undefined : key),
inputProjection: saved?.inputProjection ?? {
identityFormat: saved ? undefined : 2,
+ ...(scopedSemgrepReport && { retainedHostedSelection: true }),
+ ...(bindSemgrepScope && {
+ semgrepPartialScopeDigest: suppliedSemgrepScopeDigest(),
+ }),
inputDigest,
savedEvidenceDigest: jsonDigest(submissionFindings(requests)),
},
};
+ if (
+ hosted &&
+ (!saved || saved.approvedRepositoryUrl || matchesHostedRepository)
+ )
+ submission.approvedRepositoryUrl =
+ saved?.approvedRepositoryUrl ?? approvedUrl;
+ if (
+ saved?.approvedRepositoryPlatform ||
+ (hostedSource && !hostedSource.github)
+ )
+ submission.approvedRepositoryPlatform = "other";
if (inspectorProjectMap) submission.inspectorProjectMapRequired = true;
const preview: ExternalPublicationPreview = {
...parsed,
@@ -2312,6 +3297,11 @@ async function preparePublicationInput(
submission.inspectorProjectMapRequired &&
current.inspectorProjectMapRequired === undefined;
if (adoptProjectMap) current.inspectorProjectMapRequired = true;
+ if (current.approvedRepositoryUrl)
+ requireApprovedSource(
+ current.approvedRepositoryUrl,
+ current.approvedRepositoryPlatform,
+ );
if (
!selected ||
selected.fileKey !== fileKey ||
@@ -2335,6 +3325,12 @@ async function preparePublicationInput(
if ((await readOptionalFile(pendingPath)) !== undefined)
throw occupiedCheckpoint();
}
+ // A preview can pin the catalog URL. Tighten its SCM comparison only
+ // once the caller confirms publication, including when resuming a request.
+ const adoptStrictRepository =
+ submission.approvedRepositoryPlatform === "other" &&
+ !current.approvedRepositoryPlatform;
+ if (adoptStrictRepository) current.approvedRepositoryPlatform = "other";
const receipts = (current.receipts ?? []).map(validateImportReceipt);
if (receipts.length > requests.length)
throw new CodexSecurityError(
@@ -2345,7 +3341,33 @@ async function preparePublicationInput(
let persisted = pending !== undefined;
try {
if (selected?.checkedCanonical) current.canonicalChecked = true;
- if (pending === undefined || adoptProjectMap) {
+ if (pending === undefined) {
+ await withSourceAdmission(async () => {
+ if (hosted) {
+ for (const otherKey of new Set([
+ ...projectionKeys,
+ ...legacyKeys,
+ ])) {
+ if (
+ otherKey !== key &&
+ otherKey !== fileKey &&
+ (await readOptionalFile(
+ join(state, otherKey + ".pending.json"),
+ )) !== undefined
+ )
+ throw new CodexSecurityError(
+ "Another publication prepared this input. Run the command again to review and resume that saved request.",
+ );
+ }
+ if (await selectedCheckpoint())
+ throw new CodexSecurityError(
+ "Another publication prepared this input. Run the command again to review and resume that saved request.",
+ );
+ }
+ await writeAtomicJson(pendingPath, current);
+ });
+ persisted = true;
+ } else if (adoptProjectMap || adoptStrictRepository) {
await writeAtomicJson(pendingPath, current);
persisted = true;
} else if (selected?.checkedCanonical) {
diff --git a/sdk/typescript/src/external-findings-read.ts b/sdk/typescript/src/external-findings-read.ts
index 3bf7cbd7e..750068d0f 100644
--- a/sdk/typescript/src/external-findings-read.ts
+++ b/sdk/typescript/src/external-findings-read.ts
@@ -89,15 +89,16 @@ export function localFindingRepositories(
}
/** Repository claims retained by the Semgrep SARIF reader. */
-export function semgrepSarifRepositories({
- evidence,
-}: ExternalFinding): { url: string; branch?: string }[] {
+export function semgrepSarifRepositories(
+ { evidence }: ExternalFinding,
+ allowPartialCloudUrl = false,
+): { url: string; branch?: string }[] {
const data = evidence.source_data;
const document = data?.["documentContext"];
const driver = data?.["driverContext"];
+ const partial = allowPartialCloudUrl && document === undefined;
if (
- !isRecord(document) ||
- document["version"] !== "2.1.0" ||
+ (!partial && (!isRecord(document) || document["version"] !== "2.1.0")) ||
!isRecord(driver) ||
typeof driver["name"] !== "string" ||
!isSemgrepSarifDriver(driver["name"]) ||
@@ -116,9 +117,10 @@ export function semgrepSarifRepositories({
? [
{
url: reference["repositoryUri"],
- ...(typeof reference["branch"] === "string" && {
- branch: reference["branch"],
- }),
+ ...(!partial &&
+ typeof reference["branch"] === "string" && {
+ branch: reference["branch"],
+ }),
},
]
: [],
@@ -176,8 +178,8 @@ export async function readExternalFindings(
target,
);
if (semgrepPlatformReportRepository(findings)) return { findings };
- const sourceRepositories = findings.findings.flatMap(
- semgrepSarifRepositories,
+ const sourceRepositories = findings.findings.flatMap((finding) =>
+ semgrepSarifRepositories(finding),
);
return { findings, sourceRepositories };
}
diff --git a/sdk/typescript/src/external-import-contract.ts b/sdk/typescript/src/external-import-contract.ts
index 3785e3027..0c94a05b8 100644
--- a/sdk/typescript/src/external-import-contract.ts
+++ b/sdk/typescript/src/external-import-contract.ts
@@ -25,6 +25,7 @@ const localSchema = JSON.parse(
key === "maxLength" || key === "maxItems" ? undefined : value,
),
) as typeof schema;
+export const DEFAULT_CLOUD_STRING_BYTES = 512;
const STRING_BYTE_LIMITS: Readonly<Record<string, number>> = {
description: 65536,
snippet: 65536,
@@ -85,7 +86,7 @@ function validateModelStrings(
throw new Error(
`${field} must be nonempty and contain no NUL characters.`,
);
- const limit = STRING_BYTE_LIMITS[field] ?? 512;
+ const limit = STRING_BYTE_LIMITS[field] ?? DEFAULT_CLOUD_STRING_BYTES;
if (target === "cloud" && Buffer.byteLength(value) > limit)
throw new Error(
`${field} exceeds its Cloud limit of ${limit} UTF-8 bytes.`,
diff --git a/sdk/typescript/src/hosted-git-repository.ts b/sdk/typescript/src/hosted-git-repository.ts
index 180e31bb1..29582eaef 100644
--- a/sdk/typescript/src/hosted-git-repository.ts
+++ b/sdk/typescript/src/hosted-git-repository.ts
@@ -1,5 +1,6 @@
import { isIPv4 } from "node:net";
import {
+ type ExternalImportTarget,
cloudRepositoryUrlKey,
repositoryUrlKey,
} from "./external-import-contract.js";
@@ -221,11 +222,17 @@ export function hostedRepositoryUrlKey(
value: string,
github = false,
knownGitSuffix = false,
+ target: ExternalImportTarget = "local",
): string {
const url = value.replace(
/^(https?:\/\/)([^/?#]+)\/([^?#]*)/iu,
(_, scheme: string, authority: string, path: string) =>
- scheme + hostedHttpAuthority(authority) + "/" + hostedHttpPath(path),
+ scheme +
+ (target === "cloud" && ambiguousGitIpHost(value)
+ ? authority.toLowerCase()
+ : hostedHttpAuthority(authority)) +
+ "/" +
+ hostedHttpPath(path),
);
if (github) return cloudRepositoryUrlKey(url);
const host = URL.parse(url)?.hostname ?? "";
diff --git a/sdk/typescript/src/semgrep-findings.ts b/sdk/typescript/src/semgrep-findings.ts
index 90037a4d0..326b498e4 100644
--- a/sdk/typescript/src/semgrep-findings.ts
+++ b/sdk/typescript/src/semgrep-findings.ts
@@ -359,12 +359,9 @@ export function normalizeSemgrepSarif(
);
}
-export async function readSemgrepFindings(
- path: string,
- options: Partial<SemgrepImportOptions> = {},
- target: ExternalImportTarget = "cloud",
-): Promise<ExternalFindings> {
- const report = await readScannerJson(path, (json) => {
+/** @internal Read the original file before resolving repository provenance. */
+export async function readSemgrepReport(path: string): Promise<unknown> {
+ return readScannerJson(path, (json) => {
// Pick a parser before allocating the findings array. Verify the root:
// the member can be nested in a native report or escaped in a Platform one.
const parser = /"format"\s*:\s*"semgrep-platform-findings\/v[12]"/u.test(
@@ -381,5 +378,16 @@ export async function readSemgrepFindings(
: JSON.parse;
return parser === requiredParser ? parsed : requiredParser(json);
});
- return normalizeSemgrepFindings(report, options, target);
+}
+
+export async function readSemgrepFindings(
+ path: string,
+ options: Partial<SemgrepImportOptions> = {},
+ target: ExternalImportTarget = "cloud",
+): Promise<ExternalFindings> {
+ return normalizeSemgrepFindings(
+ await readSemgrepReport(path),
+ options,
+ target,
+ );
}
diff --git a/sdk/typescript/src/semgrep-platform-findings.ts b/sdk/typescript/src/semgrep-platform-findings.ts
index c3c0c4c12..551165813 100644
--- a/sdk/typescript/src/semgrep-platform-findings.ts
+++ b/sdk/typescript/src/semgrep-platform-findings.ts
@@ -1,4 +1,5 @@
import {
+ DEFAULT_CLOUD_STRING_BYTES,
validateCodeLineRange,
canonicalNumber,
validateExternalEvidence,
@@ -80,8 +81,6 @@ export function semgrepPlatformReportScope(
input: ExternalFindings,
provider?: string,
references: readonly ExternalSourceRepository[] = [],
- isPlatform = (row: ExternalFinding) =>
- isSemgrepPlatformEvidence(row, provider),
) {
const format = "sourceFormat" in input ? input.sourceFormat : undefined;
if (
@@ -97,7 +96,8 @@ export function semgrepPlatformReportScope(
provider === "semgrep" &&
"sourceRepositoryUrl" in input &&
input.sourceRepositoryUrl !== undefined &&
- (input.findings.length === 0 || input.findings.some(isPlatform))
+ (input.findings.length === 0 ||
+ input.findings.some((row) => isSemgrepPlatformEvidence(row, provider)))
) {
// The object-row SDK retained only the binding on the envelope, including empty reports.
return {
@@ -135,9 +135,8 @@ export function semgrepPlatformReportScope(
export function semgrepPlatformReportRepository(
input: ExternalFindings,
provider?: string,
- isPlatform?: (row: ExternalFinding) => boolean,
) {
- return semgrepPlatformReportScope(input, provider, [], isPlatform)?.url;
+ return semgrepPlatformReportScope(input, provider)?.url;
}
export interface SemgrepPlatformScope {
@@ -471,13 +470,15 @@ export function semgrepNativeProjectUrl(
function referenceRepositoryKey(
references: readonly ExternalSourceRepository[],
+ target: ExternalImportTarget = "local",
) {
- return (url: string) => repositoryContext(url, references).key(url);
+ return (url: string) => repositoryContext(url, references, target).key(url);
}
function repositoryContext(
projectUrl: string,
references: SemgrepRepositoryReference = [],
+ target: ExternalImportTarget = "local",
) {
const source = semgrepPlatformRepositoryReference(
projectUrl,
@@ -491,9 +492,10 @@ function repositoryContext(
typeof references === "function"
? references(nativeHttp(url))
: hostedRepositoryUrlKey(
- nativeHttp(url),
+ target === "cloud" ? url : nativeHttp(url),
github,
source.platform === "gitlab",
+ target,
),
name: (name: string) => (github ? name.toLowerCase() : name),
};
@@ -502,12 +504,20 @@ function repositoryContext(
export function selectionRepository(
projectUrl: string,
references: readonly ExternalSourceRepository[] = [],
+ target: ExternalImportTarget = "local",
) {
- const repository = repositoryContext(projectUrl, references);
+ const repository = repositoryContext(projectUrl, references, target);
if (
- !references.every(({ url }) =>
- semgrepReferenceMatches(repository.source, url),
- )
+ !references.every(({ url }) => {
+ if (target === "local")
+ return semgrepReferenceMatches(repository.source, url);
+ const mapped = normalizeHostedGitRepositoryUrl(
+ url,
+ projectUrl,
+ repository.source.platform !== undefined,
+ );
+ return repository.key(mapped) === repository.key(projectUrl);
+ })
)
throw new Error(
"The reader's source repository does not match the selected Semgrep project.",
@@ -832,6 +842,7 @@ function scopeProduct(native: unknown, complete: boolean) {
function readScope(
input: Record<string, unknown>,
references: SemgrepRepositoryReference,
+ target: ExternalImportTarget = "local",
) {
const complete =
input["format"] === semgrepPlatformFormat ||
@@ -874,7 +885,7 @@ function readScope(
"Semgrep native project URL does not match its recorded HTTP repository.",
);
if (mapping && projectUrl != null) {
- const key = native && repositoryContext(native, references).key;
+ const key = native && repositoryContext(native, references, target).key;
if (key ? key(native!) !== key(mapping) : projectUrl !== mapping)
throw new Error(
"Semgrep project does not match its recorded source repository URL.",
@@ -920,7 +931,7 @@ export function isSemgrepPlatformEvidence(
finding: ExternalFinding,
provider?: string,
): boolean {
- const data = finding.evidence?.source_data;
+ const data = finding.evidence.source_data;
if (generatedId.test(finding.source_finding_id)) return true;
if (!data) return false;
if (
@@ -947,6 +958,7 @@ export function isSemgrepPlatformEvidence(
function upgradeOriginalSemgrepFinding(
data: Record<string, unknown>,
references: SemgrepRepositoryReference,
+ target: ExternalImportTarget,
): Record<string, unknown> {
const {
numericFields: _witness,
@@ -958,7 +970,7 @@ function upgradeOriginalSemgrepFinding(
undefined,
typeof references === "function"
? references
- : referenceRepositoryKey(references),
+ : referenceRepositoryKey(references, target),
references,
);
return {
@@ -1023,6 +1035,7 @@ function legacyWitnessKey(
function nativeOccurrence(
retained: Record<string, unknown>,
references: SemgrepRepositoryReference,
+ target: ExternalImportTarget,
) {
let data = !isSemgrepPlatformFormat(retained["format"])
? {
@@ -1039,9 +1052,9 @@ function nativeOccurrence(
: retained;
const legacy = data["format"] === semgrepPlatformLegacyFormat;
const original = legacy && !Object.hasOwn(data, "semgrepPlatformFindingJson");
- if (original) data = upgradeOriginalSemgrepFinding(data, references);
- const { scope, aliases } = readScope(data, references);
- const repository = repositoryContext(scope.project.url, references);
+ if (original) data = upgradeOriginalSemgrepFinding(data, references, target);
+ const { scope, aliases } = readScope(data, references, target);
+ const repository = repositoryContext(scope.project.url, references, target);
const row = parseSemgrepResponse(
requiredText(data["semgrepPlatformFindingJson"], "Semgrep finding JSON"),
);
@@ -1053,6 +1066,7 @@ export function semgrepLegacyRepositoryWitnesses(
findings: readonly ExternalFinding[],
provider: string,
references: SemgrepRepositoryReference = [],
+ target: ExternalImportTarget = "local",
): ReadonlyMap<string, string> {
const witnesses = new Map<string, string>();
// Current occurrences carry their own witness. Only original rows need a sibling lookup.
@@ -1068,6 +1082,7 @@ export function semgrepLegacyRepositoryWitnesses(
const { scope, aliases, repository, row, legacy } = nativeOccurrence(
data,
references,
+ target,
);
const repo = row["repository"];
if (!isRecord(repo)) continue;
@@ -1231,7 +1246,7 @@ function checkProjectedProduct(
}
/** A marked report reserves the v1 finding's repository, ref and product for Semgrep scope. */
-function hasRetainedSemgrepScope(finding: unknown): boolean {
+export function hasRetainedSemgrepScope(finding: unknown): boolean {
if (!isRecord(finding)) return false;
const repository = finding["repository"];
if (
@@ -1262,10 +1277,14 @@ export function semgrepPlatformFindingBinding(
projectedSources: readonly (
{ url?: string; branch?: string } | undefined
)[] = [],
+ target: ExternalImportTarget = "local",
+ replayApprovedRequest = false,
) {
const retained = finding.evidence.source_data;
if (!isSemgrepPlatformEvidence(finding, provider)) {
+ // Previously approved Cloud requests keep the released projection and native checks below.
if (
+ !replayApprovedRequest &&
provider === "semgrep" &&
reportScope?.format !== undefined &&
hasRetainedSemgrepScope(retained?.["finding"])
@@ -1307,7 +1326,7 @@ export function semgrepPlatformFindingBinding(
if (!retained)
throw new Error("Missing the original Semgrep finding evidence.");
const { data, legacy, original, scope, aliases, repository, row } =
- nativeOccurrence(retained, references);
+ nativeOccurrence(retained, references, target);
checkProjectedProduct(finding, scope.product, true);
if (
reportScope?.deployment !== undefined &&
@@ -1320,6 +1339,23 @@ export function semgrepPlatformFindingBinding(
"The Semgrep report repository scope does not match the finding's deployment, project ID, ref, or product.",
);
+ const other = data["finding"];
+ if (
+ (legacy && Object.hasOwn(data, "finding")) ||
+ (isRecord(other) && Object.hasOwn(other, "id"))
+ ) {
+ const restored = restoreLegacyHostedNumbers(data)["finding"];
+ const originalRow = parseSemgrepResponse(JSON.stringify(restored));
+ const id = originalRow["id"];
+ const nativeId =
+ semgrepPlatformIdOrUndefined(id) ??
+ (typeof id === "string" ? legacyDecimalInteger(id) : undefined);
+ if (
+ (legacy || nativeId !== undefined) &&
+ stringifySemgrepFinding(originalRow) !== stringifySemgrepFinding(row)
+ )
+ throw new Error("The retained Semgrep finding representations disagree.");
+ }
const selectable = checkSemgrepPlatformRow(
row,
scope,
@@ -1444,6 +1480,7 @@ export function semgrepPlatformFindingBinding(
const details = migration?.details ?? finding.evidence.details;
return {
url: scope.project.url,
+ nativeId,
migration:
oldMappedUrl && details?.repository
? {
@@ -1462,7 +1499,7 @@ export function semgrepPlatformFindingBinding(
};
}
-/** Normalize a saved selection or a scoped Platform project export for local replay. */
+/** Normalize a saved selection or Platform project export. The default target is local. */
export function normalizeSemgrepPlatformFindings(
report: unknown,
options: string | Partial<SemgrepImportOptions> = {},
@@ -1471,8 +1508,6 @@ export function normalizeSemgrepPlatformFindings(
? (options.sourceRepositories ?? [])
: [],
) {
- if (target !== "local")
- throw new Error("Semgrep Platform findings require a local import.");
const declaredUrl =
typeof options === "string" ? options : options.sourceRepositoryUrl;
const saved = record(report, "Semgrep Platform report");
@@ -1485,12 +1520,12 @@ export function normalizeSemgrepPlatformFindings(
? upgradeSemgrepPlatformReport(
saved,
declaredUrl,
- referenceRepositoryKey(references),
+ referenceRepositoryKey(references, target),
references,
)
: saved;
- const { scope, aliases } = readScope(input, references);
- const repository = selectionRepository(scope.project.url, references);
+ const { scope, aliases } = readScope(input, references, target);
+ const repository = selectionRepository(scope.project.url, references, target);
if (
declaredUrl &&
repository.key(hostedRepositoryUrl(declaredUrl, "Semgrep")) !==
@@ -1705,11 +1740,19 @@ function normalizePlatformRows(
readId,
),
};
- const messageTitle =
+ let messageTitle =
description ??
(legacy || scope.product === "sast"
? null
: `Semgrep finding ${readId(finding["id"])}`);
+ if (
+ target === "cloud" &&
+ description &&
+ Buffer.byteLength(description) > DEFAULT_CLOUD_STRING_BYTES
+ ) {
+ const id = finding["id"];
+ messageTitle = `Semgrep finding ${text(id) ?? readId(id)}`;
+ }
if (scope.product === "sca") {
const dependency = responseRecord(
finding["found_dependency"] ?? {},
diff --git a/sdk/typescript/src/snyk-findings.ts b/sdk/typescript/src/snyk-findings.ts
index 927f37843..9f237c5b9 100644
--- a/sdk/typescript/src/snyk-findings.ts
+++ b/sdk/typescript/src/snyk-findings.ts
@@ -521,17 +521,28 @@ export function normalizeSnykJson(
);
}
+/** @internal Keep the native JSON snapshot for catalog-bound file publication. */
+export function readSnykReport(path: string): Promise<unknown> {
+ return readScannerJson(path, parseSnykReport);
+}
+
export async function readSnykFindings(
path: string,
target: ExternalImportTarget = "cloud",
references: readonly ExternalSourceRepository[] = [],
): Promise<ExternalFindings & Partial<SnykIssuesFindings>> {
- const input = await readScannerJson(path, parseSnykReport);
+ return normalizeSnykFile(await readSnykReport(path), target, references);
+}
+
+/** @internal Dispatch a parsed Snyk file using the same rules as readSnykFindings. */
+export async function normalizeSnykFile(
+ input: unknown,
+ target: ExternalImportTarget,
+ references: readonly ExternalSourceRepository[],
+): Promise<ExternalFindings & Partial<SnykIssuesFindings>> {
if (Array.isArray(input)) return normalizeSnykJson(input, target);
const document = record(input, "Snyk findings");
if (isSnykRestFormat(document["format"])) {
- if (target !== "local")
- throw new Error("Snyk REST issues currently support local import only.");
const { normalizeSnykIssues } = await import("./snyk-rest-findings.js");
return normalizeSnykIssues(
document["format"] === SNYK_REST_ISSUES_V1
diff --git a/sdk/typescript/src/snyk-rest-findings.ts b/sdk/typescript/src/snyk-rest-findings.ts
index f1597eccd..f0acc4b09 100644
--- a/sdk/typescript/src/snyk-rest-findings.ts
+++ b/sdk/typescript/src/snyk-rest-findings.ts
@@ -88,6 +88,10 @@ const scmOrigins = new Map<string, readonly string[]>([
["github-server-app", ["github-server"]],
["gitlab", ["gitlab"]],
]);
+function concreteDeployment(deployments: readonly string[] | undefined) {
+ return deployments?.length === 1 ? deployments[0] : undefined;
+}
+
const openSourceProjects = new Set([
"cargo",
"cocoapods",
@@ -352,6 +356,8 @@ function trustedScmBinding(
return { url };
}
+class SnykTargetRepositoryMismatch extends Error {}
+
function checkRemote(
remote: unknown,
selected: Scope,
@@ -388,7 +394,7 @@ function checkRemote(
gitlab,
};
if (!snykRestRepositoryMatches(binding, url))
- throw new Error(
+ throw new SnykTargetRepositoryMismatch(
"Snyk target URL does not match the selected source repository URL.",
);
return {
@@ -400,6 +406,12 @@ function checkRemote(
};
}
+function projectProduct(manager: unknown): SnykIssuesProduct | undefined {
+ if (manager === "sast") return "code";
+ if (typeof manager === "string" && openSourceProjects.has(manager))
+ return "open-source";
+}
+
function projectContext(project: Resource, selected: Scope) {
if (resourceId(project, "project") !== selected.projectId)
throw new Error(
@@ -411,9 +423,7 @@ function projectContext(project: Resource, selected: Scope) {
const scm = scmOrigins.has(origin);
if (
(!scm && origin !== "cli" && origin !== "api") ||
- (selected.product === "code"
- ? manager !== "sast"
- : !openSourceProjects.has(manager))
+ projectProduct(manager) !== selected.product
)
throw new Error(
`Snyk project type ${manager} from ${origin} is not a supported ${selected.product} project.`,
@@ -559,7 +569,7 @@ function checkTargetRelationships(
}
return {
id,
- deployment: deployments && claimed?.length === 1 ? claimed[0] : undefined,
+ deployment: deployments && concreteDeployment(claimed),
};
}
@@ -665,6 +675,22 @@ function snykIssueId(
]);
}
+function verifySnykFindingId(
+ finding: ExternalFinding,
+ attributes: Resource,
+ source: Scope,
+) {
+ const id = snykIssueId(
+ attributes,
+ source,
+ finding.evidence.source_data?.["snykRestIssueKey"],
+ );
+ if (id !== finding.source_finding_id)
+ throw new Error(
+ "The normalized finding ID does not match the original Snyk issue key and project.",
+ );
+}
+
function issuePage(
response: Resource,
selected: Scope,
@@ -814,24 +840,25 @@ function savedContext(
const snykRestId = /^snyk-rest:[0-9a-f]{64}$/u;
+function originalSnykResource(data: Resource, field: string, json: boolean) {
+ const value = data[field + (json ? "Json" : "")];
+ if (!json) return isRecord(value) ? value : undefined;
+ if (typeof value !== "string") return;
+ try {
+ const parsed = parseHostedScannerJson(value);
+ return isRecord(parsed) ? parsed : undefined;
+ } catch {
+ return;
+ }
+}
+
/** A Snyk Issues resource carries its own product and links to both its organization and project. */
function linkedSnykResources(
data: Resource,
json: boolean,
hosted: boolean,
): { issue?: Resource } | undefined {
- const original = (field: string) => {
- const value = data[field + (json ? "Json" : "")];
- if (!json) return isRecord(value) ? value : undefined;
- if (typeof value !== "string") return;
- try {
- const parsed = parseHostedScannerJson(value);
- return isRecord(parsed) ? parsed : undefined;
- } catch {
- return;
- }
- };
- const issue = original("issue");
+ const issue = originalSnykResource(data, "issue", json);
if (issue?.["type"] === "issue" && (hosted || isUuid(issue["id"]))) {
const attributes = issue["attributes"];
const links = issue["relationships"];
@@ -854,7 +881,7 @@ function linkedSnykResources(
return { issue };
}
if (!hosted && data["apiVersion"] !== apiVersion) return;
- const project = original("project");
+ const project = originalSnykResource(data, "project", json);
const attributes = project?.["attributes"];
const nativeProject =
project?.["type"] === "project" &&
@@ -863,7 +890,7 @@ function linkedSnykResources(
"origin" in attributes &&
"type" in attributes;
if (!nativeProject) {
- const target = original("target");
+ const target = originalSnykResource(data, "target", json);
if (target?.["type"] !== "target" || !isUuid(target["id"])) return;
}
if (!hosted) {
@@ -877,8 +904,78 @@ function linkedSnykResources(
return {};
}
+function retainedRelationship(
+ resource: Resource | undefined,
+ name: "target" | "integration" | "organization",
+) {
+ const links = resource?.["relationships"];
+ const relation = isRecord(links) ? links[name] : undefined;
+ const data = isRecord(relation) ? relation["data"] : undefined;
+ if (data === null) return null;
+ if (
+ isRecord(data) &&
+ (data["type"] === name ||
+ (name === "organization" && data["type"] === "org"))
+ )
+ return data;
+}
+
+/** Read only vendor-typed claims still present in an already-approved request. */
+function retainedSnykAssets(data: Resource) {
+ const assets = [];
+ const id = (resource?: Resource | null) => {
+ const value = resource?.["id"];
+ return isUuid(value) ? value.toLowerCase() : undefined;
+ };
+ for (const json of [false, true]) {
+ const project = originalSnykResource(data, "project", json);
+ let expanded;
+ if (project?.["type"] === "project") {
+ const attributes = project["attributes"];
+ const origin = isRecord(attributes) ? attributes["origin"] : undefined;
+ expanded = retainedRelationship(project, "target");
+ assets.push({
+ projectId: id(project),
+ product: projectProduct(
+ isRecord(attributes) ? attributes["type"] : undefined,
+ ),
+ targetId: expanded === null ? null : undefined,
+ organizationId: id(retainedRelationship(project, "organization")),
+ origin:
+ typeof origin === "string" && scmOrigins.has(origin)
+ ? origin
+ : undefined,
+ });
+ }
+ for (const target of [
+ expanded,
+ originalSnykResource(data, "target", json),
+ ]) {
+ if (target?.["type"] !== "target") continue;
+ const attributes = target["attributes"];
+ const url = isRecord(attributes) ? attributes["url"] : undefined;
+ const integration = retainedRelationship(target, "integration");
+ const integrationAttributes = integration?.["attributes"];
+ const type = isRecord(integrationAttributes)
+ ? integrationAttributes["integration_type"]
+ : undefined;
+ assets.push({
+ url: typeof url === "string" ? url : undefined,
+ targetId: id(target),
+ integrationId: id(integration),
+ deployment:
+ typeof type === "string"
+ ? concreteDeployment(scmOrigins.get(type))
+ : undefined,
+ organizationId: id(retainedRelationship(target, "organization")),
+ });
+ }
+ }
+ return assets;
+}
+
function snykRowEvidence(finding: ExternalFinding, hostedReport = false) {
- const retained = finding.evidence?.source_data;
+ const retained = finding.evidence.source_data;
const marked =
snykRestId.test(finding.source_finding_id) ||
isSnykRestFormat(retained?.["format"]);
@@ -892,6 +989,29 @@ export function isSnykRestFinding(finding: ExternalFinding): boolean {
return snykRowEvidence(finding) !== undefined;
}
+/** Recover the native occurrence from an earlier normalized submission with a caller-assigned ID. */
+export function snykRestCheckpointFindingId(
+ finding: ExternalFinding,
+ references: readonly ExternalSourceRepository[],
+) {
+ const data = finding.evidence.source_data;
+ const evidence = snykRowEvidence(finding);
+ if (!data || (!evidence?.v1?.issue && !evidence?.v2?.issue)) return;
+ const format = evidence.v2?.issue
+ ? SNYK_REST_ISSUES_FORMAT
+ : SNYK_REST_ISSUES_V1;
+ const document = readSnykRestDocument({
+ ...data,
+ format: data["format"] ?? format,
+ });
+ const { source } = savedContext(document, references);
+ const issue = parseHostedScannerJson(
+ requiredText(document["issueJson"], "Snyk original issue"),
+ );
+ const { attributes } = selectedIssue(scopedIssue(issue, source), source);
+ return snykIssueId(attributes, source, data["snykRestIssueKey"]);
+}
+
export function snykRestFindingRepository(
finding: ExternalFinding,
references: readonly ExternalSourceRepository[] = [],
@@ -944,15 +1064,7 @@ function bindSnykFinding(
scopedIssue(issue, source),
source,
);
- const nativeId = snykIssueId(
- attributes,
- source,
- retained["snykRestIssueKey"],
- );
- if (nativeId !== finding.source_finding_id)
- throw new Error(
- "The normalized finding ID does not match the original Snyk issue key and project.",
- );
+ verifySnykFindingId(finding, attributes, source);
projectSnykIssue(attributes, asset, { ...retained, format }, "local");
if (retain && format === SNYK_REST_ISSUES_V1) {
// The legacy witness stores the original decimal token, including its spelling.
@@ -992,11 +1104,11 @@ function bindSnykFinding(
/** Recognize a retained hosted report before verifying its selected issues. */
export function isSnykRestReport(
input: ExternalFindings & Partial<SnykIssuesFindings>,
- matchesFinding = isSnykRestFinding,
): boolean {
- if (input.snykRestContext !== undefined) return true;
- const rows = input.findings;
- return Array.isArray(rows) && rows.some(matchesFinding);
+ return (
+ input.snykRestContext !== undefined ||
+ input.findings.some(isSnykRestFinding)
+ );
}
function readIssues(document: Resource, selected: Scope) {
@@ -1194,8 +1306,6 @@ export function normalizeSnykIssues(
target: ExternalImportTarget = "local",
references: readonly ExternalSourceRepository[] = [],
): SnykIssuesFindings {
- if (target !== "local")
- throw new Error("Snyk REST issues currently support local import only.");
const wire = record(input, "Snyk REST Issues export");
const document = readSnykRestDocument(wire);
const asset = savedContext(document, references);
@@ -1254,6 +1364,8 @@ export function snykRestImportRepository(
urls: Iterable<string> = [],
trustedSources: readonly ExternalSourceRepository[] = [],
retain?: (finding: ExternalFinding, source: Resource) => void,
+ allowOpaqueLegacyRows = false,
+ declaredCloudRepository?: string,
): RepositoryBinding | undefined {
const project =
input.snykRestContext !== undefined
@@ -1268,19 +1380,46 @@ export function snykRestImportRepository(
finding,
originals: snykRowEvidence(finding, project !== undefined),
}));
- if (!project && !rows.some(({ originals }) => originals)) return;
+ const hosted =
+ project !== undefined || rows.some(({ originals }) => originals);
+ if (!hosted && !declaredCloudRepository) return;
const context = project && {
...project.source,
projectJson: JSON.stringify(project.project),
targetJson: JSON.stringify(project.target),
};
- const boundRows = rows.map(({ finding, originals }) =>
- bindSnykFinding(finding, trustedSources, originals, context, retain),
- );
+ const retainedIssues: { finding: ExternalFinding; issue: Resource }[] = [];
+ const retainedRows: Resource[] = [];
+ const boundRows = hosted
+ ? rows.flatMap(({ finding, originals }) => {
+ try {
+ return [
+ bindSnykFinding(
+ finding,
+ trustedSources,
+ originals,
+ context,
+ retain,
+ ),
+ ];
+ } catch (error) {
+ if (
+ !allowOpaqueLegacyRows ||
+ error instanceof SnykTargetRepositoryMismatch
+ )
+ throw error;
+ for (const issue of [originals?.v1?.issue, originals?.v2?.issue])
+ if (issue) retainedIssues.push({ finding, issue });
+ if (originals && finding.evidence.source_data)
+ retainedRows.push(finding.evidence.source_data);
+ return [];
+ }
+ })
+ : [];
const selected = project?.repository.scope ?? boundRows[0]?.scope;
const bindings = project ? [...boundRows, project.repository] : boundRows;
- const sourceBinding = bindings[0];
- if (!sourceBinding) return;
+ const repositoryUrl = bindings[0]?.url ?? declaredCloudRepository;
+ if (!repositoryUrl) return;
if (provider !== "snyk")
throw new Error("Use the snyk provider for Snyk REST issues.");
@@ -1295,29 +1434,83 @@ export function snykRestImportRepository(
throw new Error(
"The selected Snyk finding is missing the report's native target. Keep its full project or omit the project to use the report context.",
);
+ const retainedAssets = retainedRows.flatMap(retainedSnykAssets);
+ const monitorBindings: {
+ targetId?: string | null;
+ integrationId?: string;
+ deployment?: string;
+ }[] = [...bindings, ...retainedAssets];
for (const key of ["targetId", "integrationId", "deployment"] as const) {
- const ids = new Set(bindings.flatMap((binding) => binding[key] ?? []));
+ // Sparse targets can contradict a verified SCM deployment but cannot establish one.
+ if (key === "deployment" && !bindings.some((binding) => binding.deployment))
+ continue;
+ const ids = new Set(
+ monitorBindings
+ .map((binding) => binding[key])
+ .filter((id) => id !== undefined),
+ );
if (ids.size > 1)
throw new Error(
"The Snyk project scope names conflicting targets or integrations.",
);
}
+ const scopes = boundRows.map(({ scope }) => scope);
+ const claims = new Set(bindings.map((binding) => binding.url));
+ const retainedScms = [];
+ for (const {
+ url,
+ origin,
+ product,
+ projectId,
+ organizationId,
+ } of retainedAssets) {
+ if (url) claims.add(url);
+ if (origin) retainedScms.push(reportedScm(repositoryUrl, origin));
+ if (selected)
+ scopes.push({
+ ...selected,
+ projectId: projectId ?? selected.projectId,
+ organizationId: organizationId ?? selected.organizationId,
+ product: product ?? selected.product,
+ });
+ }
+ // Already-approved rows can lack the original project while retaining a
+ // monitored issue, partial scope, or source URL that contradicts a new claim.
+ if (selected) {
+ for (const { finding, issue } of retainedIssues) {
+ const { attributes } = selectedIssue(
+ scopedIssue(issue, selected),
+ selected,
+ );
+ if (snykRestId.test(finding.source_finding_id))
+ verifySnykFindingId(finding, attributes, selected);
+ }
+ }
+ for (const { sourceRepositoryUrl, ...fields } of retainedRows) {
+ if (sourceRepositoryUrl !== undefined)
+ claims.add(
+ requiredText(sourceRepositoryUrl, "Snyk source repository URL"),
+ );
+ if (selected) {
+ const supplied = { ...selected, ...fields };
+ scopes.push(scope(supplied, supplied.apiOrigin));
+ }
+ }
if (
- selected &&
- bindings.some(
- ({ scope, scm }) =>
- scm !== sourceBinding.scm ||
- scope.apiOrigin !== selected.apiOrigin ||
- scope.organizationId !== selected.organizationId ||
- scope.projectId !== selected.projectId ||
- scope.product !== selected.product,
- )
+ bindings.some((binding) => binding.scm !== bindings[0]?.scm) ||
+ (selected &&
+ scopes.some(
+ (scope) =>
+ scope.apiOrigin !== selected.apiOrigin ||
+ scope.organizationId !== selected.organizationId ||
+ scope.projectId !== selected.projectId ||
+ scope.product !== selected.product,
+ ))
)
throw new Error(
"The normalized finding does not match the retained Snyk project scope.",
);
- const claims = new Set(bindings.map((binding) => binding.url));
for (const binding of bindings)
if (binding.nativeUrl) claims.add(binding.nativeUrl);
if (input.sourceRepositoryUrl !== undefined)
@@ -1339,13 +1532,15 @@ export function snykRestImportRepository(
for (const url of urls) claims.add(url);
// Saved SCM metadata can contradict a caller's mapping but cannot establish one.
const source = trustedScmBinding(
- sourceBinding.url,
+ repositoryUrl,
trustedSources,
input.sourceRepositoryPlatform !== "other" &&
+ retainedScms.every(({ github }) => github) &&
bindings.every(
(binding) => binding.platform !== "other" && !binding.gitlab,
),
input.sourceRepositoryPlatform !== "github" &&
+ retainedScms.every(({ gitlab }) => gitlab) &&
bindings.every(
(binding) => binding.gitlab || binding.platform === undefined,
),
diff --git a/sdk/typescript/tests-ts/external-findings-publish.test.ts b/sdk/typescript/tests-ts/external-findings-publish.test.ts
index ad4c8c297..7a66bc869 100644
--- a/sdk/typescript/tests-ts/external-findings-publish.test.ts
+++ b/sdk/typescript/tests-ts/external-findings-publish.test.ts
@@ -36,7 +36,7 @@ import {
writeFile,
} from "node:fs/promises";
import { tmpdir } from "node:os";
-import { join } from "node:path";
+import { dirname, join } from "node:path";
import { pathToFileURL } from "node:url";
import { promisify } from "node:util";
import { gzipSync } from "node:zlib";
@@ -52,9 +52,22 @@ import {
type NormalizedExternalPublicationOptions,
type PreparedExternalPublication,
} from "../src/external-findings-publish.js";
+import { canonicalJson } from "../src/record.js";
+import {
+ normalizeSemgrepPlatformFindings,
+ selectSemgrepPlatformFindings,
+ semgrepPlatformFormat,
+} from "../src/semgrep-platform-findings.js";
+import { normalizeSemgrepFindings } from "../src/semgrep-findings.js";
+import { collectSemgrepPlatformFindings } from "../src/semgrep-platform-collector.js";
+import { normalizeSnykIssues } from "../src/snyk-rest-findings.js";
+import releasedSnykCheckpoints from "./fixtures/snyk-released-checkpoints.json" with { type: "json" };
+import { snykRestFixture } from "./support/snyk-rest-fixtures.js";
+
import {
snapshotNormalizedFindings,
type ExternalFindings,
+ type ExternalSourceRepository,
} from "../src/external-findings.js";
import {
normalizeGitHubAdvisories,
@@ -625,6 +638,7 @@ async function fixture(
loseResponse: false,
brokenReadback: false,
throttle: false,
+ catalogReads: 0,
postBudget: Infinity,
finalError: false,
};
@@ -646,13 +660,15 @@ async function fixture(
expect(incoming.headers.authorization).toBe("Bearer synthetic-token");
expect(incoming.headers["chatgpt-account-id"]).toBe("synthetic-account");
const url = new URL(incoming.url!, "http://localhost");
- if (url.pathname.endsWith("/repositories"))
+ if (url.pathname.endsWith("/repositories")) {
+ state.catalogReads++;
return json(response, {
object: "page",
data: [destination()],
has_more: false,
next: null,
});
+ }
if (url.pathname.endsWith("/source_reports")) {
const report = reports.get(url.searchParams.get("source_finding_id")!);
let summary;
@@ -810,52 +826,2578 @@ async function fixture(
json(response, { error: { message: String(error) } }, 500);
}
});
- await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
- cleanups.push(
- () =>
- new Promise<void>((resolve, reject) => {
- server.close((error) => (error ? reject(error) : resolve()));
- server.closeAllConnections();
- }),
+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
+ cleanups.push(
+ () =>
+ new Promise<void>((resolve, reject) => {
+ server.close((error) => (error ? reject(error) : resolve()));
+ server.closeAllConnections();
+ }),
+ );
+ const port = (server.address() as { port: number }).port;
+ const transport = (url: string, init: RequestInit) =>
+ fetch(
+ `http://127.0.0.1:${port}${new URL(url).pathname}${new URL(url).search}`,
+ init,
+ );
+ const deps = { environment, fetch: transport };
+ const cliDeps = { ...dependencies(), environment, cloudFetch: transport };
+ const command = [
+ "publish",
+ "findings",
+ file,
+ "--to",
+ "cloud",
+ "--repository",
+ options.repository,
+ "--provider",
+ options.provider,
+ "--source-key",
+ options.sourceKey,
+ "--format",
+ "json",
+ ];
+ return {
+ root,
+ file,
+ reports,
+ receipts,
+ posts,
+ state,
+ destination,
+ environment,
+ port,
+ deps,
+ cliDeps,
+ command,
+ async loseResponse(prepared: PreparedExternalPublication) {
+ state.loseResponse = true;
+ const failure: unknown = await prepared
+ .publish()
+ .catch((error: unknown) => error);
+ if (!(failure instanceof ExternalPublicationError))
+ throw new Error("Expected an interrupted publication", {
+ cause: failure,
+ });
+ expect(failure.message).toContain("resume the saved request");
+ return failure.result.savedSubmission!;
+ },
+ };
+}
+
+async function hostedFixture(
+ provider: "semgrep" | "snyk",
+ records: unknown,
+ url: string,
+ sourceKey = options.sourceKey,
+) {
+ const cloud = await fixture(records, url);
+ const settings = { ...options, provider, sourceKey };
+ return {
+ ...cloud,
+ prepareFile: () =>
+ prepareExternalPublication(cloud.file, settings, cloud.deps),
+ prepareNormalized: <Input extends ExternalFindings>(
+ input: Input,
+ selectedProvider: FindingImportRequest["source"]["provider"] = provider,
+ ) =>
+ prepareNormalizedExternalPublication(
+ input,
+ { ...settings, provider: selectedProvider },
+ cloud.deps,
+ ),
+ };
+}
+
+test.each(["open-source", "code"] as const)(
+ "private GitLab SSH %s findings match the catalog through file and SDK imports",
+ async (product) => {
+ const url = "https://scm.example.test/Team/Project";
+ const report = snykRestFixture(product, { sourceRepositoryUrl: url });
+ const project = JSON.parse(report.projectJson);
+ project.attributes.origin = "gitlab";
+ project.relationships.target.data.attributes.url =
+ "[email protected]:Team/Project.git";
+ report.projectJson = JSON.stringify(project);
+ const references: ExternalSourceRepository[] = [
+ { url, platform: "gitlab" },
+ ];
+ const input = normalizeSnykIssues(report, "cloud", references);
+ const f = await fixture(report, url);
+ const settings = { ...options, provider: "snyk" as const };
+ const deps = { ...f.deps, sourceRepositories: references };
+ const file = () => prepareExternalPublication(f.file, settings, deps);
+ const sdk = () =>
+ prepareNormalizedExternalPublication(input, settings, deps);
+
+ for (const wrong of [url.toLowerCase(), url + "-different"]) {
+ f.state.repositoryUrl = wrong;
+ await expect(file()).rejects.toThrow(/repository|project/iu);
+ await expect(sdk()).rejects.toThrow(/repository|project/iu);
+ }
+ f.state.repositoryUrl = url;
+ const differentReader = {
+ ...deps,
+ sourceRepositories: [
+ ...references,
+ { url: url.toLowerCase(), platform: "gitlab" as const },
+ ],
+ };
+ await expect(
+ prepareExternalPublication(f.file, settings, differentReader),
+ ).rejects.toThrow(/repository|project/iu);
+ await expect(
+ prepareNormalizedExternalPublication(input, settings, differentReader),
+ ).rejects.toThrow(/repository|project/iu);
+ if (product === "code") {
+ const mislabeled = structuredClone(input);
+ mislabeled.findings[0]!.evidence.details!.repository!.url =
+ url.toLowerCase();
+ await expect(
+ prepareNormalizedExternalPublication(mislabeled, settings, deps),
+ ).rejects.toThrow(/repository|project/iu);
+ }
+ expect(f.posts).toHaveLength(0);
+ expect((await (await file()).publish()).counts.created).toBe(1);
+ expect((await (await sdk()).publish()).counts.unchanged).toBe(1);
+ },
+);
+
+function selectedSemgrepReport(
+ project: { name: string; url: string },
+ fields: Record<string, unknown>,
+ sourceRepositories: readonly ExternalSourceRepository[] = [],
+) {
+ const ref = "refs/heads/release";
+ const finding = {
+ id: 23,
+ ref,
+ repository: { url: project.url },
+ severity: "high",
+ status: "open",
+ ...fields,
+ };
+ return selectSemgrepPlatformFindings({
+ deployment: "example",
+ ref,
+ projectResponse: JSON.stringify({ project: { id: 14, ...project } }),
+ findingsResponses: [JSON.stringify({ findings: [finding] })],
+ findingIds: ["23"],
+ sourceRepositories,
+ });
+}
+
+test("a projected Snyk issue resumes its approved private repository after a rename", async () => {
+ const url = "https://git.example.test/Team/OldProject";
+ const renamed = "https://git.example.test/Team/NewProject";
+ const capture = snykRestFixture("open-source", { sourceRepositoryUrl: url });
+ const project = JSON.parse(capture.projectJson);
+ project.attributes.origin = "cli";
+ project.relationships.target.data.attributes.url =
+ "[email protected]:team/oldproject.git";
+ capture.projectJson = JSON.stringify(project);
+ const report = normalizeSnykIssues(capture, "cloud", [
+ { url, platform: "github" },
+ ]);
+ const row = report.findings[0]!;
+ row.evidence.source_data = {
+ issueJson: row.evidence.source_data!["issueJson"],
+ };
+ const f = await hostedFixture("snyk", capture, url);
+ await f.loseResponse(await f.prepareNormalized(report));
+ f.state.repositoryUrl = renamed;
+ const resumed = await f.prepareNormalized(report);
+ expect(resumed.preview.resumed).toBe(true);
+ await resumed.publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+ const fresh = await hostedFixture("snyk", capture, renamed);
+ await expect(fresh.prepareNormalized(report)).rejects.toThrow(
+ "Snyk target URL",
+ );
+ expect(fresh.posts).toHaveLength(0);
+});
+
+test("a self-contained Snyk issue keeps its Cloud identity with or without a report header", async () => {
+ const capture = snykRestFixture("open-source");
+ const report = normalizeSnykIssues(capture);
+ const { read, findings, excluded } = report;
+ const data = findings[0]!.evidence.source_data!;
+ data["issueJson"] = JSON.stringify(
+ JSON.parse(String(data["issueJson"])),
+ null,
+ 2,
+ );
+ const f = await hostedFixture("snyk", capture, capture.sourceRepositoryUrl);
+ await f.loseResponse(await f.prepareNormalized(report));
+ const resumed = await f.prepareNormalized({ read, findings, excluded });
+ expect(resumed.preview.resumed).toBe(true);
+ await resumed.publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+ expect(
+ (await (await f.prepareNormalized(report)).publish()).counts.unchanged,
+ ).toBe(1);
+});
+
+test("a pending projected Snyk issue keeps its generated ID bound to the original API region", async () => {
+ const capture = snykRestFixture("open-source");
+ const report = normalizeSnykIssues(capture);
+ const row = report.findings[0]!;
+ row.evidence.source_data = {
+ issueJson: row.evidence.source_data!["issueJson"],
+ };
+ const f = await hostedFixture("snyk", capture, capture.sourceRepositoryUrl);
+ const prepare = (apiOrigin: string) => {
+ const projected = {
+ ...report,
+ snykRestContext: { ...report.snykRestContext, apiOrigin },
+ };
+ return prepareNormalizedExternalPublication(
+ projected,
+ { ...options, provider: "snyk" },
+ f.deps,
+ );
+ };
+ const selected = await prepare(capture.apiOrigin);
+ expect(selected.preview.findings).toEqual(
+ (await f.prepareFile()).preview.findings,
+ );
+ const pending = await f.loseResponse(selected);
+ const before = await readFile(pending);
+ await expect(prepare("https://api.snyk.io")).rejects.toThrow(
+ "normalized finding ID",
+ );
+ expect(await readFile(pending)).toEqual(before);
+ expect((await prepare(capture.apiOrigin)).preview.resumed).toBe(true);
+ await (await f.prepareFile()).publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+});
+
+test("a resumed Snyk issue checks its retained target against a new SCM mapping", async () => {
+ const url = "https://scm.example.test/Team/Project";
+ const report = snykRestFixture("open-source", { sourceRepositoryUrl: url });
+ const project = JSON.parse(report.projectJson);
+ project.attributes.origin = "cli";
+ project.relationships.target.data.attributes.url =
+ "[email protected]:team/project.git";
+ report.projectJson = JSON.stringify(project);
+ const references = [{ url, platform: "github" as const }];
+ const { read, findings, excluded } = normalizeSnykIssues(
+ report,
+ "cloud",
+ references,
+ );
+ const f = await fixture(report, url);
+ const prepare = (platform: "github" | "gitlab") =>
+ prepareNormalizedExternalPublication(
+ { read, findings, excluded },
+ { ...options, provider: "snyk" },
+ { ...f.deps, sourceRepositories: [{ url, platform }] },
+ );
+
+ await expect(prepare("gitlab")).rejects.toThrow("Snyk target URL");
+ const pending = await f.loseResponse(await prepare("github"));
+ const before = await readFile(pending);
+ await expect(prepare("gitlab")).rejects.toThrow("Snyk target URL");
+ expect(await readFile(pending)).toEqual(before);
+ await (await prepare("github")).publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+});
+
+test("Cloud Semgrep reader references retain the Git HTTP host", async () => {
+ const f = await fixture();
+ const prepare = (host: string, reader: string) => {
+ const url = `https://${host}/org/project`;
+ f.state.repositoryUrl = url;
+ return prepareNormalizedExternalPublication(
+ {
+ read: 1,
+ excluded: [],
+ findings: [
+ {
+ source_finding_id: "generic-1",
+ evidence: {
+ title: "Example issue",
+ severity: "high",
+ details: { kind: "sast", repository: { url } },
+ },
+ },
+ ],
+ },
+ { ...options, provider: "semgrep" },
+ {
+ ...f.deps,
+ sourceRepositories: [
+ { url: `https://${reader}/org/project`, platform: "github" },
+ ],
+ },
+ );
+ };
+ await expect(prepare("127.0.0.1", "0177.1.")).rejects.toThrow(
+ /does not match/,
+ );
+ for (const host of ["127.0.0.1", "[::1]", "0177.1."]) {
+ expect((await prepare(host, host)).preview.findings).toHaveLength(1);
+ }
+});
+
+test("a Semgrep file retry preserves the approved private repository's case", async () => {
+ const url = "https://scm.example.test/Team/Project";
+ const ref = "refs/heads/topic";
+ const report = {
+ format: "semgrep-platform-findings/v1",
+ deployment: "example",
+ project: { id: 10, name: "Team/Project", url },
+ ref,
+ product: "sast",
+ findingCount: 1,
+ findings: [{ id: 12, ref, repository: { url }, rule_name: "example.rule" }],
+ };
+ const f = await hostedFixture("semgrep", report, url);
+ const settings = { ...options, provider: "semgrep" as const };
+ const strict = {
+ ...f.deps,
+ sourceRepositories: [{ url, platform: "gitlab" as const }],
+ };
+ await f.loseResponse(
+ await prepareExternalPublication(f.file, settings, strict),
+ );
+ await writeFile(
+ f.file,
+ JSON.stringify({ ...report, sourceRepositoryUrl: url.toLowerCase() }),
+ );
+ await expect(
+ prepareExternalPublication(f.file, settings, strict),
+ ).rejects.toThrow(/repository/iu);
+ await expect(
+ prepareExternalPublication(f.file, settings, f.deps),
+ ).rejects.toThrow(/repository/iu);
+ expect(f.posts).toHaveLength(1);
+ await writeFile(f.file, JSON.stringify(report));
+ const retry = await prepareExternalPublication(f.file, settings, f.deps);
+ expect(retry.preview.resumed).toBe(true);
+ await retry.publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+ expect(f.receipts.size).toBe(1);
+});
+
+test.each(["file", "SDK"] as const)(
+ "a private GitHub repository recognizes an offline Semgrep capture on a %s retry",
+ async (first) => {
+ const url = "https://scm.example.test/Org/Repo";
+ const project = { name: "Org/Repo", id: 77, url };
+ const ref = "refs/heads/topic";
+ const native = {
+ id: 23,
+ ref,
+ repository: { id: project.id, name: "org/repo" },
+ rule: { name: "example.rule", message: "Example issue" },
+ location: { file_path: "src/example.ts", line: 2 },
+ severity: "informational",
+ };
+ const report = await collectSemgrepPlatformFindings(
+ {
+ project: project.name,
+ deployment: "example",
+ ref,
+ token: "synthetic-token",
+ },
+ {
+ environment: { CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: "1" },
+ fetch: async (url) =>
+ Response.json(
+ url.pathname.endsWith("/findings")
+ ? { findings: [native] }
+ : { project },
+ ),
+ },
+ );
+ const offline = normalizeSemgrepPlatformFindings(report);
+ const row = offline.findings[0]!;
+ expect(row.evidence.source_data?.["format"]).toBe(semgrepPlatformFormat);
+ const f = await hostedFixture("semgrep", report, url);
+ const sdk = () => f.prepareNormalized(offline);
+ const file = await f.prepareFile();
+ expect(file.preview.findings[0]?.evidence.source_data?.["format"]).toBe(
+ "semgrep-platform-selection/v1",
+ );
+ expect((await sdk()).preview.findings).toEqual(file.preview.findings);
+ await f.loseResponse(first === "file" ? file : await sdk());
+ const retry = await (first === "file" ? sdk() : f.prepareFile());
+ expect(retry.preview.resumed).toBe(true);
+ await retry.publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+ expect(f.receipts.size).toBe(1);
+ expect((await (await sdk()).publish()).counts.unchanged).toBe(1);
+ expect(f.reports.size).toBe(1);
+
+ row.source_finding_id = "custom-occurrence";
+ row.evidence.severity = "high";
+ row.evidence.source_data!["reviewNote"] = "Keep This Case";
+ expect((await sdk()).preview.findings[0]).toMatchObject({
+ source_finding_id: "custom-occurrence",
+ evidence: {
+ severity: "high",
+ source_data: {
+ reviewNote: "Keep This Case",
+ semgrepPlatformFindingJson:
+ row.evidence.source_data!["semgrepPlatformFindingJson"],
+ },
+ },
+ });
+ },
+);
+
+test("an independent GitLab mapping preserves Semgrep repository case on a private host", async () => {
+ const url = "https://scm.example.test/Team/Project";
+ const sourceRepositories = [{ url, platform: "gitlab" as const }];
+ const report = selectedSemgrepReport(
+ { name: "Team/Project", url },
+ { rule_name: "sample.rule" },
+ sourceRepositories,
+ );
+ const f = await fixture(report, url);
+ const settings = { ...options, provider: "semgrep" as const };
+ const deps = { ...f.deps, sourceRepositories };
+ const file = () => prepareExternalPublication(f.file, settings, deps);
+ const { findings } = (await file()).preview;
+ const input = { read: findings.length, excluded: [], findings };
+ f.state.repositoryUrl = url.toLowerCase();
+ await expect(file()).rejects.toThrow(/repository|project/iu);
+ await expect(
+ prepareNormalizedExternalPublication(input, settings, deps),
+ ).rejects.toThrow(/repository|project/iu);
+ expect(f.posts).toHaveLength(0);
+});
+
+test.each(["semgrep", "snyk"] as const)(
+ "%s checks typed-only repository evidence and reader ports against Cloud",
+ async (provider) => {
+ const url = "https://scm.example.test/Group/Project";
+ const f = await fixture([], url);
+ const prepare = (
+ typed: string,
+ reference: string,
+ platform: "github" | "gitlab",
+ ) =>
+ prepareNormalizedExternalPublication(
+ {
+ read: 1,
+ excluded: [],
+ findings: [
+ {
+ source_finding_id: "reader-1",
+ evidence: {
+ title: "Example issue",
+ severity: "high",
+ details: { kind: "sast", repository: { url: typed } },
+ },
+ },
+ ],
+ },
+ { ...options, provider },
+ { ...f.deps, sourceRepositories: [{ url: reference, platform }] },
+ );
+ await expect(prepare(url.toLowerCase(), url, "gitlab")).rejects.toThrow(
+ /repository|project/iu,
+ );
+ const withPort = url.replace(".test/", ".test:443/");
+ for (const scm of ["github", "gitlab"] as const) {
+ await expect(prepare(url, withPort, scm)).rejects.toThrow(
+ /repository|project/iu,
+ );
+ }
+ expect((await prepare(url, url, "gitlab")).preview.findings).toHaveLength(
+ 1,
+ );
+ expect(
+ (await prepare(url.toLowerCase(), url, "github")).preview.findings,
+ ).toHaveLength(1);
+ f.state.repositoryUrl = withPort;
+ await expect(prepare(withPort, url, "gitlab")).rejects.toThrow(
+ /repository|project/iu,
+ );
+ expect(
+ (await prepare(withPort, withPort, "gitlab")).preview.findings,
+ ).toHaveLength(1);
+ },
+);
+
+test("Semgrep publication compares the operator's source URL with Cloud repository port semantics", async () => {
+ const url = "https://scm.example.test/Team/Project";
+ const report = selectedSemgrepReport(
+ { name: "Team/Project", url },
+ { rule_name: "sample.rule" },
+ );
+ const f = await hostedFixture("semgrep", report, url);
+ const prepare = (sourceRepositoryUrl: string) =>
+ prepareExternalPublication(
+ f.file,
+ { ...options, provider: "semgrep", sourceRepositoryUrl },
+ f.deps,
+ );
+ const different = "https://scm.example.test:443/Team/Project";
+ await expect(prepare(different)).rejects.toThrow(/does not match/i);
+ expect(f.posts).toHaveLength(0);
+ await f.loseResponse(await prepare(url));
+ await expect(prepare(different)).rejects.toThrow(/does not match/i);
+ expect(f.posts).toHaveLength(1);
+ expect((await prepare(url)).preview.resumed).toBe(true);
+});
+
+test("Semgrep Cloud checks the original port on a selected finding's repository URL", async () => {
+ const url = "https://scm.example.test/Team/Project";
+ const report = selectedSemgrepReport(
+ { name: "Team/Project", url },
+ {
+ repository: { url: "https://scm.example.test:443/Team/Project" },
+ rule_name: "sample.rule",
+ },
+ );
+ const f = await hostedFixture("semgrep", report, url);
+ await expect(f.prepareFile()).rejects.toThrow(/repository/iu);
+ const sdk = normalizeSemgrepFindings(report, {}, "local");
+ await expect(f.prepareNormalized(sdk)).rejects.toThrow(/repository/iu);
+ expect(f.posts).toHaveLength(0);
+});
+
+test("Semgrep reader aliases also match the normalized source's SCM policy", async () => {
+ const url = "https://scm.example.test/Team/Project";
+ const report = selectedSemgrepReport(
+ { name: "Team/Project", url },
+ { rule_name: "sample.rule" },
+ );
+ const f = await hostedFixture("semgrep", report, url);
+ const native = normalizeSemgrepFindings(report, {}, "cloud");
+ const strict = { ...native, sourceRepositoryPlatform: "other" as const };
+ const prepare = (source: typeof native, readerUrl: string) =>
+ prepareNormalizedExternalPublication(
+ source,
+ { ...options, provider: "semgrep" },
+ {
+ ...f.deps,
+ sourceRepositories: [{ platform: "github", url: readerUrl }],
+ },
+ );
+ expect(
+ (await prepare(native, url.toLowerCase())).preview.findings,
+ ).toHaveLength(1);
+ expect(
+ (await prepare(native, "[email protected]:Team/Project.git")).preview
+ .findings,
+ ).toHaveLength(1);
+ expect(
+ (await prepare(strict, "[email protected]:Team/Project")).preview
+ .findings,
+ ).toHaveLength(1);
+ await expect(prepare(strict, url.toLowerCase())).rejects.toThrow(
+ /repository/iu,
+ );
+ expect(f.posts).toHaveLength(0);
+});
+
+test("an undefined optional Snyk context permits generic SDK evidence", async () => {
+ class Report {
+ snykRestContext?: Record<string, unknown>;
+ read = 1;
+ excluded = [];
+ findings = [
+ {
+ source_finding_id: "generic-1",
+ evidence: { title: "Example issue", severity: "high" as const },
+ },
+ ];
+ }
+ const f = await fixture();
+ const report = new Report();
+ const prepared = await prepareNormalizedExternalPublication(
+ report,
+ options,
+ f.deps,
+ );
+ expect((await prepared.publish()).counts.created).toBe(1);
+ report.snykRestContext = {};
+ await expect(
+ prepareNormalizedExternalPublication(
+ report,
+ { ...options, provider: "snyk" },
+ f.deps,
+ ),
+ ).rejects.toThrow("Expected a versioned Snyk REST report context");
+});
+
+test("a saved selection with Guardian evidence cannot resume a Cloud upload", async () => {
+ const f = await fixture([normalized()]);
+ const prepare = () => prepareExternalPublication(f.file, options, f.deps);
+ const path = await f.loseResponse(await prepare());
+ const saved = JSON.parse(await readFile(path, "utf8"));
+ const { source_finding_id, evidence } = saved.requests[0].items[0];
+ evidence.source_data = { format: "semgrep-guardian-selection/v1" };
+ // An older reader can store a different projection of the same finding.
+ saved.inputProjection.savedEvidenceDigest = hash(
+ "sha256",
+ canonicalJson([{ source_finding_id, evidence }]),
+ );
+ const bytes = JSON.stringify(saved);
+ await writeFile(path, bytes);
+ await expect(prepare()).rejects.toThrow(
+ /Guardian findings require a local import/,
+ );
+ expect(f.posts).toHaveLength(1);
+ expect(await readFile(path, "utf8")).toBe(bytes);
+});
+
+test("a normalized Semgrep report checks the provider before Cloud credentials", async () => {
+ const url = "https://github.com/example/project";
+ const report = normalizeSemgrepFindings(
+ selectedSemgrepReport({ name: "example/project", url }, {}),
+ {},
+ "cloud",
+ );
+ await expect(
+ prepareNormalizedExternalPublication(
+ report,
+ { ...options, provider: "wiz" },
+ {
+ environment: { CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: "1" },
+ credentials: async () => {
+ throw new Error("Unexpected credential lookup");
+ },
+ },
+ ),
+ ).rejects.toThrow("requires the semgrep provider");
+});
+
+test.each(["ticket", "HEAD", "release"] as const)(
+ "Cloud replays a previously approved Semgrep request with its original scope checks (%s)",
+ async (kind) => {
+ const url = "https://github.com/example/project";
+ const other = "https://github.com/example/other";
+ const ref = kind === "ticket" ? "refs/heads/release" : kind;
+ const scope = {
+ deployment: "example",
+ projectId: "14",
+ ref,
+ product: "sast",
+ };
+ const capture = {
+ format: "semgrep-platform-findings/v1",
+ deployment: scope.deployment,
+ ref,
+ product: scope.product,
+ project: { id: 14, name: "example/project", url },
+ findingCount: 1,
+ findings: [{ id: 23, rule_name: "example.rule", ref }],
+ };
+ const f = await hostedFixture("semgrep", capture, url);
+ const report = normalizeSemgrepFindings(capture, {}, "cloud");
+ const row = report.findings[0]!;
+ const bare = {
+ read: 1,
+ excluded: [],
+ findings: [row],
+ sourceRepositoryUrl: url,
+ };
+ const marked = {
+ ...bare,
+ sourceFormat: "semgrep-platform-findings/v1" as const,
+ semgrepPlatformScope: scope,
+ };
+ if (kind === "ticket") {
+ row.source_finding_id = "operator-assigned";
+ row.evidence.source_data = {
+ format: "ticket/v1",
+ finding: { id: "TRACK-23", repository: { url: other } },
+ };
+ await expect(f.prepareNormalized(marked)).rejects.toThrow(
+ /original collection scope/iu,
+ );
+ }
+ const path = await f.loseResponse(
+ await f.prepareNormalized(kind === "ticket" ? bare : marked),
+ );
+ const saved = JSON.parse(await readFile(path, "utf8"));
+ // The released publisher stored whether a report was marked and the original evidence digests.
+ const { inputDigest, savedEvidenceDigest } = saved.inputProjection;
+ saved.inputProjection = {
+ retainedHostedSelection: true,
+ inputDigest,
+ savedEvidenceDigest,
+ };
+ const bytes = JSON.stringify(saved);
+ await writeFile(
+ path,
+ JSON.stringify({ ...saved, accountId: "other-account" }),
+ );
+ await expect(f.prepareNormalized(marked)).rejects.toThrow(/account/iu);
+ await writeFile(path, bytes);
+ await expect(
+ f.prepareNormalized({ ...marked, sourceRepositoryUrl: other }),
+ ).rejects.toThrow(/scope|repository/iu);
+ if (kind !== "ticket") {
+ await expect(
+ f.prepareNormalized({
+ ...marked,
+ semgrepPlatformScope: { ...scope, ref: "refs/heads/" + ref },
+ }),
+ ).rejects.toThrow(/scope|ref/iu);
+ }
+ expect(await readFile(path, "utf8")).toBe(bytes);
+ expect(f.posts).toHaveLength(1);
+ const retry = await f.prepareNormalized(marked);
+ expect(retry.preview.resumed).toBe(true);
+ await retry.publish();
+ expect(f.posts).toEqual([
+ JSON.stringify(saved.requests[0]),
+ JSON.stringify(saved.requests[0]),
+ ]);
+ },
+);
+
+test.each(["ticket metadata", "retained ref"] as const)(
+ "a mixed Semgrep request retries with an unmarked row containing %s",
+ async (kind) => {
+ const url = "https://github.com/example/project";
+ const capture = selectedSemgrepReport({ name: "example/project", url }, {});
+ const report = normalizeSemgrepFindings(capture, {}, "cloud");
+ const finding =
+ kind === "ticket metadata"
+ ? { id: "T-1", title: "Ticket" }
+ : { id: 23, ref: "refs/heads/other" };
+ const unmarked = {
+ read: 2,
+ excluded: [],
+ findings: [
+ ...report.findings,
+ {
+ source_finding_id: "ticket-1",
+ evidence: {
+ title: "Ticket",
+ severity: "low" as const,
+ source_data: { finding },
+ },
+ },
+ ],
+ };
+ const f = await hostedFixture("semgrep", capture, url);
+ const first = await f.prepareNormalized(unmarked);
+ const pending = await f.loseResponse(first);
+ const marked = { ...report, ...unmarked };
+ if (kind === "retained ref") {
+ const bytes = await readFile(pending);
+ await expect(f.prepareNormalized(marked)).rejects.toThrow(/scope/iu);
+ expect(await readFile(pending)).toEqual(bytes);
+ expect(f.posts).toHaveLength(1);
+ }
+ const retry = await f.prepareNormalized(
+ kind === "ticket metadata" ? marked : unmarked,
+ );
+ expect(retry.preview.resumed).toBe(true);
+ expect(retry.preview.requests).toEqual(first.preview.requests);
+ await retry.publish();
+ expect(f.posts).toEqual([f.posts[0]!, f.posts[0]!]);
+ },
+);
+
+test("Cloud binds newly approved marked ticket context to its deployment", async () => {
+ const url = "https://github.com/example/project";
+ const f = await hostedFixture("semgrep", semgrepJson(), url);
+ const report = normalizeSemgrepFindings(
+ semgrepJson(),
+ { sourceRepositoryUrl: url },
+ "cloud",
+ );
+ report.findings[0]!.evidence.source_data = {
+ ticket: { id: "TRACK-23", ref: "HEAD" },
+ };
+ const scope = {
+ deployment: "example",
+ projectId: "14",
+ ref: "refs/heads/release",
+ product: "sast",
+ };
+ const input = {
+ ...report,
+ sourceRepositoryUrl: url,
+ sourceFormat: "semgrep-platform-findings/v1" as const,
+ semgrepPlatformScope: scope,
+ };
+ const path = await f.loseResponse(await f.prepareNormalized(input));
+ const bytes = await readFile(path, "utf8");
+ await expect(
+ f.prepareNormalized({
+ ...input,
+ semgrepPlatformScope: { ...scope, deployment: "another" },
+ }),
+ ).rejects.toThrow(/scope|deployment/iu);
+ expect(await readFile(path, "utf8")).toBe(bytes);
+ expect(f.posts).toHaveLength(1);
+ const retry = await f.prepareNormalized(input);
+ expect(retry.preview.resumed).toBe(true);
+ await retry.publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+});
+
+test.each(["marked", "unmarked"] as const)(
+ "Cloud preserves a Semgrep approval's scope when recovering the raw checkpoint key during %s retries",
+ async (retryKind) => {
+ const changedScope = retryKind === "marked";
+ const url = "https://scm.example.test/Org/Repo";
+ const capture = {
+ format: semgrepPlatformFormat,
+ repositoryAliases: ["org/repo"],
+ deployment: "example",
+ ref: "refs/heads/release",
+ product: "sast",
+ project: { id: "14", name: "Org/Repo", url },
+ findingCount: 1,
+ findings: [
+ JSON.stringify({
+ id: 23,
+ rule_name: "sample.rule",
+ repository: { id: 14, name: "org/repo" },
+ ref: "refs/heads/release",
+ }),
+ ],
+ };
+ const f = await hostedFixture("semgrep", capture, url);
+ const native = normalizeSemgrepPlatformFindings(capture);
+ const ticket = {
+ source_finding_id: "custom",
+ evidence: {
+ title: "Ticket",
+ severity: "high" as const,
+ source_data: { finding: { ref: "refs/heads/other" } },
+ },
+ };
+ const findings = [...native.findings, ticket];
+ const bare = {
+ read: 2,
+ excluded: [],
+ findings,
+ sourceRepositoryPlatform: "other" as const,
+ sourceRepositoryUrl: url,
+ };
+ const marked = { ...native, read: 2, findings };
+ const mapped = { ...bare, sourceRepositoryPlatform: "github" as const };
+ const path = await f.loseResponse(await f.prepareNormalized(bare));
+ const bytes = await readFile(path, "utf8");
+
+ // The raw publisher installs its checkpoint after the mapped reader misses the raw key.
+ const staged = path + ".staged";
+ await fs.rename(path, staged);
+ const originalRead = fs.readFile;
+ let installed = false;
+ const lookup = spyOn(fs, "readFile").mockImplementation(((
+ ...args: Parameters<typeof fs.readFile>
+ ) => {
+ const result = originalRead(...args);
+ if (args[0] !== path || installed) return result;
+ installed = true;
+ return result.finally(() => fs.rename(staged, path));
+ }) as typeof fs.readFile);
+ try {
+ if (changedScope)
+ await expect(f.prepareNormalized(marked)).rejects.toThrow(
+ /approved request/iu,
+ );
+ else
+ expect((await f.prepareNormalized(mapped)).preview.resumed).toBe(true);
+ } finally {
+ lookup.mockRestore();
+ }
+ expect(installed).toBe(true);
+ if (changedScope) expect(await readFile(path, "utf8")).toBe(bytes);
+ else {
+ await expect(fs.stat(path)).rejects.toMatchObject({ code: "ENOENT" });
+ await expect(f.prepareNormalized(marked)).rejects.toThrow(
+ /approved request/iu,
+ );
+ }
+ expect(f.posts).toHaveLength(1);
+ const resumed = await f.prepareNormalized(changedScope ? bare : mapped);
+ expect(resumed.preview.requests).toEqual(JSON.parse(bytes).requests);
+ expect(resumed.preview.resumed).toBe(true);
+ await resumed.publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+ },
+);
+
+test("Cloud checks both native Semgrep rows when a projection retains both formats", async () => {
+ const url = "https://github.com/example/project";
+ const f = await hostedFixture(
+ "semgrep",
+ selectedSemgrepReport(
+ { name: "example/project", url },
+ { rule_name: "sample.rule" },
+ ),
+ url,
+ );
+ const { findings } = (await f.prepareFile()).preview;
+ const input = { read: 1, excluded: [], findings };
+ const raw = findings[0]!.evidence.source_data!;
+ const json = String(raw["semgrepPlatformFindingJson"]);
+ const row = { ...JSON.parse(json), attempts: 1, scoreRatio: 1.25 };
+ raw["finding"] = { annotation: "operator note" };
+ expect((await f.prepareNormalized(input)).preview.findings).toHaveLength(1);
+ const score = "9007199254740993";
+ raw["semgrepPlatformFindingJson"] =
+ `{"attempts":1.0,"scoreRatio":1.250,"score":${score},${json.slice(1)}`;
+ raw["finding"] = { ...row, score };
+ raw["numericFields"] = { finding: { score } };
+ raw["format"] = "semgrep-platform-findings/v1";
+ raw["repositoryAliases"] = [];
+ expect((await f.prepareNormalized(input)).preview.findings).toHaveLength(1);
+ raw["finding"] = {
+ ...row,
+ score,
+ repository: { url: "https://github.com/foreign/project" },
+ };
+ await expect(f.prepareNormalized(input)).rejects.toThrow(
+ /Semgrep.*disagree/i,
+ );
+ raw["format"] = "semgrep-platform-selection/v1";
+ await expect(f.prepareNormalized(input)).rejects.toThrow(
+ /Semgrep.*disagree/i,
+ );
+ raw["finding"] = { ...row, score, id: 31 };
+ await expect(f.prepareNormalized(input)).rejects.toThrow(
+ /Semgrep.*disagree/i,
+ );
+ expect(f.posts).toHaveLength(0);
+});
+
+test("textual and witnessed Semgrep finding IDs are checked even above the signed range", async () => {
+ const url = "https://github.com/example/project";
+ const report = selectedSemgrepReport(
+ { name: "example/project", url },
+ { rule_name: "sample.rule" },
+ );
+ const id = "1000000000000000000002";
+ report.findings[0] = report.findings[0]!.replace('"id":23', '"id":' + id);
+ const f = await hostedFixture("semgrep", report, url);
+ const { findings } = (await f.prepareFile()).preview;
+ const input = { read: 1, excluded: [], findings };
+ const raw = findings[0]!.evidence.source_data!;
+ const original = {
+ ...JSON.parse(String(raw["semgrepPlatformFindingJson"])),
+ id,
+ };
+ raw["numericFields"] = { finding: { id } };
+ raw["finding"] = {
+ ...original,
+ repository: { url: "https://github.com/foreign/project" },
+ };
+ await expect(f.prepareNormalized(input)).rejects.toThrow(
+ /Semgrep.*disagree/i,
+ );
+ raw["finding"] = original;
+ expect((await f.prepareNormalized(input)).preview.findings).toHaveLength(1);
+ const otherId = "1000000000000000000003";
+ raw["finding"] = { ...original, id: otherId };
+ raw["numericFields"] = { finding: { id: otherId } };
+ await expect(f.prepareNormalized(input)).rejects.toThrow(
+ /Semgrep.*disagree/i,
+ );
+ const numericJson = raw["semgrepPlatformFindingJson"];
+ delete raw["numericFields"];
+ raw["semgrepPlatformFindingJson"] = JSON.stringify(original);
+ raw["finding"] = {
+ ...original,
+ repository: { url: "https://github.com/foreign/project" },
+ };
+ await expect(f.prepareNormalized(input)).rejects.toThrow(
+ /Semgrep.*disagree/i,
+ );
+ raw["finding"] = original;
+ expect((await f.prepareNormalized(input)).preview.findings).toHaveLength(1);
+ raw["semgrepPlatformFindingJson"] = numericJson;
+ await expect(f.prepareNormalized(input)).rejects.toThrow(
+ /Semgrep.*disagree/i,
+ );
+ expect(f.posts).toHaveLength(0);
+});
+
+test.each([true, false])(
+ "a new Semgrep reader mapping rechecks the original saved SCA row (exact case %s)",
+ async (exact) => {
+ const url = "https://scm.example.test/Team/Project";
+ const report = {
+ format: semgrepPlatformFormat,
+ deployment: "example",
+ project: { id: "14", name: "Team/Project", url },
+ product: "sca",
+ ref: "refs/heads/topic",
+ repositoryAliases: [],
+ findingCount: 1,
+ findings: [
+ JSON.stringify({
+ id: 23,
+ rule_name: "sample.rule",
+ repository: { url: exact ? url : url.toLowerCase() },
+ }),
+ ],
+ };
+ const input = normalizeSemgrepFindings(
+ report,
+ { sourceRepositories: [{ url, platform: "github" }] },
+ "cloud",
+ );
+ const f = await hostedFixture("semgrep", report, url);
+ const strict = () =>
+ prepareNormalizedExternalPublication(
+ input,
+ { ...options, provider: "semgrep" },
+ {
+ ...f.deps,
+ sourceRepositories: [{ url, platform: "gitlab" }],
+ },
+ );
+ if (!exact) await expect(strict()).rejects.toThrow(/repository/iu);
+ const pending = await f.loseResponse(await f.prepareNormalized(input));
+ const approved = await readFile(pending, "utf8");
+ if (exact) expect((await strict()).preview.resumed).toBe(true);
+ else await expect(strict()).rejects.toThrow(/repository/iu);
+ expect(await readFile(pending, "utf8")).toBe(approved);
+ await (await f.prepareNormalized(input)).publish();
+ expect(f.posts).toEqual([f.posts[0]!, f.posts[0]!]);
+ },
+);
+
+test("marked Semgrep rows require their provider while unmarked Wiz records can publish", async () => {
+ const url = "https://github.com/example/project";
+ const f = await fixture([], url);
+ const prepare = (
+ id: string | number | null,
+ projectUrl: string | null,
+ provider: "wiz" | "semgrep" = "wiz",
+ format?: string,
+ ) =>
+ prepareNormalizedExternalPublication(
+ {
+ read: 1,
+ excluded: [],
+ findings: [
+ {
+ source_finding_id: "opaque",
+ evidence: {
+ title: "Example",
+ severity: "low",
+ source_data: {
+ ...(format && { format }),
+ deployment: "demo",
+ project: { id: "10", name: "example/project", url: projectUrl },
+ ref: "refs/heads/topic",
+ product: "sast",
+ finding: { id },
+ },
+ },
+ },
+ ],
+ },
+ { ...options, provider },
+ f.deps,
+ );
+ await expect(
+ prepare("1.2e1", null, "wiz", "semgrep-platform-findings/v1"),
+ ).rejects.toThrow(/semgrep provider/i);
+ expect((await prepare("1.2e1", null)).preview.findings).toHaveLength(1);
+ await expect(prepare(null, null, "semgrep")).rejects.toThrow(/Semgrep/i);
+ expect(
+ (await prepare(12, url, "wiz", "wiz-findings/v1")).preview.findings,
+ ).toHaveLength(1);
+ expect(f.posts).toHaveLength(0);
+ expect((await (await prepare("WIZ-1", url)).publish()).counts.created).toBe(
+ 1,
+ );
+});
+
+async function useReleasedCheckpoint(path: string) {
+ const captured = await readFile(path, "utf8");
+ const pending = JSON.parse(captured);
+ delete pending.approvedRepositoryUrl;
+ delete pending.approvedRepositoryPlatform;
+ await writeFile(path, JSON.stringify(pending));
+
+ return () => writeFile(path, captured);
+}
+
+test.each(["semgrep", "snyk"] as const)(
+ "%s hosted findings bind the original repository and replay the approved request",
+ async (provider) => {
+ const url =
+ provider === "semgrep"
+ ? "https://scm.example.test/Example/Project"
+ : "https://github.com/example/project";
+ const report =
+ provider === "semgrep"
+ ? selectedSemgrepReport(
+ { name: "example/project", url },
+ { rule_message: "é".repeat(257) },
+ )
+ : snykRestFixture("open-source", { sourceRepositoryUrl: url });
+ const f = await hostedFixture(
+ provider,
+ report,
+ url,
+ "example/project/hosted",
+ );
+ const first = await f.prepareFile();
+ expect(first.preview.findings).toHaveLength(1);
+ if (provider === "semgrep") {
+ expect(first.preview.findings[0]!.evidence).toMatchObject({
+ title: "Semgrep finding 23",
+ description: "é".repeat(257),
+ });
+ }
+ const { findings } = first.preview;
+ const normalized = { read: 1, excluded: [], findings };
+ await expect(f.prepareNormalized(normalized, "wiz")).rejects.toThrow(
+ /provider/i,
+ );
+ if (provider === "semgrep") {
+ f.state.repositoryUrl = url.replace("Project", "%50roject");
+ expect((await f.prepareFile()).preview.findings).toHaveLength(1);
+ expect(
+ (await f.prepareNormalized(normalized)).preview.findings,
+ ).toHaveLength(1);
+ f.state.repositoryUrl = url.replace(
+ "scm.example.test/",
+ "scm.example.test:443/",
+ );
+ await expect(f.prepareFile()).rejects.toThrow(/does not match/);
+ f.state.repositoryUrl = url;
+ const alias = structuredClone(normalized);
+ alias.findings[0]!.evidence.details!.repository!.url =
+ url.toLowerCase() + ".git";
+ expect((await f.prepareNormalized(alias)).preview.findings).toHaveLength(
+ 1,
+ );
+ const strictAlias = {
+ ...alias,
+ sourceRepositoryPlatform: "other" as const,
+ };
+ await expect(f.prepareNormalized(strictAlias)).rejects.toThrow(
+ /repository/i,
+ );
+
+ const strict = {
+ ...structuredClone(normalized),
+ sourceRepositoryUrl: url,
+ sourceRepositoryPlatform: "other" as const,
+ };
+ const raw = strict.findings[0]!.evidence.source_data!;
+ const row = JSON.parse(String(raw["semgrepPlatformFindingJson"]));
+ row.repository.url = url.toLowerCase();
+ raw["semgrepPlatformFindingJson"] = JSON.stringify(row);
+ await expect(f.prepareNormalized(strict)).rejects.toThrow(/repository/i);
+ }
+ const untagged = structuredClone(normalized);
+ delete untagged.findings[0]!.evidence.source_data!["format"];
+ await expect(f.prepareNormalized(untagged, "wiz")).rejects.toThrow(
+ /provider/i,
+ );
+ const incomplete = structuredClone(normalized);
+ const payload =
+ provider === "semgrep" ? "semgrepPlatformFindingJson" : "issueJson";
+ delete incomplete.findings[0]!.evidence.source_data![payload];
+ await expect(f.prepareNormalized(incomplete)).rejects.toThrow();
+ expect(f.posts).toHaveLength(0);
+ f.state.repositoryUrl = "https://github.com/example/different";
+ await expect(f.prepareNormalized(normalized)).rejects.toThrow(
+ /repository/i,
+ );
+ expect(f.posts).toHaveLength(0);
+ f.state.repositoryUrl = url;
+
+ const pendingPath = await f.loseResponse(first);
+ if (provider === "semgrep") {
+ const { snykRestContext } = normalizeSnykIssues({
+ ...snykRestFixture("code", {
+ sourceRepositoryUrl: "https://github.com/example/other",
+ }),
+ pages: [{ startingAfter: null, responseJson: '{"data":[]}' }],
+ });
+ const mixed = { ...normalized, snykRestContext };
+ await expect(f.prepareNormalized(mixed)).rejects.toThrow(
+ /requires the snyk provider/i,
+ );
+ const opaque = {
+ ...normalized,
+ snykRestContext: { note: "projection metadata" },
+ };
+ await expect(f.prepareNormalized(opaque)).rejects.toThrow(
+ /snyk provider/i,
+ );
+ expect((await f.prepareNormalized(normalized)).preview.resumed).toBe(
+ true,
+ );
+ expect(f.posts).toHaveLength(1);
+ }
+ f.state.repositoryUrl = "https://github.com/example/renamed";
+ if (provider === "snyk") {
+ const restore = await useReleasedCheckpoint(pendingPath);
+ // The released publisher checked typed repository URLs only. The saved SCA project's
+ // original JSON does not establish which URL the catalog had at approval.
+ await expect(f.prepareFile()).rejects.toThrow(/does not match/);
+ await restore();
+ }
+ // A normalized caller can omit the redundant project/platform hints on the next try.
+ const projected = await f.prepareNormalized(normalized);
+ expect(projected.preview.resumed).toBe(true);
+ expect(projected.preview.requests).toEqual(first.preview.requests);
+ const retry = await f.prepareFile();
+ expect(retry.preview.resumed).toBe(true);
+ await retry.publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+ f.state.repositoryUrl = url;
+ const replay = await f.prepareFile();
+ expect((await replay.publish()).counts.unchanged).toBe(1);
+ expect(f.reports.size).toBe(1);
+ },
+);
+
+test("Semgrep Cloud validates a normalized report's monitored scope on import and retry", async () => {
+ const url = "https://github.com/example/project";
+ const report = selectedSemgrepReport(
+ { name: "example/project", url },
+ { rule_name: "example.rule" },
+ );
+ const f = await hostedFixture("semgrep", report, url);
+ const file = await f.prepareFile();
+ const { semgrepPlatformScope, ...normalized } =
+ normalizeSemgrepPlatformFindings(report, {}, "cloud");
+ const sarif = semgrepSarif();
+ Object.assign(sarif.runs[0]!, {
+ versionControlProvenance: [{ repositoryUri: url, branch: "other" }],
+ });
+ const findings = normalized.findings.concat(
+ normalizeSemgrepFindings(sarif, { sourceRepositoryUrl: url }).findings,
+ );
+ const mixed = {
+ ...normalized,
+ semgrepPlatformScope,
+ read: findings.length,
+ findings,
+ };
+ expect((await f.prepareNormalized(mixed)).preview.findings).toHaveLength(2);
+ await expect(
+ f.prepareNormalized({ ...mixed, sourceFormat: semgrepPlatformFormat }),
+ ).rejects.toThrow(/branch.*ref/i);
+ const other = (change: Partial<typeof semgrepPlatformScope>) =>
+ f.prepareNormalized({
+ ...normalized,
+ semgrepPlatformScope: { ...semgrepPlatformScope, ...change },
+ });
+ const { sourceFormat: _format, ...withoutFormat } = normalized;
+ Object.defineProperty(withoutFormat, "semgrepPlatformScope", {
+ get: () => semgrepPlatformScope,
+ });
+ for (const provider of ["semgrep", "wiz"] as const)
+ await expect(f.prepareNormalized(withoutFormat, provider)).rejects.toThrow(
+ /requires its source format/,
+ );
+ await expect(other({ projectId: "15" })).rejects.toThrow(
+ /scope does not match/,
+ );
+ expect(f.posts).toHaveLength(0);
+
+ await f.loseResponse(file);
+ await expect(
+ f.prepareNormalized({ ...withoutFormat, semgrepPlatformScope }),
+ ).rejects.toThrow(/requires its source format/);
+ await expect(other({ ref: "refs/heads/other" })).rejects.toThrow(
+ /scope does not match/,
+ );
+ Object.defineProperty(normalized, "semgrepPlatformScope", {
+ get: () => semgrepPlatformScope,
+ });
+ const retry = await f.prepareNormalized(normalized);
+ expect(retry.preview.requests).toEqual(file.preview.requests);
+ expect((await retry.publish()).verified).toBe(1);
+ expect(f.posts[1]).toBe(f.posts[0]);
+
+ const projected = (title: string, scoped = true) =>
+ f.prepareNormalized({
+ ...(scoped
+ ? { ...normalized, semgrepPlatformScope }
+ : { read: 1, excluded: [] }),
+ findings: [
+ { source_finding_id: "ticket", evidence: { title, severity: "high" } },
+ ],
+ });
+ await f.loseResponse(await projected("Initial title"));
+ await expect(projected("New title")).rejects.toThrow(
+ "pending hosted Cloud request",
+ );
+ await expect(projected("New title", false)).rejects.toThrow(
+ "pending hosted Cloud request",
+ );
+});
+
+test("Semgrep checks an alias witness before excluding it from the Cloud preview", async () => {
+ const url = "https://scm.example.test/Org/Project";
+ const report = (witnessUrl: string) => ({
+ format: semgrepPlatformFormat,
+ deployment: "example",
+ project: { id: "14", name: "Org/Project", url },
+ product: "sast",
+ ref: "refs/heads/topic",
+ findingCount: 2,
+ repositoryAliases: ["display"],
+ findings: [
+ JSON.stringify({
+ id: 11,
+ rule_name: "sample.rule",
+ repository: { name: "display" },
+ }),
+ JSON.stringify({
+ id: 12,
+ rule_name: "x".repeat(513),
+ repository: { name: "display", url: witnessUrl },
+ }),
+ ],
+ });
+ const f = await hostedFixture("semgrep", report(url), url);
+ const matching = await f.prepareFile();
+ expect(matching.preview.findings).toHaveLength(1);
+ expect(matching.preview.excluded).toHaveLength(1);
+ const wrong = report("https://scm.example.test:443/Org/Project");
+ expect(normalizeSemgrepFindings(wrong, {}, "local").findings).toHaveLength(2);
+ await writeFile(f.file, JSON.stringify(wrong));
+ await expect(f.prepareFile()).rejects.toThrow(/repository/i);
+ expect(f.posts).toHaveLength(0);
+});
+
+test("an original Semgrep v1 name-only finding needs a repository witness in the same batch and scope", async () => {
+ const f = await fixture();
+ const url = f.state.repositoryUrl;
+ const project = { id: 10, name: "example/project", url };
+ const ref = "refs/heads/topic";
+ const label = "display";
+ function rows(repository = project, branch = ref) {
+ const scope = {
+ format: "semgrep-platform-findings/v1",
+ deployment: "demo",
+ project: repository,
+ ref: branch,
+ product: "sast",
+ repositoryAliases: [label],
+ };
+ const row = { ref: branch, rule: { name: "fixture.rule" } };
+ const native = [
+ { ...row, id: 12, repository: { name: label } },
+ {
+ ...row,
+ id: 13,
+ repository: {
+ name: label,
+ id: String(repository.id),
+ url: repository.url,
+ },
+ },
+ ];
+ const { findings } = normalizeSemgrepFindings(
+ {
+ ...scope,
+ findingCount: native.length,
+ findings: native,
+ },
+ {},
+ "cloud",
+ );
+ for (const [i, finding] of findings.entries())
+ finding.evidence.source_data = { ...scope, finding: native[i] };
+ return findings;
+ }
+ const batch = rows();
+ const prepare = (findings: typeof batch) => {
+ const input = {
+ findings,
+ read: findings.length,
+ excluded: [],
+ sourceRepositoryUrl: url,
+ };
+ return prepareNormalizedExternalPublication(
+ input,
+ { ...options, provider: "semgrep" },
+ f.deps,
+ );
+ };
+ for (const unrelated of [
+ rows({ ...project, id: 11 }),
+ rows(project, "refs/heads/other"),
+ rows({
+ ...project,
+ name: "other/project",
+ url: "https://github.com/other/project",
+ }),
+ ]) {
+ await expect(prepare([batch[0]!, unrelated[1]!])).rejects.toThrow(
+ /witness|alias/,
+ );
+ }
+ expect(f.posts).toHaveLength(0);
+ const first = await prepare(batch);
+ const witness =
+ first.preview.findings[0]!.evidence.source_data?.["repositoryWitnessJson"];
+ expect(JSON.parse(String(witness))).toEqual({
+ name: label,
+ id: String(project.id),
+ });
+ expect((await first.publish()).counts.created).toBe(2);
+ expect((await (await prepare(batch)).publish()).counts.unchanged).toBe(2);
+});
+
+test("Semgrep Supply Chain binds package evidence to its case-sensitive source project", async () => {
+ const url = "https://scm.example.test/Team/Project";
+ const f = await hostedFixture(
+ "semgrep",
+ {
+ format: semgrepPlatformFormat,
+ deployment: "example",
+ project: { id: "14", name: "Team/Project", url },
+ product: "sca",
+ ref: "refs/heads/topic",
+ repositoryAliases: ["Team/Project"],
+ findingCount: 1,
+ findings: [
+ JSON.stringify({
+ id: 11,
+ vulnerability_identifier: "CVE-2025-0001",
+ found_dependency: {
+ package: "example-package",
+ ecosystem: "npm",
+ version: "1.0.0",
+ },
+ }),
+ ],
+ },
+ url,
+ );
+ const first = await f.prepareFile();
+ expect(
+ first.preview.findings[0]?.evidence.details?.repository,
+ ).toBeUndefined();
+ const other = "https://scm.example.test/Other/Project";
+ const sarif = semgrepSarif();
+ const run = sarif.runs[0]!;
+ Object.assign(run, { versionControlProvenance: [{ repositoryUri: other }] });
+ const mixed = normalizeSemgrepFindings(sarif, { sourceRepositoryUrl: other });
+ delete mixed.findings[0]!.evidence.details;
+ mixed.findings.push(...first.preview.findings);
+ await expect(f.prepareNormalized(mixed)).rejects.toThrow(/does not match/);
+ const prepareSarif = (repositoryUri: string) => {
+ mixed.findings[0]!.evidence.source_data!["runContext"] = {
+ versionControlProvenance: [{ repositoryUri }],
+ };
+ const input = { ...mixed, sourceRepositoryUrl: url };
+ return f.prepareNormalized(input);
+ };
+ delete mixed.findings[0]!.evidence.source_data!["documentContext"];
+ await expect(prepareSarif(other)).rejects.toThrow(/does not match/);
+ expect((await prepareSarif(url)).preview.findings).toHaveLength(2);
+ for (const unsafe of [
+ "https://[email protected]/Team/Project",
+ String.raw`https://scm.example.test\Other\Project?dest=/Team/Project`,
+ ])
+ await expect(prepareSarif(unsafe)).rejects.toThrow(/without credentials/);
+ const projected = {
+ read: 1,
+ excluded: [],
+ findings: first.preview.findings,
+ sourceRepositoryUrl: "",
+ };
+ f.state.repositoryUrl = other;
+ await expect(f.prepareNormalized(projected)).rejects.toThrow(/repository/i);
+ // A normalized SDK caller can keep the project binding while projecting only titles.
+ const opaque = {
+ ...first.preview,
+ findings: [
+ {
+ source_finding_id: "projected",
+ evidence: { title: "Dependency", severity: "low" as const },
+ },
+ ],
+ };
+ await expect(f.prepareNormalized(opaque)).rejects.toThrow(/does not match/);
+ await expect(f.prepareNormalized(opaque, "wiz")).rejects.toThrow(
+ /semgrep provider/,
+ );
+ const missingSource = { ...opaque, sourceRepositoryUrl: undefined };
+ await expect(f.prepareNormalized(missingSource)).rejects.toThrow(
+ /source-repository-url/,
+ );
+ f.state.repositoryUrl = url.toLowerCase();
+ const alias = await f.prepareFile();
+ expect(alias.preview.findings).toHaveLength(1);
+ const strict = {
+ ...projected,
+ sourceRepositoryUrl: url,
+ sourceRepositoryPlatform: "other" as const,
+ };
+ await expect(f.prepareNormalized(strict)).rejects.toThrow(/does not match/);
+ f.state.repositoryUrl = url;
+ strict.findings.push({
+ source_finding_id: "blackduck-bom:" + "a".repeat(64),
+ evidence: {
+ title: "Projected package",
+ severity: "high",
+ source_data: { sourceRepositoryUrl: url.toLowerCase() },
+ },
+ });
+ await expect(f.prepareNormalized(strict)).rejects.toThrow(/does not match/);
+ f.state.repositoryUrl = url.replace(
+ "scm.example.test/",
+ "scm.example.test:443/",
+ );
+ await expect(f.prepareFile()).rejects.toThrow(/does not match/);
+ expect(f.posts).toHaveLength(0);
+});
+
+test.each(["open-source", "code"] as const)(
+ "a released Snyk %s checkpoint retains one request across projections and renames",
+ async (product) => {
+ const url = "https://github.com/example/project";
+ const capture = snykRestFixture(product, { sourceRepositoryUrl: url });
+ const input = normalizeSnykIssues(capture);
+ delete input.findings[0]!.evidence.source_data;
+ const f = await hostedFixture("snyk", capture, url);
+ Reflect.deleteProperty(f.environment, "CODEX_SECURITY_CLOUD_BASE_URL");
+ let repository = url;
+ const bare = {
+ read: input.read,
+ findings: input.findings,
+ excluded: input.excluded,
+ };
+ const projected = Object.assign(
+ Object.create({
+ get sourceRepositoryUrl() {
+ return repository;
+ },
+ }) as typeof input,
+ bare,
+ );
+ const prepare = () => f.prepareNormalized(projected);
+
+ await expect(prepare()).rejects.toThrow(/Snyk original issue/);
+ expect(f.posts).toHaveLength(0);
+
+ // The released SDK approved these findings without retaining a native issue.
+ const { file, checkpoint } = releasedSnykCheckpoints[product];
+ const directory = join(
+ f.environment.CODEX_SECURITY_STATE_DIR,
+ "external-finding-publications",
+ );
+ await mkdir(directory, { recursive: true, mode: 0o700 });
+ await writeFile(join(directory, file), JSON.stringify(checkpoint), {
+ mode: 0o600,
+ });
+ const first = await f.prepareNormalized(bare);
+ expect(first.preview.resumed).toBe(true);
+ await f.loseResponse(first);
+ await expect(f.prepareFile()).rejects.toThrow(
+ "pending hosted Cloud request",
+ );
+ expect(f.posts).toEqual([JSON.stringify(checkpoint.requests[0])]);
+
+ f.state.repositoryUrl = "https://github.com/example/renamed";
+ await expect(prepare()).rejects.toThrow(/does not match/);
+ expect((await f.prepareNormalized(bare)).preview.resumed).toBe(true);
+ f.state.repositoryUrl = url;
+ expect((await prepare()).preview.resumed).toBe(true);
+ const bareRetry = await f.prepareNormalized(bare);
+ expect(bareRetry.preview.requests).toEqual(first.preview.requests);
+ repository = "https://github.com/example/different";
+ await expect(prepare()).rejects.toThrow(/does not match/);
+ repository = url;
+ // Binding while the catalog still matches establishes the URL for a future rename.
+ await f.loseResponse(await prepare());
+ f.state.repositoryUrl = "https://github.com/example/renamed";
+ const retry = await prepare();
+ expect(retry.preview.resumed).toBe(true);
+ await retry.publish();
+ expect(f.posts).toHaveLength(3);
+ expect(new Set(f.posts).size).toBe(1);
+ },
+);
+
+test("a released Snyk checkpoint cannot authenticate the case of a typed URL", async () => {
+ const url = "https://scm.example.test/Team/Repo";
+ const f = await hostedFixture("snyk", [], url.toLowerCase());
+ const report = snykRestFixture("code", { sourceRepositoryUrl: url });
+ const project = JSON.parse(report.projectJson);
+ project.attributes.origin = "gitlab";
+ report.projectJson = JSON.stringify(project);
+ const { snykRestContext, findings } = normalizeSnykIssues(report);
+ findings[0]!.source_finding_id = "projected";
+ delete findings[0]!.evidence.source_data;
+ const bare = { read: 1, excluded: [], findings };
+ const pending = await f.loseResponse(await f.prepareNormalized(bare));
+ await useReleasedCheckpoint(pending);
+
+ await expect(
+ f.prepareNormalized({ ...bare, snykRestContext }),
+ ).rejects.toThrow(/does not match/);
+ expect((await f.prepareNormalized(bare)).preview.resumed).toBe(true);
+});
+
+test.each([
+ ["Kit", "Kit"],
+ ["Kit", "Kit"],
+ ["åpp", "Åpp"],
+])(
+ "released catalog spelling %s cannot authorize %s after a rename",
+ async (catalog, typed) => {
+ const root = "https://github.com/example/";
+ const url = root + typed;
+ const f = await hostedFixture("snyk", [], root + catalog);
+ const { findings } = normalizeSnykIssues(
+ snykRestFixture("code", { sourceRepositoryUrl: url }),
+ );
+ findings[0]!.source_finding_id = "reader-issue";
+ delete findings[0]!.evidence.source_data;
+ const bare = { read: 1, excluded: [], findings };
+ const pending = await f.loseResponse(await f.prepareNormalized(bare));
+ await useReleasedCheckpoint(pending);
+ const original = await readFile(pending);
+
+ f.state.repositoryUrl = root + "renamed";
+ await expect(
+ f.prepareNormalized({ ...bare, sourceRepositoryUrl: url }),
+ ).rejects.toThrow(/does not match/);
+ expect(await readFile(pending)).toEqual(original);
+ const retry = await f.prepareNormalized(bare);
+ expect(retry.preview.resumed).toBe(true);
+ await retry.publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+ },
+);
+
+test("a rejected projected Black Duck row leaves a released Snyk checkpoint recoverable", async () => {
+ const url = "https://github.com/example/project";
+ const f = await hostedFixture("snyk", [], url);
+ const input = normalizeBlackDuckFindings([blackDuckBomRow()], url);
+ input.findings[0]!.evidence.source_data = { sourceRepositoryUrl: url };
+ const prepare = (sourceRepositoryUrl: string) =>
+ f.prepareNormalized({ ...input, sourceRepositoryUrl });
+ const pending = await f.loseResponse(await prepare(url));
+ await useReleasedCheckpoint(pending);
+ const original = await readFile(pending);
+
+ f.state.repositoryUrl = "https://github.com/example/renamed";
+ await expect(prepare(f.state.repositoryUrl)).rejects.toThrow(
+ /does not match/,
+ );
+ expect(await readFile(pending)).toEqual(original);
+ f.state.repositoryUrl = url;
+ const retry = await prepare(url);
+ expect(retry.preview.resumed).toBe(true);
+ await retry.publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+});
+
+test("a Snyk retry adopts case sensitivity when the operator publishes", async () => {
+ const url = "https://scm.example.test/Team/Repo";
+ const f = await hostedFixture("snyk", [], url);
+ const bare = {
+ read: 1,
+ excluded: [],
+ findings: [
+ {
+ source_finding_id: "projected",
+ evidence: { title: "Dependency", severity: "low" as const },
+ },
+ ],
+ };
+ const strict = () =>
+ f.prepareNormalized({
+ ...bare,
+ sourceRepositoryUrl: url,
+ sourceRepositoryPlatform: "other",
+ });
+ const alias = () =>
+ f.prepareNormalized({ ...bare, sourceRepositoryUrl: url.toLowerCase() });
+ const initial = await strict();
+ const pending = await f.loseResponse(initial);
+ await expect(alias()).rejects.toThrow(/does not match/);
+
+ // The released SDK did not record a repository URL or SCM policy.
+ await useReleasedCheckpoint(pending);
+ const strictRetry = await strict();
+ const aliasRetry = await alias();
+ expect(f.posts).toHaveLength(1);
+ await f.loseResponse(aliasRetry);
+ await f.loseResponse(strictRetry);
+ await expect(alias()).rejects.toThrow(/does not match/);
+
+ f.state.repositoryUrl = "https://scm.example.test/Team/Renamed";
+ await expect(
+ f.prepareNormalized({
+ ...bare,
+ sourceRepositoryUrl: f.state.repositoryUrl,
+ }),
+ ).rejects.toThrow(/does not match/);
+ const retry = await strict();
+ expect(retry.preview.resumed).toBe(true);
+ expect(retry.preview.requests).toEqual(initial.preview.requests);
+ expect((await f.prepareNormalized(bare)).preview.requests).toEqual(
+ initial.preview.requests,
+ );
+ await retry.publish();
+ expect(f.posts).toEqual(Array(4).fill(f.posts[0]));
+});
+
+test("Snyk Code accepts equivalent URL escapes and still rejects a foreign typed row", async () => {
+ const url = "https://github.com/example/project";
+ const report = snykRestFixture("code", {
+ sourceRepositoryUrl: url.replace("/project", "/%70roject"),
+ });
+ const f = await hostedFixture("snyk", report, url);
+ const preview = await f.prepareFile();
+ expect(preview.preview.findings).toHaveLength(1);
+
+ const input = normalizeSnykIssues(report);
+ expect((await f.prepareNormalized(input)).preview.findings).toHaveLength(1);
+ for (const foreign of [
+ "https://github.com/other/project",
+ "https://github.com:8443/example/project",
+ ]) {
+ input.findings[0]!.evidence.details!.repository!.url = foreign;
+ await expect(f.prepareNormalized(input)).rejects.toThrow(/repository/i);
+ }
+ expect(f.posts).toHaveLength(0);
+});
+
+test.each(["semgrep", "snyk"] as const)(
+ "%s file publication uses the authenticated GitHub repository for private-host aliases",
+ async (provider) => {
+ const url = "https://scm.example.test/Org/Repo";
+ const selected = selectedSemgrepReport(
+ { name: "Org/Repo", url },
+ { repository: { url: url.toLowerCase() }, rule_name: "sample.rule" },
+ [{ url, platform: "github" }],
+ );
+ const snyk = snykRestFixture("code", { sourceRepositoryUrl: url });
+ const project = JSON.parse(snyk.projectJson);
+ project.attributes.origin = "cli";
+ project.relationships.target.data.attributes.url =
+ "[email protected]:org/repo.git";
+ snyk.projectJson = JSON.stringify(project);
+ const report = provider === "semgrep" ? selected : snyk;
+ const f = await hostedFixture(provider, report, url);
+ const first = await f.prepareFile();
+ expect(first.preview.findings).toHaveLength(1);
+ expect(f.state.catalogReads).toBe(1);
+ const renamed = "https://scm.example.test/Another/Repo";
+ f.state.repositoryUrl = renamed;
+ await expect(f.prepareFile()).rejects.toThrow(/repository|project/iu);
+ expect(f.posts).toHaveLength(0);
+ f.state.repositoryUrl = url;
+ await f.loseResponse(first);
+ f.state.repositoryUrl = renamed;
+ const retry = await f.prepareFile();
+ expect(retry.preview.resumed).toBe(true);
+ await retry.publish();
+ expect(f.posts).toHaveLength(2);
+ expect(f.posts[1]).toBe(f.posts[0]);
+ await expect(f.prepareFile()).rejects.toThrow(/repository|project/iu);
+ },
+);
+
+test.each(["file", "operator"])(
+ "Semgrep v1 keeps the native project's explicit port when checking %s mappings",
+ async (mapping) => {
+ const url = "https://scm.example.test:443/team/project";
+ const ref = "refs/heads/release";
+ const report = {
+ format: "semgrep-platform-findings/v1",
+ deployment: "example",
+ project: { id: 14, name: "team/project", url },
+ ref,
+ product: "sast",
+ findingCount: 1,
+ findings: [{ id: 23, ref, rule: { name: "sample.check" } }],
+ };
+ const f = await fixture(report, url);
+ const prepare = async (sourceRepositoryUrl: string) => {
+ if (mapping === "file")
+ await writeFile(
+ f.file,
+ JSON.stringify({ ...report, sourceRepositoryUrl }),
+ );
+ return prepareExternalPublication(
+ f.file,
+ {
+ ...options,
+ provider: "semgrep",
+ ...(mapping === "operator" && { sourceRepositoryUrl }),
+ },
+ f.deps,
+ );
+ };
+ const withoutPort = url.replace(":443", "");
+ for (const catalog of [withoutPort, url]) {
+ f.state.repositoryUrl = catalog;
+ await expect(prepare(withoutPort)).rejects.toThrow(/does not match/);
+ }
+ expect((await prepare(url)).preview.findings).toHaveLength(1);
+ expect(f.posts).toHaveLength(0);
+ },
+);
+
+test.each([true, false])(
+ "a Semgrep v1 private SSH file retries after a rename (operator mapping %s)",
+ async (mapped) => {
+ const url = "https://scm.example.test/Org/Repo";
+ const ref = "refs/heads/release";
+ const f = await fixture(
+ {
+ format: "semgrep-platform-findings/v1",
+ deployment: "example",
+ project: {
+ id: 14,
+ name: "Org/Repo",
+ url: "[email protected]:org/repo.git",
+ },
+ product: "sast",
+ ref,
+ findingCount: 1,
+ findings: [
+ { id: 23, ref, rule: { name: "sample.rule" }, repository: { url } },
+ ],
+ },
+ url,
+ );
+ const prepare = (sourceRepositoryUrl = mapped ? url : undefined) =>
+ prepareExternalPublication(
+ f.file,
+ { ...options, provider: "semgrep", sourceRepositoryUrl },
+ f.deps,
+ );
+ await f.loseResponse(await prepare());
+ f.state.repositoryUrl = "https://scm.example.test/Org/Renamed";
+ await expect(
+ prepare(url.replace(".test/", ".test:443/")),
+ ).rejects.toThrow();
+ const retry = await prepare();
+ expect(retry.preview.resumed).toBe(true);
+ await retry.publish();
+ expect(f.posts).toHaveLength(2);
+ expect(f.posts[1]).toBe(f.posts[0]);
+ await expect(prepare()).rejects.toThrow(/repository|project/iu);
+ },
+);
+
+test.each([true, false])(
+ "an escaped projected repository in a Snyk checkpoint matches the current destination (declared %s)",
+ async (declared) => {
+ const f = await fixture();
+ const url = f.state.repositoryUrl;
+ const escaped = url.replace("/project", "/%70roject");
+ const result = normalizeSnykIssues(
+ snykRestFixture("code", { sourceRepositoryUrl: url }),
+ );
+ const input = declared
+ ? result
+ : {
+ read: result.read,
+ excluded: result.excluded,
+ findings: result.findings,
+ };
+ result.findings[0]!.evidence.details!.repository!.url = escaped;
+ const prepare = () =>
+ prepareNormalizedExternalPublication(
+ input,
+ { ...options, provider: "snyk" },
+ f.deps,
+ );
+ f.state.repositoryUrl = "https://github.com/example/different";
+ await expect(prepare()).rejects.toThrow(
+ /does not match the selected Cloud repository/,
+ );
+ expect(f.posts).toHaveLength(0);
+ f.state.repositoryUrl = url;
+ await f.loseResponse(await prepare());
+ f.state.repositoryUrl = url.replace("/project", "/Project");
+ const retry = await prepare();
+ expect(retry.preview.resumed).toBe(true);
+ await retry.publish();
+ expect(f.posts).toHaveLength(2);
+ expect(f.posts[1]).toBe(f.posts[0]);
+ },
+);
+
+test.each(["semgrep", "snyk"] as const)(
+ "a strict %s source and opaque Black Duck projection survive a catalog casing change",
+ async (provider) => {
+ const url = "https://scm.example.test/Team/Project";
+ const f = await hostedFixture(provider, [], url);
+ const projected = normalizeBlackDuckFindings([blackDuckBomRow()], url)
+ .findings[0]!;
+ projected.evidence.source_data = { sourceRepositoryUrl: url };
+ const report = {
+ read: 1,
+ excluded: [],
+ findings: [projected],
+ sourceRepositoryUrl: url,
+ sourceRepositoryPlatform: "other" as const,
+ };
+ await f.loseResponse(await f.prepareNormalized(report));
+ f.state.repositoryUrl = "https://scm.example.test/Other/Project";
+ await expect(f.prepareNormalized(report)).rejects.toThrow(/does not match/);
+ f.state.repositoryUrl = url.toLowerCase();
+ const retry = await f.prepareNormalized(report);
+ expect(retry.preview.resumed).toBe(true);
+ await retry.publish();
+ expect(f.posts).toHaveLength(2);
+ expect(f.posts[1]).toBe(f.posts[0]);
+ },
+);
+
+test.each(["semgrep", "snyk"] as const)(
+ "a projected Black Duck finding cannot resume under a different %s catalog authority",
+ async (provider) => {
+ const url = "https://127.0.0.1:443/Org/Project";
+ const f = await hostedFixture(provider, [], url);
+ const bare = normalizeBlackDuckFindings([blackDuckBomRow()], url);
+ bare.findings[0]!.evidence.source_data = { sourceRepositoryUrl: url };
+ const declared = { ...bare, sourceRepositoryUrl: url };
+ const pending = await f.loseResponse(await f.prepareNormalized(bare));
+ await useReleasedCheckpoint(pending);
+ expect((await f.prepareNormalized(declared)).preview.resumed).toBe(true);
+ const saved = await readFile(pending);
+
+ f.state.repositoryUrl = "https://0177.1.:443/Org/Project";
+ await expect(f.prepareNormalized(bare)).rejects.toThrow(/does not match/);
+ await expect(f.prepareNormalized(declared)).rejects.toThrow(
+ /does not match/,
+ );
+ expect(await readFile(pending)).toEqual(saved);
+ expect(f.posts).toHaveLength(1);
+
+ f.state.repositoryUrl = url;
+ await (await f.prepareNormalized(declared)).publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+ },
+);
+
+test("a private Snyk GitHub project uses its authenticated catalog alias", async () => {
+ const url = "https://scm.example.test/Team/Project";
+ const report = snykRestFixture("code", { sourceRepositoryUrl: url });
+ const project = JSON.parse(report.projectJson);
+ project.attributes.origin = "github-enterprise";
+ report.projectJson = JSON.stringify(project);
+ const f = await hostedFixture("snyk", report, url.toLowerCase());
+ const first = await f.prepareFile();
+ expect(first.preview.findings).toHaveLength(1);
+
+ project.attributes.origin = "gitlab";
+ report.projectJson = JSON.stringify(project);
+ await writeFile(f.file, JSON.stringify(report));
+ await expect(f.prepareFile()).rejects.toThrow(/repository/i);
+ expect(f.posts).toHaveLength(0);
+});
+
+test("an original Semgrep SDK row keeps the approved repository casing on retry", async () => {
+ const url = "https://scm.example.test/Team/Project";
+ const report = selectedSemgrepReport(
+ { name: "Team/Project", url },
+ { rule_name: "sample.rule" },
+ );
+ const f = await hostedFixture("semgrep", report, url);
+ const { findings } = (await f.prepareFile()).preview;
+ const raw = findings[0]!.evidence.source_data!;
+ raw["format"] = "semgrep-platform-findings/v1";
+ raw["finding"] = JSON.parse(report.findings[0]!);
+ delete raw["semgrepPlatformFindingJson"];
+ raw["sourceRepositoryUrl"] = url;
+ const bare = { read: 1, findings, excluded: [] };
+ const first = await f.prepareNormalized({
+ ...bare,
+ sourceRepositoryPlatform: "other",
+ });
+ await f.loseResponse(first);
+ raw["sourceRepositoryUrl"] = url.toLowerCase();
+ await expect(f.prepareNormalized(bare)).rejects.toThrow(
+ /original Semgrep input and SCM repository mapping/u,
);
- const port = (server.address() as { port: number }).port;
- const transport = (url: string, init: RequestInit) =>
- fetch(
- `http://127.0.0.1:${port}${new URL(url).pathname}${new URL(url).search}`,
- init,
+ raw["sourceRepositoryUrl"] = url;
+ expect((await f.prepareNormalized(bare)).preview.requests).toEqual(
+ first.preview.requests,
+ );
+});
+
+test("an original Semgrep SDK row keeps its approved GitHub alias after a rename", async () => {
+ const url = "https://scm.example.test/Org/Repo";
+ const ref = "refs/heads/release";
+ const scope = {
+ format: "semgrep-platform-findings/v1",
+ deployment: "example",
+ project: { id: 14, name: "Org/Repo", url },
+ ref,
+ product: "sast",
+ };
+ const finding = {
+ id: 23,
+ ref,
+ rule: { name: "sample.rule" },
+ repository: { name: "org/repo", url },
+ };
+ const f = await hostedFixture(
+ "semgrep",
+ { ...scope, findingCount: 1, findings: [finding] },
+ url,
+ );
+ const first = await f.prepareFile();
+ const findings = structuredClone(first.preview.findings);
+ findings[0]!.evidence.source_data = { ...scope, finding };
+ const sdk = { read: 1, excluded: [], findings, sourceRepositoryUrl: url };
+ await f.loseResponse(first);
+ f.state.repositoryUrl = "https://scm.example.test/Org/Renamed";
+ const retry = await f.prepareNormalized(sdk);
+ expect(retry.preview.resumed).toBe(true);
+ await retry.publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+ await expect(f.prepareNormalized(sdk)).rejects.toThrow(
+ /repository|project/iu,
+ );
+});
+
+test.each(["interrupted file", "completed SDK"])(
+ "Semgrep v1 file and original SDK rows reuse the same evidence (%s)",
+ async (first) => {
+ const url = "https://scm.example.test/Team/Project";
+ const id = "9007199254740993";
+ const scope = {
+ format: "semgrep-platform-findings/v1",
+ deployment: "example",
+ project: { id: 77, name: "Team/Project", url: null },
+ sourceRepositoryUrl: url,
+ ref: "refs/heads/release",
+ product: "sast",
+ repositoryAliases: ["project"],
+ };
+ const finding = {
+ id,
+ ref: scope.ref,
+ repository: { name: "project", url: url.toLowerCase() },
+ rule: { name: "sample.check" },
+ severity: "HIGH",
+ location: { file_path: "src\\service.ts", line: 3, end_line: 5 },
+ };
+ const report = {
+ ...scope,
+ findingCount: 1,
+ findings: [finding],
+ numericFields: { findings: { 0: { id } } },
+ };
+ const f = await hostedFixture("semgrep", report, url);
+ const { read, findings, excluded } = normalizeSemgrepFindings(
+ report,
+ { sourceRepositories: [{ url, platform: "github" }] },
+ "cloud",
);
- const deps = { environment, fetch: transport };
- const cliDeps = { ...dependencies(), environment, cloudFetch: transport };
- const command = [
- "publish",
- "findings",
- file,
- "--to",
- "cloud",
- "--repository",
- options.repository,
- "--provider",
- options.provider,
- "--source-key",
- options.sourceKey,
- "--format",
- "json",
- ];
- return {
- root,
- file,
- reports,
- receipts,
- posts,
- state,
- destination,
- environment,
- port,
- deps,
- cliDeps,
- command,
+ const sdk = { read, findings, excluded };
+ const evidence = findings[0]!.evidence;
+ evidence.severity = "unknown";
+ evidence.locations = [];
+ evidence.details!.code!.end_line = null;
+ findings[0]!.evidence.source_data = {
+ ...scope,
+ finding,
+ numericFields: { finding: { id } },
+ };
+ const otherPort = structuredClone(sdk);
+ otherPort.findings[0]!.evidence.source_data!["sourceRepositoryUrl"] =
+ url.replace(".test/", ".test:443/");
+ await expect(f.prepareNormalized(otherPort)).rejects.toThrow(
+ /repository/iu,
+ );
+ if (first === "interrupted file") {
+ await f.loseResponse(await f.prepareFile());
+ f.state.repositoryUrl = "https://scm.example.test/Team/Renamed";
+ const retry = await f.prepareNormalized(sdk);
+ expect(retry.preview.resumed).toBe(true);
+ await retry.publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+ await expect(f.prepareNormalized(sdk)).rejects.toThrow(
+ /repository|project/i,
+ );
+ } else {
+ expect(
+ (await (await f.prepareNormalized(sdk)).publish()).counts.created,
+ ).toBe(1);
+ expect((await (await f.prepareFile()).publish()).counts.unchanged).toBe(
+ 1,
+ );
+ }
+ },
+);
+
+test("a Semgrep selection with distinct submitted and native IDs remains independent", async () => {
+ const url = "https://github.com/example/project";
+ const capture = selectedSemgrepReport(
+ { name: "example/project", url },
+ { rule_name: "example.rule" },
+ );
+ const f = await hostedFixture("semgrep", capture, url);
+ const prepare = (nativeId: number, submittedId: string) => {
+ const findings = capture.findings.map((row) =>
+ JSON.stringify({ ...JSON.parse(row), id: nativeId }),
+ );
+ const input = normalizeSemgrepFindings(
+ { ...capture, findings },
+ {},
+ "cloud",
+ );
+ input.findings[0]!.source_finding_id = submittedId;
+ return f.prepareNormalized(input);
};
-}
+ await f.loseResponse(await prepare(23, "first-ticket"));
+ await expect(prepare(23, "another-ticket")).rejects.toThrow(
+ "pending hosted Cloud request",
+ );
+ await expect(prepare(24, "first-ticket")).rejects.toThrow(
+ "pending hosted Cloud request",
+ );
+ expect((await prepare(24, "another-ticket")).preview.resumed).toBe(false);
+ expect(f.posts).toHaveLength(1);
+});
+
+test.each(["semgrep", "snyk"] as const)(
+ "%s holds an overlapping native update until the initial request is recovered",
+ async (provider) => {
+ const url = "https://github.com/example/project";
+ const report =
+ provider === "semgrep"
+ ? selectedSemgrepReport(
+ { name: "example/project", url },
+ { rule_name: "example.rule" },
+ )
+ : snykRestFixture("open-source", { sourceRepositoryUrl: url });
+ const f = await hostedFixture(provider, report, url);
+ const first = await f.prepareFile();
+ const original = {
+ findings: first.preview.findings,
+ read: first.preview.findings.length,
+ excluded: [],
+ sourceRepositoryUrl: url,
+ report: { capture: "example" },
+ };
+ if (provider === "semgrep")
+ original.findings[0]!.source_finding_id = "caller-selected";
+ const changed = structuredClone(original);
+ changed.findings[0]!.evidence.title += " updated";
+ const stale = await f.prepareNormalized(changed);
+ f.state.marker = "next-generation";
+ const challenger = await f.prepareNormalized(changed);
+ const winner = await f.prepareNormalized(original);
+ const observer = observeLockContention();
+ const rename = fs.rename;
+ let competing: Promise<unknown> | undefined;
+ const install = spyOn(fs, "rename").mockImplementationOnce(
+ async (...args) => {
+ // Start the other publisher after the winner's pending scan and before its atomic rename.
+ competing = challenger.publish().catch((error: unknown) => error);
+ const first = await Promise.race([
+ observer.contended.then(() => "locked"),
+ competing.then(() => "completed"),
+ ]);
+ expect(first).toBe("locked");
+ return rename(...args);
+ },
+ );
+ let pending: string;
+ try {
+ pending = await f.loseResponse(winner);
+ expect(String(await competing)).toContain("pending hosted Cloud request");
+ } finally {
+ install.mockRestore();
+ observer.restore();
+ }
+ const bytes = await readFile(pending, "utf8");
+ if (provider === "semgrep") {
+ const projected = structuredClone(changed);
+ delete projected.findings[0]!.evidence.source_data;
+ await expect(f.prepareNormalized(projected)).rejects.toThrow(
+ "pending hosted Cloud request",
+ );
+ }
+ await expect(stale.publish()).rejects.toThrow("reset markers");
+
+ await expect(f.prepareFile()).rejects.toThrow(
+ "deployment could not be established",
+ );
+ await expect(f.prepareNormalized(changed)).rejects.toThrow(
+ "pending hosted Cloud request",
+ );
+ expect(await readFile(pending, "utf8")).toBe(bytes);
+ expect(f.posts).toHaveLength(1);
+ const captured = await f.prepareNormalized(original);
+ await (await f.prepareNormalized(original)).publish();
+ expect(f.posts[1]).toBe(f.posts[0]);
+ const newer = await f.loseResponse(await f.prepareNormalized(changed));
+ const approved = await readFile(newer, "utf8");
+ await expect(captured.publish()).rejects.toThrow(
+ "pending hosted Cloud request",
+ );
+ expect(f.posts).toHaveLength(3);
+ expect(await readFile(newer, "utf8")).toBe(approved);
+ expect(
+ (await (await f.prepareNormalized(changed)).publish()).counts.updated,
+ ).toBe(1);
+ },
+);
+
+test.each([
+ ["semgrep", "native"],
+ ["semgrep", "SDK metadata and assigned IDs"],
+ ["semgrep", "released assigned IDs"],
+ ["semgrep", "released ID only"],
+ ["semgrep", "released assigned JSON row"],
+ ["snyk", "released assigned JSON row"],
+ ["snyk", "released assigned IDs"],
+ ["semgrep", "per-finding metadata"],
+ ["semgrep", "projected metadata"],
+ ["snyk", "released unmarked with metadata"],
+ ["snyk", "released compact issue JSON"],
+ ["semgrep", "released relabeled with metadata"],
+ ["semgrep", "released normalized SDK"],
+ ["semgrep", "released partial object row"],
+ ["semgrep", "released partial JSON row"],
+ ["semgrep", "released partial native project URL"],
+ ["semgrep", "released without format"],
+ ["snyk", "released without format"],
+ ["semgrep", "released unrelated evidence"],
+ ["snyk", "released unrelated evidence"],
+ ["snyk", "native"],
+ ["snyk", "projected evidence"],
+] as const)(
+ "%s preserves the pending v1 request across a reader upgrade (%s)",
+ async (provider, mode) => {
+ const url = "https://github.com/example/project";
+ const project = { id: 14, name: "example/project", url };
+ const ref = "refs/heads/release";
+ const finding = {
+ id: 23,
+ ref,
+ repository: { url },
+ severity: "high",
+ rule_name: "example.rule",
+ };
+ const nativeFindings = [finding, { ...finding, id: 24 }];
+ const { projectJson, targetJson, pages, ...scope } = snykRestFixture(
+ "code",
+ { sourceRepositoryUrl: url },
+ );
+ const semgrep = {
+ format: "semgrep-platform-findings/v1",
+ deployment: "example",
+ project: mode === "native" ? { ...project, url: null } : project,
+ ...(mode === "native" && { sourceRepositoryUrl: url }),
+ product: "sast",
+ ref,
+ repositoryAliases: [],
+ };
+ const report =
+ provider === "semgrep"
+ ? {
+ ...semgrep,
+ findingCount: nativeFindings.length,
+ findings: nativeFindings,
+ }
+ : {
+ ...scope,
+ format: "snyk-rest-issues/v1",
+ project: JSON.parse(projectJson),
+ target: JSON.parse(targetJson),
+ pages: pages.map(({ startingAfter, responseJson }) => ({
+ startingAfter,
+ response: JSON.parse(responseJson),
+ })),
+ };
+ const f = await hostedFixture(
+ provider,
+ provider === "snyk" && mode === "released assigned JSON row"
+ ? snykRestFixture("code", { sourceRepositoryUrl: url })
+ : report,
+ url,
+ );
+ // The legacy filename below belongs to the default Cloud deployment.
+ Reflect.deleteProperty(f.environment, "CODEX_SECURITY_CLOUD_BASE_URL");
+ const prior = await f.prepareFile();
+ const current = await f.loseResponse(prior);
+ const dir = dirname(current);
+ const pending = JSON.parse(await readFile(current, "utf8"));
+ delete pending.approvedRepositoryUrl;
+ delete pending.approvedRepositoryPlatform;
+ const request = pending.requests[0] as FindingImportRequest;
+ const rows = request.items.map(({ source_finding_id, evidence }, index) => {
+ if (provider === "semgrep" && mode !== "released assigned JSON row")
+ evidence.source_data = { ...semgrep, finding: nativeFindings[index] };
+ return { source_finding_id, evidence };
+ });
+ const omittedMetadata =
+ mode === "released compact issue JSON" ||
+ (provider === "semgrep" && mode === "released without format");
+ const metadata =
+ mode === "SDK metadata and assigned IDs" || omittedMetadata
+ ? { batch: "example" }
+ : undefined;
+ const assigned =
+ metadata ||
+ mode === "projected metadata" ||
+ mode === "released assigned IDs" ||
+ mode === "released assigned JSON row" ||
+ mode === "released unrelated evidence";
+ if (assigned)
+ rows.forEach((row, index) => {
+ request.items[index]!.source_finding_id = row.source_finding_id =
+ "custom-" + index;
+ });
+ const released = mode.startsWith("released ");
+ const enrichedNative =
+ mode === "released unmarked with metadata" ||
+ mode === "released relabeled with metadata";
+ if (mode === "released compact issue JSON")
+ for (const { evidence } of rows)
+ evidence.source_data = {
+ issueJson: JSON.stringify(evidence.source_data!["issue"]),
+ };
+ if (mode === "released without format")
+ for (const { evidence } of rows) delete evidence.source_data!["format"];
+ if (enrichedNative)
+ for (const { evidence } of rows)
+ evidence.source_data =
+ provider === "snyk"
+ ? { issue: evidence.source_data!["issue"] }
+ : { ...evidence.source_data, format: "normalized/v1" };
+ if (mode === "released unrelated evidence")
+ for (const { evidence } of rows)
+ evidence.source_data = {
+ project: { label: "example" },
+ [provider === "semgrep" ? "finding" : "issue"]: { note: "custom" },
+ };
+ if (
+ mode === "projected evidence" ||
+ mode === "projected metadata" ||
+ mode === "released ID only"
+ )
+ for (const { evidence } of rows) delete evidence.source_data;
+ const partial =
+ mode === "released partial object row" ||
+ mode === "released partial JSON row" ||
+ mode === "released partial native project URL";
+ if (partial) {
+ for (const { evidence } of rows) {
+ delete evidence.details;
+ const nativeProjectUrl = "[email protected]:example/project.git";
+ if (mode === "released partial native project URL") {
+ evidence.source_data = {
+ format: "semgrep-platform-findings/v2",
+ nativeProjectUrl,
+ semgrepPlatformFindingJson: JSON.stringify({
+ ref,
+ rule_name: "example.rule",
+ }),
+ };
+ continue;
+ }
+ const finding = { ref, repository: { url: nativeProjectUrl } };
+ evidence.source_data = {
+ format: semgrep.format,
+ ...(mode === "released partial object row"
+ ? { finding }
+ : { semgrepPlatformFindingJson: JSON.stringify(finding) }),
+ };
+ }
+ }
+ const originalRows =
+ mode === "per-finding metadata" ||
+ mode === "projected metadata" ||
+ enrichedNative
+ ? rows.map((row, index) => ({ ...row, ticket: "example-" + index }))
+ : rows;
+ const identity = metadata
+ ? { findings: originalRows, report: metadata }
+ : originalRows;
+ const digest = (value: unknown) => hash("sha256", canonicalJson(value));
+ pending.inputProjection = {
+ savedEvidenceDigest: digest(rows),
+ inputDigest: released
+ ? digest(identity)
+ : digest([
+ identity,
+ [url, provider === "semgrep" ? null : false, false],
+ ]),
+ };
+ const oldKey = digest([
+ pending.accountId,
+ request.repository.id,
+ request.repository.repo_connector_id,
+ request.source,
+ identity,
+ ]);
+ const oldPath = join(dir, oldKey + ".pending.json");
+ await rm(current);
+ let bytes = JSON.stringify(pending);
+ await writeFile(oldPath, bytes, { mode: 0o600 });
+
+ const input = {
+ read: rows.length,
+ excluded: [],
+ findings: structuredClone(prior.preview.findings),
+ sourceRepositoryUrl: url,
+ ...(metadata && { report: metadata }),
+ };
+ if (assigned)
+ input.findings.forEach((row, index) => {
+ row.source_finding_id = rows[index]!.source_finding_id;
+ });
+ const prepareRows = (findings: ExternalFindings["findings"]) =>
+ f.prepareNormalized({ ...input, read: findings.length, findings });
+ const retry = metadata ? () => f.prepareNormalized(input) : f.prepareFile;
+ if (mode === "released ID only") {
+ const assigned = input.findings.map((row, i) => ({
+ ...row,
+ source_finding_id: "ticket-" + i,
+ }));
+ await expect(prepareRows(assigned)).rejects.toThrow("pending hosted");
+ expect(await readFile(oldPath, "utf8")).toBe(bytes);
+ expect(f.posts).toHaveLength(1);
+ return;
+ }
+ if (mode === "released assigned IDs" || omittedMetadata) {
+ // The scanner now generates different IDs for the original native rows.
+ await expect(f.prepareFile()).rejects.toThrow("pending hosted");
+ expect(await readFile(oldPath, "utf8")).toBe(bytes);
+ expect(f.posts).toHaveLength(1);
+ return;
+ }
+ if (mode === "released unrelated evidence") {
+ expect((await retry()).preview.resumed).toBe(false);
+ expect(await readFile(oldPath, "utf8")).toBe(bytes);
+ return;
+ }
+ if (partial) {
+ const bare = { read: rows.length, excluded: [], findings: rows };
+ await expect(
+ f.prepareNormalized({
+ ...bare,
+ sourceFormat: semgrep.format,
+ sourceRepositoryUrl: url,
+ semgrepPlatformScope: {
+ deployment: semgrep.deployment,
+ projectId: String(project.id),
+ product: semgrep.product,
+ ref: "refs/heads/other",
+ },
+ }),
+ ).rejects.toThrow(/scope/iu);
+ f.state.repositoryUrl = "https://github.com/example/renamed";
+ await expect(
+ f.prepareNormalized({
+ ...bare,
+ sourceRepositoryUrl: f.state.repositoryUrl,
+ }),
+ ).rejects.toThrow(/repository|project/i);
+ expect((await f.prepareNormalized(bare)).preview.requests).toEqual(
+ pending.requests,
+ );
+ f.state.repositoryUrl = url;
+ expect((await prepareRows(rows)).preview.requests).toEqual(
+ pending.requests,
+ );
+ return;
+ }
+ if (mode === "released normalized SDK") {
+ expect((await prepareRows(rows)).preview.resumed).toBe(true);
+ bytes = await readFile(oldPath, "utf8");
+ expect(JSON.parse(bytes)).toMatchObject({
+ approvedRepositoryUrl: url,
+ requests: pending.requests,
+ inputProjection: {
+ ...pending.inputProjection,
+ retainedHostedSelection: true,
+ },
+ });
+ }
+ const older = "Resume it with the SDK build that created it";
+ if (mode === "projected metadata") {
+ const edited = structuredClone(originalRows).reverse();
+ edited[0]!.evidence.title += " updated";
+ await expect(prepareRows(edited)).rejects.toThrow(older);
+ expect((await prepareRows(rows)).preview.resumed).toBe(false);
+ expect(await readFile(oldPath, "utf8")).toBe(bytes);
+ return;
+ }
+ await expect(retry()).rejects.toThrow(older);
+ // The exact projected path has a bare filename but a repository-bound payload digest.
+ if (mode === "projected evidence")
+ await expect(prepareRows(rows)).rejects.toThrow(older);
+ const changed = structuredClone(input.findings).reverse();
+ changed[0]!.evidence.title += " changed";
+ await expect(prepareRows(changed)).rejects.toThrow(older);
+ const unrelated = {
+ source_finding_id: "unrelated",
+ evidence: { title: "Another finding", severity: "high" as const },
+ };
+ if (mode === "native") {
+ const other = "https://github.com/example/another";
+ f.state.repositoryUrl = other;
+ const otherInput = {
+ ...input,
+ read: 1,
+ findings: [unrelated],
+ sourceRepositoryUrl: other,
+ };
+ const fresh = await prepareNormalizedExternalPublication(
+ otherInput,
+ { ...options, provider },
+ {
+ ...f.deps,
+ environment: {
+ ...f.environment,
+ CODEX_SECURITY_CLOUD_BASE_URL:
+ "http://127.0.0.1:4200/backend-api/aardvark",
+ },
+ },
+ );
+ expect(fresh.preview.resumed).toBe(false);
+ f.state.repositoryUrl = url;
+ }
+ if (enrichedNative) {
+ await expect(
+ prepareRows([input.findings[0]!, unrelated]),
+ ).rejects.toThrow(older);
+ }
+ const disjoint = prepareRows([unrelated]);
+ if (released || mode === "per-finding metadata")
+ expect((await disjoint).preview.resumed).toBe(false);
+ else await expect(disjoint).rejects.toThrow(older);
+ expect(f.posts).toHaveLength(1);
+ expect(await readFile(oldPath, "utf8")).toBe(bytes);
+ f.state.marker = "generation-2";
+ if (mode === "per-finding metadata" || enrichedNative) {
+ await expect(retry()).rejects.toThrow(
+ "deployment could not be established",
+ );
+ expect(await readFile(oldPath, "utf8")).toBe(bytes);
+ }
+ const afterReset =
+ mode === "projected evidence" ||
+ mode === "per-finding metadata" ||
+ enrichedNative
+ ? () => prepareRows(originalRows)
+ : retry;
+ await expect(afterReset()).rejects.toThrow("retired without uploading");
+ expect(
+ (await readdir(dir)).filter((name) => name.endsWith(".pending.json")),
+ ).toEqual([]);
+ expect((await retry()).preview.resumed).toBe(false);
+ expect(f.posts).toHaveLength(1);
+ },
+);
test("Sonar replays the selected evidence locally and to Cloud without signing in to Sonar again", async () => {
const repo = "https://github.com/example/project";
@@ -1486,10 +4028,7 @@ async function inspectorPublicationFixture(name = inspectorRepositoryUrl) {
const prepare = (settings: ExternalPublicationOptions = inspectorOptions) =>
prepareExternalPublication(f.file, settings, f.deps);
async function loseResponse(prepared: PreparedExternalPublication) {
- f.state.loseResponse = true;
- await expect(prepared.publish()).rejects.toThrow(
- "resume the saved request",
- );
+ await f.loseResponse(prepared);
return f.posts.at(-1)!;
}
return { f, input, project, map, prepare, loseResponse };
@@ -1738,6 +4277,22 @@ const savedDependabotData = {
};
test.each([
+ {
+ data: { format: "snyk-rest-issues/v2", issueJson: "{}" },
+ requiredProvider: "snyk",
+ digest: "e5b609dd6f1d5846f1b84457c319fb02505e55385ed2a4823f2a359fef7e3f33",
+ selected: { ...options, provider: "snyk" as const },
+ filename:
+ "d7c57e64c210ec93849be17563ba9ae4e9abbd5bd614fbc99a27346e72ce80ed.pending.json",
+ },
+ {
+ data: { format: "semgrep-platform-findings/v1" },
+ requiredProvider: "semgrep",
+ digest: "03781aba18f0321eef4e0aeccb512116387931f166fa42c148587085933cd48b",
+ selected: { ...options, provider: "veracode" as const },
+ filename:
+ "b1fda8b80a88dc44670dcd566e5b4e13f8a3cc1ddd776973a047ffe45e998987.pending.json",
+ },
{
...savedBlackDuckEvidence,
selected: options,
@@ -1797,11 +4352,15 @@ test.each([
),
};
const input = { read: 1, excluded: [], findings: [finding] };
- const prepare = () =>
- prepareNormalizedExternalPublication(input, selected, {
- ...f.deps,
- environment,
- });
+ const prepare = (sourceRepositoryUrl?: string) =>
+ prepareNormalizedExternalPublication(
+ { ...input, ...(sourceRepositoryUrl && { sourceRepositoryUrl }) },
+ selected,
+ {
+ ...f.deps,
+ environment,
+ },
+ );
// The previous publisher accepted this opaque evidence. Keep its checkpoint
// filename and digests so this exercises recovery across the provider change.
@@ -1842,13 +4401,23 @@ test.each([
expect(f.posts).toHaveLength(0);
return;
}
+ if (selected.provider === "snyk") {
+ const other = "https://github.com/example/other";
+ await expect(prepare(other)).rejects.toThrow(/does not match/);
+ expect((await prepare(f.state.repositoryUrl)).preview.resumed).toBe(true);
+ expect((await prepare(f.state.repositoryUrl)).preview.resumed).toBe(true);
+ await expect(prepare(other)).rejects.toThrow(/does not match/);
+ }
const retry = await prepare();
expect(retry.preview.resumed).toBe(true);
expect((await retry.publish()).counts.created).toBe(1);
expect(f.posts.map((body) => JSON.parse(body))).toEqual([request]);
- await expect(prepare()).rejects.toThrow(
- `requires the ${requiredProvider} provider`,
- );
+ if (selected.provider === requiredProvider)
+ await expect(prepare()).rejects.toThrow(/Snyk/i);
+ else
+ await expect(prepare()).rejects.toThrow(
+ `requires the ${requiredProvider} provider`,
+ );
},
);
@@ -4563,45 +7132,6 @@ async function stageReleasedCheckpoint(
}
// These filenames, request IDs and projections were recorded by the old SDK.
-test("a verified retry checks the scanner evidence in its saved request", async () => {
- const f = await fixture();
- const marker = "semgrep-platform-selection/v1";
- class Format extends String {
- toJSON() {
- return "ordinary";
- }
- }
- const input = {
- read: 1,
- excluded: [],
- findings: [
- {
- source_finding_id: "example",
- evidence: {
- title: "Example",
- severity: "low" as const,
- source_data: { format: new Format(marker) as unknown as string },
- },
- },
- ],
- };
- const preview = await prepareReleased(f, input);
- const request = structuredClone(preview.preview.requests[0]!);
- expect(request.items[0]!.evidence.source_data?.["format"]).toBe("ordinary");
- request.items[0]!.evidence.source_data!["format"] = marker;
- // The released publisher saved this string after detaching the evidence.
- const { path, saved } = await stageReleasedCheckpoint(f, {
- filename:
- "52123d2374947cb5393a2305c5dd90bf77749966cb9e4c7bfd9871293eab7b70.pending.json",
- requests: [request],
- inputDigest:
- "90deee40efc1c9475a8850da689b5b6da4ba22b1c9ded04592eb29b35cfb4c08",
- });
- await expect(prepareReleased(f, input)).rejects.toThrow("local import");
- expect(f.posts).toHaveLength(0);
- expect(await readFile(path, "utf8")).toBe(saved);
-});
-
async function oldScoreApproval(field: string, stageApproval = true) {
const f = await fixture();
const { filename, requestId, inputProjection } =
@@ -6611,6 +9141,37 @@ test("Semgrep publication preserves identity across JSON and SARIF with explicit
});
});
+test.each([
+ ["semgrep", semgrepJson],
+ ["opengrep", opengrepJson],
+] as const)(
+ "%s file retries a released request with its original source URL after a rename",
+ async (provider, scannerOutput) => {
+ const f = await fixture(scannerOutput());
+ const originalUrl = f.destination().url;
+ const prepare = (sourceRepositoryUrl: string) =>
+ prepareExternalPublication(
+ f.file,
+ { ...options, provider, sourceRepositoryUrl },
+ f.deps,
+ );
+ const original = await prepare(originalUrl);
+ const pending = await f.loseResponse(original);
+ await useReleasedCheckpoint(pending);
+ f.state.repositoryUrl = "https://github.com/example/renamed";
+
+ await expect(prepare("https://github.com/other/project")).rejects.toThrow(
+ /does not match/,
+ );
+ const retry = await prepare(originalUrl);
+ expect(retry.preview.resumed).toBe(true);
+ expect(retry.preview.requests).toEqual(original.preview.requests);
+ await retry.publish();
+ expect(f.posts).toEqual([f.posts[0]!, f.posts[0]!]);
+ expect(f.receipts.size).toBe(1);
+ },
+);
+
test("OpenGrep publication retains grouped evidence through Cloud readback and replay", async () => {
const input = opengrepTraceJson();
const { results, ...report } = input;
diff --git a/sdk/typescript/tests-ts/fixtures/snyk-released-checkpoints.json b/sdk/typescript/tests-ts/fixtures/snyk-released-checkpoints.json
new file mode 100644
index 000000000..770e05f4d
--- /dev/null
+++ b/sdk/typescript/tests-ts/fixtures/snyk-released-checkpoints.json
@@ -0,0 +1,131 @@
+{
+ "code": {
+ "file": "9f5e2365b32a81e81b8257099217472aa8f10616073188538ac0bd995591eb4c.pending.json",
+ "checkpoint": {
+ "accountId": "synthetic-account",
+ "requests": [
+ {
+ "request_id": "e13cc517-c37c-4ba0-ac03-866808c9a613",
+ "repository": {
+ "id": "repo-example",
+ "repo_connector_id": "github",
+ "environment_id": "environment-example",
+ "reset_marker": "generation-1"
+ },
+ "source": {
+ "provider": "snyk",
+ "source_key": "tenant-example/vulnerability-finding"
+ },
+ "items": [
+ {
+ "client_id": "item-1",
+ "source_finding_id": "snyk-rest:96875c30e9652a9e0b475e75cd19e0fb32bcba6dc0b8bfe3ef0169ee7408e95f",
+ "expected_version": 0,
+ "evidence": {
+ "title": "Synthetic finding",
+ "description": null,
+ "severity": "high",
+ "advisory_ids": [],
+ "locations": [
+ {
+ "path": "src/app.ts",
+ "line": 3
+ }
+ ],
+ "code_revision": "abc123",
+ "source_updated_at": 1767225600,
+ "details": {
+ "kind": "sast",
+ "repository": {
+ "url": "https://github.com/example/project",
+ "id": null,
+ "name": null
+ },
+ "scanner_origin": "Snyk Code",
+ "source_status": "open",
+ "rule": {
+ "id": "sample-rule",
+ "short_id": null,
+ "name": null
+ },
+ "weakness_ids": [],
+ "scanner_verdict": null,
+ "remediation_instructions": null,
+ "code": null,
+ "secret": null,
+ "configuration": null,
+ "file_url": null
+ },
+ "url": null,
+ "packages": [],
+ "branch": null,
+ "source_scan_id": null,
+ "image_digests": [],
+ "source_data": {}
+ }
+ }
+ ]
+ }
+ ],
+ "inputProjection": {
+ "inputDigest": "ab915d32fcdeb8b03c684d77c20c7371c188b9e4468ebd2fd4090d15a43d40a5",
+ "savedEvidenceDigest": "ab915d32fcdeb8b03c684d77c20c7371c188b9e4468ebd2fd4090d15a43d40a5"
+ }
+ }
+ },
+ "open-source": {
+ "file": "fff528eb227b82ed41cb3390cfd12473d1c1b756bdd4f399d8bc0117b67d9498.pending.json",
+ "checkpoint": {
+ "accountId": "synthetic-account",
+ "requests": [
+ {
+ "request_id": "dac96f20-5073-4ad4-89d8-3361cb30a072",
+ "repository": {
+ "id": "repo-example",
+ "repo_connector_id": "github",
+ "environment_id": "environment-example",
+ "reset_marker": "generation-1"
+ },
+ "source": {
+ "provider": "snyk",
+ "source_key": "tenant-example/vulnerability-finding"
+ },
+ "items": [
+ {
+ "client_id": "item-1",
+ "source_finding_id": "snyk-rest:1c938a65924060f2e39c4135d140fa882f2e6b0c4875202feee308e7e44ea815",
+ "expected_version": 0,
+ "evidence": {
+ "title": "Synthetic finding",
+ "description": null,
+ "severity": "high",
+ "advisory_ids": ["sample-rule"],
+ "packages": [
+ {
+ "name": "example-lib",
+ "installed_version": "1.0.0",
+ "ecosystem": "npm",
+ "manifest_path": "app/package.json",
+ "fixed_versions": []
+ }
+ ],
+ "source_updated_at": 1767225600,
+ "url": null,
+ "locations": [],
+ "branch": null,
+ "code_revision": null,
+ "source_scan_id": null,
+ "image_digests": [],
+ "source_data": {}
+ }
+ }
+ ]
+ }
+ ],
+ "inputProjection": {
+ "inputDigest": "9facde5e89e65c58515c4b84f445b64adf796228af93a4fba49b894b16d807a9",
+ "savedEvidenceDigest": "9facde5e89e65c58515c4b84f445b64adf796228af93a4fba49b894b16d807a9"
+ }
+ }
+ }
+}
diff --git a/sdk/typescript/tests-ts/semgrep-guardian-findings.test.ts b/sdk/typescript/tests-ts/semgrep-guardian-findings.test.ts
index 06c8be1f4..4541e1b8d 100644
--- a/sdk/typescript/tests-ts/semgrep-guardian-findings.test.ts
+++ b/sdk/typescript/tests-ts/semgrep-guardian-findings.test.ts
@@ -198,6 +198,45 @@ test("Guardian Supply Chain retains its summary and checks the operator's checko
expect(await fixture.read()).toHaveLength(0);
});
+test("SDK exports containing Guardian findings fail before Cloud credential lookup", async () => {
+ const guardian = normalizeSemgrepFindings(capture, {}, "local");
+ const normalized = {
+ read: 1,
+ excluded: [],
+ findings: [
+ {
+ source_finding_id: "example",
+ evidence: { title: "Example", severity: "high" as const },
+ },
+ ],
+ };
+ const credentials = mock(async () => {
+ throw new Error("Unexpected credential lookup");
+ });
+ for (const input of [
+ { ...normalized, toJSON: () => guardian },
+ {
+ ...normalized,
+ findings: [
+ { ...normalized.findings[0]!, toJSON: () => guardian.findings[0] },
+ ],
+ },
+ ]) {
+ await expect(
+ prepareNormalizedExternalPublication(
+ input,
+ {
+ repository: project,
+ provider: "semgrep",
+ sourceKey: "example/topic",
+ },
+ { environment, credentials },
+ ),
+ ).rejects.toThrow(/local import/);
+ }
+ expect(credentials).not.toHaveBeenCalled();
+});
+
test("empty selections and disabled imports do not call Guardian; tool errors retain their message", async () => {
const callTool = mock(async () => ({
isError: true,
diff --git a/sdk/typescript/tests-ts/semgrep-platform-selection-file.test.ts b/sdk/typescript/tests-ts/semgrep-platform-selection-file.test.ts
index 21668a2e4..5e9b09c1e 100644
--- a/sdk/typescript/tests-ts/semgrep-platform-selection-file.test.ts
+++ b/sdk/typescript/tests-ts/semgrep-platform-selection-file.test.ts
@@ -14,7 +14,10 @@ import { prepareLocalExternalImport } from "../src/external-findings-import.js";
import { prepareNormalizedLocalImport } from "../src/external-findings-local.js";
import { prepareNormalizedExternalPublication } from "../src/external-findings-publish.js";
import { normalizeSemgrepFindings } from "../src/semgrep-findings.js";
-import { selectSemgrepPlatformFindings } from "../src/semgrep-platform-findings.js";
+import {
+ normalizeSemgrepPlatformFindings,
+ selectSemgrepPlatformFindings,
+} from "../src/semgrep-platform-findings.js";
import { semgrepSarif } from "./support/semgrep-import-fixtures.js";
import { localReportFixture as reportFixture } from "./external-findings-local-helpers.js";
@@ -116,6 +119,28 @@ test("rechecks decimal native IDs when other evidence uses scientific notation",
expect((await (await fixture.prepare()).import()).counts.unchanged).toBe(1);
});
+test("Semgrep message titles stay intact by default and fall back above 512 Cloud bytes", () => {
+ const exact = "é".repeat(256);
+ const message = exact + "!";
+ expect(
+ normalizeSemgrepPlatformFindings(
+ selectRow({ rule: { message: exact } }),
+ {},
+ "cloud",
+ ).findings[0]?.evidence.title,
+ ).toBe(exact);
+ const report = selectRow({ rule: { message } });
+ expect(
+ normalizeSemgrepPlatformFindings(report).findings[0]?.evidence.title,
+ ).toBe(message);
+ expect(
+ normalizeSemgrepPlatformFindings(report, {}, "cloud").findings[0]?.evidence,
+ ).toMatchObject({
+ title: "Semgrep finding 8",
+ description: message,
+ });
+});
+
test.each(["", ".0", "e0"])(
"an archived selection keeps its original occurrence ID for an integer token with suffix %s",
(suffix) => {
@@ -206,9 +231,10 @@ test("two selected Platform SAST IDs from separate pages replay unchanged in SQL
unchanged: 2,
});
expect(await fixture.read()).toEqual(saved);
- expect(() =>
- normalizeSemgrepFindings(report, { sourceRepositoryUrl: url }, "cloud"),
- ).toThrow("local import");
+ expect(
+ normalizeSemgrepFindings(report, { sourceRepositoryUrl: url }, "cloud")
+ .findings,
+ ).toHaveLength(2);
});
test("native IDs and source URLs can identify a row with an empty display name", () => {
@@ -1027,32 +1053,14 @@ test("direct SDK imports respect the native source even with a caller-selected I
const fetch = mock(async () => {
throw new Error("Unexpected Cloud request");
});
- await expect(
- prepareNormalizedExternalPublication(normalized, options, {
- ...deps,
- fetch,
- }),
- ).rejects.toThrow("local import");
const oversized = structuredClone(normalized);
- oversized.findings[0]!.evidence.title = "a".repeat(256 * 1024);
+ oversized.findings[0]!.evidence.title = "é".repeat(256) + "!";
await expect(
prepareNormalizedExternalPublication(oversized, options, {
...deps,
fetch,
}),
- ).rejects.toThrow("local import");
- const exported = {
- source_finding_id: "example",
- evidence: { title: "Example", severity: "low" as const },
- toJSON: () => normalized.findings[0],
- };
- await expect(
- prepareNormalizedExternalPublication(
- { read: 1, excluded: [], findings: [exported] },
- options,
- { ...deps, fetch },
- ),
- ).rejects.toThrow("local import");
+ ).rejects.toThrow(/title.*512/);
expect(fetch).not.toHaveBeenCalled();
const row = normalized.findings[0]!;
@@ -1078,12 +1086,6 @@ test("direct SDK imports respect the native source even with a caller-selected I
await expect(
prepareNormalizedLocalImport(normalized, genericOptions, deps),
).rejects.toThrow("semgrep provider");
- await expect(
- prepareNormalizedExternalPublication(normalized, genericOptions, {
- ...deps,
- fetch,
- }),
- ).rejects.toThrow("local import");
const data = row.evidence.source_data!;
const original = data[rawFindingKey] as string;
data[rawFindingKey] = JSON.stringify({
@@ -1104,12 +1106,6 @@ test("direct SDK imports respect the native source even with a caller-selected I
await expect(
prepareNormalizedLocalImport(normalized, options, deps),
).rejects.toThrow(/Semgrep/i);
- await expect(
- prepareNormalizedExternalPublication(normalized, options, {
- ...deps,
- fetch,
- }),
- ).rejects.toThrow("local import");
};
delete data["deployment"];
await rejectIncomplete();
@@ -1123,7 +1119,6 @@ test("direct SDK imports respect the native source even with a caller-selected I
else delete row.evidence.source_data;
await rejectIncomplete();
}
- expect(fetch).not.toHaveBeenCalled();
const source_data = {
findingJson: "{}",
@@ -1148,7 +1143,6 @@ test("direct SDK imports respect the native source even with a caller-selected I
await expect(
prepareNormalizedExternalPublication(generic, genericOptions, {
...deps,
- fetch,
credentials,
}),
).rejects.toThrow("Reached Cloud credentials");
diff --git a/sdk/typescript/tests-ts/semgrep-platform-v1-compat.test.ts b/sdk/typescript/tests-ts/semgrep-platform-v1-compat.test.ts
index 107d8490a..0fab457f6 100644
--- a/sdk/typescript/tests-ts/semgrep-platform-v1-compat.test.ts
+++ b/sdk/typescript/tests-ts/semgrep-platform-v1-compat.test.ts
@@ -6,7 +6,6 @@ import { gzipSync } from "node:zlib";
import { semgrepJson } from "./support/semgrep-import-fixtures.js";
import { prepareLocalExternalImport } from "../src/external-findings-import.js";
import { prepareNormalizedLocalImport } from "../src/external-findings-local.js";
-import { prepareNormalizedExternalPublication } from "../src/external-findings-publish.js";
import { bundledPluginRoot, runWorkbench } from "../src/runtime.js";
import {
cloudRepositoryUrlKey,
@@ -19,6 +18,7 @@ import {
} from "../src/semgrep-findings.js";
import {
selectSemgrepPlatformFindings,
+ normalizeSemgrepPlatformFindings,
semgrepPlatformFindingRepository,
} from "../src/semgrep-platform-findings.js";
@@ -35,6 +35,48 @@ const code = {
rule: { name: "fixture.rule" },
};
+test("the standalone Cloud normalizer verifies the port on reader mappings", () => {
+ const projectUrl = "https://scm.example.test:443/Example/Project";
+ const report = {
+ ...scope,
+ format: "semgrep-platform-selection/v1",
+ project: { ...scope.project, url: projectUrl },
+ findings: [
+ JSON.stringify({ ...code, id: 12, repository: { url: projectUrl } }),
+ ],
+ };
+ const mapped = (url: string) => ({
+ sourceRepositories: [{ url, platform: "github" as const }],
+ });
+ const different = mapped(projectUrl.replace(":443", ""));
+ expect(() =>
+ normalizeSemgrepPlatformFindings(report, different, "cloud"),
+ ).toThrow(/reader's source repository/);
+ expect(
+ normalizeSemgrepPlatformFindings(report, different).findings,
+ ).toHaveLength(1);
+ expect(
+ normalizeSemgrepPlatformFindings(
+ report,
+ mapped(projectUrl.toLowerCase()),
+ "cloud",
+ ).findings,
+ ).toHaveLength(1);
+});
+
+test("Cloud shortens a long v1 message title using its original textual ID", () => {
+ const description = "é".repeat(257);
+ const report = {
+ ...scope,
+ format: "semgrep-platform-findings/v1",
+ findingCount: 1,
+ findings: [{ id: "1.3e1", rule_message: description }],
+ };
+ expect(
+ normalizeSemgrepFindings(report, {}, "cloud").findings[0]?.evidence,
+ ).toMatchObject({ title: "Semgrep finding 1.3e1", description });
+});
+
test("strongly bound current SAST rows have identical selection and backlog exclusions", () => {
const location = { file_path: "src/service.ts", line: 2, end_line: 3 };
const rows = [
@@ -964,87 +1006,21 @@ test("the retained repository witness stays stable when the API returns rows in
expect(project([byUrl, byId])).toEqual(first);
});
-test("the chosen provider distinguishes unmarked v1 scopes from generic Wiz evidence", async () => {
- const options = {
- provider: "wiz" as const,
- sourceKey: "demo/topic",
- repository: url,
- };
- const dependencies = {
- environment: { CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: "1" },
- credentials: async () => {
- throw new Error("Reached Cloud credential lookup");
+test("legacy rows with a generic format retain their repository for the Semgrep provider", () => {
+ const finding = {
+ source_finding_id: "opaque",
+ evidence: {
+ title: "Example",
+ severity: "low" as const,
+ branch: scope.ref,
+ details: { kind: "sast" as const, repository: { url } },
+ source_data: { ...scope, format: "normalized/v1", finding: { id: 12 } },
},
};
- const report = (
- id: string | number | null,
- projectUrl: string | null,
- format?: string,
- ) => ({
- read: 1,
- excluded: [],
- findings: [
- {
- source_finding_id: "opaque",
- evidence: {
- title: "Example",
- severity: "low" as const,
- source_data: {
- ...scope,
- ...(format && { format }),
- project: { ...scope.project, id: 10, url: projectUrl },
- finding: { id },
- },
- },
- },
- ],
- });
- await expect(
- prepareNormalizedExternalPublication(
- report("1.2e1", null),
- { ...options, provider: "semgrep" },
- dependencies,
- ),
- ).rejects.toThrow("local import");
- await expect(
- prepareNormalizedExternalPublication(
- report(null, null),
- { ...options, provider: "semgrep" },
- dependencies,
- ),
- ).rejects.toThrow("local import");
- await expect(
- prepareNormalizedExternalPublication(
- report(12, url, "normalized/v1"),
- { ...options, provider: "semgrep" },
- dependencies,
- ),
- ).rejects.toThrow("local import");
- const original = report(12, null, "semgrep-platform-findings/v1");
- await expect(
- prepareNormalizedExternalPublication(original, options, dependencies),
- ).rejects.toThrow("local import");
- const unmarked = report(12, null);
- await expect(
- prepareNormalizedExternalPublication(unmarked, options, dependencies),
- ).rejects.toThrow("Reached Cloud credential lookup");
- unmarked.findings[0]!.source_finding_id =
- "semgrep-platform:" + "1".repeat(64);
- await expect(
- prepareNormalizedExternalPublication(unmarked, options, dependencies),
- ).rejects.toThrow("local import");
- const branded = report(12, url, "wiz-findings/v1");
- expect(
- semgrepPlatformFindingRepository(branded.findings[0]!, "wiz"),
- ).toBeUndefined();
- await expect(
- prepareNormalizedExternalPublication(branded, options, dependencies),
- ).rejects.toThrow("Reached Cloud credential lookup");
- const generic = report("WIZ-1", url);
- await expect(
- prepareNormalizedExternalPublication(generic, options, dependencies),
- ).rejects.toThrow("Reached Cloud credential lookup");
+ expect(semgrepPlatformFindingRepository(finding, "semgrep")).toBe(url);
+ expect(semgrepPlatformFindingRepository(finding, "wiz")).toBeUndefined();
});
+
test("v2 files require exact counts and string project IDs", async () => {
const dir = await mkdtemp(join(tmpdir(), "semgrep-v2-controls-"));
const path = join(dir, "report.json");
diff --git a/sdk/typescript/tests-ts/snyk-rest-boundaries.test.ts b/sdk/typescript/tests-ts/snyk-rest-boundaries.test.ts
index dbf873d43..1098e34f6 100644
--- a/sdk/typescript/tests-ts/snyk-rest-boundaries.test.ts
+++ b/sdk/typescript/tests-ts/snyk-rest-boundaries.test.ts
@@ -3,7 +3,10 @@ import { readSnykFindings } from "../src/snyk-findings.js";
import { snykJson, snykSarif } from "./support/snyk-import-fixtures.js";
import { prepareNormalizedExternalPublication } from "../src/external-findings-publish.js";
import { prepareNormalizedLocalImport } from "../src/external-findings-local.js";
-import { normalizeSnykIssues } from "../src/snyk-rest-findings.js";
+import {
+ normalizeSnykIssues,
+ snykRestImportRepository,
+} from "../src/snyk-rest-findings.js";
import {
snykIssue,
snykLocalFixture,
@@ -11,6 +14,36 @@ import {
snykRestFixture,
} from "./support/snyk-rest-fixtures.js";
+function cloudCatalog(environment: NodeJS.ProcessEnv, repositoryUrl: string) {
+ return {
+ environment,
+ credentials: async () => ({
+ access_token: "synthetic-token",
+ account_id: "synthetic-account",
+ }),
+ fetch: async (url: string, init: RequestInit) => {
+ expect(init.method).toBe("GET");
+ expect(new URL(url).pathname).toEndWith("/repositories");
+ return Response.json({
+ object: "page",
+ has_more: false,
+ next: null,
+ data: [
+ {
+ object: "security.repository",
+ id: "repo-example",
+ repo_connector_id: "github",
+ url: repositoryUrl,
+ default_branch: "main",
+ reset_marker: "initial",
+ import_environment_id: "env-example",
+ },
+ ],
+ });
+ },
+ };
+}
+
test("a generic Snyk reader can import findings with an SSH repository URL", async () => {
const target = { repository: snykOptions.sourceRepositoryUrl };
const report = {
@@ -49,53 +82,41 @@ test("a generic Snyk reader can import findings with an SSH repository URL", asy
await expect(prepare()).rejects.toThrow("Snyk reader's repository");
});
-test("normalized Snyk REST Open Source rows cannot publish to Cloud", async () => {
- const report = snykRestFixture("open-source");
- const normalized = normalizeSnykIssues(report);
- const fetch = mock(async () => {
- throw new Error("Unexpected Cloud request");
- });
- const serialized = {
- source_finding_id: "example",
- evidence: { title: "Example", severity: "low" as const },
- toJSON: () => normalized.findings[0],
- };
- for (const input of [
- normalized,
- { read: 1, excluded: [], findings: [serialized] },
- ]) {
- await expect(
- prepareNormalizedExternalPublication(
- input,
- {
- provider: "snyk",
- repository: "https://github.com/example/another",
- sourceKey: "example",
- },
- { environment: { CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: "1" }, fetch },
- ),
- ).rejects.toThrow("local import");
- }
- expect(fetch).not.toHaveBeenCalled();
- expect(() => normalizeSnykIssues(report, "cloud")).toThrow("local import");
+test("normalized Snyk Open Source rows require their matching Cloud repository", async () => {
+ await using fixture = await snykLocalFixture();
+ const normalized = normalizeSnykIssues(
+ snykRestFixture("open-source"),
+ "cloud",
+ );
+ expect(normalized.findings).toHaveLength(1);
+ await expect(
+ prepareNormalizedExternalPublication(
+ normalized,
+ { provider: "snyk", repository: "repo-example", sourceKey: "example" },
+ cloudCatalog(fixture.environment, "https://github.com/example/another"),
+ ),
+ ).rejects.toThrow(/does not match/);
});
-test("an empty normalized Snyk capture keeps its local publication boundary", async () => {
+test("an empty normalized Snyk report stops before Cloud credentials", async () => {
const capture = snykRestFixture("open-source");
capture.pages[0]!.responseJson = JSON.stringify({ data: [], links: {} });
const normalized = normalizeSnykIssues(capture);
expect(normalized.findings).toHaveLength(0);
+ const credentials = mock(async () => {
+ throw new Error("Unexpected credentials");
+ });
await expect(
prepareNormalizedExternalPublication(
normalized,
+ { provider: "snyk", repository: "repo-example", sourceKey: "example" },
{
- provider: "snyk",
- repository: "https://github.com/example/another",
- sourceKey: "example",
+ environment: { CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: "1" },
+ credentials,
},
- { environment: { CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: "1" } },
),
- ).rejects.toThrow("Snyk REST findings require a local import");
+ ).rejects.toThrow("No supported findings");
+ expect(credentials).not.toHaveBeenCalled();
});
test("a Snyk asset cannot authorize a projected row from a second repository", async () => {
@@ -426,9 +447,7 @@ test("retagged original Snyk records remain scoped without a matching source ID
sourceKey: "example",
repository: f.repository,
};
- const credentials = mock(async () => {
- throw new Error("Unexpected Cloud credentials");
- });
+ const cloud = cloudCatalog(f.environment, snykOptions.sourceRepositoryUrl);
for (const source of [
original,
legacy,
@@ -439,11 +458,12 @@ test("retagged original Snyk records remain scoped without a matching source ID
delete row.evidence.source_data["snykRestIssueKey"];
delete row.evidence.source_data["apiVersion"];
await expect(
- prepareNormalizedExternalPublication(projected, opts, {
- environment: f.environment,
- credentials,
- }),
- ).rejects.toThrow("local import");
+ prepareNormalizedExternalPublication(
+ projected,
+ { ...opts, repository: "repo-example" },
+ cloud,
+ ),
+ ).rejects.toThrow("requires the snyk provider");
await expect(
prepareNormalizedLocalImport(projected, opts, {
environment: f.environment,
@@ -476,11 +496,12 @@ test("retagged original Snyk records remain scoped without a matching source ID
...resources,
};
await expect(
- prepareNormalizedExternalPublication(projected, opts, {
- environment: f.environment,
- credentials,
- }),
- ).rejects.toThrow("local import");
+ prepareNormalizedExternalPublication(
+ projected,
+ { ...opts, repository: "repo-example" },
+ cloud,
+ ),
+ ).rejects.toThrow("requires the snyk provider");
await expect(
prepareNormalizedLocalImport(projected, opts, {
environment: f.environment,
@@ -495,11 +516,12 @@ test("retagged original Snyk records remain scoped without a matching source ID
row.evidence.source_data = { ...original, format: "reader/v1" };
delete row.evidence.source_data[missing];
await expect(
- prepareNormalizedExternalPublication(projected, opts, {
- environment: f.environment,
- credentials,
- }),
- ).rejects.toThrow("local import");
+ prepareNormalizedExternalPublication(
+ projected,
+ { ...opts, repository: "repo-example" },
+ cloud,
+ ),
+ ).rejects.toThrow("requires the snyk provider");
await expect(
prepareNormalizedLocalImport(projected, opts, {
environment: f.environment,
@@ -527,7 +549,6 @@ test("retagged original Snyk records remain scoped without a matching source ID
),
).rejects.toThrow("both v1 and v2");
}
- expect(credentials).not.toHaveBeenCalled();
});
test("an independently verified GitHub source applies to a CLI-monitored Snyk project", async () => {
@@ -570,9 +591,40 @@ test("a serialized Snyk report keeps its monitored asset when a reader supplies
);
const serialized = JSON.parse(JSON.stringify(report)) as typeof report;
+ const projected = Object.assign(
+ Object.create({
+ get snykRestContext() {
+ return report.snykRestContext;
+ },
+ }),
+ { read: 1, excluded: [], findings: report.findings },
+ );
const credentials = mock(async () => {
- throw new Error("Unexpected Cloud credentials");
+ throw new Error("Unexpected Cloud credential lookup");
});
+ await expect(
+ prepareNormalizedExternalPublication(
+ projected,
+ { provider: "wiz", sourceKey: "project", repository: "repo-example" },
+ {
+ environment: { CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: "1" },
+ credentials,
+ },
+ ),
+ ).rejects.toThrow(/requires the snyk provider/i);
+ const malformed = { ...projected, snykRestContext: {} };
+ await expect(
+ prepareNormalizedExternalPublication(
+ malformed,
+ { provider: "snyk", sourceKey: "project", repository: "repo-example" },
+ {
+ environment: { CODEX_SECURITY_ENABLE_EXTERNAL_IMPORTS: "1" },
+ credentials,
+ },
+ ),
+ ).rejects.toThrow(/Snyk REST report context/);
+ expect(credentials).not.toHaveBeenCalled();
+
await using matching = await snykLocalFixture({ remote: repository });
const options = {
provider: "snyk" as const,
@@ -582,11 +634,10 @@ test("a serialized Snyk report keeps its monitored asset when a reader supplies
await expect(
prepareNormalizedExternalPublication(
serialized,
- { ...options, repository: other },
- { environment: matching.environment, credentials },
+ { ...options, repository: "repo-example" },
+ cloudCatalog(matching.environment, other),
),
- ).rejects.toThrow("local import");
- expect(credentials).not.toHaveBeenCalled();
+ ).rejects.toThrow("does not match");
expect(
(
await prepareNormalizedLocalImport(serialized, options, {
@@ -687,9 +738,6 @@ test("rows from a different Snyk monitor cannot use the report's source key even
.replace(snykIssue(false).attributes.key, "other-issue-key")
.replace(snykIssue(false).id, otherId),
) as typeof capture;
- const credentials = mock(async () => {
- throw new Error("Unexpected credentials");
- });
await using f = await snykLocalFixture({
remote: report.sourceRepositoryUrl,
});
@@ -713,25 +761,169 @@ test("rows from a different Snyk monitor cannot use the report's source key even
await expect(f.prepareNormalized(input)).rejects.toThrow(
"Snyk project scope",
);
+ await expect(
+ prepareNormalizedExternalPublication(
+ input,
+ {
+ provider: "snyk",
+ sourceKey: f.sourceKey,
+ repository: report.sourceRepositoryUrl,
+ },
+ cloudCatalog(f.environment, report.sourceRepositoryUrl),
+ ),
+ ).rejects.toThrow("Snyk project scope");
}
- await expect(
- prepareNormalizedExternalPublication(
- report,
+ }
+ const saved = structuredClone(original);
+ saved.source_finding_id = "saved-reader-id";
+ saved.evidence.source_data = {
+ format: capture.format,
+ projectJson: capture.projectJson,
+ targetJson: capture.targetJson,
+ };
+ const checkSaved = (monitor: typeof capture) =>
+ snykRestImportRepository(
+ { ...normalizeSnykIssues(monitor), findings: [saved] },
+ "snyk",
+ [],
+ [],
+ undefined,
+ true,
+ );
+ expect(checkSaved(capture)?.url).toBe(capture.sourceRepositoryUrl);
+ for (const id of [capture.projectId, capture.organizationId]) {
+ expect(() => checkSaved(replace(id))).toThrow(
+ "retained Snyk project scope",
+ );
+ }
+ for (const id of [target.id, integrationId]) {
+ expect(() => checkSaved(replace(id))).toThrow(
+ "conflicting targets or integrations",
+ );
+ }
+ saved.evidence.source_data = {
+ format: capture.format,
+ targetJson: capture.targetJson.replace(`"${integrationId}"`, "null"),
+ };
+ expect(checkSaved(capture)?.url).toBe(capture.sourceRepositoryUrl);
+ expect(checkSaved(replace("github-cloud-app", "github"))?.url).toBe(
+ capture.sourceRepositoryUrl,
+ );
+ expect(() =>
+ checkSaved(replace("github-cloud-app", "github-server-app")),
+ ).toThrow("conflicting targets or integrations");
+ expect(await f.read()).toHaveLength(0);
+});
+
+test("a sparse approved Snyk record still checks its retained repository URLs", () => {
+ const url = "https://scm.example.test/Team/Project";
+ const { projectJson } = snykRestFixture("open-source", {
+ sourceRepositoryUrl: url,
+ });
+ const project = JSON.parse(projectJson);
+ project.attributes.origin = "gitlab";
+ const target = project.relationships.target.data;
+ for (const source of [
+ { format: "snyk-rest-issues/v2", sourceRepositoryUrl: url },
+ { format: "snyk-rest-issues/v2", projectJson: JSON.stringify(project) },
+ { format: "snyk-rest-issues/v2", targetJson: JSON.stringify(target) },
+ { format: "snyk-rest-issues/v1", project },
+ { format: "snyk-rest-issues/v1", target },
+ ]) {
+ const input = {
+ read: 1,
+ excluded: [],
+ findings: [
{
- provider: "snyk",
- sourceKey: f.sourceKey,
- repository: report.sourceRepositoryUrl,
+ source_finding_id: "reader-selected",
+ evidence: {
+ title: "Issue",
+ severity: "low" as const,
+ source_data: source,
+ },
},
- { environment: f.environment, credentials },
- ),
- ).rejects.toThrow(/Snyk|snyk-rest/);
+ ],
+ };
+ const check = (destination?: string) =>
+ snykRestImportRepository(
+ input,
+ "snyk",
+ [],
+ destination ? [{ url: destination, platform: "github" }] : [],
+ undefined,
+ true,
+ destination,
+ );
+ expect(check()).toBeUndefined();
+ expect(check(url)?.url).toBe(url);
+ if ("project" in source || "projectJson" in source)
+ expect(() => check(url.toLowerCase())).toThrow(
+ "does not match the original Snyk project",
+ );
+ expect(() => check("https://scm.example.test/Other/Project")).toThrow(
+ "does not match the original Snyk project",
+ );
}
- expect(credentials).not.toHaveBeenCalled();
- expect(await f.read()).toHaveLength(0);
+});
+
+test("a sparse approved project retains its known product and target linkage", () => {
+ const report = normalizeSnykIssues(snykRestFixture("code"));
+ const row = report.findings[0]!;
+ row.source_finding_id = "reader-selected";
+ const project = JSON.parse(String(report.snykRestContext!["projectJson"]));
+ const check = () => {
+ row.evidence.source_data = {
+ format: "snyk-rest-issues/v2",
+ projectJson: JSON.stringify(project),
+ };
+ return snykRestImportRepository(report, "snyk", [], [], undefined, true);
+ };
+ expect(check()?.url).toBe(report.sourceRepositoryUrl);
+ project.attributes.type = "npm";
+ expect(check).toThrow("retained Snyk project scope");
+ delete project.attributes.type;
+ project.relationships.target.data = null;
+ expect(check).toThrow("conflicting targets");
+ delete project.relationships.target;
+ expect(check()?.url).toBe(report.sourceRepositoryUrl);
+});
+
+test("opaque legacy issues retain sibling scope checks on a checkpoint retry", () => {
+ const report = normalizeSnykIssues(snykRestFixture("open-source"));
+ const native = report.findings[0]!;
+ const context = native.evidence.source_data!;
+ report.findings.push({
+ ...native,
+ source_finding_id: "snyk-rest:opaque",
+ evidence: {
+ ...native.evidence,
+ source_data: { format: context["format"] },
+ },
+ });
+ const check = (allowOpaqueLegacyRows: boolean) =>
+ snykRestImportRepository(
+ report,
+ "snyk",
+ [],
+ [],
+ undefined,
+ allowOpaqueLegacyRows,
+ );
+ report.snykRestContext = context;
+ expect(() => check(false)).toThrow();
+ expect(check(true)?.url).toBe(report.sourceRepositoryUrl);
+ // A different Snyk project can monitor exactly the same Git repository.
+ report.snykRestContext = JSON.parse(
+ JSON.stringify(context).replaceAll(
+ snykOptions.projectId,
+ "44444444-4444-4444-8444-444444444444",
+ ),
+ );
+ expect(() => check(true)).toThrow("retained Snyk project scope");
});
test.each(["v1", "v2"] as const)(
- "a linked %s issue projection uses its report's project and stays local",
+ "a linked %s issue projection uses its report's project and requires the Snyk provider",
async (version) => {
const report = normalizeSnykIssues(snykRestFixture("open-source"));
const row = report.findings[0]!;
@@ -758,21 +950,21 @@ test.each(["v1", "v2"] as const)(
sourceKey: fixture.sourceKey,
repository: fixture.repository,
};
- const credentials = mock(async () => {
- throw new Error("Unexpected Cloud credentials");
- });
const { read, findings, excluded } = report;
await expect(
prepareNormalizedExternalPublication(
{ read, findings, excluded },
- { ...options, provider: "wiz" },
- { environment: fixture.environment, credentials },
+ { ...options, repository: "repo-example", provider: "wiz" },
+ cloudCatalog(fixture.environment, snykOptions.sourceRepositoryUrl),
),
- ).rejects.toThrow("local import");
- expect(credentials).not.toHaveBeenCalled();
+ ).rejects.toThrow(/requires the snyk provider/i);
const prepare = () => fixture.prepareNormalized(report);
await expect(prepare()).rejects.toThrow("selected project");
+ const retry = () =>
+ snykRestImportRepository(report, "snyk", [], [], undefined, true);
+ expect(retry).toThrow("selected project");
row.evidence.source_data = project(snykOptions.projectId);
+ expect(retry()?.url).toBe(snykOptions.sourceRepositoryUrl);
await expect(prepare()).rejects.toThrow("normalized finding ID");
row.source_finding_id = nativeId;
const issue = JSON.parse(json);
@@ -841,11 +1033,10 @@ test.each(["v1", "v2"] as const)(
await expect(
prepareNormalizedExternalPublication(
listed,
- { ...options, provider: "wiz" },
- { environment: fixture.environment, credentials },
+ { ...options, repository: "repo-example", provider: "wiz" },
+ cloudCatalog(fixture.environment, snykOptions.sourceRepositoryUrl),
),
- ).rejects.toThrow("local import");
- expect(credentials).not.toHaveBeenCalled();
+ ).rejects.toThrow(/requires the snyk provider/i);
const complete = await fixture.prepareNormalized(full);
expect((await complete.import()).counts.created).toBe(0);
expect(await fixture.read()).toHaveLength(1);
diff --git a/sdk/typescript/tests-ts/snyk-rest-findings.test.ts b/sdk/typescript/tests-ts/snyk-rest-findings.test.ts
index 35bdc095a..ffd178579 100644
--- a/sdk/typescript/tests-ts/snyk-rest-findings.test.ts
+++ b/sdk/typescript/tests-ts/snyk-rest-findings.test.ts
@@ -47,6 +47,18 @@ function transport(...responses: unknown[]) {
};
}
+test("the Issues normalizer keeps long issue titles by default", async () => {
+ const row = issue(false);
+ row.attributes.title = "é".repeat(257);
+ const report = await transport(project(), { data: [row] }).collect();
+ expect(normalizeSnykIssues(report).findings[0]?.evidence.title).toBe(
+ row.attributes.title,
+ );
+ const cloud = normalizeSnykIssues(report, "cloud");
+ expect(cloud.findings).toHaveLength(0);
+ expect(cloud.excluded[0]?.reason).toContain("title");
+});
+
test.each(["open-source", "code"] as const)(
"Snyk %s follows scoped pagination, saves original evidence, and replays unchanged in SQLite",
async (product) => {