@vltpkg/query 1.0.0-rc.33 → 1.0.0-rc.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts CHANGED
@@ -186,6 +186,8 @@ const securitySelectors = new Set([
186
186
  ':unmaintained',
187
187
  ':unpopular',
188
188
  ':unstable',
189
+ ':vuln',
190
+ ':vulnerable',
189
191
  ])
190
192
 
191
193
  const setMethodToJSON = (node: QueryResponseNode) => {
@@ -360,20 +362,9 @@ export class Query {
360
362
  i => i.type === 'licenseException',
361
363
  ),
362
364
  },
363
- malware: {
364
- low: securityArchiveEntry.alerts.some(
365
- i => i.type === 'gptAnomaly',
366
- ),
367
- medium: securityArchiveEntry.alerts.some(
368
- i => i.type === 'gptSecurity',
369
- ),
370
- high: securityArchiveEntry.alerts.some(
371
- i => i.type === 'gptMalware',
372
- ),
373
- critical: securityArchiveEntry.alerts.some(
374
- i => i.type === 'malware',
375
- ),
376
- },
365
+ malware: securityArchiveEntry.alerts.some(
366
+ i => i.type === 'malware' || i.type === 'gptMalware',
367
+ ),
377
368
  minified: securityArchiveEntry.alerts.some(
378
369
  i => i.type === 'minifiedFile',
379
370
  ),
@@ -441,6 +432,22 @@ export class Query {
441
432
  unstable: securityArchiveEntry.alerts.some(
442
433
  i => i.type === 'unstableOwnership',
443
434
  ),
435
+ vuln: {
436
+ low: securityArchiveEntry.alerts.some(
437
+ i => i.type === 'mildCVE' || i.type === 'gptAnomaly',
438
+ ),
439
+ medium: securityArchiveEntry.alerts.some(
440
+ i =>
441
+ i.type === 'potentialVulnerability' ||
442
+ i.type === 'gptSecurity',
443
+ ),
444
+ high: securityArchiveEntry.alerts.some(
445
+ i => i.type === 'cve',
446
+ ),
447
+ critical: securityArchiveEntry.alerts.some(
448
+ i => i.type === 'criticalCVE',
449
+ ),
450
+ },
444
451
  }
445
452
 
446
453
  setMethodToJSON(node)
package/src/parser.ts CHANGED
@@ -28,12 +28,13 @@ export const unescapeDots = (query: string): string =>
28
28
  const pseudoCleanUpNeeded = new Set([
29
29
  ':published',
30
30
  ':score',
31
- ':malware',
32
31
  ':severity',
33
32
  ':sev',
34
33
  ':squat',
35
34
  ':semver',
36
35
  ':v',
36
+ ':vuln',
37
+ ':vulnerable',
37
38
  ':path',
38
39
  ])
39
40
 
@@ -1,244 +1,36 @@
1
1
  import { error } from '@vltpkg/error-cause'
2
- import {
3
- asPostcssNodeWithChildren,
4
- asStringNode,
5
- asTagNode,
6
- isStringNode,
7
- isTagNode,
8
- } from '@vltpkg/dss-parser'
2
+ import { asPostcssNodeWithChildren } from '@vltpkg/dss-parser'
9
3
  import {
10
4
  assertSecurityArchive,
11
5
  removeDanglingEdges,
12
6
  removeNode,
13
- removeQuotes,
14
7
  } from './helpers.ts'
15
8
  import type { ParserState } from '../types.ts'
