mandrel 2.74.0 → 2.76.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/.agents/audit-checklists/exceptions.md +24 -0
- package/.agents/docs/runbooks/mods.md +78 -16
- package/.agents/docs/workflows.md +2 -1
- package/.agents/mods/mandrel-status/hooks/register.tsx +942 -108
- package/.agents/mods/mandrel-status/types/index.d.ts +67 -11
- package/.agents/schemas/audit-exceptions-envelope.schema.json +268 -0
- package/.agents/schemas/audit-rules.json +43 -0
- package/.agents/schemas/story-progress.schema.json +48 -0
- package/.agents/scripts/audit-exceptions.js +145 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/ci-gates.js +65 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/code-allowlists.js +143 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/dependency-audit.js +160 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/dependency-patches.js +124 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/dependency-pins.js +177 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/dependency-policy.js +243 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/index.js +30 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/inline-suppressions.js +107 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/sca-exceptions.js +55 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/test-skips.js +96 -0
- package/.agents/scripts/lib/audit-exceptions/adapters/tool-config.js +273 -0
- package/.agents/scripts/lib/audit-exceptions/classify.js +64 -0
- package/.agents/scripts/lib/audit-exceptions/dependency-index.js +266 -0
- package/.agents/scripts/lib/audit-exceptions/engine.js +249 -0
- package/.agents/scripts/lib/audit-exceptions/globs.js +106 -0
- package/.agents/scripts/lib/audit-exceptions/history.js +126 -0
- package/.agents/scripts/lib/audit-exceptions/locate.js +59 -0
- package/.agents/scripts/lib/audit-exceptions/probe.js +196 -0
- package/.agents/scripts/lib/audit-exceptions/range-subset.js +153 -0
- package/.agents/scripts/lib/audit-exceptions/read.js +101 -0
- package/.agents/scripts/lib/audit-exceptions/record.js +102 -0
- package/.agents/scripts/lib/audit-exceptions/scope.js +131 -0
- package/.agents/scripts/lib/audit-exceptions/tickets.js +98 -0
- package/.agents/scripts/lib/audit-to-stories/audit-lenses.js +1 -0
- package/.agents/scripts/lib/config/temp-paths.js +15 -0
- package/.agents/scripts/lib/observability/source-classifier.js +1 -0
- package/.agents/scripts/lib/orchestration/single-story-close/runner.js +24 -3
- package/.agents/scripts/lib/orchestration/story-handoff.js +7 -0
- package/.agents/scripts/lib/orchestration/story-progress.js +127 -0
- package/.agents/workflows/audit-exceptions.md +235 -0
- package/docs/CHANGELOG.md +20 -0
- package/package.json +1 -1
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hand-maintained allowlists in code: an array or `Set` literal bound to a
|
|
3
|
+
* name that says "these are exempt" (`KNOWN_HELP_GAPS`, `EXEMPT_*`,
|
|
4
|
+
* `*_ALLOWLIST`, `QUARANTINED_*`, …). Enumerations and traversal config
|
|
5
|
+
* (`KNOWN_KINDS`, `ALLOWED_SEVERITIES`, `SKIP_DIRS`) are not exemptions and are
|
|
6
|
+
* not read. One record per string entry, justified by its own trailing comment
|
|
7
|
+
* or else the declaration's doc comment; an entry naming a repo path that no
|
|
8
|
+
* longer exists is `dead`.
|
|
9
|
+
*
|
|
10
|
+
* @module lib/audit-exceptions/adapters/code-allowlists
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { insideString } from '../locate.js';
|
|
14
|
+
import { readText } from '../read.js';
|
|
15
|
+
import { makeRecord } from '../record.js';
|
|
16
|
+
|
|
17
|
+
const SOURCE_EXT_RE = /\.[cm]?[jt]sx?$/;
|
|
18
|
+
|
|
19
|
+
/** Group one alternative so `|` binds per alternative. */
|
|
20
|
+
function group(alt) {
|
|
21
|
+
return `(?:${alt})`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const NAME_ALTERNATIVES = [
|
|
25
|
+
'KNOWN_[A-Z0-9_]*(?:GAPS?|FAILURES?|FAILING|BROKEN|ISSUES?|VIOLATIONS?|EXCEPTIONS?|DEBT|LEAKS?|OFFENDERS?|DRIFT)',
|
|
26
|
+
'(?:EXEMPT(?:ED|IONS?)?|QUARANTINED?|GRANDFATHER(?:ED)?|WAIVED|WAIVERS?)_[A-Z0-9_]+',
|
|
27
|
+
'[A-Z0-9_]+_(?:ALLOWLIST|ALLOW_LIST|EXEMPT(?:ED|IONS?)?|EXCEPTIONS|WAIVERS?|GRANDFATHERED|QUARANTINED?)',
|
|
28
|
+
].map(group);
|
|
29
|
+
|
|
30
|
+
/** Cheap whole-file prefilter. */
|
|
31
|
+
const NAME_RE = new RegExp(NAME_ALTERNATIVES.join('|'));
|
|
32
|
+
const EXACT_NAME_RE = new RegExp(
|
|
33
|
+
['^(?:', NAME_ALTERNATIVES.join('|'), ')$'].join(''),
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const DECL_RE =
|
|
37
|
+
/(?:const|let|var)\s+([A-Z][A-Z0-9_]*)\s*(?::[^=]+)?=\s*(?:Object\.freeze\(\s*)?(?:new Set\(\s*)?\[/;
|
|
38
|
+
const ENTRY_RE = /(['"])((?:(?!\1).)+)\1/g;
|
|
39
|
+
const PATH_LIKE_RE = /^[\w.@-]+(?:\/[\w.@-]+)+\.[A-Za-z0-9]+$/;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param {string[]} lines
|
|
43
|
+
* @param {number} start - index of the declaration line.
|
|
44
|
+
* @returns {number} index of the line closing the literal.
|
|
45
|
+
*/
|
|
46
|
+
function literalEnd(lines, start) {
|
|
47
|
+
let depth = 0;
|
|
48
|
+
for (let i = start; i < lines.length; i += 1) {
|
|
49
|
+
const code = lines[i].replace(/\/\/.*$/, '');
|
|
50
|
+
depth +=
|
|
51
|
+
(code.match(/\[/g) ?? []).length - (code.match(/\]/g) ?? []).length;
|
|
52
|
+
if (depth <= 0) return i;
|
|
53
|
+
}
|
|
54
|
+
return lines.length - 1;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const COMMENT_LINE_RE = /^(?:\/\/|\/\*|\*)/;
|
|
58
|
+
|
|
59
|
+
/** The contiguous comment block directly above the declaration line. */
|
|
60
|
+
function docComment(lines, declIndex) {
|
|
61
|
+
const block = [];
|
|
62
|
+
for (
|
|
63
|
+
let i = declIndex - 1;
|
|
64
|
+
i >= 0 && COMMENT_LINE_RE.test(lines[i].trim());
|
|
65
|
+
i -= 1
|
|
66
|
+
) {
|
|
67
|
+
block.unshift(lines[i].trim().replace(/^(?:\/\/+|\/\*+|\*+\/?)\s*/, ''));
|
|
68
|
+
}
|
|
69
|
+
return block.join(' ').trim() || null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The trailing `//` comment on an entry line, or a pure comment line above. */
|
|
73
|
+
function reasonFor(lines, i) {
|
|
74
|
+
const trailing = /\/\/\s*(.*)$/.exec(lines[i])?.[1];
|
|
75
|
+
if (trailing) return trailing;
|
|
76
|
+
const prev = lines[i - 1]?.trim() ?? '';
|
|
77
|
+
return /^\/\//.test(prev) ? prev.replace(/^\/\/+\s*/, '') : null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function entryProbe(ctx, entry) {
|
|
81
|
+
if (!PATH_LIKE_RE.test(entry)) return null;
|
|
82
|
+
return ctx.scope.fileSet.has(entry)
|
|
83
|
+
? { verdict: 'live', basis: 'path-exists' }
|
|
84
|
+
: { verdict: 'dead', basis: 'path-missing' };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function entryRecords(ctx, file, name, lines, from, to) {
|
|
88
|
+
const declReason = docComment(lines, from);
|
|
89
|
+
const records = [];
|
|
90
|
+
for (let i = from; i <= to; i += 1) {
|
|
91
|
+
const code = i === from ? lines[i].slice(lines[i].indexOf('[')) : lines[i];
|
|
92
|
+
for (const m of code.replace(/\/\/.*$/, '').matchAll(ENTRY_RE)) {
|
|
93
|
+
records.push(
|
|
94
|
+
makeRecord({
|
|
95
|
+
adapter: 'code-allowlists',
|
|
96
|
+
category: 'allowlist',
|
|
97
|
+
surface: 'source',
|
|
98
|
+
file,
|
|
99
|
+
line: i + 1,
|
|
100
|
+
target: m[2],
|
|
101
|
+
rule: name,
|
|
102
|
+
justification:
|
|
103
|
+
(i === from ? null : reasonFor(lines, i)) ?? declReason,
|
|
104
|
+
probe: entryProbe(ctx, m[2]),
|
|
105
|
+
}),
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return records;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function fileRecords(ctx, file) {
|
|
113
|
+
const text = readText(ctx.root, file) ?? '';
|
|
114
|
+
if (!NAME_RE.test(text)) return [];
|
|
115
|
+
const lines = text.split('\n');
|
|
116
|
+
const records = [];
|
|
117
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
118
|
+
const decl = DECL_RE.exec(lines[i]);
|
|
119
|
+
if (
|
|
120
|
+
!decl ||
|
|
121
|
+
!EXACT_NAME_RE.test(decl[1]) ||
|
|
122
|
+
insideString(lines[i], decl.index)
|
|
123
|
+
)
|
|
124
|
+
continue;
|
|
125
|
+
const end = literalEnd(lines, i);
|
|
126
|
+
records.push(...entryRecords(ctx, file, decl[1], lines, i, end));
|
|
127
|
+
i = end;
|
|
128
|
+
}
|
|
129
|
+
return records;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export const codeAllowlists = Object.freeze({
|
|
133
|
+
id: 'code-allowlists',
|
|
134
|
+
category: 'allowlist',
|
|
135
|
+
applies: (ctx) =>
|
|
136
|
+
ctx.scope.files.some((f) => SOURCE_EXT_RE.test(f))
|
|
137
|
+
? { applies: true, reason: 'source files present' }
|
|
138
|
+
: { applies: false, reason: 'no JS/TS source files' },
|
|
139
|
+
extract: (ctx) =>
|
|
140
|
+
ctx.scope.files
|
|
141
|
+
.filter((f) => SOURCE_EXT_RE.test(f))
|
|
142
|
+
.flatMap((f) => fileRecords(ctx, f)),
|
|
143
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Supply-chain relaxations: npm-audit allowlists (`.nsprc`, `audit-ci`) and
|
|
3
|
+
* `.npmrc` settings that loosen install-time checks. An allowlisted advisory
|
|
4
|
+
* is judged by its justification and expiry only — no network lookup — but an
|
|
5
|
+
* entry scoped to a package that left the tree is `dead`.
|
|
6
|
+
*
|
|
7
|
+
* @module lib/audit-exceptions/adapters/dependency-audit
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { lineOf } from '../locate.js';
|
|
12
|
+
import { readJsonc, readText } from '../read.js';
|
|
13
|
+
import { makeRecord } from '../record.js';
|
|
14
|
+
import { nameProbe } from './dependency-policy.js';
|
|
15
|
+
|
|
16
|
+
const AUDIT_CI_FILES = Object.freeze(['audit-ci.json', 'audit-ci.jsonc']);
|
|
17
|
+
const ADVISORY_RE = /^(?:GHSA-[\w-]+|CVE-\d+-\d+|\d+)$/i;
|
|
18
|
+
|
|
19
|
+
/** `.npmrc` keys whose value loosens a check, with the loosening value. */
|
|
20
|
+
const NPMRC_RELAXATIONS = Object.freeze({
|
|
21
|
+
'legacy-peer-deps': 'true',
|
|
22
|
+
'strict-peer-dependencies': 'false',
|
|
23
|
+
'engine-strict': 'false',
|
|
24
|
+
'strict-ssl': 'false',
|
|
25
|
+
audit: 'false',
|
|
26
|
+
force: 'true',
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
function expiryText(expiry) {
|
|
30
|
+
if (typeof expiry === 'number')
|
|
31
|
+
return new Date(expiry).toISOString().slice(0, 10);
|
|
32
|
+
return typeof expiry === 'string' ? expiry.slice(0, 10) : null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Fold `notes` and `expiry` into one justification string `record.js` reads. */
|
|
36
|
+
function entryJustification(meta) {
|
|
37
|
+
if (typeof meta === 'string') return meta;
|
|
38
|
+
if (!meta || typeof meta !== 'object') return null;
|
|
39
|
+
const expiry = expiryText(meta.expiry);
|
|
40
|
+
const parts = [
|
|
41
|
+
meta.notes ?? meta.reason,
|
|
42
|
+
expiry ? `expires: ${expiry}` : null,
|
|
43
|
+
];
|
|
44
|
+
return parts.filter(Boolean).join(' ') || null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** `GHSA-x|a>b` → probe package `b`; a bare package name → itself; an id → none. */
|
|
48
|
+
function allowlistProbe(ctx, entry) {
|
|
49
|
+
const [id, scopePath] = entry.split('|');
|
|
50
|
+
if (scopePath) return nameProbe(ctx, scopePath.split('>').pop());
|
|
51
|
+
if (ADVISORY_RE.test(id))
|
|
52
|
+
return { verdict: 'live', basis: 'advisory-unprobed' };
|
|
53
|
+
return nameProbe(ctx, id.split('>').pop());
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function allowRecord(ctx, { file, text, surface, entry, meta }) {
|
|
57
|
+
return makeRecord({
|
|
58
|
+
adapter: 'dependency-audit',
|
|
59
|
+
category: 'dependency',
|
|
60
|
+
surface,
|
|
61
|
+
file,
|
|
62
|
+
line: lineOf(text, entry),
|
|
63
|
+
target: entry,
|
|
64
|
+
rule: surface,
|
|
65
|
+
justification: entryJustification(meta),
|
|
66
|
+
probe: allowlistProbe(ctx, entry),
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function nsprcRecords(ctx) {
|
|
71
|
+
if (!ctx.scope.fileSet.has('.nsprc')) return [];
|
|
72
|
+
const config = readJsonc(ctx.root, '.nsprc');
|
|
73
|
+
if (config === null) {
|
|
74
|
+
ctx.degrade('config-not-statically-readable', '.nsprc');
|
|
75
|
+
return [];
|
|
76
|
+
}
|
|
77
|
+
const text = readText(ctx.root, '.nsprc');
|
|
78
|
+
return Object.entries(config)
|
|
79
|
+
.filter(([, meta]) => meta?.active !== false)
|
|
80
|
+
.map(([entry, meta]) =>
|
|
81
|
+
allowRecord(ctx, { file: '.nsprc', text, surface: 'nsprc', entry, meta }),
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function auditCiEntries(allowlist) {
|
|
86
|
+
return (allowlist ?? []).flatMap((item) =>
|
|
87
|
+
typeof item === 'string'
|
|
88
|
+
? [{ entry: item, meta: null }]
|
|
89
|
+
: Object.entries(item ?? {}).map(([entry, meta]) => ({ entry, meta })),
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function auditCiRecords(ctx) {
|
|
94
|
+
return AUDIT_CI_FILES.filter((f) => ctx.scope.fileSet.has(f)).flatMap(
|
|
95
|
+
(file) => {
|
|
96
|
+
const config = readJsonc(ctx.root, file);
|
|
97
|
+
if (config === null) {
|
|
98
|
+
ctx.degrade('config-not-statically-readable', file);
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
const text = readText(ctx.root, file);
|
|
102
|
+
return auditCiEntries(config.allowlist)
|
|
103
|
+
.filter(({ meta }) => meta?.active !== false)
|
|
104
|
+
.map(({ entry, meta }) =>
|
|
105
|
+
allowRecord(ctx, {
|
|
106
|
+
file,
|
|
107
|
+
text,
|
|
108
|
+
surface: 'audit-ci-allowlist',
|
|
109
|
+
entry,
|
|
110
|
+
meta,
|
|
111
|
+
}),
|
|
112
|
+
);
|
|
113
|
+
},
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The comment line directly above `index`, if any. */
|
|
118
|
+
function commentAbove(lines, index) {
|
|
119
|
+
const prev = lines[index - 1]?.trim() ?? '';
|
|
120
|
+
return /^[#;]/.test(prev) ? prev.replace(/^[#;]+/, '') : null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function npmrcRecords(ctx, file) {
|
|
124
|
+
const lines = (readText(ctx.root, file) ?? '').split('\n');
|
|
125
|
+
const records = [];
|
|
126
|
+
lines.forEach((raw, i) => {
|
|
127
|
+
const m = /^\s*([\w-]+)\s*=\s*([^#;\s]+)\s*(?:[#;](.*))?$/.exec(raw);
|
|
128
|
+
if (!m || NPMRC_RELAXATIONS[m[1]] !== m[2].toLowerCase()) return;
|
|
129
|
+
records.push(
|
|
130
|
+
makeRecord({
|
|
131
|
+
adapter: 'dependency-audit',
|
|
132
|
+
category: 'dependency',
|
|
133
|
+
surface: 'npmrc',
|
|
134
|
+
file,
|
|
135
|
+
line: i + 1,
|
|
136
|
+
target: `${m[1]}=${m[2]}`,
|
|
137
|
+
rule: `npmrc-${m[1]}`,
|
|
138
|
+
justification: m[3] ?? commentAbove(lines, i),
|
|
139
|
+
probe: { verdict: 'live', basis: 'setting-in-force' },
|
|
140
|
+
}),
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
return records;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export const dependencyAudit = Object.freeze({
|
|
147
|
+
id: 'dependency-audit',
|
|
148
|
+
category: 'dependency',
|
|
149
|
+
applies: (ctx) =>
|
|
150
|
+
ctx.scope.manifests.length > 0
|
|
151
|
+
? { applies: true, reason: 'package manifests present' }
|
|
152
|
+
: { applies: false, reason: 'no package.json' },
|
|
153
|
+
extract: (ctx) => [
|
|
154
|
+
...nsprcRecords(ctx),
|
|
155
|
+
...auditCiRecords(ctx),
|
|
156
|
+
...ctx.scope.files
|
|
157
|
+
.filter((f) => path.posix.basename(f) === '.npmrc')
|
|
158
|
+
.flatMap((f) => npmrcRecords(ctx, f)),
|
|
159
|
+
],
|
|
160
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Patched dependencies: patch-package files (`patches/name+1.2.3.patch`) and
|
|
3
|
+
* pnpm `patchedDependencies` (in `package.json#pnpm` or `pnpm-workspace.yaml`).
|
|
4
|
+
* A patch is `dead` when its package left the tree, when it targets a version
|
|
5
|
+
* that no longer resolves, or when the patch file it names is gone.
|
|
6
|
+
*
|
|
7
|
+
* @module lib/audit-exceptions/adapters/dependency-patches
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { lineOf } from '../locate.js';
|
|
12
|
+
import { readText } from '../read.js';
|
|
13
|
+
import { makeRecord } from '../record.js';
|
|
14
|
+
import { noteFor } from './dependency-pins.js';
|
|
15
|
+
|
|
16
|
+
const PATCH_FILE_RE = /(?:^|\/)patches\/([^/]+)\.patch$/;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* `@scope+name+1.2.3` / `parent++child+1.0.0` → `{ name, version }`.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} stem
|
|
22
|
+
* @returns {{ name: string, version: string }|null}
|
|
23
|
+
*/
|
|
24
|
+
function parsePatchPackageStem(stem) {
|
|
25
|
+
const parts = stem.split('++').pop().split('+');
|
|
26
|
+
if (parts[0].startsWith('@') && parts.length >= 3) {
|
|
27
|
+
return { name: `${parts[0]}/${parts[1]}`, version: parts[2] };
|
|
28
|
+
}
|
|
29
|
+
return parts.length >= 2 ? { name: parts[0], version: parts[1] } : null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** pnpm `name@1.2.3` (or bare `name`, meaning every version). */
|
|
33
|
+
function parsePnpmPatchKey(key) {
|
|
34
|
+
const at = key.indexOf('@', 1);
|
|
35
|
+
return at === -1
|
|
36
|
+
? { name: key, version: null }
|
|
37
|
+
: { name: key.slice(0, at), version: key.slice(at + 1) };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {{ name: string, version: string|null }} target
|
|
42
|
+
* @param {object} index
|
|
43
|
+
* @returns {{ verdict: string, basis: string }}
|
|
44
|
+
*/
|
|
45
|
+
function patchProbe(target, index) {
|
|
46
|
+
const versions = index.present?.get(target.name);
|
|
47
|
+
if (index.present === null)
|
|
48
|
+
return { verdict: 'unknown', basis: 'no-lockfile' };
|
|
49
|
+
if (!versions) return { verdict: 'dead', basis: 'absent-from-tree' };
|
|
50
|
+
if (target.version && versions.size > 0 && !versions.has(target.version)) {
|
|
51
|
+
return { verdict: 'dead', basis: 'version-drift' };
|
|
52
|
+
}
|
|
53
|
+
return { verdict: 'live', basis: 'applies-to-resolved' };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function patchPackageRecords(ctx) {
|
|
57
|
+
const records = [];
|
|
58
|
+
for (const rel of ctx.scope.files) {
|
|
59
|
+
const m = PATCH_FILE_RE.exec(rel);
|
|
60
|
+
const target = m?.[1].includes('+') ? parsePatchPackageStem(m[1]) : null;
|
|
61
|
+
if (!target) continue;
|
|
62
|
+
records.push(
|
|
63
|
+
makeRecord({
|
|
64
|
+
adapter: 'dependency-patches',
|
|
65
|
+
category: 'dependency',
|
|
66
|
+
surface: 'patch-package',
|
|
67
|
+
file: rel,
|
|
68
|
+
line: 1,
|
|
69
|
+
target: target.name,
|
|
70
|
+
rule: 'patch-package',
|
|
71
|
+
justification: noteFor(
|
|
72
|
+
ctx.scope.manifests[0]?.pkg,
|
|
73
|
+
`patches.${target.name}`,
|
|
74
|
+
),
|
|
75
|
+
probe: patchProbe(target, ctx.deps()),
|
|
76
|
+
}),
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return records;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function pnpmPatchRecords(ctx, file, map, notesPkg) {
|
|
83
|
+
const text = readText(ctx.root, file);
|
|
84
|
+
return Object.entries(map ?? {}).map(([key, patchPath]) => {
|
|
85
|
+
const target = parsePnpmPatchKey(key);
|
|
86
|
+
const missing =
|
|
87
|
+
typeof patchPath === 'string' &&
|
|
88
|
+
!ctx.scope.fileSet.has(path.posix.normalize(patchPath));
|
|
89
|
+
return makeRecord({
|
|
90
|
+
adapter: 'dependency-patches',
|
|
91
|
+
category: 'dependency',
|
|
92
|
+
surface: 'pnpm-patchedDependencies',
|
|
93
|
+
file,
|
|
94
|
+
line: lineOf(text, key, ['patchedDependencies']),
|
|
95
|
+
target: target.name,
|
|
96
|
+
rule: 'pnpm-patchedDependencies',
|
|
97
|
+
justification: noteFor(notesPkg, `pnpm.patchedDependencies.${key}`),
|
|
98
|
+
probe: missing
|
|
99
|
+
? { verdict: 'dead', basis: 'patch-file-missing' }
|
|
100
|
+
: patchProbe(target, ctx.deps()),
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export const dependencyPatches = Object.freeze({
|
|
106
|
+
id: 'dependency-patches',
|
|
107
|
+
category: 'dependency',
|
|
108
|
+
applies: (ctx) =>
|
|
109
|
+
ctx.scope.manifests.length > 0
|
|
110
|
+
? { applies: true, reason: 'package manifests present' }
|
|
111
|
+
: { applies: false, reason: 'no package.json' },
|
|
112
|
+
extract: (ctx) => [
|
|
113
|
+
...patchPackageRecords(ctx),
|
|
114
|
+
...ctx.scope.manifests.flatMap((m) =>
|
|
115
|
+
pnpmPatchRecords(ctx, m.rel, m.pkg.pnpm?.patchedDependencies, m.pkg),
|
|
116
|
+
),
|
|
117
|
+
...pnpmPatchRecords(
|
|
118
|
+
ctx,
|
|
119
|
+
'pnpm-workspace.yaml',
|
|
120
|
+
ctx.scope.pnpmWorkspace?.patchedDependencies,
|
|
121
|
+
null,
|
|
122
|
+
),
|
|
123
|
+
],
|
|
124
|
+
});
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version pins that override what the tree would resolve on its own: npm
|
|
3
|
+
* `overrides`, pnpm `overrides` (in `package.json#pnpm` or
|
|
4
|
+
* `pnpm-workspace.yaml`) and yarn `resolutions`, in the root and every
|
|
5
|
+
* workspace manifest. A pin is `dead` when its package left the tree, or when
|
|
6
|
+
* every dependent already asks for a range inside the pin — removing it would
|
|
7
|
+
* change nothing.
|
|
8
|
+
*
|
|
9
|
+
* @module lib/audit-exceptions/adapters/dependency-pins
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { nameOfSpec } from '../dependency-index.js';
|
|
13
|
+
import { lineOf } from '../locate.js';
|
|
14
|
+
import { rangeIsSubset } from '../range-subset.js';
|
|
15
|
+
import { readText } from '../read.js';
|
|
16
|
+
import { makeRecord } from '../record.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The package.json `//` note for a key, e.g. `"//": { "overrides.js-yaml": "…" }`.
|
|
20
|
+
*
|
|
21
|
+
* @param {object} pkg
|
|
22
|
+
* @param {string} key
|
|
23
|
+
* @returns {string|null}
|
|
24
|
+
*/
|
|
25
|
+
export function noteFor(pkg, key) {
|
|
26
|
+
const notes = pkg?.['//'];
|
|
27
|
+
if (!notes || typeof notes !== 'object' || Array.isArray(notes)) return null;
|
|
28
|
+
const note = notes[key];
|
|
29
|
+
return typeof note === 'string' ? note : null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** npm `overrides` → leaves `{ key, value }`; `"."` pins the parent itself. */
|
|
33
|
+
function npmLeaves(node, out = []) {
|
|
34
|
+
for (const [key, value] of Object.entries(node ?? {})) {
|
|
35
|
+
if (typeof value === 'string') {
|
|
36
|
+
out.push({ key, target: nameOfSpec(key), value });
|
|
37
|
+
} else if (value && typeof value === 'object') {
|
|
38
|
+
if (typeof value['.'] === 'string') {
|
|
39
|
+
out.push({ key, target: nameOfSpec(key), value: value['.'] });
|
|
40
|
+
}
|
|
41
|
+
npmLeaves(
|
|
42
|
+
Object.fromEntries(Object.entries(value).filter(([k]) => k !== '.')),
|
|
43
|
+
out,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** pnpm `bar@1>foo@2` → `foo`. */
|
|
51
|
+
function pnpmTarget(key) {
|
|
52
|
+
return nameOfSpec(key.split('>').pop());
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** yarn `**\/parent/@scope/foo@1` → `@scope/foo`. */
|
|
56
|
+
function yarnTarget(key) {
|
|
57
|
+
const parts = key.split('/');
|
|
58
|
+
const last = parts.pop();
|
|
59
|
+
const scope = parts.at(-1);
|
|
60
|
+
return nameOfSpec(scope?.startsWith('@') ? `${scope}/${last}` : last);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function flatLeaves(node, toTarget) {
|
|
64
|
+
return Object.entries(node ?? {})
|
|
65
|
+
.filter(([, value]) => typeof value === 'string')
|
|
66
|
+
.map(([key, value]) => ({ key, target: toTarget(key), value }));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {string} name
|
|
71
|
+
* @param {string|null} range - the pinned range; `null` when not comparable.
|
|
72
|
+
* @param {object} index - from `buildDependencyIndex`.
|
|
73
|
+
* @returns {{ verdict: string, basis: string }}
|
|
74
|
+
*/
|
|
75
|
+
function pinProbe(name, range, index) {
|
|
76
|
+
const inTree = index.present?.has(name) ?? null;
|
|
77
|
+
if (inTree === false && !index.declared.has(name)) {
|
|
78
|
+
return { verdict: 'dead', basis: 'absent-from-tree' };
|
|
79
|
+
}
|
|
80
|
+
if (range === null || index.dependents === null) {
|
|
81
|
+
return { verdict: 'unknown', basis: 'redundancy-unprobed' };
|
|
82
|
+
}
|
|
83
|
+
const asks = index.dependents.get(name) ?? [];
|
|
84
|
+
if (asks.length === 0)
|
|
85
|
+
return { verdict: 'unknown', basis: 'no-dependent-ranges' };
|
|
86
|
+
const verdicts = asks.map((a) => rangeIsSubset(a.range, range));
|
|
87
|
+
if (verdicts.includes(false))
|
|
88
|
+
return { verdict: 'live', basis: 'dependent-needs-pin' };
|
|
89
|
+
if (verdicts.includes(null))
|
|
90
|
+
return { verdict: 'unknown', basis: 'range-undecidable' };
|
|
91
|
+
return { verdict: 'dead', basis: 'redundant' };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** `$name` → the manifest's own direct range for `name`. */
|
|
95
|
+
function resolveValue(value, pkg) {
|
|
96
|
+
const ref = /^\$(.*)$/.exec(value);
|
|
97
|
+
if (!ref) return value === '-' ? null : value;
|
|
98
|
+
const name = ref[1];
|
|
99
|
+
return pkg.dependencies?.[name] ?? pkg.devDependencies?.[name] ?? null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const SOURCES = Object.freeze([
|
|
103
|
+
{
|
|
104
|
+
surface: 'npm-overrides',
|
|
105
|
+
from: (pkg) => npmLeaves(pkg.overrides),
|
|
106
|
+
noteKey: (key) => `overrides.${key}`,
|
|
107
|
+
anchors: ['"overrides"'],
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
surface: 'pnpm-overrides',
|
|
111
|
+
from: (pkg) => flatLeaves(pkg.pnpm?.overrides, pnpmTarget),
|
|
112
|
+
noteKey: (key) => `pnpm.overrides.${key}`,
|
|
113
|
+
anchors: ['"pnpm"', '"overrides"'],
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
surface: 'yarn-resolutions',
|
|
117
|
+
from: (pkg) => flatLeaves(pkg.resolutions, yarnTarget),
|
|
118
|
+
noteKey: (key) => `resolutions.${key}`,
|
|
119
|
+
anchors: ['"resolutions"'],
|
|
120
|
+
},
|
|
121
|
+
]);
|
|
122
|
+
|
|
123
|
+
function manifestRecords(ctx, manifest) {
|
|
124
|
+
const text = readText(ctx.root, manifest.rel);
|
|
125
|
+
const records = [];
|
|
126
|
+
for (const source of SOURCES) {
|
|
127
|
+
for (const leaf of source.from(manifest.pkg)) {
|
|
128
|
+
const range = resolveValue(leaf.value, manifest.pkg);
|
|
129
|
+
records.push(
|
|
130
|
+
makeRecord({
|
|
131
|
+
adapter: 'dependency-pins',
|
|
132
|
+
category: 'dependency',
|
|
133
|
+
surface: source.surface,
|
|
134
|
+
file: manifest.rel,
|
|
135
|
+
line: lineOf(text, `"${leaf.key}"`, source.anchors),
|
|
136
|
+
target: leaf.target,
|
|
137
|
+
rule: source.surface,
|
|
138
|
+
justification: noteFor(manifest.pkg, source.noteKey(leaf.key)),
|
|
139
|
+
probe: pinProbe(leaf.target, range, ctx.deps()),
|
|
140
|
+
}),
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return records;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function workspaceYamlRecords(ctx) {
|
|
148
|
+
const overrides = ctx.scope.pnpmWorkspace?.overrides;
|
|
149
|
+
if (!overrides) return [];
|
|
150
|
+
const text = readText(ctx.root, 'pnpm-workspace.yaml');
|
|
151
|
+
return flatLeaves(overrides, pnpmTarget).map((leaf) =>
|
|
152
|
+
makeRecord({
|
|
153
|
+
adapter: 'dependency-pins',
|
|
154
|
+
category: 'dependency',
|
|
155
|
+
surface: 'pnpm-overrides',
|
|
156
|
+
file: 'pnpm-workspace.yaml',
|
|
157
|
+
line: lineOf(text, leaf.key, ['overrides:']),
|
|
158
|
+
target: leaf.target,
|
|
159
|
+
rule: 'pnpm-overrides',
|
|
160
|
+
justification: null,
|
|
161
|
+
probe: pinProbe(leaf.target, resolveValue(leaf.value, {}), ctx.deps()),
|
|
162
|
+
}),
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export const dependencyPins = Object.freeze({
|
|
167
|
+
id: 'dependency-pins',
|
|
168
|
+
category: 'dependency',
|
|
169
|
+
applies: (ctx) =>
|
|
170
|
+
ctx.scope.manifests.length > 0 || ctx.scope.pnpmWorkspace
|
|
171
|
+
? { applies: true, reason: 'package manifests present' }
|
|
172
|
+
: { applies: false, reason: 'no package.json or pnpm-workspace.yaml' },
|
|
173
|
+
extract: (ctx) => [
|
|
174
|
+
...ctx.scope.manifests.flatMap((m) => manifestRecords(ctx, m)),
|
|
175
|
+
...workspaceYamlRecords(ctx),
|
|
176
|
+
],
|
|
177
|
+
});
|