@vltpkg/query 1.0.0-rc.22 → 1.0.0-rc.24

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.
Files changed (129) hide show
  1. package/dist/attribute.d.ts +14 -0
  2. package/dist/attribute.js +132 -0
  3. package/dist/combinator.d.ts +5 -0
  4. package/dist/combinator.js +111 -0
  5. package/dist/id.d.ts +5 -0
  6. package/dist/id.js +35 -0
  7. package/dist/index.d.ts +48 -0
  8. package/dist/index.js +410 -0
  9. package/dist/parser.d.ts +14 -0
  10. package/dist/parser.js +92 -0
  11. package/dist/pseudo/abandoned.d.ts +6 -0
  12. package/dist/pseudo/abandoned.js +5 -0
  13. package/dist/pseudo/attr.d.ts +18 -0
  14. package/dist/pseudo/attr.js +57 -0
  15. package/dist/pseudo/built.d.ts +7 -0
  16. package/dist/pseudo/built.js +15 -0
  17. package/dist/pseudo/confused.d.ts +8 -0
  18. package/dist/pseudo/confused.js +18 -0
  19. package/dist/pseudo/cve.d.ts +12 -0
  20. package/dist/pseudo/cve.js +43 -0
  21. package/dist/pseudo/cwe.d.ts +12 -0
  22. package/dist/pseudo/cwe.js +42 -0
  23. package/dist/pseudo/debug.d.ts +6 -0
  24. package/dist/pseudo/debug.js +5 -0
  25. package/dist/pseudo/deprecated.d.ts +6 -0
  26. package/dist/pseudo/deprecated.js +5 -0
  27. package/dist/pseudo/dev.d.ts +5 -0
  28. package/dist/pseudo/dev.js +14 -0
  29. package/dist/pseudo/diff.d.ts +26 -0
  30. package/dist/pseudo/diff.js +75 -0
  31. package/dist/pseudo/dynamic.d.ts +6 -0
  32. package/dist/pseudo/dynamic.js +5 -0
  33. package/dist/pseudo/empty.d.ts +6 -0
  34. package/dist/pseudo/empty.js +13 -0
  35. package/dist/pseudo/entropic.d.ts +6 -0
  36. package/dist/pseudo/entropic.js +5 -0
  37. package/dist/pseudo/env.d.ts +6 -0
  38. package/dist/pseudo/env.js +5 -0
  39. package/dist/pseudo/eval.d.ts +6 -0
  40. package/dist/pseudo/eval.js +5 -0
  41. package/dist/pseudo/fs.d.ts +6 -0
  42. package/dist/pseudo/fs.js +5 -0
  43. package/dist/pseudo/helpers.d.ts +38 -0
  44. package/dist/pseudo/helpers.js +79 -0
  45. package/dist/pseudo/host.d.ts +19 -0
  46. package/dist/pseudo/host.js +79 -0
  47. package/dist/pseudo/hostname.d.ts +11 -0
  48. package/dist/pseudo/hostname.js +138 -0
  49. package/dist/pseudo/license.d.ts +12 -0
  50. package/dist/pseudo/license.js +74 -0
  51. package/dist/pseudo/link.d.ts +8 -0
  52. package/dist/pseudo/link.js +24 -0
  53. package/dist/pseudo/malware.d.ts +23 -0
  54. package/dist/pseudo/malware.js +186 -0
  55. package/dist/pseudo/minified.d.ts +6 -0
  56. package/dist/pseudo/minified.js +5 -0
  57. package/dist/pseudo/missing.d.ts +7 -0
  58. package/dist/pseudo/missing.js +14 -0
  59. package/dist/pseudo/native.d.ts +6 -0
  60. package/dist/pseudo/native.js +5 -0
  61. package/dist/pseudo/network.d.ts +6 -0
  62. package/dist/pseudo/network.js +5 -0
  63. package/dist/pseudo/obfuscated.d.ts +6 -0
  64. package/dist/pseudo/obfuscated.js +5 -0
  65. package/dist/pseudo/optional.d.ts +5 -0
  66. package/dist/pseudo/optional.js +14 -0
  67. package/dist/pseudo/outdated.d.ts +53 -0
  68. package/dist/pseudo/outdated.js +211 -0
  69. package/dist/pseudo/overridden.d.ts +7 -0
  70. package/dist/pseudo/overridden.js +16 -0
  71. package/dist/pseudo/path.d.ts +18 -0
  72. package/dist/pseudo/path.js +110 -0
  73. package/dist/pseudo/peer.d.ts +5 -0
  74. package/dist/pseudo/peer.js +14 -0
  75. package/dist/pseudo/prerelease.d.ts +17 -0
  76. package/dist/pseudo/prerelease.js +40 -0
  77. package/dist/pseudo/private.d.ts +6 -0
  78. package/dist/pseudo/private.js +15 -0
  79. package/dist/pseudo/prod.d.ts +5 -0
  80. package/dist/pseudo/prod.js +14 -0
  81. package/dist/pseudo/published.d.ts +39 -0
  82. package/dist/pseudo/published.js +179 -0
  83. package/dist/pseudo/registry.d.ts +10 -0
  84. package/dist/pseudo/registry.js +24 -0
  85. package/dist/pseudo/root.d.ts +6 -0
  86. package/dist/pseudo/root.js +17 -0
  87. package/dist/pseudo/scanned.d.ts +8 -0
  88. package/dist/pseudo/scanned.js +16 -0
  89. package/dist/pseudo/score.d.ts +15 -0
  90. package/dist/pseudo/score.js +132 -0
  91. package/dist/pseudo/scripts.d.ts +9 -0
  92. package/dist/pseudo/scripts.js +43 -0
  93. package/dist/pseudo/semver.d.ts +16 -0
  94. package/dist/pseudo/semver.js +166 -0
  95. package/dist/pseudo/severity.d.ts +14 -0
  96. package/dist/pseudo/severity.js +159 -0
  97. package/dist/pseudo/shell.d.ts +6 -0
  98. package/dist/pseudo/shell.js +5 -0
  99. package/dist/pseudo/shrinkwrap.d.ts +6 -0
  100. package/dist/pseudo/shrinkwrap.js +5 -0
  101. package/dist/pseudo/spec.d.ts +16 -0
  102. package/dist/pseudo/spec.js +101 -0
  103. package/dist/pseudo/squat.d.ts +14 -0
  104. package/dist/pseudo/squat.js +171 -0
  105. package/dist/pseudo/suspicious.d.ts +6 -0
  106. package/dist/pseudo/suspicious.js +5 -0
  107. package/dist/pseudo/tracker.d.ts +6 -0
  108. package/dist/pseudo/tracker.js +5 -0
  109. package/dist/pseudo/trivial.d.ts +6 -0
  110. package/dist/pseudo/trivial.js +5 -0
  111. package/dist/pseudo/type.d.ts +7 -0
  112. package/dist/pseudo/type.js +21 -0
  113. package/dist/pseudo/undesirable.d.ts +6 -0
  114. package/dist/pseudo/undesirable.js +5 -0
  115. package/dist/pseudo/unknown.d.ts +6 -0
  116. package/dist/pseudo/unknown.js +5 -0
  117. package/dist/pseudo/unmaintained.d.ts +6 -0
  118. package/dist/pseudo/unmaintained.js +5 -0
  119. package/dist/pseudo/unpopular.d.ts +6 -0
  120. package/dist/pseudo/unpopular.js +5 -0
  121. package/dist/pseudo/unstable.d.ts +6 -0
  122. package/dist/pseudo/unstable.js +5 -0
  123. package/dist/pseudo/workspace.d.ts +5 -0
  124. package/dist/pseudo/workspace.js +19 -0
  125. package/dist/pseudo.d.ts +5 -0
  126. package/dist/pseudo.js +366 -0
  127. package/dist/types.d.ts +124 -0
  128. package/dist/types.js +1 -0
  129. package/package.json +10 -10
