@exodus/stasis 1.0.0-beta.15 → 1.0.0-beta.17

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 (67) hide show
  1. package/README.md +1 -1
  2. package/bin/stasis.js +26 -11
  3. package/package.json +10 -9
  4. package/src/audit-corrections.js +11 -42
  5. package/src/audit.js +113 -233
  6. package/src/babel.js +39 -122
  7. package/src/bundle-from-lockfile.js +9 -21
  8. package/src/bundle-reader.js +5 -9
  9. package/src/bundle.js +0 -3
  10. package/src/cmd/build.js +50 -166
  11. package/src/cmd/bundle-options.js +110 -167
  12. package/src/cmd/bundle.js +861 -1344
  13. package/src/cmd/diff.js +1 -4
  14. package/src/cmd/github-bundle.js +46 -27
  15. package/src/cmd/output.js +17 -12
  16. package/src/cmd/sbom.js +3 -8
  17. package/src/cmd/tokens.js +10 -23
  18. package/src/define.js +34 -97
  19. package/src/diff.js +72 -128
  20. package/src/esbuild.js +0 -4
  21. package/src/flow.js +10 -25
  22. package/src/installed.js +265 -0
  23. package/src/json-stream.js +4 -9
  24. package/src/loader-mock.js +3 -8
  25. package/src/loader.js +1 -5
  26. package/src/loaders/bash.js +41 -93
  27. package/src/loaders/cargo.js +298 -805
  28. package/src/loaders/foundry.js +109 -285
  29. package/src/loaders/paths.js +13 -13
  30. package/src/loaders/php.js +210 -489
  31. package/src/loaders/rust.js +507 -1430
  32. package/src/loaders/solidity-ownership.js +48 -128
  33. package/src/loaders/solidity.js +39 -122
  34. package/src/loaders/toml.js +7 -17
  35. package/src/lockfile.js +0 -3
  36. package/src/metro-assets.js +9 -33
  37. package/src/metro-resolver.js +20 -47
  38. package/src/metro-transformer.js +0 -5
  39. package/src/metro.js +0 -4
  40. package/src/mock.js +32 -80
  41. package/src/parse.js +2 -5
  42. package/src/prune.js +0 -3
  43. package/src/resolve-fields.js +90 -220
  44. package/src/resolve-node.js +27 -45
  45. package/src/resolve-typescript.js +109 -313
  46. package/src/rollup.js +0 -4
  47. package/src/run-installed.js +10 -0
  48. package/src/sbom.js +52 -128
  49. package/src/scan.js +136 -283
  50. package/src/sixel.js +18 -41
  51. package/src/table.js +14 -35
  52. package/src/validate-notes.d.ts +3 -2
  53. package/src/validate-notes.js +25 -19
  54. package/src/validate.d.ts +38 -11
  55. package/src/validate.js +478 -512
  56. package/src/vfs-bundle/entries.js +40 -57
  57. package/src/vfs-bundle/github.js +88 -121
  58. package/src/vfs-bundle/glob.js +4 -45
  59. package/src/vfs-bundle/lazy.js +27 -56
  60. package/src/vfs-bundle/prisma.js +167 -110
  61. package/src/vfs-bundle/soldeer.js +28 -49
  62. package/src/vfs-bundle/tree.js +295 -318
  63. package/src/vfs-bundle/yarnclean.js +10 -31
  64. package/src/vfs-bundle.js +23 -64
  65. package/src/webpack.js +0 -4
  66. package/src/why.js +75 -231
  67. package/src/installed-commits.js +0 -184
package/src/audit.js CHANGED
@@ -1,53 +1,44 @@
1
1
  import { packageRepo } from '@exodus/stasis-core/bundle-util'
2
- import { dependencyEcosystem, moduleFileKey, packageRecords, sameGithub } from '@exodus/stasis-core/util'
2
+ import { dependencyEcosystem, moduleFileKey, sameGithub, upsert, withoutBom } from '@exodus/stasis-core/util'
3
3
  import { advisories } from '@preventive/upstream/advisories.js'
4
4
  import { compareVersions, valid } from '@preventive/upstream/semver.js'
5
5
  import { isEvidenceFile } from './audit-corrections.js'
6
6
  import { DIM, formatTable, painter, takesColors } from './table.js'
7
-
8
- export { formatTable }
9
7
  import { parseFile, parseFileWithKind } from './parse.js'
10
8
  import { logos } from './sixel.js'
11
- import { collectWhy, invertReason } from './why.js'
9
+ import { collectWhy, invertReason, packageRecords } from './why.js'
10
+
11
+ export { formatTable }
12
12
 
13
- // Where advisories() asks for each ecosystem's advisories: npm's registry; OSV for crates (those
14
- // vendored from a registry, which OSV takes for crates.io's) and Composer packages; for Soldeer
15
- // packages and GitHub repos (Foundry's lib/ submodules), the advisories their GitHub repository
16
- // publishes, a Soldeer package's as Soldeer names it.
17
- const SOURCES = { npm: ['npm'], cargo: ['OSV'], composer: ['OSV'], soldeer: ['Soldeer', 'GitHub'], github: ['GitHub'] }
13
+ // Where advisories() asks for each ecosystem's advisories (OSV has a GitHub repo's as a GitHub Action's).
14
+ const SOURCES = { npm: ['npm'], cargo: ['OSV'], composer: ['OSV', 'Packagist'], soldeer: ['Soldeer', 'GitHub'], github: ['OSV', 'GitHub'] }
18
15
 
19
- // Why a vendored crate that may not be crates.io's is asked nowhere: by its name, crates.io's
20
- // advisories would be another crate's, and a private git dependency's name must not reach a public
21
- // database at all.
16
+ // Asked nowhere: by its name, crates.io's advisories would be another crate's, and a private git dependency's name
17
+ // must not reach a public database at all.
22
18
  const UNREGISTERED_CRATES = {
23
19
  'cargo-git': 'a crate vendored from a git repository, not crates.io',
24
20
  'cargo-unknown': 'a vendored crate with no .cargo-checksum.json, which may be a git checkout, not crates.io\'s',
25
21
  }
