@@ -10,9 +10,11 @@ import { NPX } from '../../../../../constants/agents.mts'
1010import ENV from '../../../../../constants/env.mts'
1111import { NODE_MODULES } from '../../../../../constants/packages.mts'
1212import {
13+ getInternals ,
1314 SOCKET_CLI_ACCEPT_RISKS ,
1415 SOCKET_CLI_SHADOW_ACCEPT_RISKS ,
1516 SOCKET_CLI_SHADOW_API_TOKEN ,
17+ SOCKET_CLI_SHADOW_BIN ,
1618 SOCKET_CLI_SHADOW_PROGRESS ,
1719 SOCKET_CLI_SHADOW_SILENT ,
1820 SOCKET_CLI_VIEW_ALL_RISKS ,
@@ -30,6 +32,9 @@ import type {
3032 NodeClass ,
3133} from '../../types.mts'
3234
35+ const internals = getInternals ( )
36+ const getIpc = internals . getIpc
37+
3338export const SAFE_NO_SAVE_ARBORIST_REIFY_OPTIONS_OVERRIDES = {
3439 __proto__ : null ,
3540 audit : false ,
@@ -96,7 +101,97 @@ export class SafeArborist extends Arborist {
96101 this : SafeArborist ,
97102 ...args : Parameters < InstanceType < ArboristClass > [ 'reify' ] >
98103 ) : Promise < NodeClass > {
99- // Note: Registry no longer provides IPC, always use risky reify.
104+ const options = {
105+ __proto__ : null ,
106+ ...( args . length ? args [ 0 ] : undefined ) ,
107+ } as ArboristReifyOptions
108+
109+ const ipc = getIpc ? await getIpc ( ) : undefined
110+
111+ const binName = ipc ?. [ SOCKET_CLI_SHADOW_BIN ]
112+ if ( ! binName ) {
113+ return await this [ kRiskyReify ] ( ...args )
114+ }
115+
116+ await super . reify (
117+ {
118+ ...options ,
119+ ...SAFE_NO_SAVE_ARBORIST_REIFY_OPTIONS_OVERRIDES ,
120+ progress : false ,
121+ } ,
122+ // @ts -expect-error: TypeScript gets grumpy about rest parameters.
123+ ...args . slice ( 1 ) ,
124+ )
125+
126+ const shadowAcceptRisks = ! ! ipc ?. [ SOCKET_CLI_SHADOW_ACCEPT_RISKS ]
127+ const shadowProgress = ! ! ipc ?. [ SOCKET_CLI_SHADOW_PROGRESS ]
128+ const shadowSilent = ! ! ipc ?. [ SOCKET_CLI_SHADOW_SILENT ]
129+
130+ const acceptRisks = shadowAcceptRisks || ENV . SOCKET_CLI_ACCEPT_RISKS
131+ const reportOnlyBlocking =
132+ acceptRisks || options [ 'dryRun' ] || options [ 'yes' ]
133+ const silent = ! ! options [ 'silent' ]
134+ const spinnerInstance =
135+ silent || ! shadowProgress ? undefined : ( getSpinner ( ) ?? undefined )
136+
137+ const isShadowNpx = binName === NPX
138+ const hasExisting = await findUp ( NODE_MODULES , {
139+ cwd : process . cwd ( ) ,
140+ onlyDirectories : true ,
141+ } )
142+ const shouldCheckExisting = reportOnlyBlocking ? true : isShadowNpx
143+
144+ const needInfoOn = getDetailsFromDiff ( this . diff , {
145+ filter : {
146+ existing : shouldCheckExisting ,
147+ } ,
148+ } )
149+
150+ const alertsMap = await getAlertsMapFromArborist ( this , needInfoOn , {
151+ apiToken : ipc ?. [ SOCKET_CLI_SHADOW_API_TOKEN ] ,
152+ spinner : spinnerInstance ,
153+ filter : reportOnlyBlocking
154+ ? {
155+ actions : [ 'error' ] ,
156+ blocked : true ,
157+ existing : shouldCheckExisting ,
158+ }
159+ : {
160+ actions : [ 'error' , 'monitor' , 'warn' ] ,
161+ existing : shouldCheckExisting ,
162+ } ,
163+ } )
164+
165+ if ( alertsMap . size ) {
166+ process . exitCode = 1
167+ const viewAllRisks = ENV . SOCKET_CLI_VIEW_ALL_RISKS
168+ logAlertsMap ( alertsMap , {
169+ hideAt : viewAllRisks ? 'none' : 'middle' ,
170+ output : process . stderr ,
171+ } )
172+ throw new Error (
173+ `
174+ Socket ${ binName } exiting due to risks.${
175+ viewAllRisks
176+ ? ''
177+ : `\nView all risks - Rerun with environment variable ${ SOCKET_CLI_VIEW_ALL_RISKS } =1.`
178+ } ${
179+ acceptRisks
180+ ? ''
181+ : `\nAccept risks - Rerun with environment variable ${ SOCKET_CLI_ACCEPT_RISKS } =1.`
182+ }
183+ ` . trim ( ) ,
184+ )
185+ }
186+ if ( ! silent && ! shadowSilent ) {
187+ logger . success (
188+ `Socket ${ binName } ${ acceptRisks ? 'accepted' : 'found no' } ${ hasExisting ? ' new' : '' } risks` ,
189+ )
190+ if ( isShadowNpx ) {
191+ logger . log ( `Running ${ options . add ?. [ 0 ] } ` )
192+ }
193+ }
194+
100195 return await this [ kRiskyReify ] ( ...args )
101196 }
102197}
0 commit comments