Skip to content

Commit 64b9c44

Browse files
committed
Make safe npm alerts prettier
1 parent 4a0e7a9 commit 64b9c44

File tree

5 files changed

+137
-57
lines changed

5 files changed

+137
-57
lines changed

src/constants.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type ENV = Remap<
4747
SOCKET_CLI_ACCEPT_RISKS: boolean
4848
SOCKET_CLI_DEBUG: boolean
4949
SOCKET_CLI_NO_API_TOKEN: boolean
50+
SOCKET_CLI_VIEW_ALL_RISKS: boolean
5051
SOCKET_SECURITY_API_BASE_URL: string
5152
SOCKET_SECURITY_API_TOKEN: string
5253
XDG_DATA_HOME: string
@@ -110,6 +111,7 @@ type Constants = Remap<
110111
readonly SOCKET_CLI_SENTRY_NPM_BIN_NAME: 'socket-npm-with-sentry'
111112
readonly SOCKET_CLI_SENTRY_NPX_BIN_NAME: 'socket-npx-with-sentry'
112113
readonly SOCKET_CLI_SENTRY_PACKAGE_NAME: '@socketsecurity/cli-with-sentry'
114+
readonly SOCKET_CLI_VIEW_ALL_RISKS: 'SOCKET_CLI_VIEW_ALL_RISKS'
113115
readonly SOCKET_SECURITY_API_BASE_URL: 'SOCKET_SECURITY_API_BASE_URL'
114116
readonly SOCKET_SECURITY_API_TOKEN: 'SOCKET_SECURITY_API_TOKEN'
115117
readonly VLT: 'vlt'
@@ -183,6 +185,7 @@ const SOCKET_CLI_SENTRY_BIN_NAME_ALIAS = 'cli-with-sentry'
183185
const SOCKET_CLI_SENTRY_NPM_BIN_NAME = 'socket-npm-with-sentry'
184186
const SOCKET_CLI_SENTRY_NPX_BIN_NAME = 'socket-npx-with-sentry'
185187
const SOCKET_CLI_SENTRY_PACKAGE_NAME = `${SOCKET_SECURITY_SCOPE}/cli-with-sentry`
188+
const SOCKET_CLI_VIEW_ALL_RISKS = 'SOCKET_CLI_VIEW_ALL_RISKS'
186189
const SOCKET_SECURITY_API_BASE_URL = 'SOCKET_SECURITY_API_BASE_URL'
187190
const SOCKET_SECURITY_API_TOKEN = 'SOCKET_SECURITY_API_TOKEN'
188191
const VLT = 'vlt'
@@ -221,12 +224,14 @@ const LAZY_ENV = () => {
221224
// non-roaming application data, like temporary files, cached data, and program
222225
// settings, that are specific to the current machine and user.
223226
LOCALAPPDATA: envAsString(env['LOCALAPPDATA']),
224-
// Flag to accepts risks of previous safe npm/npx run.
227+
// Flag to accepts risks of safe-npm and safe-npx run.
225228
SOCKET_CLI_ACCEPT_RISKS: envAsBoolean(env['SOCKET_CLI_ACCEPT_RISKS']),
226229
// Flag to help debug Socket CLI.
227230
SOCKET_CLI_DEBUG: envAsBoolean(env['SOCKET_CLI_DEBUG']),
228231
// Flag to make the default API token `undefined`.
229232
SOCKET_CLI_NO_API_TOKEN: envAsBoolean(env['SOCKET_CLI_NO_API_TOKEN']),
233+
// Flag to view all risks of safe-npm and safe-npx run.
234+
SOCKET_CLI_VIEW_ALL_RISKS: envAsBoolean(env['SOCKET_CLI_VIEW_ALL_RISKS']),
230235
// Flag to change the base URL for all API-calls.
231236
// https://github.com/SocketDev/socket-cli?tab=readme-ov-file#environment-variables-for-development
232237
SOCKET_SECURITY_API_BASE_URL: envAsString(
@@ -386,6 +391,7 @@ const constants = createConstantsObject(
386391
SOCKET_CLI_SENTRY_NPM_BIN_NAME,
387392
SOCKET_CLI_SENTRY_NPX_BIN_NAME,
388393
SOCKET_CLI_SENTRY_PACKAGE_NAME,
394+
SOCKET_CLI_VIEW_ALL_RISKS,
389395
SOCKET_SECURITY_API_BASE_URL,
390396
SOCKET_SECURITY_API_TOKEN,
391397
VLT,

src/shadow/npm/arborist/lib/arborist/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import process from 'node:process'
22

3+
import { stripIndents } from 'common-tags'
4+
35
import { logger } from '@socketsecurity/registry/lib/logger'
46

57
import constants from '../../../../../constants'
@@ -15,6 +17,7 @@ const {
1517
NPX,
1618
SOCKET_CLI_ACCEPT_RISKS,
1719
SOCKET_CLI_SAFE_WRAPPER,
20+
SOCKET_CLI_VIEW_ALL_RISKS,
1821
kInternalsSymbol,
1922
[kInternalsSymbol as unknown as 'Symbol(kInternalsSymbol)']: { getIpc }
2023
} = constants
@@ -121,9 +124,17 @@ export class SafeArborist extends Arborist {
121124
})
122125
if (alertsMap.size) {
123126
process.exitCode = 1
124-
logAlertsMap(alertsMap, { output: process.stderr })
127+
logAlertsMap(alertsMap, {
128+
// Lazily access constants.ENV[SOCKET_CLI_VIEW_ALL_RISKS].
129+
hideAt: constants.ENV[SOCKET_CLI_VIEW_ALL_RISKS] ? 'none' : 'middle',
130+
output: process.stderr
131+
})
125132
throw new Error(
126-
`Socket ${binName} exiting due to risks.\nRerun with environment variable ${SOCKET_CLI_ACCEPT_RISKS}=1 to accept risks.`
133+
stripIndents`
134+
Socket ${binName} exiting due to risks.
135+
To view all risks rerun with environment variable ${SOCKET_CLI_VIEW_ALL_RISKS}=1.
136+
To accept risks rerun with environment variable ${SOCKET_CLI_ACCEPT_RISKS}=1.
137+
`
127138
)
128139
} else {
129140
logger.success(`Socket ${binName} found no risks!`)

src/utils/alert/artifact.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import constants from '../../constants'
22

33
import type { Remap } from '@socketsecurity/registry/lib/objects'
4-
import type { components } from '@socketsecurity/sdk/types/api'
4+
import type { components, operations } from '@socketsecurity/sdk/types/api'
5+
6+
export type ALERT_TYPE = keyof NonNullable<
7+
operations['getOrgSecurityPolicy']['responses']['200']['content']['application/json']['securityPolicyRules']
8+
>
9+
10+
export type CVE_ALERT_TYPE = 'cve' | 'mediumCVE' | 'mildCVE' | 'criticalCVE'
511

612
export type ArtifactAlertCve = Remap<
713
Omit<CompactSocketArtifactAlert, 'type'> & {
8-
type: CveAlertType
14+
type: CVE_ALERT_TYPE
915
}
1016
>
1117

1218
export type ArtifactAlertCveFixable = Remap<
1319
Omit<CompactSocketArtifactAlert, 'props' | 'type'> & {
14-
type: CveAlertType
20+
type: CVE_ALERT_TYPE
1521
props: {
1622
firstPatchedVersionIdentifier: string
1723
vulnerableVersionRange: string
@@ -26,8 +32,6 @@ export type ArtifactAlertUpgrade = Remap<
2632
}
2733
>
2834

29-
export type CveAlertType = 'cve' | 'mediumCVE' | 'mildCVE' | 'criticalCVE'
30-
3135
export type CompactSocketArtifactAlert = Remap<
3236
Omit<SocketArtifactAlert, 'category' | 'end' | 'file' | 'start'>
3337
>
@@ -38,10 +42,16 @@ export type CompactSocketArtifact = Remap<
3842
}
3943
>
4044

41-
export type SocketArtifact = components['schemas']['SocketArtifact']
45+
export type SocketArtifact = Remap<
46+
Omit<components['schemas']['SocketArtifact'], 'alerts'> & {
47+
alerts?: SocketArtifactAlert[]
48+
}
49+
>
4250

4351
export type SocketArtifactAlert = Remap<
44-
Omit<components['schemas']['SocketAlert'], 'props'> & {
52+
Omit<components['schemas']['SocketAlert'], 'action' | 'props' | 'type'> & {
53+
type: ALERT_TYPE
54+
action?: 'error' | 'monitor' | 'warn' | 'ignore'
4555
props?: any | undefined
4656
}
4757
>

src/utils/config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,8 @@ function normalizeConfigKey(key: keyof LocalConfig): keyof LocalConfig {
136136
return normalizedKey
137137
}
138138

139-
export function findSocketYmlSync() {
139+
export function findSocketYmlSync(dir = process.cwd()) {
140140
let prevDir = null
141-
let dir = process.cwd()
142141
while (dir !== prevDir) {
143142
let ymlPath = path.join(dir, 'socket.yml')
144143
let yml = safeReadFileSync(ymlPath)

0 commit comments

Comments
 (0)