@preventive/triage 1.0.0-alpha.42 → 1.0.0-alpha.43
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/common/managed/report-filter.ts +2 -1
- package/common/security-links.d.ts +5 -0
- package/common/security-links.js +33 -0
- package/out/{chunk-WOTMRDM3.js → chunk-7RU5EDRO.js} +61 -61
- package/out/{chunk-D7AF2JXL.js → chunk-A7M4GV7M.js} +1 -1
- package/out/{chunk-DGVQPIYB.js → chunk-LAAB5SZT.js} +1 -1
- package/out/{chunk-YQRVLRDH.js → chunk-NWZ4PVPN.js} +1 -1
- package/out/{chunk-YP2537WX.js → chunk-OSIMGFUF.js} +1 -1
- package/out/{chunk-Q7ZSIEHP.js → chunk-XOCAPDCM.js} +1 -1
- package/out/client-managed.js +19 -19
- package/out/index.html +2 -2
- package/out/view.js +54 -54
- package/package.json +9 -3
- package/risk/index.d.ts +26 -0
- package/risk/index.js +1 -1
- package/risk/npm/assess.js +60 -14
- package/risk/npm/dependencies.js +11 -3
- package/risk/npm/new-dependency.js +42 -12
- package/risk/npm/registry.js +122 -21
- package/server-managed/http.ts +63 -26
- package/server-managed/locked-packages.ts +18 -6
- package/server-managed/npm-kept.ts +38 -14
- package/server-managed/npm-packages.ts +27 -13
- package/server-managed/npm-risk.ts +69 -11
- package/server-managed/queue.ts +23 -0
- package/server-managed/report-sources.ts +1 -1
- package/server-managed/repository-risk-assess.ts +105 -0
- package/server-managed/repository-risk-audit.ts +66 -0
- package/server-managed/repository-risk.ts +255 -201
- package/server-managed/scan-downloads.ts +87 -0
- package/server-managed/sql.ts +1 -1
- package/server-managed/team-reports.ts +6 -11
- package/server-managed/upstream-cache.ts +16 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// A package's risk line, as a repository risk scan tells it
|
|
2
|
+
// (repository-risk.ts): its version's document read and assessed, as risk
|
|
3
|
+
// tells one npm removed where npm has none; and how a failure is told.
|
|
4
|
+
import { setTimeout as delay } from 'node:timers/promises'
|
|
5
|
+
import { NpmRiskError, SEVERITIES, type Severity } from '../risk/index.js'
|
|
6
|
+
import { GithubApiError } from './github-errors.ts'
|
|
7
|
+
import { NpmPackageError, type NpmVersionDocument } from './npm-packages.ts'
|
|
8
|
+
import type { RepositoryRiskLine } from './repository-risk.ts'
|
|
9
|
+
|
|
10
|
+
// An assessment refused as busy (`npm-busy`, by npm-risk.ts or the npm reads
|
|
11
|
+
// beneath it), or that npm asked to ask later (risk's `upstream-busy`, as its
|
|
12
|
+
// downloads API answers past a burst of requests), is asked again after a
|
|
13
|
+
// wait, up to this many times.
|
|
14
|
+
export const BUSY_RETRIES = 20
|
|
15
|
+
export const BUSY_WAIT_MS = 1000
|
|
16
|
+
|
|
17
|
+
// How long a package's risk line, or a lockfile's advisories, is told as it
|
|
18
|
+
// is: after it, a risk line is told stale and assessed again, as risk tells
|
|
19
|
+
// again what of a package changes in time, and advisories asked again, as npm
|
|
20
|
+
// publishes them (repository-risk.ts).
|
|
21
|
+
export const RISK_KEPT_MS = 60 * 60_000
|
|
22
|
+
|
|
23
|
+
// The most severe of `findings`, or null for none.
|
|
24
|
+
export function highestSeverity(findings: readonly { severity: Severity }[]): Severity | null {
|
|
25
|
+
return SEVERITIES.find(severity => findings.some(finding => finding.severity === severity)) ?? null
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Why a step failed, as a line says it: an error's code, and for a lockfile
|
|
29
|
+
// refused, what of it its reader refused.
|
|
30
|
+
export type RiskFailure = { error: string; detail?: string }
|
|
31
|
+
type RiskLine = Extract<RepositoryRiskLine, { type: 'risk' }>
|
|
32
|
+
|
|
33
|
+
// What an error is known by: a GitHub or npm failure's code, or `failed`;
|
|
34
|
+
// GitHub not reached with what failed (githubUnreachable), while `doing` what
|
|
35
|
+
// of a repository, and risk's with what it asked of npm and how that failed,
|
|
36
|
+
// where they say.
|
|
37
|
+
export function failureOf(error: unknown, doing?: string): RiskFailure {
|
|
38
|
+
if (error instanceof GithubApiError && error.detail) return { error: error.message, detail: doing ? `${doing}: ${error.detail}` : error.detail }
|
|
39
|
+
if (error instanceof GithubApiError || error instanceof NpmPackageError) return { error: error.message }
|
|
40
|
+
if (!(error instanceof NpmRiskError)) return { error: 'failed' }
|
|
41
|
+
return error.message === error.code ? { error: error.code } : { error: error.code, detail: error.message }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const isBusy = (error: unknown) => (error instanceof NpmPackageError && error.status === 429) || (error instanceof NpmRiskError && error.code === 'upstream-busy')
|
|
45
|
+
|
|
46
|
+
// A package to assess: npm's of its name and version, or, with an
|
|
47
|
+
// `integrity`, a mirror's held to it.
|
|
48
|
+
export interface AssessedPackage { name: string; version: string; integrity?: string }
|
|
49
|
+
|
|
50
|
+
export type Assessment = { published: string | null; findings: { id: string; severity: Severity; takenDown?: unknown }[]; score: { score: number } }
|
|
51
|
+
|
|
52
|
+
// What of a scan's source assessing a package asks (RepositoryRiskSource).
|
|
53
|
+
export interface AssessingSource {
|
|
54
|
+
version(name: string, version: string, signal: AbortSignal): Promise<NpmVersionDocument>
|
|
55
|
+
assess(doc: NpmVersionDocument, teamScoped: boolean, signal: AbortSignal): Promise<Assessment>
|
|
56
|
+
removed(name: string, version: string, teamScoped: boolean, signal: AbortSignal): Promise<Assessment>
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// A package's risk line, its version's document kept and the assessment asked
|
|
60
|
+
// again after a wait while the server is busy; null once the scan stops. A
|
|
61
|
+
// version npm has no document of is told as risk tells one npm removed
|
|
62
|
+
// (removedOf), its malware keeping whether its package was taken down.
|
|
63
|
+
export async function assessPackage(source: AssessingSource, signal: AbortSignal, pkg: AssessedPackage, teamScoped: boolean): Promise<RiskLine | null> {
|
|
64
|
+
const at = { type: 'risk', ...pkg } as const
|
|
65
|
+
let doc: NpmVersionDocument | NpmPackageError | undefined
|
|
66
|
+
for (let attempt = 0; ; attempt++) {
|
|
67
|
+
try {
|
|
68
|
+
const read = doc ??= await documentOf(source, pkg, signal)
|
|
69
|
+
if (!(read instanceof NpmPackageError) && pkg.integrity !== undefined && !pkg.integrity.split(/\s+/u).includes(read.dist.integrity)) return { ...at, error: 'integrity-mismatch' }
|
|
70
|
+
const { published, findings, score } = read instanceof NpmPackageError ? await removedOf(source, pkg, teamScoped, signal, read) : await assessedOf(source, pkg, teamScoped, signal, read)
|
|
71
|
+
const told = findings.map(({ id, severity, takenDown }) => ({ id, severity, ...takenDown === true ? { takenDown: true as const } : {} }))
|
|
72
|
+
return { ...at, published, severity: highestSeverity(told), score: score.score, findings: told }
|
|
73
|
+
} catch (error) {
|
|
74
|
+
if (signal.aborted) return null
|
|
75
|
+
if (!isBusy(error) || attempt >= BUSY_RETRIES) return { ...at, ...failureOf(error) }
|
|
76
|
+
try { await delay(BUSY_WAIT_MS * Math.min(attempt + 1, 5), undefined, { signal }) } catch { return null }
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// npm's document of a version, or its answer that it has none (a 404).
|
|
82
|
+
async function documentOf(source: AssessingSource, pkg: AssessedPackage, signal: AbortSignal): Promise<NpmVersionDocument | NpmPackageError> {
|
|
83
|
+
try { return await source.version(pkg.name, pkg.version, signal) } catch (error) {
|
|
84
|
+
if (error instanceof NpmPackageError && error.status === 404) return error
|
|
85
|
+
throw error
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// A version's assessment; where risk finds npm no longer has it, as a document
|
|
90
|
+
// read from where versions' are kept doesn't tell, as one npm removed.
|
|
91
|
+
async function assessedOf(source: AssessingSource, pkg: AssessedPackage, teamScoped: boolean, signal: AbortSignal, doc: NpmVersionDocument): Promise<Assessment> {
|
|
92
|
+
try { return await source.assess(doc, teamScoped, signal) } catch (error) {
|
|
93
|
+
if (!(error instanceof NpmRiskError && error.code === 'version-not-found')) throw error
|
|
94
|
+
return removedOf(source, pkg, teamScoped, signal, new NpmPackageError(404, 'package-not-found'))
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// A version npm has no document of, as risk tells one npm removed, as it
|
|
99
|
+
// removes every one it finds malware in (`removed`): `gone`, npm's answer of
|
|
100
|
+
// none, thrown where nothing tells of it.
|
|
101
|
+
async function removedOf(source: AssessingSource, pkg: AssessedPackage, teamScoped: boolean, signal: AbortSignal, gone: NpmPackageError): Promise<Assessment> {
|
|
102
|
+
try { return await source.removed(pkg.name, pkg.version, teamScoped, signal) } catch (error) {
|
|
103
|
+
throw error instanceof NpmRiskError && (error.code === 'version-not-found' || error.code === 'package-not-found') ? gone : error
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
// A lockfile's advisories, as a repository risk scan asks them
|
|
2
|
+
// (repository-risk.ts): npm's registry's of its packages of the registry or
|
|
3
|
+
// a mirror, asked at once, as `npm audit` asks, each told with whether it is
|
|
4
|
+
// of malware (risk's npmMalwareAdvisory); kept by the lockfile's blob, in the
|
|
5
|
+
// instance's memory and the source's `audits`, for RISK_KEPT_MS, as npm
|
|
6
|
+
// publishes advisories, or why they couldn't be had for AUDIT_FAILED_KEPT_MS.
|
|
7
|
+
import type { Advisory } from '@preventive/upstream/advisories.js'
|
|
8
|
+
import { isExactVersion } from '@preventive/upstream/semver.js'
|
|
9
|
+
import { npmMalwareAdvisory } from '../risk/index.js'
|
|
10
|
+
import { isNpmPackageName } from '../common/managed/npm-packages.js'
|
|
11
|
+
import type { LockedPackage } from './locked-packages.ts'
|
|
12
|
+
import type { Lru } from './lru.ts'
|
|
13
|
+
import type { KeptStore } from './npm-kept.ts'
|
|
14
|
+
import { RISK_KEPT_MS, type RiskFailure, failureOf } from './repository-risk-assess.ts'
|
|
15
|
+
|
|
16
|
+
export type AdvisoryRow = Advisory & { malware: boolean }
|
|
17
|
+
// A lockfile's advisories, or why they couldn't be had.
|
|
18
|
+
export type Audit = AdvisoryRow[] | RiskFailure
|
|
19
|
+
|
|
20
|
+
export interface AuditSource {
|
|
21
|
+
// npm's registry's advisories of these packages' versions, asked at once.
|
|
22
|
+
advisories(packages: { name: string; versions: string[] }[], signal: AbortSignal): Promise<Advisory[]>
|
|
23
|
+
// Where audits are kept beyond this instance, by the lockfile's blob.
|
|
24
|
+
audits?: KeptStore<Audit>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// How long a lockfile's advisories that couldn't be had are told so, rather
|
|
28
|
+
// than asked again: a scan's next parts don't wait on an audit that failed,
|
|
29
|
+
// as by timing out, again and again.
|
|
30
|
+
const AUDIT_FAILED_KEPT_MS = 10 * 60_000
|
|
31
|
+
|
|
32
|
+
// Whether an audit kept `at` is told as it is: its advisories within
|
|
33
|
+
// RISK_KEPT_MS, why they couldn't be had within AUDIT_FAILED_KEPT_MS.
|
|
34
|
+
const isFresh = ({ at, audit }: { at: number; audit: Audit }) => Date.now() - at < (Array.isArray(audit) ? RISK_KEPT_MS : AUDIT_FAILED_KEPT_MS)
|
|
35
|
+
|
|
36
|
+
// The audit of a lockfile's `packages`, by its `key` as kept (`memory`, then
|
|
37
|
+
// the source's), else asked and kept; null where it locks none to ask of, or
|
|
38
|
+
// once `signal` aborts.
|
|
39
|
+
export async function auditLockfile(source: AuditSource, memory: Lru<{ at: number; audit: Audit }>, signal: AbortSignal, key: string,
|
|
40
|
+
packages: readonly LockedPackage[]): Promise<Audit | null> {
|
|
41
|
+
let kept = memory.get(key)
|
|
42
|
+
if (!kept || !isFresh(kept)) {
|
|
43
|
+
const stored = await source.audits?.read(key)
|
|
44
|
+
kept = stored ? { at: stored.at, audit: stored.value } : undefined
|
|
45
|
+
}
|
|
46
|
+
if (kept && isFresh(kept)) { memory.set(key, kept); return kept.audit }
|
|
47
|
+
const asked = new Map<string, Set<string>>()
|
|
48
|
+
for (const { name, version, source: from } of packages) {
|
|
49
|
+
if ((from !== 'registry' && from !== 'mirror') || !isNpmPackageName(name) || !isExactVersion(version)) continue
|
|
50
|
+
if (!asked.has(name)) asked.set(name, new Set())
|
|
51
|
+
asked.get(name)!.add(version)
|
|
52
|
+
}
|
|
53
|
+
if (asked.size === 0) return null
|
|
54
|
+
let audit: Audit
|
|
55
|
+
try {
|
|
56
|
+
audit = (await source.advisories([...asked].map(([name, versions]) => ({ name, versions: [...versions] })), signal))
|
|
57
|
+
.map(advisory => ({ ...advisory, malware: npmMalwareAdvisory(advisory) }))
|
|
58
|
+
} catch (error) {
|
|
59
|
+
if (signal.aborted) return null
|
|
60
|
+
audit = failureOf(error)
|
|
61
|
+
}
|
|
62
|
+
const now = Date.now()
|
|
63
|
+
memory.set(key, { at: now, audit })
|
|
64
|
+
await source.audits?.write(key, audit, now)
|
|
65
|
+
return audit
|
|
66
|
+
}
|