Skip to content

Commit 86578bb

Browse files
committed
Make shadow bin runs more silent
1 parent 14e8d62 commit 86578bb

6 files changed

Lines changed: 77 additions & 48 deletions

File tree

src/commands/manifest/run-cdxgen.mts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import { logger } from '@socketsecurity/registry/lib/logger'
88
import constants from '../../constants.mts'
99
import shadowBin from '../../shadow/npm/bin.mts'
1010

11-
import type { ShadowBinResult } from '../../shadow/npm/bin.mts'
11+
import type {
12+
ShadowBinOptions,
13+
ShadowBinResult,
14+
} from '../../shadow/npm/bin.mts'
1215

1316
const { PACKAGE_LOCK_JSON, YARN, YARN_LOCK } = constants
1417

@@ -62,6 +65,15 @@ function argvToArray(argvObj: ArgvObject): string[] {
6265
export async function runCdxgen(argvObj: ArgvObject): Promise<ShadowBinResult> {
6366
let cleanupPackageLock = false
6467
const argvMutable = { __proto__: null, ...argvObj } as ArgvObject
68+
const shadowOpts: ShadowBinOptions = {
69+
ipc: {
70+
[constants.SOCKET_CLI_SHADOW_ACCEPT_RISKS]: true,
71+
[constants.SOCKET_CLI_SHADOW_API_TOKEN]:
72+
constants.SOCKET_PUBLIC_API_TOKEN,
73+
[constants.SOCKET_CLI_SHADOW_SILENT]: true,
74+
},
75+
stdio: 'inherit',
76+
}
6577
if (
6678
argvMutable['type'] !== YARN &&
6779
nodejsPlatformTypes.has(argvMutable['type'] as string) &&
@@ -81,10 +93,7 @@ export async function runCdxgen(argvObj: ArgvObject): Promise<ShadowBinResult> {
8193
'--source-file',
8294
`./${YARN_LOCK}`,
8395
],
84-
{
85-
apiToken: constants.SOCKET_PUBLIC_API_TOKEN,
86-
stdio: 'inherit',
87-
},
96+
shadowOpts,
8897
)
8998
await synpPromise
9099
argvMutable['type'] = 'npm'
@@ -100,13 +109,7 @@ export async function runCdxgen(argvObj: ArgvObject): Promise<ShadowBinResult> {
100109
`@cyclonedx/cdxgen@${constants.ENV.INLINED_SOCKET_CLI_CYCLONEDX_CDXGEN_VERSION}`,
101110
...argvToArray(argvMutable),
102111
],
103-
{
104-
apiToken: constants.SOCKET_PUBLIC_API_TOKEN,
105-
env: {
106-
[constants.SOCKET_CLI_ACCEPT_RISKS]: '1',
107-
},
108-
stdio: 'inherit',
109-
},
112+
shadowOpts,
110113
)
111114

112115
shadowResult.spawnPromise.process.on('exit', () => {

src/constants.mts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ const {
2626
},
2727
} = registryConstants
2828

29-
type RegistryEnv = typeof registryConstants.ENV
29+
export type RegistryEnv = typeof registryConstants.ENV
3030

31-
type RegistryInternals = (typeof registryConstants)['Symbol(kInternalsSymbol)']
31+
export type RegistryInternals =
32+
(typeof registryConstants)['Symbol(kInternalsSymbol)']
3233

33-
type Sentry = any
34+
export type Sentry = any
3435

35-
type Internals = Remap<
36+
export type Internals = Remap<
3637
Omit<RegistryInternals, 'getIpc'> &
3738
Readonly<{
3839
getIpc: {
@@ -46,7 +47,7 @@ type Internals = Remap<
4647
}>
4748
>
4849

49-
type ENV = Remap<
50+
export type ENV = Remap<
5051
RegistryEnv &
5152
Readonly<{
5253
DISABLE_GITHUB_CACHE: boolean
@@ -89,19 +90,21 @@ type ENV = Remap<
8990
}>
9091
>
9192

92-
type ProcessEnv = {
93+
export type ProcessEnv = {
9394
[K in keyof ENV]?: string
9495
}
9596

96-
type IPC = Readonly<{
97+
export type IPC = Readonly<{
9798
SOCKET_CLI_FIX?: string | undefined
9899
SOCKET_CLI_OPTIMIZE?: boolean | undefined
100+
SOCKET_CLI_SHADOW_ACCEPT_RISKS?: boolean | undefined
99101
SOCKET_CLI_SHADOW_API_TOKEN?: string | undefined
100102
SOCKET_CLI_SHADOW_BIN?: string | undefined
101103
SOCKET_CLI_SHADOW_PROGRESS?: boolean | undefined
104+
SOCKET_CLI_SHADOW_SILENT?: boolean | undefined
102105
}>
103106

104-
type Constants = Remap<
107+
export type Constants = Remap<
105108
Omit<typeof registryConstants, 'Symbol(kInternalsSymbol)' | 'ENV' | 'IPC'> & {
106109
readonly 'Symbol(kInternalsSymbol)': Internals
107110
readonly ALERT_TYPE_CRITICAL_CVE: 'criticalCVE'
@@ -128,9 +131,11 @@ type Constants = Remap<
128131
readonly SOCKET_CLI_FIX: 'SOCKET_CLI_FIX'
129132
readonly SOCKET_CLI_ISSUES_URL: 'https://github.com/SocketDev/socket-cli/issues'
130133
readonly SOCKET_CLI_OPTIMIZE: 'SOCKET_CLI_OPTIMIZE'
134+
readonly SOCKET_CLI_SHADOW_ACCEPT_RISKS: 'SOCKET_CLI_SHADOW_ACCEPT_RISKS'
131135
readonly SOCKET_CLI_SHADOW_API_TOKEN: 'SOCKET_CLI_SHADOW_API_TOKEN'
132136
readonly SOCKET_CLI_SHADOW_BIN: 'SOCKET_CLI_SHADOW_BIN'
133137
readonly SOCKET_CLI_SHADOW_PROGRESS: 'SOCKET_CLI_SHADOW_PROGRESS'
138+
readonly SOCKET_CLI_SHADOW_SILENT: 'SOCKET_CLI_SHADOW_SILENT'
134139
readonly SOCKET_CLI_VIEW_ALL_RISKS: 'SOCKET_CLI_VIEW_ALL_RISKS'
135140
readonly SOCKET_DEFAULT_BRANCH: 'socket-default-branch'
136141
readonly SOCKET_DEFAULT_REPOSITORY: 'socket-default-repository'
@@ -199,9 +204,11 @@ const SOCKET_CLI_BIN_NAME = 'socket'
199204
const SOCKET_CLI_FIX = 'SOCKET_CLI_FIX'
200205
const SOCKET_CLI_ISSUES_URL = 'https://github.com/SocketDev/socket-cli/issues'
201206
const SOCKET_CLI_OPTIMIZE = 'SOCKET_CLI_OPTIMIZE'
207+
const SOCKET_CLI_SHADOW_ACCEPT_RISKS = 'SOCKET_CLI_SHADOW_ACCEPT_RISKS'
202208
const SOCKET_CLI_SHADOW_API_TOKEN = 'SOCKET_CLI_SHADOW_API_TOKEN'
203209
const SOCKET_CLI_SHADOW_BIN = 'SOCKET_CLI_SHADOW_BIN'
204210
const SOCKET_CLI_SHADOW_PROGRESS = 'SOCKET_CLI_SHADOW_PROGRESS'
211+
const SOCKET_CLI_SHADOW_SILENT = 'SOCKET_CLI_SHADOW_SILENT'
205212
const SOCKET_CLI_VIEW_ALL_RISKS = 'SOCKET_CLI_VIEW_ALL_RISKS'
206213
const SOCKET_DEFAULT_BRANCH = 'socket-default-branch'
207214
const SOCKET_DEFAULT_REPOSITORY = 'socket-default-repository'
@@ -639,9 +646,11 @@ const constants: Constants = createConstantsObject(
639646
SOCKET_CLI_FIX,
640647
SOCKET_CLI_ISSUES_URL,
641648
SOCKET_CLI_OPTIMIZE,
649+
SOCKET_CLI_SHADOW_ACCEPT_RISKS,
642650
SOCKET_CLI_SHADOW_API_TOKEN,
643651
SOCKET_CLI_SHADOW_BIN,
644652
SOCKET_CLI_SHADOW_PROGRESS,
653+
SOCKET_CLI_SHADOW_SILENT,
645654
SOCKET_CLI_VIEW_ALL_RISKS,
646655
SOCKET_DEFAULT_BRANCH,
647656
SOCKET_DEFAULT_REPOSITORY,

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-ignore
22
import UntypedArborist from '@npmcli/arborist/lib/arborist/index.js'
33

4+
import { debugDir } from '@socketsecurity/registry/lib/debug'
45
import { logger } from '@socketsecurity/registry/lib/logger'
56

67
import constants from '../../../../../constants.mts'
@@ -14,11 +15,6 @@ import type {
1415
} from '../../types.mts'
1516

1617
const {
17-
SOCKET_CLI_ACCEPT_RISKS,
18-
SOCKET_CLI_SHADOW_API_TOKEN,
19-
SOCKET_CLI_SHADOW_BIN,
20-
SOCKET_CLI_SHADOW_PROGRESS,
21-
SOCKET_CLI_VIEW_ALL_RISKS,
2218
kInternalsSymbol,
2319
[kInternalsSymbol as unknown as 'Symbol(kInternalsSymbol)']: { getIpc },
2420
} = constants
@@ -96,11 +92,15 @@ export class SafeArborist extends Arborist {
9692
__proto__: null,
9793
...(args.length ? args[0] : undefined),
9894
} as ArboristReifyOptions
95+
9996
const ipc = await getIpc()
100-
const binName = ipc[SOCKET_CLI_SHADOW_BIN]
97+
debugDir('inspect', { ipc })
98+
99+
const binName = ipc[constants.SOCKET_CLI_SHADOW_BIN]
101100
if (!binName) {
102101
return await this[kRiskyReify](...args)
103102
}
103+
104104
await super.reify(
105105
{
106106
...options,
@@ -110,13 +110,19 @@ export class SafeArborist extends Arborist {
110110
// @ts-ignore: TypeScript gets grumpy about rest parameters.
111111
...args.slice(1),
112112
)
113-
const acceptRisks = constants.ENV.SOCKET_CLI_ACCEPT_RISKS
114-
const progress = ipc[SOCKET_CLI_SHADOW_PROGRESS]
115-
const spinner =
116-
options['silent'] || !progress ? undefined : constants.spinner
113+
114+
const shadowAcceptRisks = ipc[constants.SOCKET_CLI_SHADOW_ACCEPT_RISKS]
115+
const shadowProgress = ipc[constants.SOCKET_CLI_SHADOW_PROGRESS]
116+
const shadowSilent = ipc[constants.SOCKET_CLI_SHADOW_SILENT]
117+
118+
const acceptRisks =
119+
shadowAcceptRisks || constants.ENV.SOCKET_CLI_ACCEPT_RISKS
120+
const silent = !!options['silent']
121+
const spinner = silent || !shadowProgress ? undefined : constants.spinner
117122
const isShadowNpx = binName === 'npx'
123+
118124
const alertsMap = await getAlertsMapFromArborist(this, {
119-
apiToken: ipc[SOCKET_CLI_SHADOW_API_TOKEN],
125+
apiToken: ipc[constants.SOCKET_CLI_SHADOW_API_TOKEN],
120126
spinner,
121127
filter:
122128
acceptRisks || options.dryRun || options['yes']
@@ -130,6 +136,7 @@ export class SafeArborist extends Arborist {
130136
existing: isShadowNpx,
131137
},
132138
})
139+
133140
if (alertsMap.size) {
134141
process.exitCode = 1
135142
const viewAllRisks = constants.ENV.SOCKET_CLI_VIEW_ALL_RISKS
@@ -142,22 +149,23 @@ export class SafeArborist extends Arborist {
142149
Socket ${binName} exiting due to risks.${
143150
viewAllRisks
144151
? ''
145-
: `\nView all risks - Rerun with environment variable ${SOCKET_CLI_VIEW_ALL_RISKS}=1.`
152+
: `\nView all risks - Rerun with environment variable ${constants.SOCKET_CLI_VIEW_ALL_RISKS}=1.`
146153
}${
147154
acceptRisks
148155
? ''
149-
: `\nAccept risks - Rerun with environment variable ${SOCKET_CLI_ACCEPT_RISKS}=1.`
156+
: `\nAccept risks - Rerun with environment variable ${constants.SOCKET_CLI_ACCEPT_RISKS}=1.`
150157
}
151158
`.trim(),
152159
)
153-
} else if (!options['silent']) {
160+
} else if (!silent && !shadowSilent) {
154161
logger.success(
155162
`Socket ${binName} ${acceptRisks ? 'accepted' : 'found no'} risks`,
156163
)
157164
if (isShadowNpx) {
158165
logger.log(`Running ${options.add![0]}`)
159166
}
160167
}
168+
161169
return await this[kRiskyReify](...args)
162170
}
163171
}

src/shadow/npm/bin.mts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ import constants from '../../constants.mts'
1212
import { cmdFlagsToString } from '../../utils/cmd.mts'
1313
import { getPublicApiToken } from '../../utils/sdk.mts'
1414

15+
import type { IPC } from '../../constants.mts'
1516
import type {
1617
SpawnExtra,
1718
SpawnOptions,
1819
SpawnResult,
1920
} from '@socketsecurity/registry/lib/spawn'
2021

2122
export type ShadowBinOptions = SpawnOptions & {
22-
apiToken?: string | undefined
23+
ipc?: IPC | undefined
2324
}
2425

2526
export type ShadowBinResult = {
@@ -33,8 +34,8 @@ export default async function shadowBin(
3334
extra?: SpawnExtra | undefined,
3435
): Promise<ShadowBinResult> {
3536
const {
36-
apiToken = getPublicApiToken(),
3737
env: spawnEnv,
38+
ipc,
3839
...spawnOpts
3940
} = { __proto__: null, ...options } as ShadowBinOptions
4041
const isShadowNpm = binName === 'npm'
@@ -119,9 +120,10 @@ export default async function shadowBin(
119120

120121
spawnPromise.process.send({
121122
[constants.SOCKET_IPC_HANDSHAKE]: {
122-
[constants.SOCKET_CLI_SHADOW_API_TOKEN]: apiToken,
123+
[constants.SOCKET_CLI_SHADOW_API_TOKEN]: getPublicApiToken(),
123124
[constants.SOCKET_CLI_SHADOW_BIN]: binName,
124125
[constants.SOCKET_CLI_SHADOW_PROGRESS]: progressArg,
126+
...ipc,
125127
},
126128
})
127129

src/utils/alert/artifact.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
CompactSocketArtifact,
88
CompactSocketArtifactAlert,
99
SocketArtifact,
10-
SocketArtifactAlert
10+
SocketArtifactAlert,
1111
} from '@socketsecurity/sdk'
1212

1313
export type {
@@ -16,7 +16,7 @@ export type {
1616
CompactSocketArtifact,
1717
CompactSocketArtifactAlert,
1818
SocketArtifact,
19-
SocketArtifactAlert
19+
SocketArtifactAlert,
2020
}
2121

2222
export type CVE_ALERT_TYPE = 'cve' | 'mediumCVE' | 'mildCVE' | 'criticalCVE'

src/utils/coana.mts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import constants from '../constants.mts'
55
import { getDefaultApiToken } from './sdk.mts'
66
import shadowBin from '../shadow/npm/bin.mts'
77

8+
import type { ShadowBinOptions } from '../shadow/npm/bin.mts'
89
import type { CResult } from '../types.mts'
9-
import type {
10-
SpawnExtra,
11-
SpawnOptions,
12-
} from '@socketsecurity/registry/lib/spawn'
10+
import type { SpawnExtra } from '@socketsecurity/registry/lib/spawn'
1311

1412
export function extractTier1ReachabilityScanId(
1513
socketFactsFile: string,
@@ -26,13 +24,17 @@ export function extractTier1ReachabilityScanId(
2624
export async function spawnCoana(
2725
args: string[] | readonly string[],
2826
orgSlug?: string,
29-
options?: SpawnOptions | undefined,
27+
options?: ShadowBinOptions | undefined,
3028
extra?: SpawnExtra | undefined,
3129
): Promise<CResult<string>> {
32-
const { env: spawnEnv, ...spawnOpts } = {
30+
const {
31+
env: spawnEnv,
32+
ipc,
33+
...spawnOpts
34+
} = {
3335
__proto__: null,
3436
...options,
35-
} as SpawnOptions
37+
} as ShadowBinOptions
3638
const mixinsEnv: Record<string, string> = {
3739
SOCKET_CLI_VERSION: constants.ENV.INLINED_SOCKET_CLI_VERSION,
3840
}
@@ -60,11 +62,16 @@ export async function spawnCoana(
6062
],
6163
{
6264
...spawnOpts,
63-
apiToken: constants.SOCKET_PUBLIC_API_TOKEN,
6465
env: {
6566
...mixinsEnv,
6667
...spawnEnv,
67-
[constants.SOCKET_CLI_ACCEPT_RISKS]: '1',
68+
},
69+
ipc: {
70+
[constants.SOCKET_CLI_SHADOW_ACCEPT_RISKS]: true,
71+
[constants.SOCKET_CLI_SHADOW_API_TOKEN]:
72+
constants.SOCKET_PUBLIC_API_TOKEN,
73+
[constants.SOCKET_CLI_SHADOW_SILENT]: true,
74+
...ipc,
6875
},
6976
},
7077
extra,

0 commit comments

Comments
 (0)