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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -75,6 +75,7 @@ _Lockfiles (npm/pnpm/etc) not mentioned: they are like the "tarball" column, but
75
75
  | `stasis prune` | trim `node_modules` to the lockfile, verifying the rest |
76
76
  | `stasis audit stasis.lock.json` | report advisories for a lockfile's or bundle's dependencies |
77
77
  | `stasis audit --why app.stasis.code.br` | same, with the import chains that pull each flagged package in |
78
+ | `stasis audit` | same, for the bundle `stasis bundle` would build of `stasis.config.json`'s entries, without writing it |
78
79
  | `stasis validate app.stasis.code.br` | check a bundle's own code against its repository and its dependencies against their published packages, held to its lockfile |
79
80
  | `stasis sbom --format=spdx stasis.lock.json` | export an SPDX SBOM for a lockfile or bundle |
80
81
  | `stasis sbom --format=cyclonedx app.stasis.code.br` | export a CycloneDX SBOM for a lockfile or bundle |
package/bin/stasis.js CHANGED
@@ -24,13 +24,14 @@ const HELP = `Usage:
24
24
  stasis bundle [--cargo] [--cargo-features=a,b,pkg/c] [--cargo-no-default-features] [--cargo-all-features] [--cargo-target=(triple|host)] [--cargo-manifests] [--add] [--output=(path|-)] path/to/file.rs ...
25
25
  stasis bundle [--add] [--output=(path|-)] path/to/file.(php|sh|bash) ...
26
26
  stasis bundle [--deptree] [--babel] [--metro-resolver] [--cargo] [--cargo-target=(triple|host)] [--lockfile=path/to/stasis.lock.json] [--brotli-quality=0..11] [--add] [--output=(path|-)] [entry ...] (the entries' options from stasis.config.json's entries)
27
- stasis github-bundle (owner/name[/path]|--github=owner/name [--directory=path]) [--sha=commit|--tag=name] [--package-manager=(pnpm|yarn1|npm|soldeer) [--package-manager-version=version]] [--no-lazy] [--generate=prisma] [--lockfile=path/to/stasis.lock.json] [--output=(path|-)] [stasis bundle's options for the entries] [path/in/repo/to/(file.(js|ts|jsx|tsx)|file.sol|dir) ...]
27
+ stasis github-bundle (owner/name[/path]|--github=owner/name [--directory=path]) [--sha=commit|--tag=name] [--package-manager=(pnpm|yarn1|npm|soldeer) [--package-manager-version=version]] [--no-lazy] [--generate=prisma|--no-generate] [--lockfile=path/to/stasis.lock.json] [--output=(path|-)] [stasis bundle's options for the entries] [path/in/repo/to/(file.(js|ts|jsx|tsx)|file.sol|dir) ...]
28
28
  stasis add path/to/(file|dir) ...
29
29
  stasis build --output=(dir|file.js) [--format=(esm|cjs|iife)] [--platform=(node|browser|neutral|hermes)] [--babel|--flow] [--minify] [--sourcemap] [--define=K=V ...] [--external=pkg ...] [--loader=.ext:name ...] [--metro=(ios|android|...) [--assets-dest=dir]] path/to/(stasis.code.br|stasis.lock.json) [entry]
30
30
  stasis extract [--output=path/to/dir] [--lock=(frozen|add|replace|ignore|none)] path/to/bundle.stasis.code.br
31
31
  stasis diff --stat [--imports] path/to/(lockfile|bundle) path/to/(lockfile|bundle)
32
32
  stasis prune [path/to/project]
33
- stasis audit [--why|--why-deep] [--why-full] [--reason=consumer] [--repo-advisories] path/to/(lockfile|bundle) ...
33
+ stasis audit [--why|--why-deep] [--why-full] [--reason=consumer] [--repo-advisories|--no-repo-advisories] path/to/(lockfile|bundle) ...
34
+ stasis audit [--why|--why-deep] [--why-full] [--reason=consumer] [--repo-advisories|--no-repo-advisories] (the bundle stasis bundle builds of stasis.config.json's entries, not written)
34
35
  stasis validate [--github=owner/name] [--sha=commit] [--directory=path] [--only-deps] [--lockfile=path | --no-lockfile] [--allow-unverified] [--show-valid] path/to/bundle
35
36
  stasis sbom --format=(spdx|cyclonedx) [--output=(path|-)] path/to/(lockfile|bundle) ...
36
37
  stasis --version
@@ -50,6 +51,23 @@ function fail(message) {
50
51
  process.exit(1)
51
52
  }
52
53
 
54
+ // `build()`'s result, what it warns (console.warn's) held meanwhile: dropped where it succeeds, said
55
+ // where it fails, before its error is.
56
+ async function warningsOnFailure(build) {
57
+ const warn = console.warn
58
+ const held = []
59
+ console.warn = (...args) => held.push(args)
60
+ try {
61
+ return await build()
62
+ } catch (error) {
63
+ console.warn = warn
64
+ for (const args of held) console.warn(...args)
65
+ throw error
66
+ } finally {
67
+ console.warn = warn
68
+ }
69
+ }
70
+
53
71
  // The per-user cache directory, as each platform names it (XDG ignores a relative path).
54
72
  function userCacheDir() {
55
73
  if (isAbsolute(process.env.XDG_CACHE_HOME ?? '')) return join(process.env.XDG_CACHE_HOME, 'stasis')
@@ -156,6 +174,7 @@ if (command === '-v' || command === '--version') {
156
174
  'package-json': { type: 'boolean' },
157
175
  'brotli-quality': { type: 'string' },
158
176
  generate: { type: 'string', multiple: true },
177
+ 'no-generate': { type: 'boolean' },
159
178
  }
160
179
  const parse = (args) => parseLeadingOptions(args, options, { valueFlags: valueFlagsOf(options), onError: fail })
161
180
  // The repo as `owner/name[/path]`, for --github=owner/name [--directory=path]: the first argument
@@ -175,6 +194,7 @@ if (command === '-v' || command === '--version') {
175
194
  }
176
195
  if (github === undefined) fail('Error: github-bundle requires the repo to bundle: owner/name[/path], or --github=owner/name')
177
196
  if (values.sha !== undefined && values.tag !== undefined) fail('Error: github-bundle takes --sha or --tag, not both')
197
+ if (values.generate !== undefined && values['no-generate']) fail('Error: github-bundle takes --generate or --no-generate, not both')
178
198
  // Whether each option applies to the entries is buildGitHubBundle's to say.
179
199
  const list = (value) => [value ?? []].flat().flatMap((v) => v.split(',')).map((s) => s.trim()).filter(Boolean)
180
200
  const brotliQuality = brotliQualityOption(values['brotli-quality'], fail)
@@ -218,7 +238,8 @@ if (command === '-v' || command === '--version') {
218
238
  resources: values.resources === undefined ? undefined : list(values.resources),
219
239
  define,
220
240
  packageJSON: Boolean(values['package-json']),
221
- generate: list(values.generate),
241
+ // Detected without either, unless stasis.config.json's generatePrisma says not.
242
+ generate: values['no-generate'] ? [] : values.generate === undefined ? undefined : list(values.generate),
222
243
  brotliQuality,
223
244
  })
224
245
  } else if (command === 'add') {
@@ -313,23 +334,72 @@ if (command === '-v' || command === '--version') {
313
334
  } else if (command === 'prune') {
314
335
  await pruneCli(argv, { label: 'stasis', fail })
315
336
  } else if (command === 'audit') {
316
- const options = { why: { type: 'boolean' }, 'why-deep': { type: 'boolean' }, 'why-full': { type: 'boolean' }, reason: { type: 'string' }, 'repo-advisories': { type: 'boolean' } }
337
+ const options = { why: { type: 'boolean' }, 'why-deep': { type: 'boolean' }, 'why-full': { type: 'boolean' }, reason: { type: 'string' }, 'repo-advisories': { type: 'boolean' }, 'no-repo-advisories': { type: 'boolean' } }
317
338
  const values = parseLeadingOptions(argv, options, { valueFlags: valueFlagsOf(options), onError: fail })
318
- if (argv.length === 0) fail('Nothing to audit: no path to file given')
319
- const repoAdvisories = Boolean(values['repo-advisories'])
320
- if (repoAdvisories && !process.env.GITHUB_TOKEN) fail('Error: --repo-advisories requires a GitHub token in GITHUB_TOKEN')
339
+ if (values['repo-advisories'] && values['no-repo-advisories']) fail('Error: --repo-advisories and --no-repo-advisories: give one')
340
+ // With no file, the bundle a bare `stasis bundle` would write of the entries stasis.config.json's
341
+ // `entries` names, planned (and refused) as it plans them, and built after the checks below.
342
+ let plan
343
+ if (argv.length === 0) {
344
+ const cwd = process.cwd()
345
+ const { bundlePlan, readConfigEntries } = await import('../src/cmd/bundle-options.js')
346
+ try {
347
+ const config = readConfigEntries(cwd)
348
+ const entries = [...(config?.values() ?? [])].map(({ entry }) => entry)
349
+ if (entries.length === 0) fail('Nothing to audit: no path to file given, nor entries in stasis.config.json to bundle')
350
+ plan = { cwd, ...bundlePlan({ cwd, entries, values: {}, config }) }
351
+ } catch (cause) {
352
+ fail(`Error: ${cause.message}`)
353
+ }
354
+ }
355
+ // Each package's GitHub repository is asked for its advisories, a request a package, unless
356
+ // --no-repo-advisories: with githubToken's token, as GitHub's anonymous rate wouldn't last a project.
357
+ // Without one, they are not asked, which a line above the table says; --repo-advisories fails instead.
358
+ const token = (await import('../src/cmd/tokens.js')).githubToken()
359
+ if (values['repo-advisories'] && !token) fail('Error: --repo-advisories requires a GitHub token in GITHUB_TOKEN')
360
+ const repoAdvisories = !values['no-repo-advisories'] && Boolean(token)
361
+ const tokenless = !values['no-repo-advisories'] && !token
362
+ // Built as `stasis bundle` builds it, but neither written nor compressed, nor its warnings said
363
+ // unless it fails (its error is, as ever).
364
+ let files = argv.map((f) => resolve(f))
365
+ if (plan !== undefined) {
366
+ const { buildCommandBundle } = await import('../src/cmd/bundle.js')
367
+ files = [await warningsOnFailure(() => buildCommandBundle(plan))]
368
+ }
321
369
  // A Soldeer package's and a GitHub repo's advisories are their repository's, asked of GitHub with
322
- // GITHUB_TOKEN where it is set; the client asks nothing for any other package by itself.
323
- const github = (await import('@preventive/upstream/github.js')).createClient({ token: process.env.GITHUB_TOKEN || null })
370
+ // githubToken's token where there is one; the client asks nothing for any other package by itself.
371
+ const github = (await import('@preventive/upstream/github.js')).createClient({ token })
324
372
  // Upstream caches what it looks up (a package's GitHub repo, a month each) only where told to, and
325
373
  // answers from it without asking: a per-user directory, never one shared with less trusted jobs.
326
374
  const { setCacheDir } = await import('@preventive/upstream/npm.js')
327
375
  setCacheDir(userCacheDir())
328
- const { audit, printAuditReport } = await import('../src/audit.js')
329
- const files = argv.map((f) => resolve(f))
330
- const report = await audit(files, { why: Boolean(values.why), whyDeep: Boolean(values['why-deep']), whyFull: Boolean(values['why-full']), reason: values.reason, repoAdvisories, github })
331
- printAuditReport(report)
332
- process.exitCode = report.rows.length === 0 ? 0 : 1
376
+ const { AuditError, audit, printAuditReport } = await import('../src/audit.js')
377
+ // With no file, the package manager's lockfile installing the project too: its packages the bundle
378
+ // doesn't hold checked for malware (npm's registry alone), it being found or refused told, not failed.
379
+ let lockfile = null
380
+ if (plan !== undefined) {
381
+ try {
382
+ lockfile = await (await import('../src/validate.js')).installedNpmPackages(plan.cwd)
383
+ } catch (cause) {
384
+ console.error(`Lockfile not checked for malware: ${cause.message}`)
385
+ }
386
+ }
387
+ let report
388
+ try {
389
+ // --reason audits that consumer's packages alone; one no file records is refused before any is asked about.
390
+ // OSV is asked about npm packages too, never a private one (upstream checks each name is public).
391
+ report = await audit(files, { why: Boolean(values.why), whyDeep: Boolean(values['why-deep']), whyFull: Boolean(values['why-full']), reason: values.reason, repoAdvisories, github, osv: true, lockfile })
392
+ } catch (cause) {
393
+ if (cause instanceof AuditError) fail(`Error: ${cause.message}`)
394
+ throw cause
395
+ }
396
+ // Each advisory's source as an icon, where the terminal draws sixel images, sized to its cells.
397
+ const { capabilities, logos } = await import('../src/sixel.js')
398
+ const terminal = await capabilities()
399
+ const notes = tokenless ? ['Repository advisories require GITHUB_TOKEN; continuing without them'] : []
400
+ printAuditReport(report, { icons: terminal.sixel && logos(terminal.cell, terminal.background), notes })
401
+ // Malware npm's registry tells of in any dependency fails it too, whatever --reason.
402
+ process.exitCode = report.rows.length === 0 && report.npmMalware.length === 0 ? 0 : 1
333
403
  } else if (command === 'validate') {
334
404
  // Exits 0 where the bundle is valid, 1 where it is invalid, 2 where a file it was to check went
335
405
  // unverified (unless --allow-unverified), and 3 where it refused the bundle, or failed.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/stasis",
3
- "version": "1.0.0-beta.14",
3
+ "version": "1.0.0-beta.15",
4
4
  "description": "File-level source bundles and lockfiles",
5
5
  "type": "module",
6
6
  "exports": {
@@ -20,8 +20,15 @@
20
20
  "./prune": "./src/prune.js",
21
21
  "./sbom": "./src/sbom.js",
22
22
  "./audit-corrections": "./src/audit-corrections.js",
23
- "./validate": "./src/validate.js",
23
+ "./validate": {
24
+ "types": "./src/validate.d.ts",
25
+ "default": "./src/validate.js"
26
+ },
24
27
  "./vfs-bundle": "./src/vfs-bundle.js",
28
+ "./validate-notes": {
29
+ "types": "./src/validate-notes.d.ts",
30
+ "default": "./src/validate-notes.js"
31
+ },
25
32
  "./logo.svg": "./logo.svg"
26
33
  },
27
34
  "files": [
@@ -72,8 +79,12 @@
72
79
  "src/resolve-typescript.js",
73
80
  "src/rollup.js",
74
81
  "src/sbom.js",
82
+ "src/sixel.js",
75
83
  "src/table.js",
84
+ "src/validate.d.ts",
76
85
  "src/validate.js",
86
+ "src/validate-notes.d.ts",
87
+ "src/validate-notes.js",
77
88
  "src/scan.js",
78
89
  "src/vfs-bundle.js",
79
90
  "src/vfs-bundle/entries.js",
@@ -106,12 +117,13 @@
106
117
  "homepage": "https://github.com/PreventiveMeasures/stasis#readme",
107
118
  "dependencies": {
108
119
  "@exodus/bytes": "^1.16.0",
109
- "@exodus/stasis-core": "1.0.0-beta.14",
110
- "@exodus/stasis-plugins": "1.0.0-beta.14",
120
+ "@exodus/stasis-core": "1.0.0-beta.15",
121
+ "@exodus/stasis-plugins": "1.0.0-beta.15",
111
122
  "@preventive/archive": "^1.2.2",
112
123
  "@preventive/deptree": "^1.3.3",
113
124
  "@preventive/lockfile": "^1.2.0",
114
- "@preventive/upstream": "^1.4.0",
125
+ "@preventive/rn": "1.0.0-alpha.4",
126
+ "@preventive/upstream": "^1.5.1",
115
127
  "@preventive/vfs": "^1.1.0",
116
128
  "flow-remove-types": "^2.322.0",
117
129
  "oxc-parser": "^0.152.0"
package/src/audit.js CHANGED
@@ -3,10 +3,11 @@ import { dependencyEcosystem, moduleFileKey, packageRecords, sameGithub } from '
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
7
 
8
8
  export { formatTable }
9
9
  import { parseFile, parseFileWithKind } from './parse.js'
10
+ import { logos } from './sixel.js'
10
11
  import { collectWhy, invertReason } from './why.js'
11
12
 
12
13
  // Where advisories() asks for each ecosystem's advisories: npm's registry; OSV for crates (those
@@ -33,6 +34,15 @@ const versionOf = (ecosystem, version) => (ecosystem === 'github' && version ===
33
34
  // prefixed by the ecosystem for the others, whose names may be npm's too.
34
35
  const keyOf = (ecosystem, name, version) => `${ecosystem === 'npm' ? '' : `${ecosystem}:`}${name}@${version}`
35
36
 
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))
40
+
41
+ // A refused audit, as a usage error: one for the message alone, never a stack.
42
+ export class AuditError extends Error {
43
+ name = 'AuditError'
44
+ }
45
+
36
46
  // Composer's dev versions (`dev-main`, `1.x-dev`), as Composer tells them, a `#ref` dropped: no