package/dist/index.js ADDED
@@ -0,0 +1,410 @@
1
+ import { error } from '@vltpkg/error-cause';
2
+ import { joinDepIDTuple } from '@vltpkg/dep-id/browser';
3
+ import { parse, isPostcssNodeWithChildren, asPostcssNodeWithChildren, isSelectorNode, isPseudoNode, isIdentifierNode, isAttributeNode, } from '@vltpkg/dss-parser';
4
+ import { attribute } from "./attribute.js";
5
+ import { combinator } from "./combinator.js";
6
+ import { id } from "./id.js";
7
+ import { pseudo } from "./pseudo.js";
8
+ export * from "./types.js";
9
+ const noopFn = async (state) => state;
10
+ const selectors = {
11
+ attribute,
12
+ /* c8 ignore start */
13
+ class: async (state) => {
14
+ throw error('Unsupported selector', { found: state.current });
15
+ },
16
+ /* c8 ignore end */
17
+ combinator,
18
+ comment: async (state) => {
19
+ if (state.current.value && !state.comment) {
20
+ const commentValue = state.current.value;
21
+ const cleanComment = commentValue
22
+ .replace(/^\/\*/, '')
23
+ .replace(/\*\/$/, '')
24
+ .trim();
25
+ state.comment = cleanComment;
26
+ }
27
+ return state;
28
+ },
29
+ id,
30
+ nesting: noopFn,
31
+ pseudo,
32
+ root: noopFn,
33
+ selector: async (state) => {
34
+ state.partial.nodes = new Set(state.initial.nodes);
35
+ state.partial.edges = new Set(state.initial.edges);
36
+ return state;
37
+ },
38
+ string: async (state) => {
39
+ throw error('Unsupported selector', { found: state.current });
40
+ },
41
+ tag: async (state) => {
42
+ if (state.current.value !== '{' && state.current.value !== '}') {
43
+ throw error('Unsupported selector', { found: state.current });
44
+ }
45
+ return state;
46
+ },
47
+ universal: noopFn,
48
+ };
49
+ const selectorsMap = new Map(Object.entries(selectors));
50
+ export const walk = async (state) => {
51
+ await state.cancellable();
52
+ const parserFn = selectorsMap.get(state.current.type);
53
+ if (!parserFn) {
54
+ if (state.loose) {
55
+ return state;
56
+ }
57
+ throw error(`Missing parser for query node: ${state.current.type}`, {
58
+ found: state.current,
59
+ });
60
+ }
61
+ state = await parserFn(state);
62
+ // pseudo selectors handle their own sub selectors
63
+ if (isPostcssNodeWithChildren(state.current) &&
64
+ state.current.type !== 'pseudo') {
65
+ const node = asPostcssNodeWithChildren(state.current);
66
+ if (node.nodes.length) {
67
+ for (let i = 0; i < node.nodes.length; i++) {
68
+ const current = node.nodes[i];
69
+ /* c8 ignore next -- impossible but TS doesn't know that */
70
+ if (!current)
71
+ continue;
72
+ const childState = {
73
+ ...state,
74
+ current,
75
+ next: node.nodes[i + 1],
76
+ prev: node.nodes[i - 1],
77
+ };
78
+ state = await walk(childState);
79
+ }
80
+ }
81
+ if (isSelectorNode(node)) {
82
+ for (const edge of state.partial.edges) {
83
+ state.collect.edges.add(edge);
84
+ }
85
+ for (const node of state.partial.nodes) {
86
+ state.collect.nodes.add(node);
87
+ }
88
+ }
89
+ }
90
+ return state;
91
+ };
92
+ // A list of known security selectors that rely on
93
+ // data from the security-archive in order to work
94
+ const securitySelectors = new Set([
95
+ ':abandoned',
96
+ ':confused',
97
+ ':cve',
98
+ ':cwe',
99
+ ':debug',
100
+ ':deprecated',
101
+ ':dynamic',
102
+ ':entropic',
103
+ ':env',
104
+ ':eval',
105
+ ':fs',
106
+ ':license',
107
+ ':malware',
108
+ ':minified',
109
+ ':native',
110
+ ':network',
111
+ ':obfuscated',
112
+ ':scanned',
113
+ ':score',
114
+ ':sev',
115
+ ':severity',
116
+ ':shell',
117
+ ':shrinkwrap',
118
+ ':squat',
119
+ ':suspicious',
120
+ ':tracker',
121
+ ':trivial',
122
+ ':undesirable',
123
+ ':unknown',
124
+ ':unmaintained',
125
+ ':unpopular',
126
+ ':unstable',
127
+ ]);
128
+ const setMethodToJSON = (node) => {
129
+ const { toJSON } = node;
130
+ const insights = node.insights;
131
+ node.toJSON = () => ({
132
+ ...toJSON.call(node),
133
+ insights,
134
+ });
135
+ };
136
+ /**
137
+ * The Query class is used to search the graph for nodes and edges
138
+ * using the Dependency Selector Syntax (DSS).
139
+ */
140
+ export class Query {
141
+ #cache;
142
+ #diffFiles;
143
+ #edges;
144
+ #nodes;
145
+ #importers;
146
+ #retries;
147
+ #securityArchive;
148
+ #hostContexts;
149
+ /**
150
+ * Helper method to determine if a given query string is using any of
151
+ * the known security selectors. This is useful so that operations can
152
+ * skip hydrating the security archive if it's not needed.
153
+ */
154
+ static hasSecuritySelectors(query) {
155
+ for (const selector of securitySelectors) {
156
+ if (query.includes(selector)) {
157
+ return true;
158
+ }
159
+ }
160
+ return false;
161
+ }
162
+ /**
163
+ * Sorts an array of QueryResponse objects by specificity. Objects with
164
+ * higher idCounter values come first, if idCounter values are equal,
165
+ * then objects with higher commonCounter values come first. Otherwise,
166
+ * the original order is preserved.
167
+ */
168
+ static specificitySort(responses) {
169
+ return [...responses].sort((a, b) => {
170
+ // First compare by idCounter (higher comes first)
171
+ if (a.specificity.idCounter !== b.specificity.idCounter) {
172
+ return b.specificity.idCounter - a.specificity.idCounter;
173
+ }
174
+ // If idCounter values are equal, compare by commonCounter
175
+ if (a.specificity.commonCounter !== b.specificity.commonCounter) {
176
+ return (b.specificity.commonCounter - a.specificity.commonCounter);
177
+ }
178
+ // If both counters are equal, preserve original order
179
+ return 0;
180
+ });
181
+ }
182
+ constructor({ edges, nodes, importers, retries, securityArchive, hostContexts, diffFiles, }) {
183
+ this.#cache = new Map();
184
+ this.#diffFiles = diffFiles;
185
+ this.#edges = edges;
186
+ this.#nodes = nodes;
187
+ this.#importers = importers;
188
+ this.#retries = retries ?? 3;
189
+ this.#securityArchive = securityArchive;
190
+ this.#hostContexts = hostContexts;
191
+ }
192
+ #getQueryResponseEdges(_edges) {
193
+ return Array.from(_edges);
194
+ }
195
+ #getQueryResponseNodes(_nodes) {
196
+ const nodes = Array.from(_nodes);
197
+ for (const node of nodes) {
198
+ const securityArchiveEntry = this.#securityArchive?.get(node.id);
199
+ // if a security archive entry is not found then the insights object
200
+ // should just be empty with scanned=false
201
+ if (!securityArchiveEntry) {
202
+ node.insights = {
203
+ scanned: false,
204
+ };
205
+ setMethodToJSON(node);
206
+ continue;
207
+ }
208
+ // if a security archive entry is found then we can populate the insights
209
+ node.insights = {
210
+ scanned: true,
211
+ score: securityArchiveEntry.score,
212
+ abandoned: securityArchiveEntry.alerts.some(i => i.type === 'missingAuthor'),
213
+ confused: securityArchiveEntry.alerts.some(i => i.type === 'manifestConfusion'),
214
+ cve: securityArchiveEntry.alerts
215
+ .map(i => i.props?.cveId)
216
+ .filter(i => i !== undefined),
217
+ cwe: Array.from(new Set(securityArchiveEntry.alerts
218
+ .filter(i => i.props?.cveId)
219
+ .flatMap(i => i.props?.cwes?.map(j => j.id)))),
220
+ debug: securityArchiveEntry.alerts.some(i => i.type === 'debugAccess'),
221
+ deprecated: securityArchiveEntry.alerts.some(i => i.type === 'deprecated'),
222
+ dynamic: securityArchiveEntry.alerts.some(i => i.type === 'dynamicRequire'),
223
+ entropic: securityArchiveEntry.alerts.some(i => i.type === 'highEntropyStrings'),
224
+ env: securityArchiveEntry.alerts.some(i => i.type === 'envVars'),
225
+ eval: securityArchiveEntry.alerts.some(i => i.type === 'usesEval'),
226
+ fs: securityArchiveEntry.alerts.some(i => i.type === 'filesystemAccess'),
227
+ license: {
228
+ unlicensed: securityArchiveEntry.alerts.some(i => i.type === 'explicitlyUnlicensedItem'),
229
+ misc: securityArchiveEntry.alerts.some(i => i.type === 'miscLicenseIssues'),
230
+ restricted: securityArchiveEntry.alerts.some(i => i.type === 'nonpermissiveLicense'),
231
+ ambiguous: securityArchiveEntry.alerts.some(i => i.type === 'ambiguousClassifier'),
232
+ copyleft: securityArchiveEntry.alerts.some(i => i.type === 'copyleftLicense'),
233
+ unknown: securityArchiveEntry.alerts.some(i => i.type === 'unidentifiedLicense'),
234
+ none: securityArchiveEntry.alerts.some(i => i.type === 'noLicenseFound'),
235
+ exception: securityArchiveEntry.alerts.some(i => i.type === 'licenseException'),
236
+ },
237
+ malware: {
238
+ low: securityArchiveEntry.alerts.some(i => i.type === 'gptAnomaly'),
239
+ medium: securityArchiveEntry.alerts.some(i => i.type === 'gptSecurity'),
240
+ high: securityArchiveEntry.alerts.some(i => i.type === 'gptMalware'),
241
+ critical: securityArchiveEntry.alerts.some(i => i.type === 'malware'),
242
+ },
243
+ minified: securityArchiveEntry.alerts.some(i => i.type === 'minifiedFile'),
244
+ native: securityArchiveEntry.alerts.some(i => i.type === 'hasNativeCode'),
245
+ network: securityArchiveEntry.alerts.some(i => i.type === 'networkAccess'),
246
+ obfuscated: securityArchiveEntry.alerts.some(i => i.type === 'obfuscatedFile'),
247
+ scripts: securityArchiveEntry.alerts.some(i => i.type === 'installScripts'),
248
+ severity: {
249
+ low: securityArchiveEntry.alerts.some(i => i.type === 'mildCVE'),
250
+ medium: securityArchiveEntry.alerts.some(i => i.type === 'potentialVulnerability'),
251
+ high: securityArchiveEntry.alerts.some(i => i.type === 'cve'),
252
+ critical: securityArchiveEntry.alerts.some(i => i.type === 'criticalCVE'),
253
+ },
254
+ shell: securityArchiveEntry.alerts.some(i => i.type === 'shellAccess'),
255
+ shrinkwrap: securityArchiveEntry.alerts.some(i => i.type === 'shrinkwrap'),
256
+ squat: {
257
+ medium: securityArchiveEntry.alerts.some(i => i.type === 'gptDidYouMean'),
258
+ critical: securityArchiveEntry.alerts.some(i => i.type === 'didYouMean'),
259
+ },
260
+ suspicious: securityArchiveEntry.alerts.some(i => i.type === 'suspiciousStarActivity'),
261
+ tracker: securityArchiveEntry.alerts.some(i => i.type === 'telemetry'),
262
+ trivial: securityArchiveEntry.alerts.some(i => i.type === 'trivialPackage'),
263
+ undesirable: securityArchiveEntry.alerts.some(i => i.type === 'troll'),
264
+ unknown: securityArchiveEntry.alerts.some(i => i.type === 'newAuthor'),
265
+ unmaintained: securityArchiveEntry.alerts.some(i => i.type === 'unmaintained'),
266
+ unpopular: securityArchiveEntry.alerts.some(i => i.type === 'unpopularPackage'),
267
+ unstable: securityArchiveEntry.alerts.some(i => i.type === 'unstableOwnership'),
268
+ };
269
+ setMethodToJSON(node);
270
+ }
271
+ return nodes;
272
+ }
273
+ /**
274
+ * Search the graph for nodes and edges that match the given query.
275
+ */
276
+ async search(query, { signal, scopeIDs = [joinDepIDTuple(['file', '.'])], }) {
277
+ if (!query)
278
+ return {
279
+ edges: [],
280
+ nodes: [],
281
+ importers: [],
282
+ comment: '',
283
+ specificity: { idCounter: 0, commonCounter: 0 },
284
+ };
285
+ const cachedResult = this.#cache.get(query);
286
+ if (cachedResult) {
287
+ return cachedResult;
288
+ }
289
+ const nodes = this.#nodes;
290
+ const edges = this.#edges;
291
+ const importers = this.#importers;
292
+ // includes virtual workspace edges in the searched edges
293
+ for (const importer of importers) {
294
+ if (!importer.workspaces)
295
+ continue;
296
+ for (const edge of importer.workspaces.values()) {
297
+ edges.add(edge);
298
+ }
299
+ }
300
+ // parse the query string into AST
301
+ const current = parse(query);
302
+ // set loose mode for the entire parse in case there are multiple selectors
303
+ // so that using invalid pseudo selectors or other query language parser
304
+ // errors won't throw an error,
305
+ // e.g: `:root > *, #a, :foo` still returns results for `:root > ` and `#a`
306
+ // while :foo is ignored
307
+ const loose = asPostcssNodeWithChildren(current).nodes.length > 1;
308
+ // builds initial state and walks over it,
309
+ // retrieving the collected result
310
+ const { collect, comment, importers: stateResultImporters, specificity, } = await walk({
311
+ cancellable: async () => {
312
+ await new Promise(resolve => {
313
+ setTimeout(resolve, 0);
314
+ });
315
+ signal.throwIfAborted();
316
+ },
317
+ current,
318
+ initial: {
319
+ nodes: new Set(nodes),
320
+ edges: new Set(edges),
321
+ },
322
+ collect: {
323
+ nodes: new Set(),
324
+ edges: new Set(),
325
+ },
326
+ comment: '',
327
+ diffFiles: this.#diffFiles,
328
+ loose,
329
+ importers,
330
+ partial: { nodes, edges },
331
+ retries: this.#retries,
332
+ signal,
333
+ securityArchive: this.#securityArchive,
334
+ scopeIDs,
335
+ walk,
336
+ specificity: { idCounter: 0, commonCounter: 0 },
337
+ hostContexts: this.#hostContexts,
338
+ });
339
+ const res = {
340
+ edges: this.#getQueryResponseEdges(collect.edges),
341
+ nodes: this.#getQueryResponseNodes(collect.nodes),
342
+ importers: this.#getQueryResponseNodes(stateResultImporters),
343
+ comment,
344
+ specificity,
345
+ };
346
+ this.#cache.set(query, res);
347
+ return res;
348
+ }
349
+ /**
350
+ * Parses a query string in order to retrieve an array of tokens.
351
+ */
352
+ static getQueryTokens(query) {
353
+ if (!query)
354
+ return [];
355
+ const tokens = [];
356
+ const ast = (q) => {
357
+ try {
358
+ return parse(q);
359
+ }
360
+ catch (_e) {
361
+ return ast(q.slice(0, -1));
362
+ }
363
+ };
364
+ const processNode = (node) => {
365
+ for (const key of selectorsMap.keys()) {
366
+ if (node.type === key && node.type !== 'root') {
367
+ let token = `${node.spaces.before}${node.value}${node.spaces.after}`;
368
+ if (isIdentifierNode(node)) {
369
+ token = `${node.spaces.before}#${node.value}${node.spaces.after}`;
370
+ }
371
+ else if (isSelectorNode(node)) {
372
+ token = `${node.spaces.before},${node.spaces.after}`;
373
+ }
374
+ else if (isAttributeNode(node)) {
375
+ token = String(node.source?.start?.column &&
376
+ node.source.end?.column &&
377
+ `${node.spaces.before}${query.slice(node.source.start.column - 1, node.source.end.column)}${node.spaces.after}`);
378
+ }
379
+ if (isPostcssNodeWithChildren(node) &&
380
+ isPseudoNode(node) &&
381
+ node.nodes.length) {
382
+ token = String(token.split('(')[0]);
383
+ token += '(';
384
+ }
385
+ if (!isSelectorNode(node) ||
386
+ node.parent?.nodes.indexOf(node) !== 0) {
387
+ tokens.push({
388
+ ...node,
389
+ token,
390
+ });
391
+ }
392
+ }
393
+ }
394
+ if (isPostcssNodeWithChildren(node)) {
395
+ for (const child of node.nodes) {
396
+ processNode(child);
397
+ }
398
+ if (isPseudoNode(node) && node.nodes.length) {
399
+ tokens.push({
400
+ ...node,
401
+ token: ')' + node.spaces.after,
402
+ type: 'pseudo',
403
+ });
404
+ }
405
+ }
406
+ };
407
+ processNode(ast(query));
408
+ return tokens;
409
+ }
410
+ }
@@ -0,0 +1,14 @@
1
+ import type { Root } from 'postcss-selector-parser';
2
+ /**
3
+ * Escapes forward slashes in specific patterns matching @scoped/name paths
4
+ * This will allow usage of unescaped forward slashes necessary for scoped
5
+ * package names in the id selector.
6
+ */
7
+ export declare const escapeScopedNamesSlashes: (query: string) => string;
8
+ export declare const escapeDots: (query: string) => string;
9
+ export declare const unescapeDots: (query: string) => string;
10
+ /**
11
+ * Parses a CSS selector string into an AST
12
+ * Handles escaping of forward slashes in specific patterns
13
+ */
14
+ export declare const parse: (query: string) => Root;
package/dist/parser.js ADDED
@@ -0,0 +1,92 @@
1
+ import postcssSelectorParser from 'postcss-selector-parser';
2
+ import { asSelectorNode, isCombinatorNode, isPseudoNode, isTagNode, } from '@vltpkg/dss-parser';
3
+ /**
4
+ * Escapes forward slashes in specific patterns matching @scoped/name paths
5
+ * This will allow usage of unescaped forward slashes necessary for scoped
6
+ * package names in the id selector.
7
+ */
8
+ export const escapeScopedNamesSlashes = (query) => query.replace(/(#@(\w|-|\.)+)\//gm, (_, scope) => `${scope}\\/`);
9
+ export const escapeDots = (query) => query.replaceAll('.', '\\.');
10
+ export const unescapeDots = (query) => query.replaceAll('\\.', '.');
11
+ const pseudoCleanUpNeeded = new Set([
12
+ ':published',
13
+ ':score',
14
+ ':malware',
15
+ ':severity',
16
+ ':sev',
17
+ ':squat',
18
+ ':semver',
19
+ ':v',
20
+ ':path',
21
+ ]);
22
+ const hasParamsToEscape = (node) => pseudoCleanUpNeeded.has(node.value);
23
+ /**
24
+ * Parses a CSS selector string into an AST
25
+ * Handles escaping of forward slashes in specific patterns
26
+ */
27
+ export const parse = (query) => {
28
+ const escapedQuery = escapeDots(escapeScopedNamesSlashes(query));
29
+ const transformAst = (root) => {
30
+ root.walk((node) => {
31
+ // clean up the escaped dots
32
+ if (node.value && typeof node.value === 'string') {
33
+ node.value = unescapeDots(node.value);
34
+ }
35
+ if (isPseudoNode(node) && hasParamsToEscape(node)) {
36
+ // these are pseudo nodes that should only take strings as
37
+ // parameters, so in this preparse step we clean up anything
38
+ // that was recognized as a postcss node and transform that
39
+ // into something that can be most likely parsed as a string
40
+ for (const n of node.nodes) {
41
+ // the parameters have a selector node that wraps them up
42
+ const selector = asSelectorNode(n);
43
+ selector.nodes.forEach((currentNode, index, arr) => {
44
+ // get the next node, we'll update it later
45
+ const nextNode = arr[index + 1];
46
+ // if the current node is a combinator node, we'll need to
47
+ // escape it, we do so by removing the node entirely and
48
+ // updating the contents of the next node with its value
49
+ if (isCombinatorNode(currentNode) &&
50
+ isTagNode(nextNode)) {
51
+ nextNode.value = `${currentNode.spaces.before}${currentNode.value}${currentNode.spaces.after}${nextNode.value}`;
52
+ // make sure to also update the source position
53
+ // references, those are used by the syntax highlighter
54
+ if (nextNode.source?.start?.line &&
55
+ currentNode.source?.start?.line) {
56
+ nextNode.source.start.line =
57
+ currentNode.source.start.line;
58
+ }
59
+ if (nextNode.source?.start?.column &&
60
+ currentNode.source?.start?.column) {
61
+ nextNode.source.start.column =
62
+ currentNode.source.start.column;
63
+ }
64
+ // removes the current node from the selector node
65
+ arr.splice(index, 1);
66
+ }
67
+ });
68
+ // after removing combinator nodes, if we end up with multiple
69
+ // tags in the selector node, we need to smush them together
70
+ selector.nodes.reduce((acc, currentNode) => {
71
+ if (currentNode === acc)
72
+ return acc;
73
+ acc.value = `${acc.value}${currentNode.spaces.before}${currentNode.value}${currentNode.spaces.after}`;
74
+ // make sure to also update the source position refs
75
+ if (currentNode.source?.end?.line &&
76
+ acc.source?.end?.line) {
77
+ acc.source.end.line = currentNode.source.end.line;
78
+ }
79
+ if (currentNode.source?.end?.column &&
80
+ acc.source?.end?.column) {
81
+ acc.source.end.column = currentNode.source.end.column;
82
+ }
83
+ return acc;
84
+ }, selector.first);
85
+ // the selector wrapper node should have a single node
86
+ selector.nodes.length = 1;
87
+ }
88
+ }
89
+ });
90
+ };
91
+ return postcssSelectorParser(transformAst).astSync(escapedQuery);
92
+ };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Filters out any node that does not have a **missingAuthor** report alert.
3
+ */
4
+ export declare const abandoned: (state: import("../types.ts").ParserState) => Promise<import("../types.ts").ParserState & {
5
+ securityArchive: NonNullable<import("../types.ts").ParserState["securityArchive"]>;
6
+ }>;
@@ -0,0 +1,5 @@
1
+ import { createSecuritySelectorFilter } from "./helpers.js";
2
+ /**
3
+ * Filters out any node that does not have a **missingAuthor** report alert.
4
+ */
5
+ export const abandoned = createSecuritySelectorFilter('abandoned', 'missingAuthor');
@@ -0,0 +1,18 @@
1
+ import type { ParserState } from '../types.ts';
2
+ import type { PostcssNode } from '@vltpkg/dss-parser';
3
+ export type AttrInternals = {
4
+ attribute: string;
5
+ insensitive: boolean;
6
+ operator?: string;
7
+ value?: string;
8
+ properties: string[];
9
+ };
10
+ /**
11
+ * Parses the internal / nested selectors of a `:attr` selector.
12
+ */
13
+ export declare const parseInternals: (nodes: PostcssNode[]) => AttrInternals;
14
+ /**
15
+ * :attr Pseudo-Selector, allows for retrieving nodes based on nested
16
+ * properties of the `package.json` metadata.
17
+ */
18
+ export declare const attr: (state: ParserState) => Promise<ParserState>;
@@ -0,0 +1,57 @@
1
+ import { error } from '@vltpkg/error-cause';
2
+ import { asAttributeNode, asPostcssNodeWithChildren, asStringNode, asTagNode, isStringNode, } from '@vltpkg/dss-parser';
3
+ import { attributeSelectorsMap, filterAttributes, } from "../attribute.js";
4
+ import { removeQuotes } from "./helpers.js";
5
+ /**
6
+ * Parses the internal / nested selectors of a `:attr` selector.
7
+ */
8
+ export const parseInternals = (nodes) => {
9
+ // the last part is the attribute selector
10
+ const attributeSelector = asAttributeNode(asPostcssNodeWithChildren(nodes.pop()).nodes[0]);
11
+ // all preppending selectors are naming nested properties
12
+ const properties = [];
13
+ for (const selector of nodes) {
14
+ const selectorNode = asPostcssNodeWithChildren(selector).nodes[0];
15
+ // Handle both quoted string and tag nodes
16
+ if (isStringNode(selectorNode)) {
17
+ properties.push(removeQuotes(asStringNode(selectorNode).value));
18
+ }
19
+ else {
20
+ properties.push(asTagNode(selectorNode).value);
21
+ }
22
+ }
23
+ // include the attribute selector as the last part of the property lookup
24
+ properties.push(attributeSelector.attribute);
25
+ return {
26
+ attribute: attributeSelector.attribute,
27
+ insensitive: attributeSelector.insensitive || false,
28
+ operator: attributeSelector.operator,
29
+ value: attributeSelector.value,
30
+ properties,
31
+ };
32
+ };
33
+ /**
34
+ * :attr Pseudo-Selector, allows for retrieving nodes based on nested
35
+ * properties of the `package.json` metadata.
36
+ */
37
+ export const attr = async (state) => {
38
+ // Parses and retrieves the values for the nested selectors
39
+ let internals;
40
+ try {
41
+ internals = parseInternals(asPostcssNodeWithChildren(state.current).nodes);
42
+ }
43
+ catch (err) {
44
+ throw error('Failed to parse :attr selector', {
45
+ cause: err,
46
+ });
47
+ }
48
+ // reuses the attribute selector logic to filter the nodes
49
+ const comparator = internals.operator ?
50
+ attributeSelectorsMap.get(internals.operator)
51
+ : undefined;
52
+ const value = internals.value || '';
53
+ const propertyName = internals.attribute;
54
+ const insensitive = internals.insensitive;
55
+ const prefixProperties = internals.properties;
56
+ return filterAttributes(state, comparator, value, propertyName, insensitive, prefixProperties);
57
+ };
@@ -0,0 +1,7 @@
1
+ import type { ParserState } from '../types.ts';
2
+ /**
3
+ * :built Pseudo-Selector will only match packages that have
4
+ * a `buildState` property set to 'built', indicating they have
5
+ * been successfully built during the reify process.
6
+ */
7
+ export declare const built: (state: ParserState) => Promise<ParserState>;
@@ -0,0 +1,15 @@
1
+ import { removeNode, removeDanglingEdges } from "./helpers.js";
2
+ /**
3
+ * :built Pseudo-Selector will only match packages that have
4
+ * a `buildState` property set to 'built', indicating they have
5
+ * been successfully built during the reify process.
6
+ */
7
+ export const built = async (state) => {
8
+ for (const node of state.partial.nodes) {
9
+ if (node.buildState !== 'built') {
10
+ removeNode(state, node);
11
+ }
12
+ }
13
+ removeDanglingEdges(state);
14
+ return state;
15
+ };
@@ -0,0 +1,8 @@
1
+ import type { ParserState } from '../types.js';
2
+ /**
3
+ * Filters out any node that does not have a **manifestConfusion** report alert.
4
+ * Also includes any node that has been marked as **confused**.
5
+ */
6
+ export declare const confused: (state: ParserState) => Promise<ParserState & {
7
+ securityArchive: NonNullable<ParserState["securityArchive"]>;
8
+ }>;
@@ -0,0 +1,18 @@
1
+ import { assertSecurityArchive, removeDanglingEdges, removeNode, } from "./helpers.js";
2
+ /**
3
+ * Filters out any node that does not have a **manifestConfusion** report alert.
4
+ * Also includes any node that has been marked as **confused**.
5
+ */
6
+ export const confused = async (state) => {
7
+ assertSecurityArchive(state, 'confused');
8
+ for (const node of state.partial.nodes) {
9
+ const report = state.securityArchive.get(node.id);
10
+ const exclude = !node.confused &&
11
+ !report?.alerts.some(alert => alert.type === 'manifestConfusion');
12
+ if (exclude) {
13
+ removeNode(state, node);
14
+ }
15
+ }
16
+ removeDanglingEdges(state);
17
+ return state;
18
+ };
@@ -0,0 +1,12 @@
1
+ import type { ParserState } from '../types.ts';
2
+ import type { PostcssNode } from '@vltpkg/dss-parser';
3
+ export type CveInternals = {
4
+ cveId: string;
5
+ };
6
+ export declare const parseInternals: (nodes: PostcssNode[]) => CveInternals;
7
+ /**
8
+ * Filters out any node that does not have a CVE alert with the specified CVE ID.
9
+ */
10
+ export declare const cve: (state: ParserState) => Promise<ParserState & {
11
+ securityArchive: NonNullable<ParserState["securityArchive"]>;
12
+ }>;