26
22
 
27
- // A dependency's version as it is audited: a GitHub repo's `.gitmodules` branch `.`, git's for the
28
- // superproject's own branch, which the bundle does not know and advisories() takes for no branch, is
29
- // the 0.0.0 stasis versions a repo with no version of its own by. Every advisory range covers both,
30
- // as it does a branch name.
23
+ // A GitHub repo's `.gitmodules` branch `.` (git's for the superproject's own, which the bundle doesn't
24
+ // know) is audited as 0.0.0, the version stasis gives a repo with none; every advisory range covers both.
31
25
  const versionOf = (ecosystem, version) => (ecosystem === 'github' && version === '.' ? '0.0.0' : version)
32
26
 
33
- // A package's key across the audit: `name@version` for npm, as collectWhy keys its chains, and
34
- // prefixed by the ecosystem for the others, whose names may be npm's too.
35
- const keyOf = (ecosystem, name, version) => `${ecosystem === 'npm' ? '' : `${ecosystem}:`}${name}@${version}`
27
+ // `name@version` for npm, as collectWhy keys its chains; the others' prefixed, as their names may be npm's too.
28
+ const keyOf = ({ ecosystem, name }, version) => `${ecosystem === 'npm' ? '' : `${ecosystem}:`}${name}@${version}`
36
29
 
37
- // A package's key as collectPackages and collectReasons tell packages apart: keyOf it, but a copy a dependency
38
- // ships with no version, which is one package for each host, by its host's `name@version` too.
39
- const packageKey = (ecosystem, name, version, host) => (version === undefined ? `${ecosystem}:${name} in ${host}` : keyOf(ecosystem, name, version))
30
+ // A versionless copy a dependency ships is one package per host.
31
+ const packageKey = (pkg, host) => (pkg.version === undefined ? `${pkg.ecosystem}:${pkg.name} in ${host}` : keyOf(pkg, pkg.version))
40
32
 
41
- // A refused audit, as a usage error: one for the message alone, never a stack.
33
+ // A usage error: its message alone, never a stack.
42
34
  export class AuditError extends Error {
43
35
  name = 'AuditError'
44
36
  }
45
37
 