16
- import type { PostcssNode } from '@vltpkg/dss-parser'
17
-
18
- export type MalwareKinds =
19
- | '0'
20
- | '1'
21
- | '2'
22
- | '3'
23
- | 'critical'
24
- | 'high'
25
- | 'medium'
26
- | 'low'
27
- | undefined
28
-
29
- export type MalwareAlertTypes =
30
- 'malware' | 'gptMalware' | 'gptSecurity' | 'gptAnomaly' | undefined
31
-
32
- export type MalwareComparator = '>' | '<' | '>=' | '<=' | undefined
33
-
34
- const kindsMap = new Map<MalwareKinds, MalwareAlertTypes>([
35
- ['critical', 'malware'],
36
- ['high', 'gptMalware'],
37
- ['medium', 'gptSecurity'],
38
- ['low', 'gptAnomaly'],
39
- ['0', 'malware'],
40
- ['1', 'gptMalware'],
41
- ['2', 'gptSecurity'],
42
- ['3', 'gptAnomaly'],
43
- ])
44
-
45
- // Map numerical values to their respective kinds for comparison operations
46
- const kindLevelMap = new Map<MalwareKinds, number>([
47
- ['critical', 0],
48
- ['high', 1],
49
- ['medium', 2],
50
- ['low', 3],
51
- ['0', 0],
52
- ['1', 1],
53
- ['2', 2],
54
- ['3', 3],
55
- ])
56
9
 
57
- const kinds = new Set(kindsMap.keys())
58
-
59
- export const isMalwareKind = (
60
- value?: string,
61
- ): value is MalwareKinds => kinds.has(value as MalwareKinds)
62
-
63
- export const asMalwareKind = (value?: string): MalwareKinds => {
64
- if (!isMalwareKind(value)) {
65
- throw error('Expected a valid malware kind', {
66
- found: value,
67
- validOptions: Array.from(kinds),
68
- })
69
- }
70
- return value
71
- }
72
-
73
- export const parseInternals = (
74
- nodes: PostcssNode[],
75
- ): { kind: MalwareKinds; comparator: MalwareComparator } => {
76
- // Handle case where no parameters are provided (parameterless :malware)
77
- if (!nodes[0]) {
78
- return { kind: undefined, comparator: undefined }
79
- }
80
-
81
- const selectorNode = asPostcssNodeWithChildren(nodes[0])
82
- if (!selectorNode.nodes[0]) {
83
- return { kind: undefined, comparator: undefined }
84
- }
85
-
86
- let kindValue = ''
87
- let comparator: MalwareComparator = undefined
88
- let kind: MalwareKinds
89
-
90
- // Parse the parameter (kind with optional comparator)
91
- if (isStringNode(selectorNode.nodes[0])) {
92
- kindValue = removeQuotes(
93
- asStringNode(selectorNode.nodes[0]).value,
94
- )
95
- } else if (isTagNode(selectorNode.nodes[0])) {
96
- kindValue = asTagNode(selectorNode.nodes[0]).value
97
- }
98
-
99
- // Extract comparator if present
100
- if (kindValue.startsWith('>=')) {
101
- comparator = '>='
102
- kindValue = kindValue.substring(2)
103
- } else if (kindValue.startsWith('<=')) {
104
- comparator = '<='
105
- kindValue = kindValue.substring(2)
106
- } else if (kindValue.startsWith('>')) {
107
- comparator = '>'
108
- kindValue = kindValue.substring(1)
109
- } else if (kindValue.startsWith('<')) {
110
- comparator = '<'
111
- kindValue = kindValue.substring(1)
112
- }
113
-
114
- // Validate the kind without comparator
115
- if (!comparator) {
116
- kind = asMalwareKind(kindValue)
117
- } else {
118
- // For comparisons, just make sure it's a valid numeric value or a valid kind
119
- if (isMalwareKind(kindValue)) {
120
- kind = kindValue
121
- } else {
122
- throw error(
123
- 'Expected a valid malware kind or number between 0-3',
124
- {
125
- found: kindValue,
126
- },
127
- )
128
- }
129
- }
130
-
131
- return { kind, comparator }
132
- }
10
+ const malwareAlertTypes = new Set(['malware', 'gptMalware'])
133
11
 
134
12
  /**
135
- * :malware Pseudo-Selector, matches nodes with malware alerts.
13
+ * :malware Pseudo-Selector, matches nodes with any malware-related alert.
136
14
  *
137
- * Usage:
138
- * - :malware - matches malware with severity >= medium (critical, high, medium but not low)
139
- * - :malware(critical) - matches specific malware kind
140
- * - :malware(>1) - matches malware with severity greater than 1
141
- * - :malware(">=medium") - matches malware with severity >= medium
15
+ * This is a binary selector — no parameters. A node matches if it has
16
+ * at least one alert whose type is `malware` or `gptMalware`.
142
17
  */
