@exodus/stasis 1.0.0-beta.8 → 1.0.0-rc.0
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 +7 -2
- package/bin/stasis.js +263 -446
- package/package.json +38 -13
- package/src/audit-corrections.js +11 -42
- package/src/audit.js +256 -212
- package/src/babel.js +119 -82
- package/src/bundle-from-lockfile.js +9 -21
- package/src/bundle-reader.js +24 -8
- package/src/bundle.js +0 -3
- package/src/cmd/build.js +211 -117
- package/src/cmd/bundle-options.js +347 -0
- package/src/cmd/bundle.js +1718 -908
- package/src/cmd/diff.js +1 -4
- package/src/cmd/github-bundle.js +48 -21
- package/src/cmd/output.js +24 -12
- package/src/cmd/sbom.js +6 -9
- package/src/cmd/tokens.js +60 -0
- package/src/define.js +218 -0
- package/src/diff.js +73 -129
- package/src/esbuild.js +0 -4
- package/src/flow.js +27 -0
- package/src/installed.js +265 -0
- package/src/json-stream.js +4 -9
- package/src/loader-mock.js +3 -8
- package/src/loader.js +1 -5
- package/src/loaders/bash.js +43 -120
- package/src/loaders/cargo.js +301 -808
- package/src/loaders/foundry.js +112 -290
- package/src/loaders/paths.js +15 -26
- package/src/loaders/php.js +221 -475
- package/src/loaders/rust.js +518 -1461
- package/src/loaders/solidity-ownership.js +51 -142
- package/src/loaders/solidity.js +42 -188
- package/src/loaders/toml.js +7 -17
- package/src/lockfile.js +0 -3
- package/src/metro-assets.js +41 -0
- package/src/metro-resolver.js +58 -133
- package/src/metro-transformer.js +0 -6
- package/src/metro.js +0 -4
- package/src/mock.js +32 -80
- package/src/parse.js +8 -7
- package/src/prune.js +0 -3
- package/src/resolve-fields.js +170 -235
- package/src/resolve-node.js +33 -46
- package/src/resolve-typescript.js +166 -298
- package/src/rollup.js +0 -4
- package/src/run-installed.js +10 -0
- package/src/sbom.js +70 -97
- package/src/scan.js +241 -278
- package/src/sixel.js +310 -0
- package/src/table.js +95 -0
- package/src/validate-notes.d.ts +19 -0
- package/src/validate-notes.js +72 -0
- package/src/validate.d.ts +159 -0
- package/src/validate.js +1315 -0
- package/src/vfs-bundle/entries.js +54 -46
- package/src/vfs-bundle/github.js +115 -92
- package/src/vfs-bundle/glob.js +379 -0
- package/src/vfs-bundle/lazy.js +99 -0
- package/src/vfs-bundle/prisma.js +235 -105
- package/src/vfs-bundle/soldeer.js +28 -49
- package/src/vfs-bundle/tree.js +434 -259
- package/src/vfs-bundle/yarnclean.js +79 -0
- package/src/vfs-bundle.js +25 -51
- package/src/webpack.js +0 -4
- package/src/why.js +79 -231
package/src/audit.js
CHANGED
|
@@ -1,109 +1,108 @@
|
|
|
1
1
|
import { packageRepo } from '@exodus/stasis-core/bundle-util'
|
|
2
|
-
import { dependencyEcosystem, moduleFileKey, 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 { DIM, formatTable, painter, takesColors } from './table.js'
|
|
6
7
|
import { parseFile, parseFileWithKind } from './parse.js'
|
|
7
|
-
import {
|
|
8
|
+
import { logos } from './sixel.js'
|
|
9
|
+
import { collectWhy, invertReason, packageRecords } from './why.js'
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
// vendored from a registry, which OSV takes for crates.io's) and Composer packages; for Soldeer
|
|
11
|
-
// packages and GitHub repos (Foundry's lib/ submodules), the advisories their GitHub repository
|
|
12
|
-
// publishes, a Soldeer package's as Soldeer names it.
|
|
13
|
-
const SOURCES = { npm: ['npm'], cargo: ['OSV'], composer: ['OSV'], soldeer: ['Soldeer', 'GitHub'], github: ['GitHub'] }
|
|
11
|
+
export { formatTable }
|
|
14
12
|
|
|
15
|
-
//
|
|
16
|
-
|
|
17
|
-
|
|
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'] }
|
|
15
|
+
|
|
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.
|
|
18
18
|
const UNREGISTERED_CRATES = {
|
|
19
19
|
'cargo-git': 'a crate vendored from a git repository, not crates.io',
|
|
20
20
|
'cargo-unknown': 'a vendored crate with no .cargo-checksum.json, which may be a git checkout, not crates.io\'s',
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
// A
|
|
24
|
-
//
|
|
25
|
-
// the 0.0.0 stasis versions a repo with no version of its own by. Every advisory range covers both,
|
|
26
|
-
// 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.
|
|
27
25
|
const versionOf = (ecosystem, version) => (ecosystem === 'github' && version === '.' ? '0.0.0' : version)
|
|
28
26
|
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
|
|
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
32
|
|
|
33
|
-
//
|
|
34
|
-
|
|
33
|
+
// A usage error: its message alone, never a stack.
|
|
34
|
+
export class AuditError extends Error {
|
|
35
|
+
name = 'AuditError'
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// `dev-main`, `1.x-dev` (a `#ref` dropped), which no advisory database lists.
|
|
35
39
|
const isComposerDev = (version) => /^dev-|-dev$/iu.test(version.replace(/#.*$/su, ''))
|
|
36
40
|
|
|
37
|
-
// Why `pkg` cannot be audited, or undefined
|
|
38
|
-
function unaudited({ ecosystem, version }) {
|
|
41
|
+
// Why `pkg` cannot be audited, or undefined.
|
|
42
|
+
function unaudited({ ecosystem, version, vendoredIn }) {
|
|
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`
|
|
39
44
|
if (Object.hasOwn(UNREGISTERED_CRATES, ecosystem)) return UNREGISTERED_CRATES[ecosystem]
|
|
40
45
|
if (!Object.hasOwn(SOURCES, ecosystem)) return `no advisories are looked up for ${ecosystem}`
|
|
41
46
|
if (ecosystem === 'composer' && isComposerDev(version)) return 'a Composer dev version, which no advisory database lists'
|
|
42
47
|
return undefined
|
|
43
48
|
}
|
|
44
49
|
|
|
45
|
-
//
|
|
46
|
-
//
|
|
47
|
-
// vendored from git, or from where no `.cargo-checksum.json` tells, is listed but never asked about).
|
|
48
|
-
//
|
|
49
|
-
// A package counts as present only when its REAL code is, and recorded = present:
|
|
50
|
-
// an artifact records exactly the files it ships or attested -- imported, entry,
|
|
51
|
-
// or `add`-ed (the last two have no in-edge in the resolution graph, so presence
|
|
52
|
-
// must NOT be derived from edges). The package is audited only if some recorded
|
|
53
|
-
// file is code evidence (see audit-corrections.js) -- not a corrected file (ws's
|
|
54
|
-
// noop browser.js stub), not a manifest (package.json, recorded for resolver /
|
|
55
|
-
// metadata reads; Cargo.toml). So a ws shipped only as browser.js (+ manifest) is
|
|
56
|
-
// skipped, while one whose real code was bundled stays. Which consumers and import
|
|
57
|
-
// EDGES reach that code is the reason column's concern -- collectReasons and
|
|
58
|
-
// why.js apply the same evidence rule per file/edge there.
|
|
59
|
-
//
|
|
60
|
-
// A package's `github` is the repository its artifact records for it (its package.json's), where
|
|
61
|
-
// one does; else, for an npm package a bundle carries the package.json of (one built before builds
|
|
62
|
-
// recorded `repo`), the one that names, read by the rule builds record it by. A GitHub repo is its
|
|
63
|
-
// own, and has none.
|
|
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 }`.
|
|
64
52
|
export function collectPackagesFromFile(file) {
|
|
65
53
|
const out = []
|
|
66
54
|
const { kind, artifact } = parseFileWithKind(file)
|
|
67
|
-
for (const
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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 })
|
|
59
|
+
continue
|
|
60
|
+
}
|
|
73
61
|
let github = ecosystem === 'github' ? undefined : repo?.github
|
|
74
62
|
if (github === undefined && ecosystem === 'npm' && kind === 'bundle') github = manifestRepo(files['package.json'])
|
|
75
|
-
out.push({ ecosystem, name, version
|
|
63
|
+
out.push({ ecosystem, name, version, ...(github === undefined ? {} : { github }) })
|
|
76
64
|
}
|
|
77
65
|
return out
|
|
78
66
|
}
|
|
79
67
|
|
|
80
|
-
//
|
|
81
|
-
|
|
82
|
-
|
|
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.
|
|
83
84
|
function manifestRepo(text) {
|
|
84
85
|
if (typeof text !== 'string') return undefined
|
|
85
86
|
try {
|
|
86
|
-
return packageRepo(JSON.parse(text
|
|
87
|
+
return packageRepo(JSON.parse(withoutBom(text)))?.github
|
|
87
88
|
} catch {
|
|
88
89
|
return undefined
|
|
89
90
|
}
|
|
90
91
|
}
|
|
91
92
|
|
|
92
|
-
// Versions in semver's order where both are semver's, else as numbers in text compare.
|
|
93
93
|
const byNumbers = new Intl.Collator('en', { numeric: true }).compare
|
|
94
94
|
const byVersion = (a, b) => (valid(a) && valid(b) ? compareVersions(a, b) : byNumbers(a, b))
|
|
95
95
|
|
|
96
|
-
// Each package once, with the GitHub repository its
|
|
97
|
-
//
|
|
98
|
-
// with two, by two of its versions (a package that moved) or by two artifacts, has none, and is
|
|
99
|
-
// looked up as before.
|
|
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.
|
|
100
98
|
export function collectPackages(files) {
|
|
101
|
-
const byKey = new Map()
|
|
99
|
+
const byKey = new Map() // packageKey -> { pkg, hosts, only }
|
|
102
100
|
const repos = new Map() // `ecosystem:name` -> the one repository recorded for it, or null
|
|
103
101
|
for (const file of files) {
|
|
104
|
-
for (const { github, ...pkg } of collectPackagesFromFile(file)) {
|
|
105
|
-
const
|
|
106
|
-
if (
|
|
102
|
+
for (const { github, vendoredIn, ...pkg } of collectPackagesFromFile(file)) {
|
|
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)
|
|
107
106
|
if (github === undefined) continue
|
|
108
107
|
const name = `${pkg.ecosystem}:${pkg.name}`
|
|
109
108
|
const known = repos.get(name)
|
|
@@ -113,225 +112,270 @@ export function collectPackages(files) {
|
|
|
113
112
|
}
|
|
114
113
|
}
|
|
115
114
|
return [...byKey.values()]
|
|
116
|
-
.map((pkg) => {
|
|
115
|
+
.map(({ pkg, hosts, only }) => {
|
|
117
116
|
const github = repos.get(`${pkg.ecosystem}:${pkg.name}`)
|
|
118
|
-
return github ? {
|
|
117
|
+
return { ...pkg, ...(github ? { github } : {}), ...(hosts.size === 0 ? {} : { vendoredIn: [...hosts].toSorted(), vendoredOnly: only }) }
|
|
119
118
|
})
|
|
120
119
|
.toSorted((a, b) => a.ecosystem.localeCompare(b.ecosystem) || a.name.localeCompare(b.name) || byVersion(a.version, b.version))
|
|
121
120
|
}
|
|
122
121
|
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
// resolved back to its owning package via the module file listing. Only evidence files attribute
|
|
126
|
-
// (see audit-corrections.js): a consumer that recorded nothing of a package but a corrected file or
|
|
127
|
-
// its manifest carries none of its real code and is NOT a reason -- e.g. webpack shipping only ws's
|
|
128
|
-
// noop browser.js stub, while `run` bundles the real ws.
|
|
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.
|
|
129
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) {
|
|
130
130
|
const byPkg = new Map()
|
|
131
|
+
const consumers = new Set()
|
|
131
132
|
for (const file of files) {
|
|
132
133
|
const artifact = parseFile(file)
|
|
133
134
|
const fileReasons = invertReason(artifact.reason)
|
|
134
135
|
if (!fileReasons) continue
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
for (const rel of Object.keys(
|
|
140
|
-
if (
|
|
141
|
-
fileToPkg.set(moduleFileKey(dir, rel), keyOf(ecosystem, name, versionOf(ecosystem, version)))
|
|
136
|
+
for (const [consumer, list] of Object.entries(artifact.reason)) if (Array.isArray(list)) consumers.add(consumer)
|
|
137
|
+
const fileToPkgs = new Map()
|
|
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)
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
|
-
for (const [f,
|
|
145
|
-
const key
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
for (const consumer of consumers) set.add(consumer)
|
|
144
|
+
for (const [f, recorded] of fileReasons) {
|
|
145
|
+
for (const key of fileToPkgs.get(f) ?? []) {
|
|
146
|
+
const set = upsert(byPkg, key, () => new Set())
|
|
147
|
+
for (const consumer of recorded) set.add(consumer)
|
|
148
|
+
}
|
|
150
149
|
}
|
|
151
150
|
}
|
|
152
|
-
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
|
+
}
|
|
153
167
|
}
|
|
154
168
|
|
|
155
|
-
|
|
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 }
|
|
156
171
|
|
|
157
|
-
//
|
|
158
|
-
|
|
159
|
-
const CONSUMER_RANK = { run: 1, add: 2 }
|
|
172
|
+
// Bundler plugins first, ties alphabetically.
|
|
173
|
+
const CONSUMER_RANK = { run: 1, add: 2, extra: 3 }
|
|
160
174
|
const consumerRank = (c) => CONSUMER_RANK[c] ?? 0
|
|
161
175
|
const byConsumerOrder = (a, b) => consumerRank(a) - consumerRank(b) || a.localeCompare(b)
|
|
162
176
|
|
|
163
|
-
//
|
|
164
|
-
//
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
// `reasonFilter`, when set, narrows the cell to a single consumer: the `--why`
|
|
168
|
-
// chains are already filtered upstream (see collectWhy), so only the consumer
|
|
169
|
-
// list needs pruning here. collectWhy follows npm's import graph alone, so an
|
|
170
|
-
// advisory of another ecosystem keeps its consumer list under `--why` too.
|
|
171
|
-
function reasonCell({ ecosystem, name, versions: affected }, reasonsByPkg, whyByPkg, reasonFilter) {
|
|
172
|
-
const chains = whyByPkg !== null && ecosystem === 'npm'
|
|
173
|
-
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'
|
|
174
181
|
const source = chains ? whyByPkg : reasonsByPkg
|
|
175
|
-
|
|
176
|
-
for (const p of source.get(keyOf(ecosystem, name, v)) ?? []) parts.add(p)
|
|
177
|
-
}
|
|
178
|
-
// --why: group `consumer: path` lines by consumer and order the groups
|
|
179
|
-
// plugins -> run -> add (this also re-unites a consumer's lines when they were
|
|
180
|
-
// split across affected versions); within a group collectWhy's compressed order
|
|
181
|
-
// 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)) ?? [])]))
|
|
182
183
|
if (chains) {
|
|
184
|
+
// Grouping re-unites a consumer's lines split across versions; within a group collectWhy's order stands.
|
|
183
185
|
const byConsumer = Map.groupBy(parts, (line) => {
|
|
184
186
|
const i = line.indexOf(': ')
|
|
185
187
|
return i === -1 ? '' : line.slice(0, i)
|
|
186
188
|
})
|
|
187
189
|
return [...byConsumer.keys()].toSorted(byConsumerOrder).flatMap((c) => byConsumer.get(c)).join('\n')
|
|
188
190
|
}
|
|
189
|
-
|
|
190
|
-
return consumers.toSorted(byConsumerOrder).join(', ')
|
|
191
|
+
return [...parts].filter((c) => !reasonFilter || c === reasonFilter).toSorted(byConsumerOrder).join(', ')
|
|
191
192
|
}
|
|
192
193
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
194
|
+
function installedCell(adv, version, vendoredByKey) {
|
|
195
|
+
const pkg = vendoredByKey.get(keyOf(adv, version))
|
|
196
|
+
if (pkg?.vendoredIn === undefined) return version
|
|
197
|
+
return `${version} (${pkg.vendoredOnly ? '' : 'also '}vendored in ${pkg.vendoredIn.join(', ')})`
|
|
198
|
+
}
|
|
199
|
+
|
|
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.
|
|
204
|
+
export function flattenAdvisories(found, reasonsByPkg = new Map(), whyByPkg = null, reasonFilter = null, vendoredByKey = new Map()) {
|
|
197
205
|
const rows = []
|
|
198
206
|
for (const adv of found) {
|
|
199
207
|
const reason = reasonCell(adv, reasonsByPkg, whyByPkg, reasonFilter)
|
|
200
|
-
// --reason
|
|
201
|
-
// 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.
|
|
202
209
|
if (reasonFilter && reason === '') continue
|
|
203
210
|
rows.push({
|
|
204
211
|
ecosystem: adv.ecosystem,
|
|
205
212
|
package: adv.name,
|
|
206
|
-
installed: adv.versions.join(', '),
|
|
213
|
+
installed: adv.versions.map((v) => installedCell(adv, v, vendoredByKey)).join(', '),
|
|
207
214
|
vulnerable: adv.range ?? '',
|
|
208
|
-
severity: adv.severity ?? '',
|
|
215
|
+
severity: adv.malware ? 'malware' : adv.severity ?? '',
|
|
209
216
|
title: adv.title ?? '',
|
|
210
217
|
id: adv.id,
|
|
218
|
+
source: adv.source,
|
|
211
219
|
reason,
|
|
212
220
|
})
|
|
213
221
|
}
|
|
214
|
-
rows.
|
|
215
|
-
const sa = SEVERITY_ORDER[a.severity] ?? 99
|
|
216
|
-
const sb = SEVERITY_ORDER[b.severity] ?? 99
|
|
217
|
-
if (sa !== sb) return sa - sb
|
|
218
|
-
if (a.package !== b.package) return a.package < b.package ? -1 : 1
|
|
219
|
-
if (a.ecosystem !== b.ecosystem) return a.ecosystem < b.ecosystem ? -1 : 1
|
|
220
|
-
return a.title < b.title ? -1 : 1
|
|
221
|
-
})
|
|
222
|
-
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))
|
|
223
223
|
}
|
|
224
224
|
|
|
225
|
-
//
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
//
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
const header = columns.map((c) => [c])
|
|
240
|
-
const body = rows.map((r) => linesOf(r))
|
|
241
|
-
const widths = columns.map((c, i) =>
|
|
242
|
-
Math.max(c.length, ...body.map((cells) => Math.max(0, ...cells[i].map((l) => l.length))))
|
|
243
|
-
)
|
|
244
|
-
const pad = (s, w) => s.padEnd(w)
|
|
245
|
-
const line = (l, m, r, fill) => l + widths.map((w) => fill.repeat(w + 2)).join(m) + r
|
|
246
|
-
// A row spans as many physical lines as its tallest cell; shorter cells pad
|
|
247
|
-
// out with blanks so the borders stay aligned.
|
|
248
|
-
const render = (cells) => {
|
|
249
|
-
const height = Math.max(...cells.map((c) => c.length))
|
|
250
|
-
const out = []
|
|
251
|
-
for (let i = 0; i < height; i++) {
|
|
252
|
-
out.push('│ ' + cells.map((c, j) => pad(c[i] ?? '', widths[j])).join(' │ ') + ' │')
|
|
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.
|
|
229
|
+
why = why || whyDeep || whyFull
|
|
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(', ')}`}`)
|
|
253
239
|
}
|
|
254
|
-
|
|
240
|
+
reason = name(reason)
|
|
255
241
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
render(header),
|
|
259
|
-
line('├', '┼', '┤', '─'),
|
|
260
|
-
...body.map(render),
|
|
261
|
-
line('└', '┴', '┘', '─'),
|
|
262
|
-
].join('\n')
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
// `repoAdvisories` and `github` (a @preventive/upstream/github.js client) go to advisories() as they
|
|
266
|
-
// are; a Soldeer package or a GitHub repo needs the client, their repository being their only source.
|
|
267
|
-
// A package that cannot be audited is `skipped`, with why (`because`), and asked for nothing.
|
|
268
|
-
export async function audit(files, { why = false, whyDeep = false, whyFull = false, reason = null, repoAdvisories = false, github } = {}) {
|
|
269
|
-
// --why-deep and --why-full imply --why. Deep keeps every chain instead of the
|
|
270
|
-
// default pruning of chains whose full suffix is already a chain (see
|
|
271
|
-
// collectWhy/dropSuffixed); full spells chains out with no `...` collapse.
|
|
272
|
-
why = why || whyDeep || whyFull
|
|
273
|
-
const packages = collectPackages(files)
|
|
242
|
+
const all = collectPackages(files)
|
|
243
|
+
const packages = reason ? all.filter((pkg) => indexed().byPkg.get(packageKey(pkg, pkg.vendoredIn?.[0]))?.has(reason)) : all
|
|
274
244
|
const skipped = packages.flatMap((pkg) => {
|
|
275
245
|
const because = unaudited(pkg)
|
|
276
246
|
return because === undefined ? [] : [{ ...pkg, because }]
|
|
277
247
|
})
|
|
278
248
|
const asked = packages.filter((pkg) => unaudited(pkg) === undefined)
|
|
279
|
-
|
|
280
|
-
|
|
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
|
+
}
|
|
281
264
|
}
|
|
282
|
-
|
|
265
|
+
const others = [...byKey.values()]
|
|
266
|
+
if (asked.length === 0 && others.length === 0) {
|
|
267
|
+
return { packages, skipped, advisories: [], rows: [], npmMalware: [], why, whyDeep, whyFull, reason }
|
|
268
|
+
}
|
|
269
|
+
const query = (list) => list.map(({ version, vendoredIn: _in, vendoredOnly: _only, ...pkg }) => ({ ...pkg, versions: [version] }))
|
|
270
|
+
let result, elsewhere
|
|
283
271
|
try {
|
|
284
|
-
result = await
|
|
272
|
+
;[result, elsewhere] = await Promise.all([
|
|
273
|
+
asked.length > 0 ? advisories(query(asked), { repoAdvisories, github, osv }) : [],
|
|
274
|
+
others.length > 0 ? advisories(query(others)) : [],
|
|
275
|
+
])
|
|
285
276
|
} catch (cause) {
|
|
286
|
-
// Refused input or a malformed answer is an assertion that says so itself
|
|
287
|
-
// HTTP failure gets the context of which request it was.
|
|
277
|
+
// Refused input or a malformed answer is an assertion that says so itself.
|
|
288
278
|
if (cause?.code === 'ERR_ASSERTION') throw cause
|
|
289
|
-
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'] : [])])
|
|
290
280
|
throw new Error(`${[...sources].join('/')} advisories request failed: ${cause.message}`, { cause })
|
|
291
281
|
}
|
|
292
|
-
|
|
293
|
-
//
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
//
|
|
297
|
-
|
|
298
|
-
// unrelated to it.
|
|
299
|
-
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
|
|
300
288
|
if (why) {
|
|
301
|
-
const
|
|
302
|
-
|
|
303
|
-
const reasons = npm.length < result.length ? collectReasons(files) : undefined
|
|
304
|
-
rows = flattenAdvisories(result, reasons, collectWhy(files, targetKeys, reason, { deep: whyDeep, full: whyFull }), reason)
|
|
305
|
-
} else {
|
|
306
|
-
rows = flattenAdvisories(result, collectReasons(files), null, reason)
|
|
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 })
|
|
307
291
|
}
|
|
308
|
-
|
|
292
|
+
const rows = flattenAdvisories(result, indexed().byPkg, whyByPkg, reason, vendoredOf(asked))
|
|
293
|
+
return { packages, skipped, advisories: result, rows, npmMalware, why, whyDeep, whyFull, reason }
|
|
309
294
|
}
|
|
310
295
|
|
|
311
|
-
//
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
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(', ')})`
|
|
316
308
|
}
|
|
317
309
|
|
|
318
|
-
|
|
319
|
-
|
|
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}` : ''}`
|
|
320
334
|
if (packages.length === 0) {
|
|
321
|
-
err.write(`${scanned}\
|
|
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)
|
|
322
337
|
return
|
|
323
338
|
}
|
|
324
|
-
|
|
325
|
-
|
|
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) {
|
|
326
374
|
if (rows.length === 0) return
|
|
327
|
-
|
|
328
|
-
const
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
if (rows.some((r) => r.reason) && !(reason && !why)) columns.splice(columns.indexOf('installed') + 1, 0, 'reason')
|
|
334
|
-
// Under --why the reason cell is a list of `consumer: path` lines; let it span
|
|
335
|
-
// multiple physical rows instead of collapsing to one.
|
|
336
|
-
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`)
|
|
337
381
|
}
|