37
47
  // advisory database lists those, and advisories() takes releases alone.
38
48
  const isComposerDev = (version) => /^dev-|-dev$/iu.test(version.replace(/#.*$/su, ''))
@@ -119,7 +129,7 @@ export function collectPackages(files) {
119
129
  const repos = new Map() // `ecosystem:name` -> the one repository recorded for it, or null
120
130
  for (const file of files) {
121
131
  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)
132
+ const key = packageKey(pkg.ecosystem, pkg.name, pkg.version, vendoredIn)
123
133
  if (!byKey.has(key)) byKey.set(key, pkg)
124
134
  if (!vendored.has(key)) vendored.set(key, { hosts: new Set(), only: true })
125
135
  if (vendoredIn === undefined) vendored.get(key).only = false
@@ -147,36 +157,72 @@ export function collectPackages(files) {
147
157
  // (see audit-corrections.js): a consumer that recorded nothing of a package but a corrected file or
148
158
  // its manifest carries none of its real code and is NOT a reason -- e.g. webpack shipping only ws's
149
159
  // 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.
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.
151
162
  export function collectReasons(files) {
163
+ return reasonIndex(files).byPkg
164
+ }
165
+
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.
168
+ function reasonIndex(files) {
152
169
  const byPkg = new Map()
170
+ const consumers = new Set()
153
171
  for (const file of files) {
154
172
  const artifact = parseFile(file)
155
173
  const fileReasons = invertReason(artifact.reason)
156
174
  if (!fileReasons) continue
175
+ for (const [consumer, list] of Object.entries(artifact.reason)) if (Array.isArray(list)) consumers.add(consumer)
157
176
  const fileToPkgs = new Map()
158
- for (const [dir, { name, version, ecosystem: tagged, files: modFiles }] of packageRecords(artifact.modules)) {
177
+ for (const [dir, { name, version, ecosystem: tagged, files: modFiles, vendoredIn }] of packageRecords(artifact.modules)) {
159
178
  const ecosystem = dependencyEcosystem(dir, tagged)
160
- if (ecosystem === undefined || !name || !version) continue
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}`)
161
182
  for (const rel of Object.keys(modFiles)) {
162
- if (!isEvidenceFile(name, version, rel, ecosystem)) continue
183
+ if (!isEvidenceFile(name, version ?? null, rel, ecosystem)) continue
163
184
  const f = moduleFileKey(dir, rel)
164
185
  if (!fileToPkgs.has(f)) fileToPkgs.set(f, [])
165
- fileToPkgs.get(f).push(keyOf(ecosystem, name, versionOf(ecosystem, version)))
186
+ fileToPkgs.get(f).push(key)
166
187
  }
167
188
  }
168
- for (const [f, consumers] of fileReasons) {
189
+ for (const [f, recorded] of fileReasons) {
169
190
  for (const key of fileToPkgs.get(f) ?? []) {
170
191
  let set = byPkg.get(key)
171
192
  if (set === undefined) byPkg.set(key, (set = new Set()))
172
- for (const consumer of consumers) set.add(consumer)
193
+ for (const consumer of recorded) set.add(consumer)
173
194
  }
174
195
  }
175
196
  }
176
- return byPkg
197
+ return { byPkg, consumers }
198
+ }
199
+
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
+ const BUNDLE_METRO = 'bundle:metro'
203
+ const METRO = 'metro'
204
+
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.
214
+ function namedIndex({ byPkg, consumers }) {
215
+ const name = consumerNames(consumers)
216
+ return {
217
+ byPkg: new Map([...byPkg].map(([key, set]) => [key, new Set([...set].map(name))])),
218
+ consumers: new Set([...consumers].map(name)),
219
+ name,
220
+ }
177
221
  }
178
222
 
179
- const SEVERITY_ORDER = { critical: 0, high: 1, moderate: 2, low: 3, info: 4, none: 5 }
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.
225
+ const SEVERITY_ORDER = { malware: 0, critical: 1, high: 2, moderate: 3, low: 4, info: 5, none: 6 }
180
226
 
181
227
  // Consumer display order: bundler plugins (metro, webpack, esbuild, ...) first,
182
228
  // then `run`, then `add`, then `extra` (stasis.config.json's). Ties broken alphabetically.
@@ -237,9 +283,10 @@ export function flattenAdvisories(found, reasonsByPkg = new Map(), whyByPkg = nu
237
283
  package: adv.name,
238
284
  installed: adv.versions.map((v) => installedCell(adv, v, vendoredByKey)).join(', '),
239
285
  vulnerable: adv.range ?? '',
240
- severity: adv.severity ?? '',
286
+ severity: adv.malware ? 'malware' : adv.severity ?? '',
241
287
  title: adv.title ?? '',
242
288
  id: adv.id,
289
+ source: adv.source,
243
290
  reason,
244
291
  })
245
292
  }
@@ -254,77 +301,201 @@ export function flattenAdvisories(found, reasonsByPkg = new Map(), whyByPkg = nu
254
301
  return rows
255
302
  }
256
303
 
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.
257
307
  // `repoAdvisories` and `github` (a @preventive/upstream/github.js client) go to advisories() as they
258
308
  // are; a Soldeer package or a GitHub repo needs the client, their repository being their only source.
259
309
  // 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 } = {}) {
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.
322
+ export async function audit(files, { why = false, whyDeep = false, whyFull = false, reason = null, repoAdvisories = false, github, osv = false, lockfile = null } = {}) {
261
323
  // --why-deep and --why-full imply --why. Deep keeps every chain instead of the
262
324
  // default pruning of chains whose full suffix is already a chain (see
263
325
  // collectWhy/dropSuffixed); full spells chains out with no `...` collapse.
264
326
  why = why || whyDeep || whyFull
265
- const packages = collectPackages(files)
327
+ let index // namedIndex(reasonIndex(files)), once it is needed
328
+ const indexed = () => (index ??= namedIndex(reasonIndex(files)))
329
+ if (reason) {
330
+ const { consumers, name } = indexed()
331
+ if (!consumers.has(name(reason))) {
332
+ const known = [...consumers].toSorted(byConsumerOrder)
333
+ throw new AuditError(`--reason=${reason}: ${known.length === 0
334
+ ? 'the files record no consumers (only a bundle does)'
335
+ : `no file records that consumer; they record ${known.join(', ')}`}`)
336
+ }
337
+ reason = name(reason)
338
+ }
339
+ 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
266
341
  const skipped = packages.flatMap((pkg) => {
267
342
  const because = unaudited(pkg)
268
343
  return because === undefined ? [] : [{ ...pkg, because }]
269
344
  })
270
345
  const asked = packages.filter((pkg) => unaudited(pkg) === undefined)
271
- if (asked.length === 0) {
272
- return { packages, skipped, advisories: [], rows: [], why }
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.
348
+ 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)
358
+ }
359
+ const others = [...byKey.values()]
360
+ if (asked.length === 0 && others.length === 0) {
361
+ return { packages, skipped, advisories: [], rows: [], npmMalware: [], why, whyDeep, whyFull, reason }
273
362
  }
363
+ const query = (list) => list.map(({ version, vendoredIn: _in, vendoredOnly: _only, ...pkg }) => ({ ...pkg, versions: [version] }))
274
364
  let result
365
+ let elsewhere
275
366
  try {
276
- result = await advisories(asked.map(({ version, vendoredIn: _in, vendoredOnly: _only, ...pkg }) => ({ ...pkg, versions: [version] })), { repoAdvisories, github })
367
+ ;[result, elsewhere] = await Promise.all([
368
+ asked.length > 0 ? advisories(query(asked), { repoAdvisories, github, osv }) : [],
369
+ others.length > 0 ? advisories(query(others)) : [],
370
+ ])
277
371
  } catch (cause) {
278
372
  // Refused input or a malformed answer is an assertion that says so itself; a transport or
279
373
  // HTTP failure gets the context of which request it was.
280
374
  if (cause?.code === 'ERR_ASSERTION') throw cause
281
- const sources = new Set([...asked.flatMap((pkg) => SOURCES[pkg.ecosystem]), ...(repoAdvisories ? ['GitHub'] : [])])
375
+ 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
376
  throw new Error(`${[...sources].join('/')} advisories request failed: ${cause.message}`, { cause })
283
377
  }
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.
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.
380
+ 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.
288
389
  // `reason` (--reason) narrows both the paths (in collectWhy) and the consumer
289
390
  // list (in flattenAdvisories) to a single consumer, dropping advisories
290
391
  // 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
392
+ let whyByPkg = null
293
393
  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)
394
+ const targetKeys = new Set(result.filter((adv) => adv.ecosystem === 'npm').flatMap((adv) => adv.versions.map((v) => keyOf(adv.ecosystem, adv.name, v))))
395
+ whyByPkg = collectWhy(files, targetKeys, reason, { deep: whyDeep, full: whyFull, consumerName: indexed().name })
300
396
  }
301
- return { packages, skipped, advisories: result, rows, why, whyDeep, whyFull, reason }
397
+ const rows = flattenAdvisories(result, indexed().byPkg, whyByPkg, reason, vendoredOf(asked))
398
+ return { packages, skipped, advisories: result, rows, npmMalware, why, whyDeep, whyFull, reason }
302
399
  }
303
400
 
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(', ')
401
+ // Each severity's colour, the worst the loudest (malware white on red); an advisory without one (RustSec's)
402
+ // `unrated`, dimmed.
403
+ const SEVERITY_TONES = { malware: ['bold', 'white', 'bgRed'], critical: ['bold', 'magenta'], high: ['bold', 'red'], moderate: 'yellow', low: 'cyan', info: DIM, none: DIM, unrated: DIM }
404
+ const severityOf = (row) => row.severity || 'unrated'
405
+
406
+ // The columns whose text wraps where the table is wider than the terminal: the long ones, free text first.
407
+ const WRAPPED = ['title', 'reason', 'vulnerable', 'installed']
408
+
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.
411
+ function alertStats(rows, paint, colored) {
412
+ const count = `${rows.length} alert${rows.length === 1 ? '' : 's'}`
413
+ if (rows.length === 0) return paint(['bold', 'green'], count)
414
+ const bySeverity = Map.groupBy(rows, severityOf)
415
+ return `${paint('bold', count)} (${[...bySeverity].map(([severity, list]) => paint(SEVERITY_TONES[severity], badge(severity, `${list.length} ${severity}`, colored))).join(', ')})`
309
416
  }
310
417
 
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'}`
418
+ // Malware as a badge: its colour a space wider on either side, where there is colour.
419
+ const badge = (severity, text, colored) => (severity === 'malware' && colored ? ` ${text} ` : text)
420
+
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.
423
+ 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.
425
+ const ICON_CELLS = 2
426
+ // An icon, drawn with the cursor saved and put back, as a terminal may leave it below the image, then
427
+ // moved past the cells it covers, as text written over them would erase it.
428
+ const drawIcon = (icon) => `\x1b7${icon}\x1b8\x1b[${ICON_CELLS}C`
429
+ // Written before a line with an icon: two lines below it, then back up. A terminal moving the cursor
430
+ // below the image then never scrolls, which would put the cursor back a line off.
431
+ const ROOM_BELOW = '\n\n\x1b[2A'
432
+
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.
438
+ 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
+ const icons = given === true ? logos() : given || null
440
+ const paintOut = painter(out, colors)
441
+ const paintErr = painter(err, colors)
442
+ const coloredOut = colors ?? takesColors(out)
443
+ const coloredErr = colors ?? takesColors(err)
444
+ for (const note of notes) err.write(`${paintErr('yellow', note)}\n`)
445
+ const scanned = `Scanned ${packages.length} package${packages.length === 1 ? '' : 's'}${reason ? ` recorded by ${reason}` : ''}`
313
446
  if (packages.length === 0) {
314
- err.write(`${scanned}\nNo dependencies found in the input files\n`)
447
+ err.write(`${scanned}\n${paintErr('yellow', reason ? `No dependencies recorded by ${reason}` : 'No dependencies found in the input files')}\n`)
448
+ printMalware(npmMalware, err, paintErr, coloredErr)
315
449
  return
316
450
  }
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`)
451
+ if (rows.length > 0) {
452
+ // The ecosystem column only where one is not npm's, which the others were all once.
453
+ 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).
458
+ if (rows.some((r) => r.reason) && !(reason && !why)) columns.splice(columns.indexOf('installed') + 1, 0, 'reason')
459
+ // A chain's arrows and `...` dimmed, so the packages in it stand out. An id's blank cells its icon.
460
+ const iconOf = (row) => icons?.[SOURCE_LOGOS[row.source]]
461
+ const paint = (text, column, row) => {
462
+ if (column === 'id' && iconOf(row)) return drawIcon(iconOf(row)) + text.slice(ICON_CELLS)
463
+ if (column === 'severity') return paintOut(SEVERITY_TONES[row.severity.trim()], text)
464
+ if (column === 'package') return paintOut('bold', text)
465
+ if (column === 'reason' && why) return text.replaceAll(/(?<=^| )(?:->|\.\.\.)(?= |$)/gu, (arrow) => paintOut(DIM, arrow))
466
+ return text
467
+ }
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
+ const table = formatTable(rows.map((row) => ({ ...row, severity: badge(severityOf(row), severityOf(row), coloredOut), ...(icons && { id: `${' '.repeat(ICON_CELLS)} ${row.id}` }) })), columns, {
471
+ multiline: why ? ['reason'] : [],
472
+ wrap: WRAPPED,
473
+ width,
474
+ paint,
475
+ paintHeader: (text) => paintOut('bold', text),
476
+ paintFrame: (text) => paintOut(DIM, text),
477
+ })
478
+ out.write(`${icons ? table.split('\n').map((line) => (line.includes('\x1bP') ? ROOM_BELOW + line : line)).join('\n') : table}\n`)
479
+ }
480
+ if (skipped.length > 0) {
481
+ const names = skipped.map((pkg) => `${pkg.ecosystem} ${pkg.name}${pkg.version === undefined ? '' : `@${pkg.version}`}`)
482
+ const pad = Math.max(...names.map((name) => name.length))
483
+ const lines = skipped.map((pkg, i) => ` ${names[i].padEnd(pad)} ${paintErr(DIM, pkg.because)}`)
484
+ err.write(`${rows.length > 0 ? '\n' : ''}${paintErr('yellow', 'Not audited:')}\n${lines.join('\n')}\n`)
485
+ }
486
+ err.write(`${rows.length > 0 || skipped.length > 0 ? '\n' : ''}${scanned}: ${alertStats(rows, paintErr, coloredErr)}\n`)
487
+ printMalware(npmMalware, err, paintErr, coloredErr)
488
+ }
489
+
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
+ function printMalware(rows, err, paint, colored) {
319
494
  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')
495
+ const packages = new Set(rows.map((row) => `${row.package}@${row.installed}`)).size
496
+ const names = rows.map((row) => `${row.package} ${row.installed}`)
497
+ const pad = (list) => Math.max(...list.map((text) => text.length))
498
+ const [nameWidth, idWidth, titleWidth] = [pad(names), pad(rows.map((row) => row.id)), pad(rows.map((row) => row.title))]
499
+ 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}`)
500
+ err.write(`\n${paint(SEVERITY_TONES.malware, badge('malware', 'malware', colored))} in ${packages} dependenc${packages === 1 ? 'y' : 'ies'}:\n${lines.join('\n')}\n`)
330
501
  }