diff --git a/.gitignore b/.gitignore index 3d333045c..ebee77952 100644 --- a/.gitignore +++ b/.gitignore @@ -999,6 +999,7 @@ !/src/commands/scan/output-create-new-scan.test.mts !/src/commands/scan/output-delete-scan.mts !/src/commands/scan/output-diff-scan.mts +!/src/commands/scan/output-diff-scan.test.mts !/src/commands/scan/output-list-scans.mts !/src/commands/scan/output-scan-config-result.mts !/src/commands/scan/output-scan-github.mts diff --git a/src/commands/scan/cmd-scan-diff.mts b/src/commands/scan/cmd-scan-diff.mts index 8c8c15fbc..8ff442226 100644 --- a/src/commands/scan/cmd-scan-diff.mts +++ b/src/commands/scan/cmd-scan-diff.mts @@ -65,6 +65,12 @@ async function run( description: 'Force override the organization slug, overrides the default org from config', }, + strictBlocking: { + type: 'boolean', + default: false, + description: + 'Fail when unchanged blocking alerts are present in the scan diff', + }, }, help: (command, config) => ` Usage @@ -87,6 +93,7 @@ async function run( Examples $ ${command} aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 $ ${command} aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 --json + $ ${command} aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 --strict-blocking `, } @@ -107,6 +114,7 @@ async function run( json, markdown, org: orgFlag, + strictBlocking, } = cli.flags as { depth: number dryRun: boolean @@ -114,6 +122,7 @@ async function run( json: boolean markdown: boolean org: string + strictBlocking: boolean } const interactive = !!cli.flags['interactive'] @@ -185,5 +194,6 @@ async function run( orgSlug, outputKind, file, + strictBlocking, }) } diff --git a/src/commands/scan/cmd-scan-diff.test.mts b/src/commands/scan/cmd-scan-diff.test.mts index 7cf9b16e2..a18b82b1a 100644 --- a/src/commands/scan/cmd-scan-diff.test.mts +++ b/src/commands/scan/cmd-scan-diff.test.mts @@ -44,10 +44,12 @@ describe('socket scan diff', async () => { --json Output as JSON --markdown Output as Markdown --org Force override the organization slug, overrides the default org from config + --strict-blocking Fail when unchanged blocking alerts are present in the scan diff Examples $ socket scan diff aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 - $ socket scan diff aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 --json" + $ socket scan diff aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 --json + $ socket scan diff aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 --strict-blocking" `, ) expect(`\n ${stderr}`).toMatchInlineSnapshot(` diff --git a/src/commands/scan/handle-diff-scan.mts b/src/commands/scan/handle-diff-scan.mts index 5299523d5..215967d0f 100644 --- a/src/commands/scan/handle-diff-scan.mts +++ b/src/commands/scan/handle-diff-scan.mts @@ -10,6 +10,7 @@ export async function handleDiffScan({ id2, orgSlug, outputKind, + strictBlocking, }: { depth: number file: string @@ -17,6 +18,7 @@ export async function handleDiffScan({ id2: string orgSlug: string outputKind: OutputKind + strictBlocking: boolean }): Promise { const data = await fetchDiffScan({ id1, @@ -28,5 +30,6 @@ export async function handleDiffScan({ depth, file, outputKind, + strictBlocking, }) } diff --git a/src/commands/scan/output-diff-scan.mts b/src/commands/scan/output-diff-scan.mts index 5c51ee54d..9e526ca8b 100644 --- a/src/commands/scan/output-diff-scan.mts +++ b/src/commands/scan/output-diff-scan.mts @@ -21,10 +21,12 @@ export async function outputDiffScan( depth, file, outputKind, + strictBlocking, }: { depth: number file: string outputKind: OutputKind + strictBlocking: boolean }, ): Promise { if (!result.ok) { @@ -51,6 +53,28 @@ export async function outputDiffScan( return } + const changedArtifacts = [ + ...result.data.artifacts.added, + ...result.data.artifacts.updated, + ...result.data.artifacts.replaced, + ] + if ( + changedArtifacts.some(artifact => + artifact.alerts?.some(alert => alert.action === 'error'), + ) + ) { + process.exitCode = 1 + } + + if (strictBlocking) { + const unchangedBlocking = (result.data.artifacts.unchanged ?? []).filter( + artifact => artifact.alerts?.some(alert => alert.action === 'error'), + ) + if (unchangedBlocking.length > 0) { + process.exitCode = 1 + } + } + const dashboardUrl = result.data.diff_report_url const dashboardMessage = dashboardUrl ? `\n View this diff scan in the Socket dashboard: ${colors.cyan(dashboardUrl)}` diff --git a/src/commands/scan/output-diff-scan.test.mts b/src/commands/scan/output-diff-scan.test.mts new file mode 100644 index 000000000..92902fabd --- /dev/null +++ b/src/commands/scan/output-diff-scan.test.mts @@ -0,0 +1,84 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' + +import { outputDiffScan } from './output-diff-scan.mts' + +vi.mock('../../utils/logger.mts', () => ({ + logger: { error: vi.fn(), fail: vi.fn(), info: vi.fn(), log: vi.fn() }, +})) + +const diff = { + after: { id: 'after' }, + artifacts: { + added: [], + removed: [], + replaced: [], + unchanged: [ + { + type: 'npm', + name: 'left-pad', + version: '1.0.0', + alerts: [{ action: 'error', type: 'test-alert', key: 'alert-1' }], + }, + ], + updated: [], + }, + before: { id: 'before' }, + directDependenciesChanged: false, + diff_report_url: '', +} + +describe('outputDiffScan strict blocking', () => { + afterEach(() => { + process.exitCode = 0 + vi.clearAllMocks() + }) + + it('fails for new blocking alerts in ordinary diff mode', async () => { + const changed = structuredClone(diff) + changed.artifacts.added.push({ + type: 'npm', + name: 'new-package', + version: '1.0.0', + alerts: [{ action: 'error', type: 'test-alert', key: 'alert-2' }], + }) + await outputDiffScan({ ok: true, data: changed } as never, { + depth: 0, + file: '', + outputKind: 'json', + strictBlocking: false, + }) + expect(process.exitCode).toBe(1) + }) + + it('fails when an unchanged alert blocks under strict mode', async () => { + await outputDiffScan({ ok: true, data: diff } as never, { + depth: 0, + file: '', + outputKind: 'json', + strictBlocking: true, + }) + expect(process.exitCode).toBe(1) + }) + + it('does not fail on unchanged blocking alerts by default', async () => { + await outputDiffScan({ ok: true, data: diff } as never, { + depth: 0, + file: '', + outputKind: 'json', + strictBlocking: false, + }) + expect(process.exitCode).toBe(0) + }) + + it('does not fail when unchanged alerts are nonblocking', async () => { + const nonBlocking = structuredClone(diff) + nonBlocking.artifacts.unchanged[0]!.alerts![0]!.action = 'warn' + await outputDiffScan({ ok: true, data: nonBlocking } as never, { + depth: 0, + file: '', + outputKind: 'json', + strictBlocking: true, + }) + expect(process.exitCode).toBe(0) + }) +}) diff --git a/test/snapshots/termless/cli.native.socket-help-scan-diff.svg b/test/snapshots/termless/cli.native.socket-help-scan-diff.svg index 0d8821ed6..9b4be3bfc 100644 --- a/test/snapshots/termless/cli.native.socket-help-scan-diff.svg +++ b/test/snapshots/termless/cli.native.socket-help-scan-diff.svg @@ -1,4 +1,4 @@ - + CLI: v2.1.0-rc.0 | cmd: socket scan diff | org: (not set) | token: (not set) @@ -55,21 +55,25 @@ --org Force override the organization slug, overrides the default org from config - + --strict-blocking Fail when unchanged blocking alerts are present in the scan diff - Examples + - $ socket scan diff aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 + Examples - $ socket scan diff aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 --json + $ socket scan diff aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 - + $ socket scan diff aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 --json - + $ socket scan diff aaa0aa0a-aaaa-0000-0a0a-0000000a00a0 aaa1aa1a-aaaa-1111-1a1a-1111111a11a1 --strict-blocking - \ No newline at end of file + + + + + \ No newline at end of file