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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,11 @@
!/src/commands/ci/gitlab-commit-status-context.mts
!/src/commands/ci/publish-gitlab-commit-status.mts
!/src/commands/scan/diff-scan-verdict.mts
!/src/commands/scan/diff-scan-verdict.test.mts
!/src/commands/scan/notifications/
!/src/commands/scan/notifications/report.mts
!/src/commands/scan/notifications/slack-webhook.mts
!/src/commands/scan/notifications/slack-webhook.test.mts
!/src/commands/ci/publish-gitlab-mr-notes.mts
!/src/commands/ci/publish-pr-comments.mts
!/src/commands/ci/publish-pr-comments.test.mts
Expand Down
34 changes: 34 additions & 0 deletions docs/commands/scan/report.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,37 @@ socket-dependency-scan:
The report uses GitLab Dependency Scanning schema version `15.0.0`. Findings
are generated from the scan's package and alert data before CLI report folding
or policy-level filtering is applied.

## Slack CI notifications

The scan creation report flow can send an opt-in Slack summary after it evaluates
the report. Store the webhook URL in your CI secret store and expose the JSON
configuration only to trusted jobs:

```sh
export SOCKET_SLACK_CONFIG_JSON='{"url":"https://hooks.slack.com/services/..."}'
export SOCKET_SLACK_LEVELS='block,warn'
socket scan create --report
```

For multiple destinations, use named URLs and configure each destination's
filters:

```json
{
"url": [
{"name":"security","url":"https://hooks.slack.com/services/..."}
],
"url_configs": {
"security": {
"repos": ["api"],
"alert_types": ["cve"],
"severities": ["critical", "high"]
}
}
}
```

