Skip to content

Commit 22c8512

Browse files
committed
Ensure blocking alerts are always shown and update related test
1 parent e3c0f4d commit 22c8512

File tree

8 files changed

+179
-172
lines changed

8 files changed

+179
-172
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"bs": "npm run build:dist; npm exec socket --",
6161
"s": "npm exec socket --",
6262
"test": "run-s check test:*",
63-
"test:prepare": "cross-env VITEST=1 npm run build",
63+
"test:prepare": "cross-env VITEST=1 npm run build && del-cli 'test/**/node_modules'",
6464
"test:unit": "vitest --run",
6565
"test:unit:update": "vitest --run --update",
6666
"test:unit:coverage": "vitest run --coverage",

src/commands/fix/npm-fix.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export async function npmFix(
5353
include: {
5454
existing: true,
5555
unfixable: false,
56-
upgrade: false
56+
upgradable: false
5757
}
5858
})
5959

src/commands/fix/pnpm-fix.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export async function pnpmFix(
4848
include: {
4949
existing: true,
5050
unfixable: false,
51-
upgrade: false
51+
upgradable: false
5252
}
5353
})
5454

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,10 @@ export class SafeArborist extends Arborist {
8484
__proto__: null,
8585
...(args.length ? args[0] : undefined)
8686
} as ArboristReifyOptions
87-
if (
88-
options.dryRun ||
89-
options['yes'] ||
90-
// Lazily access constants.ENV[SOCKET_CLI_ACCEPT_RISKS].
91-
constants.ENV[SOCKET_CLI_ACCEPT_RISKS]
92-
) {
93-
return await this[kRiskyReify](...args)
94-
}
9587
const binName = await getIpc(SOCKET_CLI_SAFE_WRAPPER)
9688
if (!binName) {
9789
return await this[kRiskyReify](...args)
9890
}
99-
const isSafeNpm = binName === NPM
100-
const isSafeNpx = binName === NPX
101-
// Lazily access constants.spinner.
102-
const { spinner } = constants
10391
await super.reify(
10492
{
10593
...options,
@@ -109,17 +97,33 @@ export class SafeArborist extends Arborist {
10997
// @ts-ignore: TS gets grumpy about rest parameters.
11098
...args.slice(1)
11199
)
100+
// Lazily access constants.spinner.
101+
const { spinner } = constants
102+
const isSafeNpm = binName === NPM
103+
const isSafeNpx = binName === NPX
112104
const alertsMap = await getAlertsMapFromArborist(this, {
113105
spinner,
114-
include: {
115-
existing: isSafeNpx,
116-
unfixable: isSafeNpm
117-
}
106+
include:
107+
options.dryRun ||
108+
options['yes'] ||
109+
// Lazily access constants.ENV[SOCKET_CLI_ACCEPT_RISKS].
110+
constants.ENV[SOCKET_CLI_ACCEPT_RISKS]
111+
? {
112+
blocked: true,
113+
critical: false,
114+
cve: false,
115+
unfixable: false
116+
}
117+
: {
118+
existing: isSafeNpx,
119+
unfixable: isSafeNpm
120+
}
118121
})
119122
if (alertsMap.size) {
123+
process.exitCode = 1
120124
logAlertsMap(alertsMap, { output: process.stderr })
121125
throw new Error(
122-
`Socket ${binName} exiting due to risks.\nRerun with the environment variable ${SOCKET_CLI_ACCEPT_RISKS}=1 to accept the risks of installing these packages.`
126+
`Socket ${binName} exiting due to risks.\nRerun with environment variable ${SOCKET_CLI_ACCEPT_RISKS}=1 to accept risks.`
123127
)
124128
} else {
125129
logger.success(`Socket ${binName} found no risks!`)

src/utils/lockfile/package-lock-json.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { addArtifactToAlertsMap } from '../socket-package-alert'
1818
import type { Diff } from '../../shadow/npm/arborist/lib/arborist/types'
1919
import type { SafeEdge } from '../../shadow/npm/arborist/lib/edge'
2020
import type { SafeNode } from '../../shadow/npm/arborist/lib/node'
21-
import type { AlertsByPkgId } from '../socket-package-alert'
21+
import type { AlertIncludeFilter, AlertsByPkgId } from '../socket-package-alert'
2222
import type { Spinner } from '@socketsecurity/registry/lib/spinner'
2323

2424
type Packument = Exclude<
@@ -184,15 +184,7 @@ export function findPackageNodes(
184184
return matches
185185
}
186186

187-
type AlertIncludeFilter = {
188-
critical?: boolean | undefined
189-
cve?: boolean | undefined
190-
existing?: boolean | undefined
191-
unfixable?: boolean | undefined
192-
upgrade?: boolean | undefined
193-
}
194-
195-
type GetAlertsMapFromArboristOptions = {
187+
export type GetAlertsMapFromArboristOptions = {
196188
consolidate?: boolean | undefined
197189
include?: AlertIncludeFilter | undefined
198190
spinner?: Spinner | undefined
@@ -210,11 +202,12 @@ export async function getAlertsMapFromArborist(
210202

211203
const include = {
212204
__proto__: null,
205+
blocked: true,
213206
critical: true,
214207
cve: true,
215208
existing: false,
216209
unfixable: true,
217-
upgrade: false,
210+
upgradable: false,
218211
..._include
219212
} as AlertIncludeFilter
220213

@@ -252,8 +245,9 @@ export async function getAlertsMapFromArborist(
252245
const sockSdk = await setupSdk(getPublicToken())
253246

254247
const toAlertsMapOptions = {
255-
overrides,
256-
...options
248+
...options,
249+
include,
250+
overrides
257251
}
258252

259253
for await (const batchPackageFetchResult of sockSdk.batchPackageStream(

src/utils/lockfile/pnpm-lock-yaml.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,11 @@ import { getPublicToken, setupSdk } from '../sdk'
44
import { addArtifactToAlertsMap } from '../socket-package-alert'
55

66
import type { CompactSocketArtifact } from '../alert/artifact'
7-
import type { AlertsByPkgId } from '../socket-package-alert'
7+
import type { AlertIncludeFilter, AlertsByPkgId } from '../socket-package-alert'
88
import type { Lockfile } from '@pnpm/lockfile-file'
99
import type { Spinner } from '@socketsecurity/registry/lib/spinner'
1010

11-
type AlertIncludeFilter = {
12-
critical?: boolean | undefined
13-
cve?: boolean | undefined
14-
existing?: boolean | undefined
15-
unfixable?: boolean | undefined
16-
upgrade?: boolean | undefined
17-
}
18-
19-
type GetAlertsMapFromPnpmLockfileOptions = {
11+
export type GetAlertsMapFromPnpmLockfileOptions = {
2012
consolidate?: boolean | undefined
2113
include?: AlertIncludeFilter | undefined
2214
spinner?: Spinner | undefined
@@ -34,11 +26,12 @@ export async function getAlertsMapFromPnpmLockfile(
3426

3527
const include = {
3628
__proto__: null,
29+
blocked: true,
3730
critical: true,
3831
cve: true,
3932
existing: false,
4033
unfixable: true,
41-
upgrade: false,
34+
upgradable: false,
4235
..._include
4336
} as AlertIncludeFilter
4437

0 commit comments

Comments
 (0)