Skip to content

Commit 6de9bab

Browse files
committed
Change include/exclude to a filterConfig
1 parent 04a90b9 commit 6de9bab

7 files changed

Lines changed: 114 additions & 113 deletions

File tree

src/commands/fix/agent-fix.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export async function agentFix(
145145
let count = 0
146146

147147
const infoByPartialPurl = getCveInfoFromAlertsMap(alertsMap, {
148-
exclude: { upgradable: true },
148+
filter: { upgradable: false },
149149
})
150150
if (!infoByPartialPurl) {
151151
spinner?.stop()

src/commands/fix/shared.mts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { getOwn } from '@socketsecurity/registry/lib/objects'
2+
3+
import { toFilterConfig } from '../../utils/filter-config.mts'
4+
15
import type { GetAlertsMapFromPurlsOptions } from '../../utils/alerts-map.mts'
26
import type { Remap } from '@socketsecurity/registry/lib/objects'
37

@@ -11,16 +15,15 @@ export function getFixAlertsMapOptions(
1115
consolidate: true,
1216
nothrow: true,
1317
...options,
14-
include: {
15-
__proto__: null,
18+
filter: toFilterConfig({
1619
existing: true,
17-
unfixable: false,
20+
fixable: true,
1821
upgradable: false,
19-
...options?.include,
20-
},
22+
...getOwn(options, 'filter'),
23+
}),
2124
} as Remap<
2225
Omit<GetAlertsMapFromPurlsOptions, 'include' | 'overrides' | 'spinner'> & {
23-
include: Exclude<GetAlertsMapFromPurlsOptions['include'], undefined>
26+
filter: Exclude<GetAlertsMapFromPurlsOptions['filter'], undefined>
2427
}
2528
>
2629
}

src/shadow/npm/arborist-helpers.mts

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ import semver from 'semver'
33
import { PackageURL } from '@socketregistry/packageurl-js'
44
import { getManifestData } from '@socketsecurity/registry'
55
import { debugFn } from '@socketsecurity/registry/lib/debug'
6-
import { hasOwn } from '@socketsecurity/registry/lib/objects'
6+
import { getOwn, hasOwn } from '@socketsecurity/registry/lib/objects'
77
import { fetchPackagePackument } from '@socketsecurity/registry/lib/packages'
88

99
import constants from '../../constants.mts'
1010
import { Edge } from './arborist/index.mts'
1111
import { DiffAction } from './arborist/types.mts'
1212
import { getAlertsMapFromPurls } from '../../utils/alerts-map.mts'
13+
import { toFilterConfig } from '../../utils/filter-config.mts'
1314
import { npa } from '../../utils/npm-package-arg.mts'
1415
import { applyRange, getMajor, getMinVersion } from '../../utils/semver.mts'
1516
import { idToNpmPurl } from '../../utils/spec.mts'
@@ -24,7 +25,7 @@ import type {
2425
import type { AliasResult } from '../../utils/npm-package-arg.mts'
2526
import type { RangeStyle } from '../../utils/semver.mts'
2627
import type {
27-
AlertIncludeFilter,
28+
AlertFilter,
2829
AlertsByPurl,
2930
} from '../../utils/socket-package-alert.mts'
3031
import type { EditablePackageJson } from '@socketsecurity/registry/lib/packages'
@@ -173,7 +174,7 @@ export function findPackageNodes(
173174

174175
export type GetAlertsMapFromArboristOptions = {
175176
consolidate?: boolean | undefined
176-
include?: AlertIncludeFilter | undefined
177+
filter?: AlertFilter | undefined
177178
nothrow?: boolean | undefined
178179
spinner?: Spinner | undefined
179180
}
@@ -185,28 +186,25 @@ export async function getAlertsMapFromArborist(
185186
const opts = {
186187
__proto__: null,
187188
consolidate: false,
188-
include: undefined,
189189
nothrow: false,
190190
...options,
191-
} as GetAlertsMapFromArboristOptions
192-
193-
opts.include = {
194-
__proto__: null,
195-
// Leave 'actions' unassigned so it can be given a default value in
196-
// subsequent functions where `options` is passed.
197-
// actions: undefined,
198-
blocked: true,
199-
critical: true,
200-
cve: true,
201-
existing: false,
202-
unfixable: true,
203-
upgradable: false,
204-
...opts.include,
205-
} as AlertIncludeFilter
191+
filter: toFilterConfig({
192+
// Leave 'actions' unassigned so it can be given a default value in
193+
// subsequent functions where `options` is passed.
194+
// actions: undefined,
195+
blocked: true,
196+
critical: true,
197+
cve: true,
198+
existing: false,
199+
fixable: false,
200+
upgradable: false,
201+
...getOwn(options, 'filter'),
202+
}),
203+
} as GetAlertsMapFromArboristOptions & { filter: AlertFilter }
206204

207205
const needInfoOn = getDetailsFromDiff(arb.diff, {
208-
include: {
209-
unchanged: opts.include.existing,
206+
filter: {
207+
existing: opts.filter.existing,
210208
},
211209
})
212210

@@ -228,17 +226,17 @@ export async function getAlertsMapFromArborist(
228226

229227
return await getAlertsMapFromPurls(purls, {
230228
overrides,
231-
...options,
229+
...opts,
232230
})
233231
}
234232

235-
export type DiffQueryIncludeFilter = {
236-
unchanged?: boolean | undefined
233+
export type DiffQueryFilter = {
234+
existing?: boolean | undefined
237235
unknownOrigin?: boolean | undefined
238236
}
239237

240238
export type DiffQueryOptions = {
241-
include?: DiffQueryIncludeFilter | undefined
239+
filter?: DiffQueryFilter | undefined
242240
}
243241

244242
export type PackageDetail = {
@@ -257,12 +255,11 @@ export function getDetailsFromDiff(
257255
return details
258256
}
259257

260-
const include = {
261-
__proto__: null,
262-
unchanged: false,
258+
const filterConfig = toFilterConfig({
259+
existing: false,
263260
unknownOrigin: true,
264-
...({ __proto__: null, ...options } as DiffQueryOptions).include,
265-
} as DiffQueryIncludeFilter
261+
...getOwn(options, 'filter'),
262+
}) as DiffQueryFilter
266263

267264
const queue: Diff[] = [...diff.children]
268265
let pos = 0
@@ -296,7 +293,7 @@ export function getDetailsFromDiff(
296293
}
297294
if (keep && pkgNode?.resolved && (!oldNode || oldNode.resolved)) {
298295
if (
299-
include.unknownOrigin ||
296+
filterConfig.unknownOrigin ||
300297
getUrlOrigin(pkgNode.resolved) === NPM_REGISTRY_URL
301298
) {
302299
details.push({
@@ -310,12 +307,12 @@ export function getDetailsFromDiff(
310307
queue[queueLength++] = child
311308
}
312309
}
313-
if (include.unchanged) {
310+
if (filterConfig.existing) {
314311
const { unchanged } = diff
315312
for (let i = 0, { length } = unchanged; i < length; i += 1) {
316313
const pkgNode = unchanged[i]!
317314
if (
318-
include.unknownOrigin ||
315+
filterConfig.unknownOrigin ||
319316
getUrlOrigin(pkgNode.resolved!) === NPM_REGISTRY_URL
320317
) {
321318
details.push({

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,19 @@ export class SafeArborist extends Arborist {
123123
const isSafeNpx = binName === NPX
124124
const alertsMap = await getAlertsMapFromArborist(this, {
125125
spinner,
126-
include:
126+
filter:
127127
acceptRisks || options.dryRun || options['yes']
128128
? {
129129
actions: ['error'],
130130
blocked: true,
131131
critical: false,
132132
cve: false,
133133
existing: true,
134-
unfixable: false,
134+
fixable: false,
135135
}
136136
: {
137137
existing: isSafeNpx,
138-
unfixable: isSafeNpm,
138+
fixable: !isSafeNpm,
139139
},
140140
})
141141
if (alertsMap.size) {

src/utils/alerts-map.mts

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import { arrayUnique } from '@socketsecurity/registry/lib/arrays'
22
import { debugDir } from '@socketsecurity/registry/lib/debug'
33
import { logger } from '@socketsecurity/registry/lib/logger'
4+
import { getOwn } from '@socketsecurity/registry/lib/objects'
45

6+
import { toFilterConfig } from './filter-config.mts'
57
import { extractPurlsFromPnpmLockfile } from './pnpm.mts'
6-
import { getPublicToken, setupSdk } from './sdk.mts'
8+
import { getPublicApiToken, setupSdk } from './sdk.mts'
79
import { addArtifactToAlertsMap } from './socket-package-alert.mts'
810

911
import type { CompactSocketArtifact } from './alert/artifact.mts'
10-
import type {
11-
AlertIncludeFilter,
12-
AlertsByPurl,
13-
} from './socket-package-alert.mts'
12+
import type { AlertFilter, AlertsByPurl } from './socket-package-alert.mts'
1413
import type { LockfileObject } from '@pnpm/lockfile.fs'
1514
import type { Spinner } from '@socketsecurity/registry/lib/spinner'
1615

1716
export type GetAlertsMapFromPnpmLockfileOptions = {
1817
consolidate?: boolean | undefined
19-
include?: AlertIncludeFilter | undefined
18+
include?: AlertFilter | undefined
2019
overrides?: { [key: string]: string } | undefined
2120
nothrow?: boolean | undefined
2221
spinner?: Spinner | undefined
@@ -35,7 +34,7 @@ export async function getAlertsMapFromPnpmLockfile(
3534

3635
export type GetAlertsMapFromPurlsOptions = {
3736
consolidate?: boolean | undefined
38-
include?: AlertIncludeFilter | undefined
37+
filter?: AlertFilter | undefined
3938
overrides?: { [key: string]: string } | undefined
4039
nothrow?: boolean | undefined
4140
spinner?: Spinner | undefined
@@ -48,24 +47,21 @@ export async function getAlertsMapFromPurls(
4847
const opts = {
4948
__proto__: null,
5049
consolidate: false,
51-
include: undefined,
5250
nothrow: false,
5351
...options,
54-
} as GetAlertsMapFromPurlsOptions
55-
56-
opts.include = {
57-
__proto__: null,
58-
// Leave 'actions' unassigned so it can be given a default value in
59-
// subsequent functions where `options` is passed.
60-
// actions: undefined,
61-
blocked: true,
62-
critical: true,
63-
cve: true,
64-
existing: false,
65-
unfixable: true,
66-
upgradable: false,
67-
...opts.include,
68-
} as AlertIncludeFilter
52+
filter: toFilterConfig({
53+
// Leave 'actions' unassigned so it can be given a default value in
54+
// subsequent functions where `options` is passed.
55+
// actions: undefined,
56+
blocked: true,
57+
critical: true,
58+
cve: true,
59+
existing: false,
60+
fixable: false,
61+
upgradable: false,
62+
...getOwn(options, 'filter'),
63+
}),
64+
} as GetAlertsMapFromPurlsOptions & { filter: AlertFilter }
6965

7066
const uniqPurls = arrayUnique(purls)
7167
debugDir('silly', { purls: uniqPurls })
@@ -82,7 +78,7 @@ export async function getAlertsMapFromPurls(
8278

8379
spinner?.start(getText())
8480

85-
const sockSdkCResult = await setupSdk({ apiToken: getPublicToken() })
81+
const sockSdkCResult = await setupSdk({ apiToken: getPublicApiToken() })
8682
if (!sockSdkCResult.ok) {
8783
spinner?.stop()
8884
throw new Error('Auth error: Try to run `socket login` first')
@@ -92,7 +88,7 @@ export async function getAlertsMapFromPurls(
9288
const alertsMapOptions = {
9389
overrides: opts.overrides,
9490
consolidate: opts.consolidate,
95-
include: opts.include,
91+
filter: opts.filter,
9692
spinner,
9793
}
9894

@@ -104,10 +100,10 @@ export async function getAlertsMapFromPurls(
104100
queryParams: {
105101
alerts: 'true',
106102
compact: 'true',
107-
...(opts.include.actions
108-
? { actions: opts.include.actions.join(',') }
103+
//...(opts.filter.fixable ? { fixable: 'true' } : {}),
104+
...(Array.isArray(opts.filter.actions)
105+
? { actions: opts.filter.actions.join(',') }
109106
: {}),
110-
...(opts.include.unfixable ? {} : { fixable: 'true' }),
111107
},
112108
},
113109
)) {

src/utils/filter-config.mts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { isObject } from '@socketsecurity/registry/lib/objects'
2+
3+
export type FilterConfig = {
4+
[key: string]: boolean | string[]
5+
}
6+
7+
export function toFilterConfig(obj: any): FilterConfig {
8+
const normalized = { __proto__: null } as unknown as FilterConfig
9+
const keys = isObject(obj) ? Object.keys(obj) : []
10+
for (const key of keys) {
11+
const value = obj[key]
12+
if (typeof value === 'boolean' || Array.isArray(value)) {
13+
normalized[key] = value
14+
}
15+
}
16+
return normalized
17+
}

0 commit comments

Comments
 (0)