143
18
  export const malware = async (state: ParserState) => {
144
19
  assertSecurityArchive(state, 'malware')
145
20
 
146
- let internals
147
- try {
148
- internals = parseInternals(
149
- asPostcssNodeWithChildren(state.current).nodes,
21
+ const currentNode = asPostcssNodeWithChildren(state.current)
22
+ if (currentNode.nodes.length > 0) {
23
+ throw error(
24
+ ':malware selector does not accept parameters. Use :vuln(<severity>) for severity-based filtering.',
150
25
  )
151
- } catch (err) {
152
- throw error('Failed to parse :malware selector', { cause: err })
153
- }
154
-
155
- const { kind, comparator } = internals
156
- const alertName = comparator ? undefined : kindsMap.get(kind)
157
-
158
- for (const node of state.partial.nodes) {
159
- const report = state.securityArchive.get(node.id)
160
- // Always exclude nodes that don't have security data or alerts
161
- if (!report?.alerts || report.alerts.length === 0) {
162
- removeNode(state, node)
163
- }
164
26
  }
165
27
 
166
28
  for (const node of state.partial.nodes) {
167
29
  const report = state.securityArchive.get(node.id)
168
- let exclude = true
169
-
170
- if (report) {
171
- if (kind === undefined && comparator === undefined) {
172
- // Parameterless :malware - match malware alerts with severity >= medium (exclude low/gptAnomaly)
173
- exclude = !report.alerts.some(
174
- alert =>
175
- alert.type === 'malware' ||
176
- alert.type === 'gptMalware' ||
177
- alert.type === 'gptSecurity',
178
- )
179
- } else if (comparator) {
180
- // retrieve the value to compare against
181
- const kindLevel = kindLevelMap.get(kind)
182
- // the kindLevel value has already been validated at this point
183
- // and thus can never return an undefined/falsy value but ts doesn't
184
- // know about that, so we have the extra check here
185
- /* c8 ignore next - impossible */
186
- if (kindLevel == null) break
187
-
188
- // Check each alert to find any that match our comparison criteria
189
- for (const alert of report.alerts) {
190
- // Get the numerical value of the alert type
191
- const alertType = alert.type
192
-
193
- // retrieve a key to the current alert level to be compared against
194
- const currentAlertLevelKey = [...kindsMap.entries()].find(
195
- ([_, alertValue]) => alertValue === alertType,
196
- )?.[0]
197
-
198
- // perform the comparison based on the user-provided kindLevel
199
- if (currentAlertLevelKey) {
200
- const currentAlertLevel = kindLevelMap.get(
201
- currentAlertLevelKey,
202
- )
203
- /* c8 ignore next - impossible but ts doesn't know */
204
- if (currentAlertLevel == null) continue
205
-
206
- switch (comparator) {
207
- case '>':
208
- if (currentAlertLevel > kindLevel) {
209
- exclude = false
210
- }
211
- break
212
- case '<':
213
- if (currentAlertLevel < kindLevel) {
214
- exclude = false
215
- }
216
- break
217
- case '>=':
218
- if (currentAlertLevel >= kindLevel) {
219
- exclude = false
220
- }
221
- break
222
- case '<=':
223
- if (currentAlertLevel <= kindLevel) {
224
- exclude = false
225
- }
226
- break
227
- }
228
-
229
- // If we've found a match, no need to check other alerts
230
- if (!exclude) break
231
- }
232
- }
233
- } else {
234
- // Original exact match behavior
235
- exclude = !report.alerts.some(
236
- alert => alert.type === alertName,
237
- )
238
- }
239
- }
240
-
241
- if (exclude) {
30
+ const hasMalware = report?.alerts.some(alert =>
31
+ malwareAlertTypes.has(alert.type),
32
+ )
33
+ if (!hasMalware) {
242
34
  removeNode(state, node)
243
35
  }
244
36
  }
@@ -0,0 +1,273 @@
1
+ import { error } from '@vltpkg/error-cause'
2
+ import {
3
+ asPostcssNodeWithChildren,
4
+ asStringNode,
5
+ asTagNode,
6
+ isStringNode,
7
+ isTagNode,
8
+ } from '@vltpkg/dss-parser'
9
+ import {
10
+ assertSecurityArchive,
11
+ removeDanglingEdges,
12
+ removeNode,
13
+ removeQuotes,
14
+ } from './helpers.ts'
15
+ import type { ParserState } from '../types.ts'
16
+ import type { PostcssNode } from '@vltpkg/dss-parser'
17
+
18
+ export type VulnKinds =
19
+ | '0'
20
+ | '1'
21
+ | '2'
22
+ | '3'
23
+ | 'critical'
24
+ | 'high'
25
+ | 'medium'
26
+ | 'low'
27
+ | undefined
28
+
29
+ export type VulnComparator = '>' | '<' | '>=' | '<=' | undefined
30
+
31
+ // Valid vulnerability kind names
32
+ const kinds = new Set<string | undefined>([
33
+ 'critical',
34
+ 'high',
35
+ 'medium',
36
+ 'low',
37
+ '0',
38
+ '1',
39
+ '2',
40
+ '3',
41
+ ])
42
+
43
+ // Map numerical values to their respective kinds for comparison operations
44
+ const kindLevelMap = new Map<VulnKinds, number>([
45
+ ['critical', 0],
46
+ ['high', 1],
47
+ ['medium', 2],
48
+ ['low', 3],
49
+ ['0', 0],
50
+ ['1', 1],
51
+ ['2', 2],
52
+ ['3', 3],
53
+ ])
54
+
55
+ // Maps alert types to their severity level for comparison operations.
56
+ // gptSecurity maps to medium (2), gptAnomaly maps to low (3).
57
+ const alertLevelMap = new Map<string, number>([
58
+ ['criticalCVE', 0],
59
+ ['cve', 1],
60
+ ['potentialVulnerability', 2],
61
+ ['gptSecurity', 2],
62
+ ['mildCVE', 3],
63
+ ['gptAnomaly', 3],
64
+ ])
65
+
66
+ // Maps alert.severity strings to numeric levels for cveId-bearing alerts
67
+ // whose type may not be in alertLevelMap.
68
+ const severityLevelMap = new Map<string, number>([
69
+ ['critical', 0],
70
+ ['high', 1],
71
+ ['medium', 2],
72
+ ['low', 3],
73
+ ])
74
+
75
+ /**
76
+ * Returns the severity level for an alert, considering both its type
77
+ * (via alertLevelMap) and its cveId + severity fields. Alerts with a
78
+ * cveId are treated as vulnerability alerts even if their type is not
79
+ * in the closed set.
80
+ */
81
+ const getAlertLevel = (alert: {
82
+ type: string
83
+ severity?: string
84
+ props?: { cveId?: string }
85
+ }): number | undefined => {
86
+ const level = alertLevelMap.get(alert.type)
87
+ if (level != null) return level
88
+ // Fall back to severity field for alerts carrying a CVE id
89
+ if (alert.props?.cveId && alert.severity) {
90
+ return severityLevelMap.get(alert.severity)
91
+ }
92
+ return undefined
93
+ }
94
+
95
+ export const isVulnKind = (value?: string): value is VulnKinds =>
96
+ kinds.has(value)
97
+
98
+ export const asVulnKind = (value?: string): VulnKinds => {
99
+ if (!isVulnKind(value)) {
100
+ throw error('Expected a valid vuln kind', {
101
+ found: value,
102
+ validOptions: Array.from(kinds),
103
+ })
104
+ }
105
+ return value
106
+ }
107
+
108
+ export const parseInternals = (
109
+ nodes: PostcssNode[],
110
+ ): { kind: VulnKinds; comparator: VulnComparator } => {
111
+ // Handle case where no parameters are provided (parameterless :vuln)
112
+ if (!nodes[0]) {
113
+ return { kind: undefined, comparator: undefined }
114
+ }
115
+
116
+ const selectorNode = asPostcssNodeWithChildren(nodes[0])
117
+ if (!selectorNode.nodes[0]) {
118
+ return { kind: undefined, comparator: undefined }
119
+ }
120
+
121
+ let kindValue = ''
122
+ let comparator: VulnComparator = undefined
123
+ let kind: VulnKinds
124
+
125
+ // Parse the parameter (kind with optional comparator)
126
+ if (isStringNode(selectorNode.nodes[0])) {
127
+ kindValue = removeQuotes(
128
+ asStringNode(selectorNode.nodes[0]).value,
129
+ )
130
+ } else if (isTagNode(selectorNode.nodes[0])) {
131
+ kindValue = asTagNode(selectorNode.nodes[0]).value
132
+ }
133
+
134
+ // Extract comparator if present
135
+ if (kindValue.startsWith('>=')) {
136
+ comparator = '>='
137
+ kindValue = kindValue.substring(2)
138
+ } else if (kindValue.startsWith('<=')) {
139
+ comparator = '<='
140
+ kindValue = kindValue.substring(2)
141
+ } else if (kindValue.startsWith('>')) {
142
+ comparator = '>'
143
+ kindValue = kindValue.substring(1)
144
+ } else if (kindValue.startsWith('<')) {
145
+ comparator = '<'
146
+ kindValue = kindValue.substring(1)
147
+ }
148
+
149
+ // Validate the kind without comparator
150
+ if (!comparator) {
151
+ kind = asVulnKind(kindValue)
152
+ } else {
153
+ // For comparisons, just make sure it's a valid numeric value or a valid kind
154
+ if (isVulnKind(kindValue)) {
155
+ kind = kindValue
156
+ } else {
157
+ throw error(
158
+ 'Expected a valid vuln kind or number between 0-3',
159
+ {
160
+ found: kindValue,
161
+ },
162
+ )
163
+ }
164
+ }
165
+
166
+ return { kind, comparator }
167
+ }
168
+
169
+ /**
170
+ * :vuln / :vulnerable Pseudo-Selector, matches nodes with vulnerability alerts.
171
+ *
172
+ * Usage:
173
+ * - :vuln - matches vulnerabilities with severity >= medium (critical, high, medium but not low)
174
+ * - :vuln(critical) - matches specific vulnerability kind
175
+ * - :vuln(>1) - matches vulnerabilities with severity greater than 1
176
+ * - :vuln(">=medium") - matches vulnerabilities with severity >= medium
177
+ */
178
+ export const vuln = async (state: ParserState) => {
179
+ assertSecurityArchive(state, 'vuln')
180
+
181
+ let internals
182
+ try {
183
+ internals = parseInternals(
184
+ asPostcssNodeWithChildren(state.current).nodes,
185
+ )
186
+ } catch (err) {
187
+ throw error('Failed to parse :vuln selector', { cause: err })
188
+ }
189
+
190
+ const { kind, comparator } = internals
191
+
192
+ for (const node of state.partial.nodes) {
193
+ const report = state.securityArchive.get(node.id)
194
+ // Always exclude nodes that don't have security data or alerts
195
+ if (!report?.alerts || report.alerts.length === 0) {
196
+ removeNode(state, node)
197
+ }
198
+ }
199
+
200
+ for (const node of state.partial.nodes) {
201
+ const report = state.securityArchive.get(node.id)
202
+ let exclude = true
203
+
204
+ if (report) {
205
+ if (kind === undefined && comparator === undefined) {
206
+ // Parameterless :vuln - match vuln alerts with severity >= medium
207
+ // Includes: criticalCVE, cve, potentialVulnerability, gptSecurity,
208
+ // and any alert carrying a cveId prop
209
+ // Excludes: mildCVE, gptAnomaly (low severity)
210
+ exclude = !report.alerts.some(alert => {
211
+ const level = getAlertLevel(alert)
212
+ return level != null && level <= 2
213
+ })
214
+ } else if (comparator) {
215
+ // retrieve the value to compare against
216
+ const kindLevel = kindLevelMap.get(kind)
217
+ // the kindLevel value has already been validated at this point
218
+ // and thus can never return an undefined/falsy value but ts doesn't
219
+ // know about that, so we have the extra check here
220
+ /* c8 ignore next - impossible */
221
+ if (kindLevel == null) break
222
+
223
+ // Check each alert to find any that match our comparison criteria
224
+ for (const alert of report.alerts) {
225
+ const currentAlertLevel = getAlertLevel(alert)
226
+ if (currentAlertLevel == null) continue
227
+
228
+ switch (comparator) {
229
+ case '>':
230
+ if (currentAlertLevel > kindLevel) {
231
+ exclude = false
232
+ }
233
+ break
234
+ case '<':
235
+ if (currentAlertLevel < kindLevel) {
236
+ exclude = false
237
+ }
238
+ break
239
+ case '>=':
240
+ if (currentAlertLevel >= kindLevel) {
241
+ exclude = false
242
+ }
243
+ break
244
+ case '<=':
245
+ if (currentAlertLevel <= kindLevel) {
246
+ exclude = false
247
+ }
248
+ break
249
+ }
250
+
251
+ // If we've found a match, no need to check other alerts
252
+ if (!exclude) break
253
+ }
254
+ } else {
255
+ // Exact match behavior — also matches gptSecurity/gptAnomaly
256
+ // at the same level as their mapped kind, and cveId-bearing alerts
257
+ const targetLevel = kindLevelMap.get(kind)
258
+ exclude = !report.alerts.some(alert => {
259
+ const level = getAlertLevel(alert)
260
+ return level != null && level === targetLevel
261
+ })
262
+ }
263
+ }
264
+
265
+ if (exclude) {
266
+ removeNode(state, node)
267
+ }
268
+ }
269
+
270
+ removeDanglingEdges(state)
271
+
272
+ return state
273
+ }
package/src/pseudo.ts CHANGED
@@ -63,7 +63,7 @@ import { unknown } from './pseudo/unknown.ts'
63
63
  import { unmaintained } from './pseudo/unmaintained.ts'
64
64
  import { unpopular } from './pseudo/unpopular.ts'
65
65
  import { unstable } from './pseudo/unstable.ts'
66
- import { vulnerable } from './pseudo/vulnerable.ts'
66
+ import { vuln } from './pseudo/vuln.ts'
67
67
  import { workspace } from './pseudo/workspace.ts'
68
68
 
69
69
  import type { EdgeLike, NodeLike } from '@vltpkg/types'
@@ -367,8 +367,8 @@ const pseudoSelectors = new Map<string, ParserFn>(
367
367
  unpopular,
368
368
  unstable,
369
369
  v: semver,
370
- vuln: vulnerable,
371
- vulnerable,
370
+ vuln,
371
+ vulnerable: vuln,
372
372
  workspace,
373
373
  }),
374
374
  )
package/src/types.ts CHANGED
@@ -109,7 +109,7 @@ export type Insights = {
109
109
  eval?: boolean
110
110
  fs?: boolean
111
111
  license?: LicenseInsights
112
- malware?: MalwareInsights
112
+ malware?: boolean
113
113
  minified?: boolean
114
114
  native?: boolean
115
115
  network?: boolean
@@ -129,6 +129,7 @@ export type Insights = {
129
129
  unmaintained?: boolean
130
130
  unpopular?: boolean
131
131
  unstable?: boolean
132
+ vuln?: VulnInsights
132
133
  }
133
134
 
134
135
  export type LicenseInsights = {
@@ -149,8 +150,8 @@ export type LeveledInsights = {
149
150
  critical: boolean
150
151
  }
151
152
 
152
- export type MalwareInsights = LeveledInsights
153
153
  export type SeverityInsights = LeveledInsights
154
+ export type VulnInsights = LeveledInsights
154
155
 
155
156
  export type SquatInsights = {
156
157
  medium: boolean