Skip to content

Commit 768711b

Browse files
authored
fix(scan): grade reports from alert actions (#1706)
1 parent 5baf3f0 commit 768711b

8 files changed

Lines changed: 230 additions & 341 deletions

‎requirements.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@
101101
"permissions": ["full-scans:create"]
102102
},
103103
"scan:report": {
104-
"quota": 2,
105-
"permissions": ["full-scans:list", "security-policy:read"]
104+
"quota": 1,
105+
"permissions": ["full-scans:list"]
106106
},
107107
"scan:view": {
108108
"quota": 1,

‎src/commands/scan/cmd-scan-report.test.mts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ describe('socket scan report', async () => {
2626
$ socket scan report [options] <SCAN_ID> [OUTPUT_PATH]
2727
2828
API Token Requirements
29-
- Quota: 2 units
30-
- Permissions: full-scans:list and security-policy:read
29+
- Quota: 1 unit
30+
- Permissions: full-scans:list
3131
3232
Options
3333
--fold Fold reported alerts to some degree (default 'none')

‎src/commands/scan/fetch-report-data.mts‎

Lines changed: 30 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -2,179 +2,66 @@ import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
22
import { logger } from '@socketsecurity/registry/lib/logger'
33

44
import constants from '../../constants.mts'
5-
import { handleApiCallNoSpinner, queryApiSafeText } from '../../utils/api.mts'
5+
import { queryApiSafeText } from '../../utils/api.mts'
66
import { formatErrorWithDetail } from '../../utils/errors.mts'
7-
import { setupSdk } from '../../utils/sdk.mts'
87

98
import type { CResult } from '../../types.mts'
109
import type { SocketArtifact } from '../../utils/alert/artifact.mts'
11-
import type { SetupSdkOptions } from '../../utils/sdk.mts'
12-
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
1310

1411
export type FetchScanData = {
1512
includeLicensePolicy?: boolean | undefined
16-
sdkOpts?: SetupSdkOptions | undefined
1713
}
1814

19-
/**
20-
* This fetches all the relevant pieces of data to generate a report, given a
21-
* full scan ID.
22-
*/
15+
/** Fetch the scan data needed to generate a report. */
2316
export async function fetchScanData(
2417
orgSlug: string,
2518
scanId: string,
2619
options?: FetchScanData | undefined,
27-
): Promise<
28-
CResult<{
29-
scan: SocketArtifact[]
30-
securityPolicy: SocketSdkSuccessResult<'getOrgSecurityPolicy'>['data']
31-
}>
32-
> {
33-
const { includeLicensePolicy, sdkOpts } = {
34-
__proto__: null,
35-
...options,
36-
} as FetchScanData
37-
const sockSdkCResult = await setupSdk(sdkOpts)
38-
if (!sockSdkCResult.ok) {
39-
return sockSdkCResult
40-
}
41-
const sockSdk = sockSdkCResult.data
42-
43-
let policyStatus = 'requested...'
44-
let scanStatus = 'requested...'
45-
let finishedFetching = false
46-
20+
): Promise<CResult<{ scan: SocketArtifact[] }>> {
4721
const { spinner } = constants
22+
spinner.start('Scan result: requested...')
4823

49-
function updateScan(status: string) {
50-
scanStatus = status
51-
updateProgress()
52-
}
53-
54-
function updatePolicy(status: string) {
55-
policyStatus = status
56-
updateProgress()
57-
}
58-
59-
function updateProgress() {
60-
if (finishedFetching) {
61-
spinner.stop()
62-
logger.info(
63-
`Scan result: ${scanStatus}. Security policy: ${policyStatus}.`,
64-
)
65-
} else {
66-
spinner.start(
67-
`Scan result: ${scanStatus}. Security policy: ${policyStatus}.`,
68-
)
69-
}
70-
}
71-
72-
async function fetchScanResult(): Promise<CResult<SocketArtifact[]>> {
24+
try {
7325
const result = await queryApiSafeText(
74-
`orgs/${orgSlug}/full-scans/${encodeURIComponent(scanId)}${includeLicensePolicy ? '?include_license_details=true' : ''}`,
26+
`orgs/${orgSlug}/full-scans/${encodeURIComponent(scanId)}${options?.includeLicensePolicy ? '?include_license_details=true' : ''}`,
7527
)
7628

77-
updateScan(`response received`)
78-
7929
if (!result.ok) {
30+
spinner.stop()
31+
logger.info('Scan result: request failed.')
8032
return result
8133
}
8234

83-
const ndJsonString = result.data
84-
85-
// This is nd-json; each line is a json object.
86-
const lines = ndJsonString.split('\n').filter(Boolean)
87-
let ok = true
88-
const data = lines.map(line => {
35+
const data: SocketArtifact[] = []
36+
for (const line of result.data.split('\n').filter(Boolean)) {
8937
try {
90-
return JSON.parse(line)
91-
} catch (e) {
92-
ok = false
38+
data.push(JSON.parse(line))
39+
} catch (error) {
9340
debugFn('error', 'Failed to parse report data line as JSON')
94-
debugDir('error', { error: e, line })
95-
return
41+
debugDir('error', { error, line })
42+
spinner.stop()
43+
logger.info('Scan result: received invalid JSON response.')
44+
return {
45+
ok: false,
46+
message: 'Invalid Socket API response',
47+
cause:
48+
'The Socket API responded with at least one line that was not valid JSON. Please report if this persists.',
49+
}
9650
}
97-
}) as unknown as SocketArtifact[]
98-
99-
if (ok) {
100-
updateScan('success')
101-
return { ok: true, data }
10251
}
10352

104-
updateScan('received invalid JSON response')
105-
53+
spinner.stop()
54+
logger.info('Scan result: success.')
55+
return { ok: true, data: { scan: data } }
56+
} catch (error) {
57+
spinner.stop()
58+
logger.info('Scan result: request failed.')
10659
return {
10760
ok: false,
108-
message: 'Invalid Socket API response',
61+
message: 'Socket API error',
10962
cause:
110-
'The Socket API responded with at least one line that was not valid JSON. Please report if this persists.',
63+
formatErrorWithDetail('Error requesting scan', error) ||
64+
'Error requesting scan: (no error message found)',
11165
}
11266
}
113-
114-
async function fetchSecurityPolicy(): Promise<
115-
CResult<SocketSdkSuccessResult<'getOrgSecurityPolicy'>['data']>
116-
> {
117-
const result = await handleApiCallNoSpinner(
118-
sockSdk.getOrgSecurityPolicy(orgSlug),
119-
'GetOrgSecurityPolicy',
120-
)
121-
122-
updatePolicy('received policy')
123-
124-
return result
125-
}
126-
127-
updateProgress()
128-
129-
const [scan, securityPolicy]: [
130-
CResult<SocketArtifact[]>,
131-
CResult<SocketSdkSuccessResult<'getOrgSecurityPolicy'>['data']>,
132-
] = await Promise.all([
133-
fetchScanResult().catch(e => {
134-
updateScan('failure; unknown blocking error occurred')
135-
return {
136-
ok: false as const,
137-
message: 'Socket API error',
138-
cause:
139-
formatErrorWithDetail('Error requesting scan', e) ||
140-
'Error requesting scan: (no error message found)',
141-
}
142-
}),
143-
fetchSecurityPolicy().catch(e => {
144-
updatePolicy('failure; unknown blocking error occurred')
145-
return {
146-
ok: false as const,
147-
message: 'Socket API error',
148-
cause:
149-
formatErrorWithDetail('Error requesting policy', e) ||
150-
'Error requesting policy: (no error message found)',
151-
}
152-
}),
153-
]).finally(() => {
154-
finishedFetching = true
155-
updateProgress()
156-
})
157-
158-
if (!scan.ok) {
159-
return scan
160-
}
161-
if (!securityPolicy.ok) {
162-
return securityPolicy
163-
}
164-
165-
if (!Array.isArray(scan.data)) {
166-
return {
167-
ok: false,
168-
message: 'Failed to fetch',
169-
cause: 'Was unable to fetch scan result, bailing',
170-
}
171-
}
172-
173-
return {
174-
ok: true,
175-
data: {
176-
scan: scan.data satisfies SocketArtifact[],
177-
securityPolicy: securityPolicy.data,
178-
},
179-
}
18067
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
process.env['SOCKET_CLI_API_TOKEN'] = 'test-token'
2+
process.env['SOCKET_CLI_API_BASE_URL'] = 'https://api.socket.dev/v0/'
3+
4+
import nock from 'nock'
5+
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
6+
7+
import { fetchScanData } from './fetch-report-data.mts'
8+
9+
const BASE_HOST = 'https://api.socket.dev'
10+
11+
describe('fetchScanData', () => {
12+
beforeEach(() => {
13+
nock.cleanAll()
14+
nock.disableNetConnect()
15+
})
16+
17+
afterEach(() => {
18+
nock.cleanAll()
19+
nock.enableNetConnect()
20+
})
21+
22+
it('fetches scan alerts with resolved actions without requesting policy', async () => {
23+
nock(BASE_HOST)
24+
.get('/v0/orgs/test-org/full-scans/scan-1')
25+
.reply(
26+
200,
27+
`${JSON.stringify({
28+
type: 'npm',
29+
name: 'lodash',
30+
version: '4.17.21',
31+
alerts: [{ type: 'envVars', action: 'error' }],
32+
})}\n`,
33+
)
34+
35+
const result = await fetchScanData('test-org', 'scan-1')
36+
37+
expect(result).toEqual({
38+
ok: true,
39+
data: {
40+
scan: [
41+
{
42+
type: 'npm',
43+
name: 'lodash',
44+
version: '4.17.21',
45+
alerts: [{ type: 'envVars', action: 'error' }],
46+
},
47+
],
48+
},
49+
})
50+
expect(nock.isDone()).toBe(true)
51+
})
52+
53+
it('requests license details when the license option is enabled', async () => {
54+
nock(BASE_HOST)
55+
.get('/v0/orgs/test-org/full-scans/scan-2')
56+
.query({ include_license_details: 'true' })
57+
.reply(200, '')
58+
59+
const result = await fetchScanData('test-org', 'scan-2', {
60+
includeLicensePolicy: true,
61+
})
62+
63+
expect(result).toEqual({ ok: true, data: { scan: [] } })
64+
expect(nock.isDone()).toBe(true)
65+
})
66+
})

‎src/commands/scan/generate-report.mts‎

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import { getSocketDevPackageOverviewUrlFromPurl } from '../../utils/socket-url.m
33

44
import type { FOLD_SETTING, REPORT_LEVEL } from './types.mts'
55
import type { CResult } from '../../types.mts'
6-
import type { SocketArtifact } from '../../utils/alert/artifact.mts'
6+
import type { ALERT_ACTION, SocketArtifact } from '../../utils/alert/artifact.mts'
77
import type { Spinner } from '@socketsecurity/registry/lib/spinner'
8-
import type { SocketSdkSuccessResult } from '@socketsecurity/sdk'
98

109
type AlertKey = string
1110
type EcoMap = Map<string, ReportLeafNode | PackageMap>
@@ -40,7 +39,6 @@ export type ReportLeafNode = {
4039
// failed. It won't reflect the healthy state.
4140
export function generateReport(
4241
scan: SocketArtifact[],
43-
securityPolicy: SocketSdkSuccessResult<'getOrgSecurityPolicy'>['data'],
4442
{
4543
fold,
4644
orgSlug,
@@ -72,7 +70,7 @@ export function generateReport(
7270

7371
// In the context of a report;
7472
// - the alert.severity is irrelevant
75-
// - the securityPolicyDefault is irrelevant
73+
// - policy defaults are irrelevant because the API resolves actions per alert
7674
// - the report defaults to healthy:true with no alerts
7775
// - the appearance of an alert will trigger the policy action;
7876
// - error: healthy will end up as false, add alerts to report
@@ -92,22 +90,19 @@ export function generateReport(
9290

9391
let healthy = true
9492

95-
const securityRules = securityPolicy.securityPolicyRules
96-
if (securityRules) {
97-
// Note: reportLevel: error > warn > monitor > ignore > defer
98-
scan.forEach(artifact => {
99-
const {
100-
alerts,
101-
name: pkgName = UNKNOWN_VALUE,
102-
type: ecosystem,
103-
version = UNKNOWN_VALUE,
104-
} = artifact
93+
// Note: reportLevel: error > warn > monitor > ignore > defer
94+
scan.forEach(artifact => {
95+
const {
96+
alerts,
97+
name: pkgName = UNKNOWN_VALUE,
98+
type: ecosystem,
99+
version = UNKNOWN_VALUE,
100+
} = artifact
105101

106-
alerts?.forEach(
107-
(alert: NonNullable<SocketArtifact['alerts']>[number]) => {
108-
const alertName = alert.type as keyof typeof securityRules // => policy[type]
109-
const action = securityRules[alertName]?.action || ''
110-
switch (action) {
102+
alerts?.forEach(
103+
(alert: NonNullable<SocketArtifact['alerts']>[number]) => {
104+
const action = alert.action as ALERT_ACTION | undefined
105+
switch (action) {
111106
case constants.REPORT_LEVEL_ERROR: {
112107
healthy = false
113108
if (!short) {
@@ -180,31 +175,13 @@ export function generateReport(
180175
break
181176
}
182177

183-
case constants.REPORT_LEVEL_DEFER: {
184-
// Not sure but ignore for now. Defer to later ;)
185-
if (!short && reportLevel === constants.REPORT_LEVEL_DEFER) {
186-
addAlert(
187-
artifact,
188-
violations,
189-
fold,
190-
ecosystem,
191-
pkgName,
192-
version,
193-
alert,
194-
action,
195-
)
196-
}
197-
break
198-
}
199-
200178
default: {
201-
// This value was not emitted from the Socket API at the time of writing.
179+
// Alerts without a resolved policy action are not reportable.
202180
}
203-
}
204-
},
205-
)
206-
})
207-
}
181+
}
182+
},
183+
)
184+
})
208185

209186
spinner?.successAndStop(`Generated reported in ${Date.now() - now} ms`)
210187

0 commit comments

Comments
 (0)