@exodus/stasis 1.0.0-beta.14 → 1.0.0-beta.16

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