@exodus/stasis 1.0.0-beta.10 → 1.0.0-beta.11

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@exodus/stasis",
3
- "version": "1.0.0-beta.10",
3
+ "version": "1.0.0-beta.11",
4
4
  "description": "File-level source bundles and lockfiles",
5
5
  "type": "module",
6
6
  "exports": {
@@ -95,12 +95,12 @@
95
95
  "homepage": "https://github.com/PreventiveMeasures/stasis#readme",
96
96
  "dependencies": {
97
97
  "@exodus/bytes": "^1.16.0",
98
- "@exodus/stasis-core": "1.0.0-beta.10",
99
- "@exodus/stasis-plugins": "1.0.0-beta.10",
100
- "@preventive/archive": "1.0.0-beta.4",
101
- "@preventive/deptree": "1.0.0-alpha.11",
102
- "@preventive/lockfile": "1.0.0-alpha.7",
103
- "@preventive/upstream": "1.0.0-alpha.12",
98
+ "@exodus/stasis-core": "1.0.0-beta.11",
99
+ "@exodus/stasis-plugins": "1.0.0-beta.11",
100
+ "@preventive/archive": "^1.2.0",
101
+ "@preventive/deptree": "^1.0.1",
102
+ "@preventive/lockfile": "^1.2.0",
103
+ "@preventive/upstream": "^1.3.0",
104
104
  "@preventive/vfs": "1.0.0-beta.4",
105
105
  "oxc-parser": "^0.152.0"
106
106
  },
package/src/audit.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { packageRepo } from '@exodus/stasis-core/bundle-util'
2
- import { dependencyEcosystem, moduleFileKey, sameGithub } from '@exodus/stasis-core/util'
2
+ import { dependencyEcosystem, moduleFileKey, packageRecords, sameGithub } 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'
@@ -35,7 +35,8 @@ const keyOf = (ecosystem, name, version) => `${ecosystem === 'npm' ? '' : `${eco
35
35
  const isComposerDev = (version) => /^dev-|-dev$/iu.test(version.replace(/#.*$/su, ''))
36
36
 
37
37
  // Why `pkg` cannot be audited, or undefined where it can.
38
- function unaudited({ ecosystem, version }) {
38
+ function unaudited({ ecosystem, version, vendoredIn }) {
39
+ if (version === undefined) return `a copy vendored in ${vendoredIn.join(', ')} whose package.json gives no version, so no advisory can be matched to it`
39
40
  if (Object.hasOwn(UNREGISTERED_CRATES, ecosystem)) return UNREGISTERED_CRATES[ecosystem]
40
41
  if (!Object.hasOwn(SOURCES, ecosystem)) return `no advisories are looked up for ${ecosystem}`
41
42
  if (ecosystem === 'composer' && isComposerDev(version)) return 'a Composer dev version, which no advisory database lists'
@@ -61,15 +62,25 @@ function unaudited({ ecosystem, version }) {
61
62
  // one does; else, for an npm package a bundle carries the package.json of (one built before builds
62
63
  // recorded `repo`), the one that names, read by the rule builds record it by. A GitHub repo is its
63
64
  // own, and has none.
65
+ //
66
+ // A copy of a package a dependency ships (its record's `vendored`, as packageRecords splits it out) is
67
+ // audited too, as the package it is, by the same evidence rule over the files it holds, with
68
+ // `vendoredIn` its host's `name@version`; its host, all of whose files the copy's are, as before. A copy
69
+ // whose package.json gives no version is listed with none, as not audited.
64
70
  export function collectPackagesFromFile(file) {
65
71
  const out = []
66
72
  const { kind, artifact } = parseFileWithKind(file)
67
- for (const [dir, { name, version, ecosystem: tagged, repo, files }] of artifact.modules) {
73
+ for (const [dir, { name, version, ecosystem: tagged, repo, files, vendoredIn }] of packageRecords(artifact.modules)) {
68
74
  const ecosystem = dependencyEcosystem(dir, tagged)
69
75
  // First-party code is never sent to a public registry (leaks names, adds noise).
70
76
  if (ecosystem === undefined) continue
71
- if (!name || !version) continue
72
- if (!Object.keys(files).some((rel) => isEvidenceFile(name, version, rel, ecosystem))) continue
77
+ if (!name || (!version && vendoredIn === undefined)) continue
78
+ if (!Object.keys(files).some((rel) => isEvidenceFile(name, version ?? null, rel, ecosystem))) continue
79
+ if (vendoredIn !== undefined) {
80
+ const host = artifact.modules.get(vendoredIn)
81
+ out.push({ ecosystem, name, version: version === undefined ? undefined : versionOf(ecosystem, version), vendoredIn: `${host.name}@${host.version}` })
82
+ continue
83
+ }
73
84
  let github = ecosystem === 'github' ? undefined : repo?.github
74
85
  if (github === undefined && ecosystem === 'npm' && kind === 'bundle') github = manifestRepo(files['package.json'])
75
86
  out.push({ ecosystem, name, version: versionOf(ecosystem, version), ...(github === undefined ? {} : { github }) })
@@ -96,14 +107,20 @@ const byVersion = (a, b) => (valid(a) && valid(b) ? compareVersions(a, b) : byNu
96
107
  // Each package once, with the GitHub repository its artifacts record for its name, which
97
108
  // advisories() asks instead of looking one up. It takes one repository a name, so a name recorded
98
109
  // with two, by two of its versions (a package that moved) or by two artifacts, has none, and is
99
- // looked up as before.
110
+ // looked up as before. One some dependency ships a copy of carries `vendoredIn`, the hosts' sorted
111
+ // `name@version`s, and `vendoredOnly`, whether that copy is all there is of it; a copy without a version
112
+ // is one package per host.
100
113
  export function collectPackages(files) {
101
114
  const byKey = new Map()
115
+ const vendored = new Map() // key -> { hosts, only }
102
116
  const repos = new Map() // `ecosystem:name` -> the one repository recorded for it, or null
103
117
  for (const file of files) {
104
- for (const { github, ...pkg } of collectPackagesFromFile(file)) {
105
- const key = keyOf(pkg.ecosystem, pkg.name, pkg.version)
118
+ for (const { github, vendoredIn, ...pkg } of collectPackagesFromFile(file)) {
119
+ const key = pkg.version === undefined ? `${pkg.ecosystem}:${pkg.name} in ${vendoredIn}` : keyOf(pkg.ecosystem, pkg.name, pkg.version)
106
120
  if (!byKey.has(key)) byKey.set(key, pkg)
121
+ if (!vendored.has(key)) vendored.set(key, { hosts: new Set(), only: true })
122
+ if (vendoredIn === undefined) vendored.get(key).only = false
123
+ else vendored.get(key).hosts.add(vendoredIn)
107
124
  if (github === undefined) continue
108
125
  const name = `${pkg.ecosystem}:${pkg.name}`
109
126
  const known = repos.get(name)
@@ -112,10 +129,11 @@ export function collectPackages(files) {
112
129
  else if (known !== null && !sameGithub(known, github)) repos.set(name, null)
113
130
  }
114
131
  }
115
- return [...byKey.values()]
116
- .map((pkg) => {
132
+ return [...byKey]
133
+ .map(([key, pkg]) => {
117
134
  const github = repos.get(`${pkg.ecosystem}:${pkg.name}`)
118
- return github ? { ...pkg, github } : pkg
135
+ const { hosts, only } = vendored.get(key)
136
+ return { ...pkg, ...(github ? { github } : {}), ...(hosts.size === 0 ? {} : { vendoredIn: [...hosts].toSorted(), vendoredOnly: only }) }
119
137
  })
120
138
  .toSorted((a, b) => a.ecosystem.localeCompare(b.ecosystem) || a.name.localeCompare(b.name) || byVersion(a.version, b.version))
121
139
  }
@@ -126,27 +144,30 @@ export function collectPackages(files) {
126
144
  // (see audit-corrections.js): a consumer that recorded nothing of a package but a corrected file or
127
145
  // its manifest carries none of its real code and is NOT a reason -- e.g. webpack shipping only ws's
128
146
  // noop browser.js stub, while `run` bundles the real ws.
147
+ // A file of a copy a dependency ships (packageRecords) attributes to both: the copy and its host.
129
148
  export function collectReasons(files) {
130
149
  const byPkg = new Map()
131
150
  for (const file of files) {
132
151
  const artifact = parseFile(file)
133
152
  const fileReasons = invertReason(artifact.reason)
134
153
  if (!fileReasons) continue
135
- const fileToPkg = new Map()
136
- for (const [dir, { name, version, ecosystem: tagged, files: modFiles }] of artifact.modules) {
154
+ const fileToPkgs = new Map()
155
+ for (const [dir, { name, version, ecosystem: tagged, files: modFiles }] of packageRecords(artifact.modules)) {
137
156
  const ecosystem = dependencyEcosystem(dir, tagged)
138
157
  if (ecosystem === undefined || !name || !version) continue
139
158
  for (const rel of Object.keys(modFiles)) {
140
159
  if (!isEvidenceFile(name, version, rel, ecosystem)) continue
141
- fileToPkg.set(moduleFileKey(dir, rel), keyOf(ecosystem, name, versionOf(ecosystem, version)))
160
+ const f = moduleFileKey(dir, rel)
161
+ if (!fileToPkgs.has(f)) fileToPkgs.set(f, [])
162
+ fileToPkgs.get(f).push(keyOf(ecosystem, name, versionOf(ecosystem, version)))
142
163
  }
143
164
  }
144
165
  for (const [f, consumers] of fileReasons) {
145
- const key = fileToPkg.get(f)
146
- if (key === undefined) continue
147
- let set = byPkg.get(key)
148
- if (set === undefined) byPkg.set(key, (set = new Set()))
149
- for (const consumer of consumers) set.add(consumer)
166
+ for (const key of fileToPkgs.get(f) ?? []) {
167
+ let set = byPkg.get(key)
168
+ if (set === undefined) byPkg.set(key, (set = new Set()))
169
+ for (const consumer of consumers) set.add(consumer)
170
+ }
150
171
  }
151
172
  }
152
173
  return byPkg
@@ -190,10 +211,18 @@ function reasonCell({ ecosystem, name, versions: affected }, reasonsByPkg, whyBy
190
211
  return consumers.toSorted(byConsumerOrder).join(', ')
191
212
  }
192
213
 
214
+ // An installed version as the table shows it: with the dependencies that ship a copy of it, where one does
215
+ // (`vendoredByKey`, collectPackages' packages by keyOf).
216
+ function installedCell({ ecosystem, name }, version, vendoredByKey) {
217
+ const pkg = vendoredByKey.get(keyOf(ecosystem, name, version))
218
+ if (pkg?.vendoredIn === undefined) return version
219
+ return `${version} (${pkg.vendoredOnly ? '' : 'also '}vendored in ${pkg.vendoredIn.join(', ')})`
220
+ }
221
+
193
222
  // `found` is what @preventive/upstream's advisories() answers: one row per advisory range, whose
194
223
  // `versions` are the audited versions it covers (never empty -- a range covering none is dropped
195
224
  // there, so the table and exit code reflect only real hits).
196
- export function flattenAdvisories(found, reasonsByPkg = new Map(), whyByPkg = null, reasonFilter = null) {
225
+ export function flattenAdvisories(found, reasonsByPkg = new Map(), whyByPkg = null, reasonFilter = null, vendoredByKey = new Map()) {
197
226
  const rows = []
198
227
  for (const adv of found) {
199
228
  const reason = reasonCell(adv, reasonsByPkg, whyByPkg, reasonFilter)
@@ -203,7 +232,7 @@ export function flattenAdvisories(found, reasonsByPkg = new Map(), whyByPkg = nu
203
232
  rows.push({
204
233
  ecosystem: adv.ecosystem,
205
234
  package: adv.name,
206
- installed: adv.versions.join(', '),
235
+ installed: adv.versions.map((v) => installedCell(adv, v, vendoredByKey)).join(', '),
207
236
  vulnerable: adv.range ?? '',
208
237
  severity: adv.severity ?? '',
209
238
  title: adv.title ?? '',
@@ -281,7 +310,7 @@ export async function audit(files, { why = false, whyDeep = false, whyFull = fal
281
310
  }
282
311
  let result
283
312
  try {
284
- result = await advisories(asked.map(({ version, ...pkg }) => ({ ...pkg, versions: [version] })), { repoAdvisories, github })
313
+ result = await advisories(asked.map(({ version, vendoredIn: _in, vendoredOnly: _only, ...pkg }) => ({ ...pkg, versions: [version] })), { repoAdvisories, github })
285
314
  } catch (cause) {
286
315
  // Refused input or a malformed answer is an assertion that says so itself; a transport or
287
316
  // HTTP failure gets the context of which request it was.
@@ -296,14 +325,15 @@ export async function audit(files, { why = false, whyDeep = false, whyFull = fal
296
325
  // `reason` (--reason) narrows both the paths (in collectWhy) and the consumer
297
326
  // list (in flattenAdvisories) to a single consumer, dropping advisories
298
327
  // unrelated to it.
328
+ const vendoredByKey = new Map(asked.filter((pkg) => pkg.vendoredIn !== undefined).map((pkg) => [keyOf(pkg.ecosystem, pkg.name, pkg.version), pkg]))
299
329
  let rows
300
330
  if (why) {
301
331
  const npm = result.filter((adv) => adv.ecosystem === 'npm')
302
332
  const targetKeys = new Set(npm.flatMap((adv) => adv.versions.map((v) => keyOf(adv.ecosystem, adv.name, v))))
303
333
  const reasons = npm.length < result.length ? collectReasons(files) : undefined
304
- rows = flattenAdvisories(result, reasons, collectWhy(files, targetKeys, reason, { deep: whyDeep, full: whyFull }), reason)
334
+ rows = flattenAdvisories(result, reasons, collectWhy(files, targetKeys, reason, { deep: whyDeep, full: whyFull }), reason, vendoredByKey)
305
335
  } else {
306
- rows = flattenAdvisories(result, collectReasons(files), null, reason)
336
+ rows = flattenAdvisories(result, collectReasons(files), null, reason, vendoredByKey)
307
337
  }
308
338
  return { packages, skipped, advisories: result, rows, why, whyDeep, whyFull, reason }
309
339
  }
@@ -322,7 +352,7 @@ export function printAuditReport({ packages, skipped = [], rows, why = false, re
322
352
  return
323
353
  }
324
354
  err.write(`${scanned}: ${alertStats(rows)}\n`)
325
- for (const pkg of skipped) err.write(`Not audited: ${pkg.ecosystem} ${pkg.name}@${pkg.version}, ${pkg.because}\n`)
355
+ for (const pkg of skipped) err.write(`Not audited: ${pkg.ecosystem} ${pkg.name}${pkg.version === undefined ? '' : `@${pkg.version}`}, ${pkg.because}\n`)
326
356
  if (rows.length === 0) return
327
357
  // The ecosystem column only where one is not npm's, which the others were all once.
328
358
  const columns = ['severity', ...(rows.some((r) => r.ecosystem !== 'npm') ? ['ecosystem'] : []), 'package', 'installed', 'vulnerable', 'title', 'id']
@@ -57,11 +57,13 @@ function ancestorsOf(directory) {
57
57
  return [...out, undefined]
58
58
  }
59
59
 
60
- // The files of the subtree `tree`, or null where one is a symlink out of it.
61
- async function treeFiles(client, { github, tree, where }) {
60
+ // The files of the subtree `tree` at `directory`, or null where one is a symlink out of it. Upstream
61
+ // refuses a tarball whose symlinks climb above its root, unless told how far they may: as far as the
62
+ // repo's root, as many levels as `directory` is deep, less one (its tarball's top directory).
63
+ async function treeFiles(client, { github, tree, directory, where }) {
62
64
  let files
63
65
  try {
64
- files = await treeEntries(await client.getRepoTreeTarball({ repo: github, tree }), where)
66
+ files = await treeEntries(await client.getRepoTreeTarball({ repo: github, tree, symlinksAbove: directory.split('/').length - 1 }), where)
65
67
  } catch (error) {
66
68
  if (error instanceof ArchiveError) return null // e.g. a symlink out of the subtree
67
69
  throw error
@@ -129,7 +131,7 @@ async function subtreeEntries(client, list, { github, directory, packageManager,
129
131
  if (!(await installedAlone(packageManager, listing.map((entry) => entry.path), () => Promise.all(above.map(names))))) return null
130
132
  // Its tree id, as the listing above it holds it, held to that directory's own.
131
133
  const tree = (await list(above[0])).find((entry) => entry.path === posix.basename(directory) && entry.type === 'tree')?.sha
132
- const files = tree === undefined ? null : await treeFiles(client, { github, tree, where })
134
+ const files = tree === undefined ? null : await treeFiles(client, { github, tree, directory, where })
133
135
  return files === null || refersAbove(files, lockfileOf(packageManager)) ? null : files
134
136
  }
135
137
 
package/src/why.js CHANGED
@@ -1,4 +1,4 @@
1
- import { hasNodeModulesSegment, moduleFileKey } from '@exodus/stasis-core/util'
1
+ import { hasNodeModulesSegment, moduleFileKey, packageRecords } from '@exodus/stasis-core/util'
2
2
  import { isEvidenceFile } from './audit-corrections.js'
3
3
  import { parseFile } from './parse.js'
4
4
 
@@ -54,12 +54,14 @@ const TERMINAL_PACKAGES = new Set(['@babel/core', 'react-native', 'metro'])
54
54
  // `fileToDir` is complete; `dirFiles` only holds files that count as real code of
55
55
  // the dir (see audit-corrections.js) -- a consumer that recorded nothing of a
56
56
  // package but a corrected file / its package.json manifest is not one of its
57
- // reasons, exactly as in collectReasons.
57
+ // reasons, exactly as in collectReasons. A copy a dependency ships (packageRecords) is a module of its
58
+ // own here, so a chain can end at it (`next -> react-is`): its files are its node's, while its host's
59
+ // evidence keeps them too, the host's reasons being those of all its files, as in collectReasons.
58
60
  function moduleIndex(artifact) {
59
61
  const fileToDir = new Map()
60
62
  const dirInfo = new Map()
61
63
  const dirFiles = new Map()
62
- for (const [dir, { name, version, files }] of artifact.modules) {
64
+ for (const [dir, { name, version, files }] of packageRecords(artifact.modules)) {
63
65
  dirInfo.set(dir, { name: name ?? null, version: version ?? null, dep: hasNodeModulesSegment(dir) })
64
66
  const list = []
65
67
  for (const rel of Object.keys(files)) {