Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/commands/ci.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ nonzero status if the scan fails your security policy.
socket ci
```

On pull requests, use `--strict-blocking` to fail when a blocking alert is
present on an unchanged dependency as well as when the PR introduces one:

```sh
socket ci --strict-blocking
```

Strict blocking is off by default. `--disable-blocking` takes precedence and
keeps findings from failing the job.

Use `--auto-manifest` to include locally generated manifests for supported
projects. Run `socket ci --help` for all options.

Expand Down
16 changes: 14 additions & 2 deletions src/commands/ci/cmd-ci.mts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ const config: CliCommandConfig = {
description:
'Return success for findings and infrastructure failures, but keep usage errors non-zero.',
},
strictBlocking: {
type: 'boolean',
default: false,
description:
'Fail for existing blocking alerts as well as alerts introduced by this PR.',
},
exitCodeOnApiError: {
type: 'number',
default: 3,
Expand All @@ -58,7 +64,8 @@ const config: CliCommandConfig = {
Socket API token. Exit codes are 0 for success, 1 for blocking findings, 2
for usage errors, 3 for infrastructure errors, and 130 for interruption.
--disable-blocking returns 0 for findings and runtime failures, but not
usage errors. --exit-code-on-api-error changes the API failure code.
usage errors. --strict-blocking also fails for blocking alerts on unchanged
dependencies. --exit-code-on-api-error changes the API failure code.

The --auto-manifest flag does the same as the one from \`socket scan create\`
but is not enabled by default since the CI is less likely to be set up with
Expand All @@ -69,6 +76,7 @@ const config: CliCommandConfig = {
$ ${command}
$ ${command} --auto-manifest
$ ${command} --disable-blocking
$ ${command} --strict-blocking
$ ${command} --exit-code-on-api-error 5
`,
}
Expand Down Expand Up @@ -121,5 +129,9 @@ async function run(
return
}

await handleCi(Boolean(cli.flags['autoManifest']), jsonFormat === 'python-v1')
await handleCi(
Boolean(cli.flags['autoManifest']),
jsonFormat === 'python-v1',
Boolean(cli.flags['strictBlocking']),
)
}
8 changes: 7 additions & 1 deletion src/commands/ci/handle-ci.mts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import type { PythonDiffJsonContext } from '../scan/python-diff-json.mts'
export async function handleCi(
autoManifest: boolean,
pythonJsonFormat = false,
strictBlocking = false,
): Promise<void> {
debugFn('notice', 'Starting CI scan')
debugDir('inspect', { autoManifest, pythonJsonFormat })
Expand Down Expand Up @@ -73,6 +74,7 @@ export async function handleCi(
: pullRequestContext.provider === 'azure'
? process.env['BUILD_SOURCEVERSION'] || null
: null,
strictBlocking,
}
if (pythonJsonFormat && !pullRequest) {
setCiExitCode('usage')
Expand Down Expand Up @@ -117,7 +119,7 @@ export async function handleCi(
// Only skip when a clean checkout and an authoritative supported-file list
// prove this PR has no dependency manifest changes. Auto-manifest generation
// can derive dependencies from source/build inputs, so it always scans.
if (pullRequestContext.number && !autoManifest) {
if (pullRequestContext.number && !autoManifest && !strictBlocking) {
const supportedFilesCResult = await fetchSupportedScanFileNames({
orgSlug,
silence: true,
Expand Down Expand Up @@ -175,6 +177,7 @@ export async function handleCi(
autoManifest,
branchName,
cwd,
strictBlocking,
Comment thread
jdalton marked this conversation as resolved.
Resolved
onDiffOutcome,
orgSlug,
pullRequestContext,
Expand All @@ -195,10 +198,12 @@ async function runCiScan({
pythonJsonContext,
pythonJsonFormat,
repoName,
strictBlocking,
}: {
autoManifest: boolean
branchName: string
cwd: string
strictBlocking: boolean
onDiffOutcome: (outcome: {
blocking: boolean
incomplete: boolean
Expand Down Expand Up @@ -260,6 +265,7 @@ async function runCiScan({
orgSlug,
outputKind: 'json',
pythonJsonFormat,
strictBlocking,
pythonJsonContext,
// When 'pendingHead' is true, it requires 'branchName' set and 'tmp' false.
pendingHead: true,
Expand Down
7 changes: 5 additions & 2 deletions src/commands/scan/diff-scan-verdict.mts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ export function getDiffScanBlocking(
)
}

export function getDiffScanVerdict(diffScan: DiffScanData['diff_scan']): {
export function getDiffScanVerdict(
diffScan: DiffScanData['diff_scan'],
strictBlocking = false,
): {
blocking: boolean
incomplete: boolean
reportUrl: string | null
} {
return {
blocking: getDiffScanBlocking(diffScan),
blocking: getDiffScanBlocking(diffScan, strictBlocking),
incomplete: diffScan.incomplete === true,
reportUrl: diffScan.html_url,
}
Expand Down
5 changes: 4 additions & 1 deletion src/commands/scan/handle-create-new-scan.mts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ import {
import { buildToolsFor } from '../manifest/scripts/build-tool.mts'

import type { ReachabilityFallback } from './output-create-new-scan.mts'
import type { PythonDiffJsonContext } from './python-diff-json.mts'
import type { ReachabilityOptions } from './perform-reachability-analysis.mts'
import type { PythonDiffJsonContext } from './python-diff-json.mts'
import type { REPORT_LEVEL } from './types.mts'
import type { OutputKind } from '../../types.mts'
import type { FactsOnlyTypes } from '../../utils/coana.mts'
Expand Down Expand Up @@ -119,6 +119,7 @@ export type HandleCreateNewScanConfig = {
| undefined
pythonJsonFormat?: boolean | undefined
pythonJsonContext?: PythonDiffJsonContext | undefined
strictBlocking?: boolean | undefined
branchName: string
commitHash: string
commitMessage: string
Expand Down Expand Up @@ -207,6 +208,7 @@ async function createNewScan(
repoName,
report,
reportLevel,
strictBlocking = false,
targets,
tmp,
workspace,
Expand Down Expand Up @@ -604,6 +606,7 @@ async function createNewScan(
outputKind,
pythonJsonFormat,
pythonJsonContext,
strictBlocking,
})
} else {
await handleScanReport({
Expand Down
6 changes: 4 additions & 2 deletions src/commands/scan/handle-diff-scan.mts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export async function handlePrDiffScan({
outputKind,
pythonJsonContext = {},
pythonJsonFormat = false,
strictBlocking = false,
}: {
id1: string
id2: string
Expand All @@ -67,20 +68,21 @@ export async function handlePrDiffScan({
outputKind: OutputKind
pythonJsonFormat?: boolean | undefined
pythonJsonContext?: PythonDiffJsonContext | undefined
strictBlocking?: boolean | undefined
}): Promise<void> {
const result = await fetchDiffScan({ id1, id2, orgSlug })
await outputDiffScan(result, {
depth: 2,
file: '',
outputKind,
strictBlocking: false,
strictBlocking,
pythonJsonFormat,
pythonJsonContext,
})
if (result.ok) {
await onDiffReport?.(
result.data.diff_scan.id,
getDiffScanVerdict(result.data.diff_scan),
getDiffScanVerdict(result.data.diff_scan, strictBlocking),
)
}
}
Loading
Loading