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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@
!/src/commands/ci/fetch-default-org-slug.test.mts
!/src/commands/ci/handle-ci.mts
!/src/commands/ci/handle-ci.test.mts
!/src/commands/ci/pr-ignore-commands.test.mts
!/src/commands/ci/handle-ci-skip.test.mts
!/src/commands/ci/detect-ci-pull-request-context.test.mts
!/src/commands/ci/select-baseline-scan.mts
!/src/commands/ci/select-baseline-scan.test.mts
Expand Down
25 changes: 25 additions & 0 deletions src/commands/ci/cmd-ci.mts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,24 @@ const config: CliCommandConfig = {
description:
'Fail for existing blocking alerts as well as alerts introduced by this PR.',
},
disableIgnore: {
type: 'boolean',
default: false,
description:
'Ignore no PR comments and do not show @SocketSecurity ignore instructions.',
},
exitCodeOnApiError: {
type: 'number',
default: 3,
description:
'Exit code for Socket API errors (default: 3). Ignored with --disable-blocking.',
},
ignoreAuthorization: {
type: 'string',
default: 'enforce',
description:
'Authorization policy for PR ignore commands: enforce (default), strict, or off.',
},
jsonFormat: {
type: 'string',
description:
Expand Down Expand Up @@ -77,6 +89,8 @@ const config: CliCommandConfig = {
$ ${command} --auto-manifest
$ ${command} --disable-blocking
$ ${command} --strict-blocking
$ ${command} --disable-ignore
$ ${command} --ignore-authorization strict
$ ${command} --exit-code-on-api-error 5
`,
}
Expand Down Expand Up @@ -129,9 +143,20 @@ async function run(
return
}

const ignoreAuthorization = String(cli.flags['ignoreAuthorization'] ?? 'enforce')
if (!['enforce', 'strict', 'off'].includes(ignoreAuthorization)) {
logger.fail('--ignore-authorization must be enforce, strict, or off.')
setCiExitCode('usage')
return
}

await handleCi(
Boolean(cli.flags['autoManifest']),
jsonFormat === 'python-v1',
Boolean(cli.flags['strictBlocking']),
{
authorization: ignoreAuthorization as 'enforce' | 'strict' | 'off',
disabled: Boolean(cli.flags['disableIgnore']),
},
)
}
43 changes: 43 additions & 0 deletions src/commands/ci/handle-ci.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { detectCiPullRequestContext } from './detect-ci-pull-request-context.mts'
import { getDefaultOrgSlug } from './fetch-default-org-slug.mts'
import {
markPrIgnoreCommandsProcessed,
readPrIgnoreCommands,
} from './pr-ignore-commands.mts'
import {
reportGitLabCommitStatusFailure,
withGitLabCommitStatus,
Expand Down Expand Up @@ -34,11 +38,16 @@ import {
} from '../scan/python-diff-json.mts'

import type { PythonDiffJsonContext } from '../scan/python-diff-json.mts'
import type { IgnoreAuthorization } from './pr-ignore-commands.mts'

export async function handleCi(
autoManifest: boolean,
pythonJsonFormat = false,
strictBlocking = false,
ignoreSettings: {
authorization?: IgnoreAuthorization | undefined
disabled?: boolean | undefined
} = {},
): Promise<void> {
debugFn('notice', 'Starting CI scan')
debugDir('inspect', { autoManifest, pythonJsonFormat })
Expand Down Expand Up @@ -183,6 +192,8 @@ export async function handleCi(
pullRequestContext,
pythonJsonContext,
pythonJsonFormat,
ignoreAuthorization: ignoreSettings.authorization ?? 'enforce',
disableIgnore: ignoreSettings.disabled ?? false,
repoName,
})
})
Expand All @@ -192,6 +203,8 @@ async function runCiScan({
autoManifest,
branchName,
cwd,
disableIgnore,
ignoreAuthorization,
onDiffOutcome,
orgSlug,
pullRequestContext,
Expand All @@ -213,6 +226,8 @@ async function runCiScan({
pullRequestContext: Awaited<ReturnType<typeof detectCiPullRequestContext>>
pythonJsonContext: PythonDiffJsonContext
pythonJsonFormat: boolean
ignoreAuthorization: IgnoreAuthorization
disableIgnore: boolean
repoName: string
}): Promise<void> {
const { number: pullRequest } = pullRequestContext
Expand Down Expand Up @@ -266,6 +281,13 @@ async function runCiScan({
debugDir('inspect', { orgSlug, cwd, branchName, repoName })

const [owner, repo] = (constants.ENV.GITHUB_REPOSITORY || '').split('/')
const ignoreCommands = baselineScanId
? await readPrIgnoreCommands({
authorization: ignoreAuthorization,
context: pullRequestContext,
disabled: disableIgnore,
})
: []
await handleCreateNewScan({
autoManifest,
branchName,
Expand All @@ -286,6 +308,7 @@ async function runCiScan({
pendingHead: true,
pullRequest,
pullRequestUrl: pullRequestContext.url,
prIgnoreCommands: ignoreCommands,
baselineScanId,
onDiffReport: async (diffScanId, outcome) => {
onDiffOutcome(outcome)
Expand All @@ -302,6 +325,10 @@ async function runCiScan({
console.error(
'Socket could not publish GitLab merge request notes: missing target project metadata. Check CI_MERGE_REQUEST_PROJECT_ID, CI_MERGE_REQUEST_PROJECT_URL, and CI_SERVER_URL.',
)
await markPrIgnoreCommandsProcessed({
commands: ignoreCommands,
context: pullRequestContext,
})
return
}
try {
Expand All @@ -314,6 +341,8 @@ async function runCiScan({
process.env['SOCKET_CLI_DISABLE_SECURITY_COMMENT'],
),
orgSlug,
ignoreCommands,
disableIgnore,
mergeRequestIid: pullRequest,
projectId,
projectUrl,
Expand All @@ -326,6 +355,10 @@ async function runCiScan({
errorMessage(error),
)
}
await markPrIgnoreCommandsProcessed({
commands: ignoreCommands,
context: pullRequestContext,
})
return
}
if (
Expand All @@ -334,6 +367,10 @@ async function runCiScan({
!repo ||
!constants.ENV.SOCKET_CLI_GITHUB_TOKEN
) {
await markPrIgnoreCommandsProcessed({
commands: ignoreCommands,
context: pullRequestContext,
})
return
}
try {
Expand All @@ -346,6 +383,8 @@ async function runCiScan({
process.env['SOCKET_CLI_DISABLE_SECURITY_COMMENT'],
),
orgSlug,
ignoreCommands,
disableIgnore,
owner,
pullRequest,
repo,
Expand All @@ -357,6 +396,10 @@ async function runCiScan({
'Socket could not publish the scan comments. Check the Socket API scopes and GitHub pull request write permission.\n',
)
}
await markPrIgnoreCommandsProcessed({
commands: ignoreCommands,
context: pullRequestContext,
})
},
reach: {
dynamicSbomInference: undefined,
Expand Down
Loading
Loading