46
- // Composer's dev versions (`dev-main`, `1.x-dev`), as Composer tells them, a `#ref` dropped: no
47
- // advisory database lists those, and advisories() takes releases alone.
38
+ // `dev-main`, `1.x-dev` (a `#ref` dropped), which no advisory database lists.
48
39
  const isComposerDev = (version) => /^dev-|-dev$/iu.test(version.replace(/#.*$/su, ''))
49
40
 
50
- // Why `pkg` cannot be audited, or undefined where it can.
41
+ // Why `pkg` cannot be audited, or undefined.
51
42
  function unaudited({ ecosystem, version, vendoredIn }) {
52
43
  if (version === undefined) return `a copy vendored in ${vendoredIn.join(', ')} whose package.json gives no version, so no advisory can be matched to it`
53
44
  if (Object.hasOwn(UNREGISTERED_CRATES, ecosystem)) return UNREGISTERED_CRATES[ecosystem]
@@ -56,84 +47,62 @@ function unaudited({ ecosystem, version, vendoredIn }) {
56
47
  return undefined
57
48
  }
58
49
 
59
- // Only audit installed dependencies, `{ ecosystem, name, version }`: an npm package, a vendored
60
- // crate, a Composer package, a Soldeer package or a GitHub repo, as the bundle tags it (a crate
61
- // vendored from git, or from where no `.cargo-checksum.json` tells, is listed but never asked about).
62
- //
63
- // A package counts as present only when its REAL code is, and recorded = present:
64
- // an artifact records exactly the files it ships or attested -- imported, entry,
65
- // or `add`-ed (the last two have no in-edge in the resolution graph, so presence
66
- // must NOT be derived from edges). The package is audited only if some recorded
67
- // file is code evidence (see audit-corrections.js) -- not a corrected file (ws's
68
- // noop browser.js stub), not a manifest (package.json, recorded for resolver /
69
- // metadata reads; Cargo.toml). So a ws shipped only as browser.js (+ manifest) is
70
- // skipped, while one whose real code was bundled stays. Which consumers and import
71
- // EDGES reach that code is the reason column's concern -- collectReasons and
72
- // why.js apply the same evidence rule per file/edge there.
73
- //
74
- // A package's `github` is the repository its artifact records for it (its package.json's), where
75
- // one does; else, for an npm package a bundle carries the package.json of (one built before builds
76
- // recorded `repo`), the one that names, read by the rule builds record it by. A GitHub repo is its
77
- // own, and has none.
78
- //
79
- // A copy of a package a dependency ships (its record's `vendored`, as packageRecords splits it out) is
80
- // audited too, as the package it is, by the same evidence rule over the files it holds, with
81
- // `vendoredIn` its host's `name@version`; its host, all of whose files the copy's are, as before. A copy
82
- // whose package.json gives no version is listed with none, as not audited.
50
+ // The dependencies whose real code `file` carries, `{ ecosystem, name, version, github }` (the repository recorded,
51
+ // else a bundled package.json's), or for a copy a dependency ships `{ ecosystem, name, version, vendoredIn }`.
83
52
  export function collectPackagesFromFile(file) {
84
53
  const out = []
85
54
  const { kind, artifact } = parseFileWithKind(file)
86
- for (const [dir, { name, version, ecosystem: tagged, repo, files, vendoredIn }] of packageRecords(artifact.modules)) {
87
- const ecosystem = dependencyEcosystem(dir, tagged)
88
- // First-party code is never sent to a public registry (leaks names, adds noise).
89
- if (ecosystem === undefined) continue
90
- if (!name || (!version && vendoredIn === undefined)) continue
91
- if (!Object.keys(files).some((rel) => isEvidenceFile(name, version ?? null, rel, ecosystem))) continue
92
- if (vendoredIn !== undefined) {
93
- const host = artifact.modules.get(vendoredIn)
94
- out.push({ ecosystem, name, version: version === undefined ? undefined : versionOf(ecosystem, version), vendoredIn: `${host.name}@${host.version}` })
55
+ for (const { name, version, ecosystem, repo, files, host, isEvidence } of auditedRecords(artifact.modules)) {
56
+ if (!Object.keys(files).some(isEvidence)) continue
57
+ if (host !== undefined) {
58
+ out.push({ ecosystem, name, version, vendoredIn: host })
95
59
  continue
96
60
  }
97
61
  let github = ecosystem === 'github' ? undefined : repo?.github
98
62
  if (github === undefined && ecosystem === 'npm' && kind === 'bundle') github = manifestRepo(files['package.json'])
99
- out.push({ ecosystem, name, version: versionOf(ecosystem, version), ...(github === undefined ? {} : { github }) })
63
+ out.push({ ecosystem, name, version, ...(github === undefined ? {} : { github }) })
100
64
  }
101
65
  return out
102
66
  }
103
67
 
104
- // The GitHub repository a bundled package.json's text names (packageRepo), or undefined where it
105
- // names none or doesn't parse. Read as Node reads one, past a byte order mark (packageJSONText),
106
- // which a bundle keeps in the text. A lockfile holds the file's digest alone, never asked here.
68
+ // Each dependency of `modules` the audit may count: first-party code never reaches a public registry.
69
+ function* auditedRecords(modules) {
70
+ for (const [dir, { name, version, ecosystem: tagged, repo, files, vendoredIn }] of packageRecords(modules)) {
71
+ const ecosystem = dependencyEcosystem(dir, tagged)
72
+ if (ecosystem === undefined || !name || (!version && vendoredIn === undefined)) continue
73
+ const host = vendoredIn === undefined ? undefined : modules.get(vendoredIn)
74
+ yield {
75
+ dir, name, ecosystem, repo, files,
76
+ version: version === undefined ? undefined : versionOf(ecosystem, version),
77
+ host: host === undefined ? undefined : `${host.name}@${host.version}`,
78
+ isEvidence: (rel) => isEvidenceFile(name, version ?? null, rel, ecosystem),
79
+ }
80
+ }
81
+ }
82
+
83
+ // Past a byte order mark, as Node reads one, which a bundle keeps in the text.
107
84
  function manifestRepo(text) {
108
85
  if (typeof text !== 'string') return undefined
109
86
  try {
110
- return packageRepo(JSON.parse(text.replace(/^\uFEFF/u, '')))?.github
87
+ return packageRepo(JSON.parse(withoutBom(text)))?.github
111
88
  } catch {
112
89
  return undefined
113
90
  }
114
91
  }
115
92
 
116
- // Versions in semver's order where both are semver's, else as numbers in text compare.
117
93
  const byNumbers = new Intl.Collator('en', { numeric: true }).compare
118
94
  const byVersion = (a, b) => (valid(a) && valid(b) ? compareVersions(a, b) : byNumbers(a, b))
119
95
 
120
- // Each package once, with the GitHub repository its artifacts record for its name, which
121
- // advisories() asks instead of looking one up. It takes one repository a name, so a name recorded
122
- // with two, by two of its versions (a package that moved) or by two artifacts, has none, and is
123
- // looked up as before. One some dependency ships a copy of carries `vendoredIn`, the hosts' sorted
124
- // `name@version`s, and `vendoredOnly`, whether that copy is all there is of it; a copy without a version
125
- // is one package per host.
96
+ // Each package once, with the GitHub repository recorded for its name (none where two are), which advisories() asks
97
+ // rather than look one up; a vendored one with `vendoredIn`, its hosts, and `vendoredOnly`, whether that's all of it.
126
98
  export function collectPackages(files) {
127
- const byKey = new Map()
128
- const vendored = new Map() // key -> { hosts, only }
99
+ const byKey = new Map() // packageKey -> { pkg, hosts, only }
129
100
  const repos = new Map() // `ecosystem:name` -> the one repository recorded for it, or null
130
101
  for (const file of files) {
131
102
  for (const { github, vendoredIn, ...pkg } of collectPackagesFromFile(file)) {
132
- const key = packageKey(pkg.ecosystem, pkg.name, pkg.version, vendoredIn)
133
- if (!byKey.has(key)) byKey.set(key, pkg)
134
- if (!vendored.has(key)) vendored.set(key, { hosts: new Set(), only: true })
135
- if (vendoredIn === undefined) vendored.get(key).only = false
136
- else vendored.get(key).hosts.add(vendoredIn)
103
+ const entry = upsert(byKey, packageKey(pkg, vendoredIn), () => ({ pkg, hosts: new Set(), only: true }))
104
+ if (vendoredIn === undefined) entry.only = false
105
+ else entry.hosts.add(vendoredIn)
137
106
  if (github === undefined) continue
138
107
  const name = `${pkg.ecosystem}:${pkg.name}`
139
108
  const known = repos.get(name)
@@ -142,29 +111,21 @@ export function collectPackages(files) {
142
111
  else if (known !== null && !sameGithub(known, github)) repos.set(name, null)
143
112
  }
144
113
  }
145
- return [...byKey]
146
- .map(([key, pkg]) => {
114
+ return [...byKey.values()]
115
+ .map(({ pkg, hosts, only }) => {
147
116
  const github = repos.get(`${pkg.ecosystem}:${pkg.name}`)
148
- const { hosts, only } = vendored.get(key)
149
117
  return { ...pkg, ...(github ? { github } : {}), ...(hosts.size === 0 ? {} : { vendoredIn: [...hosts].toSorted(), vendoredOnly: only }) }
150
118
  })
151
119
  .toSorted((a, b) => a.ecosystem.localeCompare(b.ecosystem) || a.name.localeCompare(b.name) || byVersion(a.version, b.version))
152
120
  }
153
121
 
154
- // Map each audited package (keyOf it) to the bundle consumers ("reasons") that recorded its files.
155
- // Only bundles carry a reason map ({ consumer: [file, ...] }); lockfiles omit it. Each file is
156
- // resolved back to its owning package via the module file listing. Only evidence files attribute
157
- // (see audit-corrections.js): a consumer that recorded nothing of a package but a corrected file or
158
- // its manifest carries none of its real code and is NOT a reason -- e.g. webpack shipping only ws's
159
- // noop browser.js stub, while `run` bundles the real ws.
160
- // A file of a copy a dependency ships (packageRecords) attributes to both: the copy and its host; a copy
161
- // with no version by packageKey.
122
+ // Each audited package (packageKey) -> the bundle consumers that recorded an evidence file of it: webpack shipping
123
+ // only ws's noop browser.js stub is no reason for ws.
162
124
  export function collectReasons(files) {
163
125
  return reasonIndex(files).byPkg
164
126
  }
165
127
 
166
- // collectReasons' map (`byPkg`), and every consumer the files' reason maps record (`consumers`), a consumer
167
- // that recorded first-party code alone too: what --reason may name.
128
+ // `consumers`, what --reason may name, holds one that recorded first-party code alone too.
168
129
  function reasonIndex(files) {
169
130
  const byPkg = new Map()
170
131
  const consumers = new Set()
@@ -174,22 +135,15 @@ function reasonIndex(files) {
174
135
  if (!fileReasons) continue
175
136
  for (const [consumer, list] of Object.entries(artifact.reason)) if (Array.isArray(list)) consumers.add(consumer)
176
137
  const fileToPkgs = new Map()
177
- for (const [dir, { name, version, ecosystem: tagged, files: modFiles, vendoredIn }] of packageRecords(artifact.modules)) {
178
- const ecosystem = dependencyEcosystem(dir, tagged)
179
- if (ecosystem === undefined || !name || (!version && vendoredIn === undefined)) continue
180
- const host = vendoredIn === undefined ? undefined : artifact.modules.get(vendoredIn)
181
- const key = packageKey(ecosystem, name, version === undefined ? undefined : versionOf(ecosystem, version), host && `${host.name}@${host.version}`)
182
- for (const rel of Object.keys(modFiles)) {
183
- if (!isEvidenceFile(name, version ?? null, rel, ecosystem)) continue
184
- const f = moduleFileKey(dir, rel)
185
- if (!fileToPkgs.has(f)) fileToPkgs.set(f, [])
186
- fileToPkgs.get(f).push(key)
138
+ for (const record of auditedRecords(artifact.modules)) {
139
+ const key = packageKey(record, record.host)
140
+ for (const rel of Object.keys(record.files)) {
141
+ if (record.isEvidence(rel)) upsert(fileToPkgs, moduleFileKey(record.dir, rel), () => []).push(key)
187
142
  }
188
143
  }
189
144
  for (const [f, recorded] of fileReasons) {
190
145
  for (const key of fileToPkgs.get(f) ?? []) {
191
- let set = byPkg.get(key)
192
- if (set === undefined) byPkg.set(key, (set = new Set()))
146
+ const set = upsert(byPkg, key, () => new Set())
193
147
  for (const consumer of recorded) set.add(consumer)
194
148
  }
195
149
  }
@@ -197,22 +151,14 @@ function reasonIndex(files) {
197
151
  return { byPkg, consumers }
198
152
  }
199
153
 
200
- // The consumer `stasis bundle --metro` records a Metro build's files under, and the one Metro's own
201
- // plugin records a build Metro ran under.
202
154
  const BUNDLE_METRO = 'bundle:metro'
203
155
  const METRO = 'metro'
204
156
 
205
- // How audit names the consumers the files record (`consumers`, reasonIndex's): each as it is, but
206
- // `bundle:metro` as `metro` where none records `metro`, which it then can't be taken for.
207
- // -> consumer -> its name
208
- function consumerNames(consumers) {
209
- if (!consumers.has(BUNDLE_METRO) || consumers.has(METRO)) return (consumer) => consumer
210
- return (consumer) => (consumer === BUNDLE_METRO ? METRO : consumer)
211
- }
212
-
213
- // reasonIndex's `byPkg` and `consumers` with each consumer as audit names it (consumerNames), and `name`, that naming.
157
+ // `bundle:metro` named `metro` where none records `metro`, so it can't be taken for one.
214
158
  function namedIndex({ byPkg, consumers }) {
215
- const name = consumerNames(consumers)
159
+ const name = consumers.has(BUNDLE_METRO) && !consumers.has(METRO)
160
+ ? (consumer) => (consumer === BUNDLE_METRO ? METRO : consumer)
161
+ : (consumer) => consumer
216
162
  return {
217
163
  byPkg: new Map([...byPkg].map(([key, set]) => [key, new Set([...set].map(name))])),
218
164
  consumers: new Set([...consumers].map(name)),
@@ -220,63 +166,46 @@ function namedIndex({ byPkg, consumers }) {
220
166
  }
221
167
  }
222
168
 
223
- // `malware` first: a package that is malware is worse than any vulnerability in one. An advisory
224
- // advisories() flags as malware (`malware: true`) is rated so, whatever its source rates it.
169
+ // A package that is malware is worse than any vulnerability in one.
225
170
  const SEVERITY_ORDER = { malware: 0, critical: 1, high: 2, moderate: 3, low: 4, info: 5, none: 6 }
226
171
 
227
- // Consumer display order: bundler plugins (metro, webpack, esbuild, ...) first,
228
- // then `run`, then `add`, then `extra` (stasis.config.json's). Ties broken alphabetically.
172
+ // Bundler plugins first, ties alphabetically.
229
173
  const CONSUMER_RANK = { run: 1, add: 2, extra: 3 }
230
174
  const consumerRank = (c) => CONSUMER_RANK[c] ?? 0
231
175
  const byConsumerOrder = (a, b) => consumerRank(a) - consumerRank(b) || a.localeCompare(b)
232
176
 
233
- // Build a row's `reason` cell by unioning over exactly the affected versions it
234
- // covers. Without `--why` that's the `, `-joined set of bundle consumers; with
235
- // `--why` it's the newline-joined `consumer: a -> b -> c` import-path lines
236
- // (which REPLACE the consumer list). Consumers are ordered plugins -> run -> add -> extra.
237
- // `reasonFilter`, when set, narrows the cell to a single consumer: the `--why`
238
- // chains are already filtered upstream (see collectWhy), so only the consumer
239
- // list needs pruning here. collectWhy follows npm's import graph alone, so an
240
- // advisory of another ecosystem keeps its consumer list under `--why` too.
241
- function reasonCell({ ecosystem, name, versions: affected }, reasonsByPkg, whyByPkg, reasonFilter) {
242
- const chains = whyByPkg !== null && ecosystem === 'npm'
243
- const parts = new Set()
177
+ // The `, `-joined consumers, or under --why the `consumer: a -> b -> c` lines by consumer. collectWhy follows npm's
178
+ // import graph alone (filtering by `reasonFilter` itself), so another ecosystem's advisory keeps its consumers.
179
+ function reasonCell(adv, reasonsByPkg, whyByPkg, reasonFilter) {
180
+ const chains = whyByPkg !== null && adv.ecosystem === 'npm'
244
181
  const source = chains ? whyByPkg : reasonsByPkg
245
- for (const v of affected) {
246
- for (const p of source.get(keyOf(ecosystem, name, v)) ?? []) parts.add(p)
247
- }
248
- // --why: group `consumer: path` lines by consumer and order the groups
249
- // plugins -> run -> add -> extra (this also re-unites a consumer's lines when they were
250
- // split across affected versions); within a group collectWhy's compressed order
251
- // is preserved. Otherwise: the `, `-joined consumer set in the same order.
182
+ const parts = new Set(adv.versions.flatMap((v) => [...(source.get(keyOf(adv, v)) ?? [])]))
252
183
  if (chains) {
184
+ // Grouping re-unites a consumer's lines split across versions; within a group collectWhy's order stands.
253
185
  const byConsumer = Map.groupBy(parts, (line) => {
254
186
  const i = line.indexOf(': ')
255
187
  return i === -1 ? '' : line.slice(0, i)
256
188
  })
257
189
  return [...byConsumer.keys()].toSorted(byConsumerOrder).flatMap((c) => byConsumer.get(c)).join('\n')
258
190
  }
259
- const consumers = reasonFilter ? [...parts].filter((c) => c === reasonFilter) : [...parts]
260
- return consumers.toSorted(byConsumerOrder).join(', ')
191
+ return [...parts].filter((c) => !reasonFilter || c === reasonFilter).toSorted(byConsumerOrder).join(', ')
261
192
  }
262
193
 
263
- // An installed version as the table shows it: with the dependencies that ship a copy of it, where one does
264
- // (`vendoredByKey`, collectPackages' packages by keyOf).
265
- function installedCell({ ecosystem, name }, version, vendoredByKey) {
266
- const pkg = vendoredByKey.get(keyOf(ecosystem, name, version))
194
+ function installedCell(adv, version, vendoredByKey) {
195
+ const pkg = vendoredByKey.get(keyOf(adv, version))
267
196
  if (pkg?.vendoredIn === undefined) return version
268
197
  return `${version} (${pkg.vendoredOnly ? '' : 'also '}vendored in ${pkg.vendoredIn.join(', ')})`
269
198
  }
270
199
 
271
- // `found` is what @preventive/upstream's advisories() answers: one row per advisory range, whose
272
- // `versions` are the audited versions it covers (never empty -- a range covering none is dropped
273
- // there, so the table and exit code reflect only real hits).
200
+ const severityRank = (row) => SEVERITY_ORDER[row.severity] ?? 99
201
+ const byText = (a, b) => (a === b ? 0 : a < b ? -1 : 1)
202
+
203
+ // `found` is advisories()' answer: one row per advisory range, its `versions` the audited ones it covers.
274
204
  export function flattenAdvisories(found, reasonsByPkg = new Map(), whyByPkg = null, reasonFilter = null, vendoredByKey = new Map()) {
275
205
  const rows = []
276
206
  for (const adv of found) {
277
207
  const reason = reasonCell(adv, reasonsByPkg, whyByPkg, reasonFilter)
278
- // --reason keeps only advisories tied to that consumer: once the cell is
279
- // narrowed to it, an empty cell means this package isn't related to it.
208
+ // Under --reason, an empty cell is an advisory unrelated to that consumer.
280
209
  if (reasonFilter && reason === '') continue
281
210
  rows.push({
282
211
  ecosystem: adv.ecosystem,
@@ -290,41 +219,15 @@ export function flattenAdvisories(found, reasonsByPkg = new Map(), whyByPkg = nu
290
219
  reason,
291
220
  })
292
221
  }
293
- rows.sort((a, b) => {
294
- const sa = SEVERITY_ORDER[a.severity] ?? 99
295
- const sb = SEVERITY_ORDER[b.severity] ?? 99
296
- if (sa !== sb) return sa - sb
297
- if (a.package !== b.package) return a.package < b.package ? -1 : 1
298
- if (a.ecosystem !== b.ecosystem) return a.ecosystem < b.ecosystem ? -1 : 1
299
- return a.title < b.title ? -1 : 1
300
- })
301
- return rows
222
+ return rows.toSorted((a, b) => severityRank(a) - severityRank(b) || byText(a.package, b.package) || byText(a.ecosystem, b.ecosystem) || (a.title < b.title ? -1 : 1))
302
223
  }
303
224
 
304
- // `files` are the lockfiles' and bundles' paths, or a Bundle or Lockfile in place of one (one built in
305
- // memory, as `stasis audit` with no file builds stasis.config.json's entries' bundle), taken as the
306
- // file would read.
307
- // `repoAdvisories` and `github` (a @preventive/upstream/github.js client) go to advisories() as they
308
- // are; a Soldeer package or a GitHub repo needs the client, their repository being their only source.
309
- // A package that cannot be audited is `skipped`, with why (`because`), and asked for nothing.
310
- // `reason` (--reason) audits only the packages that consumer recorded (collectReasons): no other is
311
- // counted, listed, or asked about as they are. One no file records is refused (AuditError) before anything
312
- // is, naming those they do; a lockfile records none. `osv` asks OSV about npm packages too, those whose
313
- // name npm's registry serves to anyone; advisories() never sends it a private one.
314
- // `npmMalware` is what npm's registry tells of malware in any npm dependency, whatever `reason`: rows as
315
- // `rows` are, each package's consumers its reason. Under --reason, the npm packages its consumer didn't
316
- // record are asked of npm's registry alone for it (fast, where OSV and their repositories aren't), as are
317
- // those `lockfile` (`{ path, packages }`, validate.js installedNpmPackages', a package manager's) locks that
318
- // the files don't, deduped with the files' before anything is asked; such a package's reason its file name.
319
- // Consumers are named as consumerNames names them, across all the files: `bundle:metro` as `metro`
320
- // where none records `metro`, in the rows, the chains and the `reason` returned, and `reason` may
321
- // name it either way.
225
+ // The advisories of `files`' packages (paths, or artifacts in memory); `reason` audits only what that consumer
226
+ // recorded. `npmMalware`: npm's malware advisories of every npm dependency, whatever `reason`, and `lockfile`'s too.
322
227
  export async function audit(files, { why = false, whyDeep = false, whyFull = false, reason = null, repoAdvisories = false, github, osv = false, lockfile = null } = {}) {
323
- // --why-deep and --why-full imply --why. Deep keeps every chain instead of the
324
- // default pruning of chains whose full suffix is already a chain (see
325
- // collectWhy/dropSuffixed); full spells chains out with no `...` collapse.
228
+ // --why-deep (keep every chain, see dropSuffixed) and --why-full (no `...` collapse) imply --why.
326
229
  why = why || whyDeep || whyFull
327
- let index // namedIndex(reasonIndex(files)), once it is needed
230
+ let index
328
231
  const indexed = () => (index ??= namedIndex(reasonIndex(files)))
329
232
  if (reason) {
330
233
  const { consumers, name } = indexed()
@@ -337,77 +240,66 @@ export async function audit(files, { why = false, whyDeep = false, whyFull = fal
337
240
  reason = name(reason)
338
241
  }
339
242
  const all = collectPackages(files)
340
- const packages = reason ? all.filter((pkg) => indexed().byPkg.get(packageKey(pkg.ecosystem, pkg.name, pkg.version, pkg.vendoredIn?.[0]))?.has(reason)) : all
243
+ const packages = reason ? all.filter((pkg) => indexed().byPkg.get(packageKey(pkg, pkg.vendoredIn?.[0]))?.has(reason)) : all
341
244
  const skipped = packages.flatMap((pkg) => {
342
245
  const because = unaudited(pkg)
343
246
  return because === undefined ? [] : [{ ...pkg, because }]
344
247
  })
345
248
  const asked = packages.filter((pkg) => unaudited(pkg) === undefined)
346
- // Under --reason, the npm packages its consumer didn't record, and those `lockfile` locks alone, for npm's
347
- // malware advisories, each once, by keyOf: none asked about already.
249
+ // The npm packages --reason's consumer didn't record, and those `lockfile` locks alone, each once.
250
+ const keyOfPkg = (pkg) => keyOf(pkg, pkg.version)
348
251
  const own = new Set(packages)
349
- const keyed = (pkg) => [keyOf(pkg.ecosystem, pkg.name, pkg.version), pkg]
350
- const byKey = new Map((reason ? all.filter((pkg) => !own.has(pkg) && pkg.ecosystem === 'npm' && unaudited(pkg) === undefined) : []).map(keyed))
351
- const askedKeys = new Set(asked.map((pkg) => keyed(pkg)[0]))
352
- const lockedOnly = new Set()
353
- for (const { name, version } of lockfile?.packages ?? []) {
354
- const [key, pkg] = keyed({ ecosystem: 'npm', name, version })
355
- if (askedKeys.has(key) || byKey.has(key)) continue
356
- byKey.set(key, pkg)
357
- lockedOnly.add(key)
252
+ const byKey = new Map(all.filter((pkg) => !own.has(pkg) && pkg.ecosystem === 'npm' && unaudited(pkg) === undefined).map((pkg) => [keyOfPkg(pkg), pkg]))
253
+ const askedKeys = new Set(asked.map(keyOfPkg))
254
+ const lockedOnly = new Map() // keyOf -> the names of the lockfiles alone locking it
255
+ for (const one of lockfile === null ? [] : [lockfile].flat()) {
256
+ const named = one.name ?? one.path.split(/[/\\]/u).at(-1)
257
+ for (const { name, version } of one.packages) {
258
+ const pkg = { ecosystem: 'npm', name, version }
259
+ const key = keyOfPkg(pkg)
260
+ if (askedKeys.has(key) || (byKey.has(key) && !lockedOnly.has(key))) continue
261
+ byKey.set(key, pkg)
262
+ lockedOnly.set(key, (lockedOnly.get(key) ?? new Set()).add(named))
263
+ }
358
264
  }
359
265
  const others = [...byKey.values()]
360
266
  if (asked.length === 0 && others.length === 0) {
361
267
  return { packages, skipped, advisories: [], rows: [], npmMalware: [], why, whyDeep, whyFull, reason }
362
268
  }
363
269
  const query = (list) => list.map(({ version, vendoredIn: _in, vendoredOnly: _only, ...pkg }) => ({ ...pkg, versions: [version] }))
364
- let result
365
- let elsewhere
270
+ let result, elsewhere
366
271
  try {
367
272
  ;[result, elsewhere] = await Promise.all([
368
273
  asked.length > 0 ? advisories(query(asked), { repoAdvisories, github, osv }) : [],
369
274
  others.length > 0 ? advisories(query(others)) : [],
370
275
  ])
371
276
  } catch (cause) {
372
- // Refused input or a malformed answer is an assertion that says so itself; a transport or
373
- // HTTP failure gets the context of which request it was.
277
+ // Refused input or a malformed answer is an assertion that says so itself.
374
278
  if (cause?.code === 'ERR_ASSERTION') throw cause
375
279
  const sources = new Set([...asked.flatMap((pkg) => SOURCES[pkg.ecosystem]), ...(others.length > 0 ? ['npm'] : []), ...(osv && asked.some((pkg) => pkg.ecosystem === 'npm') ? ['OSV'] : []), ...(repoAdvisories ? ['GitHub'] : [])])
376
280
  throw new Error(`${[...sources].join('/')} advisories request failed: ${cause.message}`, { cause })
377
281
  }
378
- const vendoredOf = (list) => new Map(list.filter((pkg) => pkg.vendoredIn !== undefined).map((pkg) => [keyOf(pkg.ecosystem, pkg.name, pkg.version), pkg]))
379
- // npm's registry's malware advisories (`registry` rows, not OSV's records), of every npm dependency.
282
+ const vendoredOf = (list) => new Map(list.filter((pkg) => pkg.vendoredIn !== undefined).map((pkg) => [keyOfPkg(pkg), pkg]))
283
+ // The registry's, not OSV's records, by their consumers, or the names of the lockfiles alone locking them.
380
284
  const malware = [...result, ...elsewhere].filter((adv) => adv.malware && adv.source === 'registry')
381
- // Their consumers, and the lockfile's name for those it alone locks.
382
- const malwareReasons = () => (lockedOnly.size === 0 ? indexed().byPkg : new Map([...indexed().byPkg, ...[...lockedOnly].map((key) => [key, new Set([lockfile.path.split(/[/\\]/u).at(-1)])])]))
383
- const npmMalware = malware.length === 0 ? [] : flattenAdvisories(malware, malwareReasons(), null, null, vendoredOf([...asked, ...others]))
384
- // `--why` REPLACES the consumer list with per-consumer import paths for npm's
385
- // advisories, the only ones collectWhy has paths for: another ecosystem's keep
386
- // their consumers. Restrict the (potentially expensive) path search to the
387
- // packages that actually carry an advisory; its consumers are named as the
388
- // cells' are (consumerNames), which takes every file's consumers to know.
389
- // `reason` (--reason) narrows both the paths (in collectWhy) and the consumer
390
- // list (in flattenAdvisories) to a single consumer, dropping advisories
391
- // unrelated to it.
285
+ const npmMalware = malware.length === 0 ? [] : flattenAdvisories(malware, new Map([...indexed().byPkg, ...lockedOnly]), null, null, vendoredOf([...asked, ...others]))
286
+ // --why's chains REPLACE the consumers of npm's advisories.
392
287
  let whyByPkg = null
393
288
  if (why) {
394
- const targetKeys = new Set(result.filter((adv) => adv.ecosystem === 'npm').flatMap((adv) => adv.versions.map((v) => keyOf(adv.ecosystem, adv.name, v))))
289
+ const targetKeys = new Set(result.filter((adv) => adv.ecosystem === 'npm').flatMap((adv) => adv.versions.map((v) => keyOf(adv, v))))
395
290
  whyByPkg = collectWhy(files, targetKeys, reason, { deep: whyDeep, full: whyFull, consumerName: indexed().name })
396
291
  }
397
292
  const rows = flattenAdvisories(result, indexed().byPkg, whyByPkg, reason, vendoredOf(asked))
398
293
  return { packages, skipped, advisories: result, rows, npmMalware, why, whyDeep, whyFull, reason }
399
294
  }
400
295
 
401
- // Each severity's colour, the worst the loudest (malware white on red); an advisory without one (RustSec's)
402
- // `unrated`, dimmed.
296
+ // An advisory without a severity (RustSec's) is `unrated`.
403
297
  const SEVERITY_TONES = { malware: ['bold', 'white', 'bgRed'], critical: ['bold', 'magenta'], high: ['bold', 'red'], moderate: 'yellow', low: 'cyan', info: DIM, none: DIM, unrated: DIM }
404
298
  const severityOf = (row) => row.severity || 'unrated'
405
299
 
406
- // The columns whose text wraps where the table is wider than the terminal: the long ones, free text first.
407
300
  const WRAPPED = ['title', 'reason', 'vulnerable', 'installed']
408
301
 
409
- // `5 alerts (1 critical, 2 high, 1 low, 1 unrated)`: the rows, then each severity present, in the rows'
410
- // own order (most severe first), so the parts add up; each in its colour, and none in green.
302
+ // `5 alerts (1 critical, 2 high, 1 low, 1 unrated)`, in the rows' own order, so the parts add up.
411
303
  function alertStats(rows, paint, colored) {
412
304
  const count = `${rows.length} alert${rows.length === 1 ? '' : 's'}`
413
305
  if (rows.length === 0) return paint(['bold', 'green'], count)
@@ -415,13 +307,12 @@ function alertStats(rows, paint, colored) {
415
307
  return `${paint('bold', count)} (${[...bySeverity].map(([severity, list]) => paint(SEVERITY_TONES[severity], badge(severity, `${list.length} ${severity}`, colored))).join(', ')})`
416
308
  }
417
309
 
418
- // Malware as a badge: its colour a space wider on either side, where there is colour.
310
+ // Malware's colour a space wider on either side, where there is colour.
419
311
  const badge = (severity, text, colored) => (severity === 'malware' && colored ? ` ${text} ` : text)
420
312
 
421
- // Where an advisory comes from (upstream's `source`), as sixel.js draws it: npm's registry, a GitHub
422
- // repository's advisories, or OSV.
313
+ // Packagist's, standing for an advisory only where OSV has no record of it, has no icon.
423
314
  const SOURCE_LOGOS = { registry: 'npm', repository: 'github', osv: 'osv' }
424
- // The cells an icon covers: two, as sixel.js logos() picks icons no wider than two of the terminal's cells.
315
+ // As sixel.js logos() picks icons no wider than two of the terminal's cells.
425
316
  const ICON_CELLS = 2
426
317
  // An icon, drawn with the cursor saved and put back, as a terminal may leave it below the image, then
427
318
  // moved past the cells it covers, as text written over them would erase it.
@@ -430,11 +321,8 @@ const drawIcon = (icon) => `\x1b7${icon}\x1b8\x1b[${ICON_CELLS}C`
430
321
  // below the image then never scrolls, which would put the cursor back a line off.
431
322
  const ROOM_BELOW = '\n\n\x1b[2A'
432
323
 
433
- // The advisories as a table on `out`, fitted to `width` (the terminal's, where `out` is one) by wrapping
434
- // its long columns, after `notes` on `err` (lines of what the audit leaves out, each in yellow); on
435
- // `err`, the packages not audited, then the summary, last, so it is what is read after the table. Severities in colour, the header bold and the box dimmed, where each stream takes
436
- // colours (`colors`, else takesColors). With `icons` (the logos sixel.js logos() picks for a terminal
437
- // that draws sixel images, true for its 16-pixel ones), each id has its source's icon before it.
324
+ // The advisories as a table on `out`, fitted to `width`; on `err`, `notes` before it, then the packages not audited
325
+ // and the summary. With `icons` (sixel.js logos()'s, true for its 16-pixel ones), each id has its source's.
438
326
  export function printAuditReport({ packages, skipped = [], rows, npmMalware = [], why = false, reason = null }, { out = process.stdout, err = process.stderr, colors, width = out.isTTY ? out.columns : undefined, icons: given = false, notes = [] } = {}) {
439
327
  const icons = given === true ? logos() : given || null
440
328
  const paintOut = painter(out, colors)
@@ -449,12 +337,9 @@ export function printAuditReport({ packages, skipped = [], rows, npmMalware = []
449
337
  return
450
338
  }
451
339
  if (rows.length > 0) {
452
- // The ecosystem column only where one is not npm's, which the others were all once.
340
+ // The ecosystem column only where one is not npm's.
453
341
  const columns = ['severity', ...(rows.some((r) => r.ecosystem !== 'npm') ? ['ecosystem'] : []), 'package', 'installed', 'vulnerable', 'title', 'id']
454
- // Surface the reason column only when some advisory has provenance -- bundle
455
- // consumers, or (with --why) import paths. Under --reason WITHOUT --why every
456
- // cell is just the filter value repeated, so drop the column then; --why still
457
- // earns it (the import chains differ per row).
342
+ // Not under --reason without --why, where every cell is that consumer.
458
343
  if (rows.some((r) => r.reason) && !(reason && !why)) columns.splice(columns.indexOf('installed') + 1, 0, 'reason')
459
344
  // A chain's arrows and `...` dimmed, so the packages in it stand out. An id's blank cells its icon.
460
345
  const iconOf = (row) => icons?.[SOURCE_LOGOS[row.source]]
@@ -465,8 +350,6 @@ export function printAuditReport({ packages, skipped = [], rows, npmMalware = []
465
350
  if (column === 'reason' && why) return text.replaceAll(/(?<=^| )(?:->|\.\.\.)(?= |$)/gu, (arrow) => paintOut(DIM, arrow))
466
351
  return text
467
352
  }
468
- // Under --why the reason cell is a list of `consumer: path` lines; let it span
469
- // multiple physical rows instead of collapsing to one.
470
353
  const table = formatTable(rows.map((row) => ({ ...row, severity: badge(severityOf(row), severityOf(row), coloredOut), ...(icons && { id: `${' '.repeat(ICON_CELLS)} ${row.id}` }) })), columns, {
471
354
  multiline: why ? ['reason'] : [],
472
355
  wrap: WRAPPED,
@@ -487,9 +370,6 @@ export function printAuditReport({ packages, skipped = [], rows, npmMalware = []
487
370
  printMalware(npmMalware, err, paintErr, coloredErr)
488
371
  }
489
372
 
490
- // What npm's registry tells of malware in any dependency, whatever --reason (audit()'s `npmMalware`), after
491
- // the summary, apart from the table: each advisory a line, its package and version, id and title, and the
492
- // consumers that recorded it.
493
373
  function printMalware(rows, err, paint, colored) {
494
374
  if (rows.length === 0) return
495
375
  const packages = new Set(rows.map((row) => `${row.package}@${row.installed}`)).size