`alert_types` takes precedence over `severities`, matching the Python CLI.
Delivery errors are reported as warnings and do not change the scan decision.
Existing report viewing commands do not send notifications.
4 changes: 3 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 @@ -604,6 +604,7 @@ async function createNewScan(
outputKind,
pythonJsonFormat,
pythonJsonContext,
notificationContext: { repository: repoName, branch: branchName },
})
} else {
await handleScanReport({
Expand All @@ -615,6 +616,7 @@ async function createNewScan(
reportLevel,
scanId,
short: false,
notificationContext: { repository: repoName, branch: branchName },
})
}
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/commands/scan/handle-diff-scan.mts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export async function handleDiffScan({
export async function handlePrDiffScan({
id1,
id2,
notificationContext,
onDiffReport,
orgSlug,
outputKind,
Expand All @@ -67,6 +68,9 @@ export async function handlePrDiffScan({
outputKind: OutputKind
pythonJsonFormat?: boolean | undefined
pythonJsonContext?: PythonDiffJsonContext | undefined
notificationContext?:
| { repository?: string | undefined; branch?: string | undefined }
| undefined
}): Promise<void> {
const result = await fetchDiffScan({ id1, id2, orgSlug })
await outputDiffScan(result, {
Expand All @@ -76,6 +80,7 @@ export async function handlePrDiffScan({
strictBlocking: false,
pythonJsonFormat,
pythonJsonContext,
notificationContext: result.ok ? notificationContext : undefined,
})
if (result.ok) {
await onDiffReport?.(
Expand Down
4 changes: 4 additions & 0 deletions src/commands/scan/handle-scan-report.mts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { fetchScanData } from './fetch-report-data.mts'
import { outputScanReport } from './output-scan-report.mts'

import type { NotificationMetadata } from './notifications/report.mts'
import type { FOLD_SETTING, REPORT_LEVEL } from './types.mts'
import type { OutputKind } from '../../types.mts'

export type HandleScanReportConfig = {
orgSlug: string
notificationContext?: NotificationMetadata | undefined
scanId: string
includeLicensePolicy: boolean
outputKind: OutputKind
Expand All @@ -23,6 +25,7 @@ export async function handleScanReport({
fold,
gitlabSecurityFile,
includeLicensePolicy,
notificationContext,
orgSlug,
outputKind,
reportLevel,
Expand All @@ -42,6 +45,7 @@ export async function handleScanReport({
outputKind,
reportLevel,
short,
notificationContext,
enableGitlabSecurity: enableGitlabSecurity ?? false,
gitlabSecurityFile:
gitlabSecurityFile ?? 'gl-dependency-scanning-report.json',
Expand Down
127 changes: 127 additions & 0 deletions src/commands/scan/notifications/report.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import type { SocketArtifact } from '../../../utils/alert/artifact.mts'
import type { PythonDiffJson } from '../python-diff-json.mts'

export type NotificationFinding = {
packageName: string
ecosystem: string
version: string
alertType: string
severity: string
action: string
url?: string | undefined
advisoryId?: string | undefined
purl?: string | undefined
introducedBy?: Array<[string, string]> | undefined
manifests?: string | undefined
}

export type NotificationReport = {
kind: 'diff' | 'scan'
scanId?: string | undefined
diffScanId?: string | undefined
repository?: string | undefined
branch?: string | undefined
scanUrl?: string | undefined
decision: 'pass' | 'fail' | 'unknown' | 'not-evaluated'
summary: string
findings: NotificationFinding[]
}

export type NotificationMetadata = {
repository?: string | undefined
branch?: string | undefined
}

export type NotificationContext = NotificationMetadata & {
decision: NotificationReport['decision']
}

function advisoryId(props: Record<string, unknown>): string | undefined {
for (const value of [props['ghsaId'], props['cveId']]) {
const ids = (Array.isArray(value) ? value : [value]).filter(
(id): id is string => typeof id === 'string' && Boolean(id.trim()),
)
if (ids.length) {
return ids.join(', ')
}
}
return undefined
}

function findingsFromArtifacts(
artifacts: SocketArtifact[],
): NotificationFinding[] {
const findings: NotificationFinding[] = []
for (const artifact of artifacts) {
for (const alert of artifact.alerts ?? []) {
const props = alert.props ?? {}
findings.push({
packageName: artifact.name ?? 'unknown',
ecosystem: artifact.type ?? 'unknown',
version: artifact.version ?? 'unknown',
alertType: alert.type ?? 'unknown',
severity: alert.severity ?? 'unknown',
action: alert.action ?? 'unknown',
url: typeof props['url'] === 'string' ? props['url'] : undefined,
advisoryId: advisoryId(props),
})
}
}
return findings
}

export function summarize(findings: NotificationFinding[]): string {
const blocking = findings.filter(finding => finding.action === 'error').length
const warning = findings.filter(finding => finding.action === 'warn').length
return `${findings.length} finding${findings.length === 1 ? '' : 's'} (${blocking} blocking, ${warning} warning)`
}
Comment thread
jdalton marked this conversation as resolved.
Resolved

export function createScanNotificationReport(
scan: SocketArtifact[],
context: NotificationContext & {
scanId?: string | undefined
scanUrl?: string | undefined
},
): NotificationReport {
const findings = findingsFromArtifacts(scan)
return {
kind: 'scan',
scanId: context.scanId,
repository: context.repository,
branch: context.branch,
scanUrl: context.scanUrl,
decision: context.decision,
summary: summarize(findings),
findings,
}
}

export function createDiffNotificationReport(
result: PythonDiffJson,
context: NotificationContext,
): NotificationReport {
const findings: NotificationFinding[] = result.new_alerts.map(alert => ({
packageName: alert.pkg_name ?? 'unknown',
ecosystem: alert.pkg_type ?? 'unknown',
version: alert.pkg_version ?? 'unknown',
alertType: alert.type,
severity: alert.severity ?? 'unknown',
action: alert.action ?? 'unknown',
url: alert.url ?? undefined,
advisoryId: advisoryId(alert.props),
purl: alert.purl ?? undefined,
introducedBy: alert.introduced_by,
manifests: alert.manifests,
}))
return {
kind: 'diff',
scanId: result.head_scan_id ?? undefined,
diffScanId: result.diff_scan_id ?? undefined,
repository: result.repo ?? context.repository,
branch: result.branch ?? context.branch,
scanUrl: result.diff_url ?? result.report_url ?? undefined,
decision: result.decision,
summary: summarize(findings),
findings,
}
}
Loading
Loading