@lateos/npm-scan 0.18.2 → 1.0.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.
Files changed (113) hide show
  1. package/CHANGELOG.md +265 -233
  2. package/LICENSING.md +19 -19
  3. package/README.de.md +708 -708
  4. package/README.fr.md +707 -707
  5. package/README.ja.md +704 -704
  6. package/README.md +861 -826
  7. package/README.zh.md +708 -708
  8. package/VALIDATION.md +92 -0
  9. package/backend/cra.js +68 -68
  10. package/backend/db/pg-schema.sql +155 -0
  11. package/backend/db/schema.sql +32 -32
  12. package/backend/db.js +88 -88
  13. package/backend/detectors/atk-001-lifecycle.js +17 -17
  14. package/backend/detectors/atk-002-obfusc.js +261 -261
  15. package/backend/detectors/atk-003-creds.js +13 -13
  16. package/backend/detectors/atk-004-persist.js +13 -13
  17. package/backend/detectors/atk-005-exfil.js +13 -13
  18. package/backend/detectors/atk-006-depconf.js +14 -14
  19. package/backend/detectors/atk-007-typosquat.js +34 -34
  20. package/backend/detectors/atk-008-tarball-tamper.js +91 -91
  21. package/backend/detectors/atk-009-dormant-trigger.js +62 -62
  22. package/backend/detectors/atk-010-sandbox-evasion.js +50 -50
  23. package/backend/detectors/atk-011-transitive-prop.js +76 -76
  24. package/backend/detectors/config/thresholds.js +66 -0
  25. package/backend/detectors/config/whitelist.json +74 -0
  26. package/backend/detectors/cve-2026-48710-badhost/codePattern.js +99 -99
  27. package/backend/detectors/cve-2026-48710-badhost/findings.js +105 -105
  28. package/backend/detectors/cve-2026-48710-badhost/index.js +15 -15
  29. package/backend/detectors/cve-2026-48710-badhost/manifest.js +305 -305
  30. package/backend/detectors/cve-2026-48710-badhost/transitive.js +189 -189
  31. package/backend/detectors/hf-impersonation/index.js +396 -396
  32. package/backend/detectors/hf-impersonation/jaro-winkler.js +44 -44
  33. package/backend/detectors/hf-impersonation/known-orgs.js +5 -5
  34. package/backend/detectors/hf-impersonation/simhash.js +46 -46
  35. package/backend/detectors/index.js +87 -81
  36. package/backend/detectors/lib/ast-patterns.js +21 -0
  37. package/backend/detectors/lib/entropy-analyzer.js +24 -0
  38. package/backend/detectors/megalodon/d1-workflow-scan.js +147 -147
  39. package/backend/detectors/megalodon/d2-credential-harvest.js +61 -61
  40. package/backend/detectors/megalodon/d3-publish-velocity.js +67 -67
  41. package/backend/detectors/megalodon/d4-publisher-drift.js +124 -124
  42. package/backend/detectors/megalodon/d5-bot-commit-identity.js +3 -3
  43. package/backend/detectors/megalodon/d6-date-anachronism.js +3 -3
  44. package/backend/detectors/megalodon/index.js +80 -80
  45. package/backend/detectors/megalodon/types.js +9 -9
  46. package/backend/detectors/mini-shai-hulud/d1-burst-publish.js +42 -42
  47. package/backend/detectors/mini-shai-hulud/d2-sibling-compromise.js +116 -116
  48. package/backend/detectors/mini-shai-hulud/d3-slsa-mismatch.js +72 -72
  49. package/backend/detectors/mini-shai-hulud/d4-maintainer-anomaly.js +45 -45
  50. package/backend/detectors/mini-shai-hulud/d5-ioc-check.js +95 -95
  51. package/backend/detectors/mini-shai-hulud/d6-token-exfil.js +38 -38
  52. package/backend/detectors/mini-shai-hulud/index.js +118 -118
  53. package/backend/detectors/mini-shai-hulud/iocs.json +79 -79
  54. package/backend/detectors/tier1-binary-embed.js +34 -5
  55. package/backend/detectors/tier1-obfuscation-heuristics.js +156 -0
  56. package/backend/detectors/tier1-slsa-attestation.js +12 -0
  57. package/backend/detectors/tier1-version-anomaly.js +187 -0
  58. package/backend/detectors.test.js +88 -0
  59. package/backend/fetch.js +175 -175
  60. package/backend/index.js +4 -4
  61. package/backend/license.js +89 -89
  62. package/backend/lockfile.js +379 -379
  63. package/backend/pdf.js +245 -245
  64. package/backend/policy.js +193 -193
  65. package/backend/report.js +254 -254
  66. package/backend/sbom.js +66 -66
  67. package/backend/scripts/analyze-false-positives.js +146 -0
  68. package/backend/scripts/analyze-validation.js +151 -0
  69. package/backend/scripts/detect-false-positives.js +93 -0
  70. package/backend/scripts/fetch-top-packages.js +129 -0
  71. package/backend/scripts/validate-detectors.js +142 -0
  72. package/backend/siem/cef.js +32 -32
  73. package/backend/siem/ecs.js +40 -40
  74. package/backend/siem/index.js +18 -18
  75. package/backend/siem/qradar.js +56 -56
  76. package/backend/siem/sentinel.js +27 -27
  77. package/backend/tests-d5-enhanced.test.js +46 -0
  78. package/backend/tests-d6-version-anomaly.test.js +58 -0
  79. package/backend/tests-d6.test.js +116 -0
  80. package/backend/tests-d6c.test.js +106 -0
  81. package/backend/tests-d7-obfuscation.test.js +91 -0
  82. package/backend/tests.test.js +898 -0
  83. package/backend/vsix-scan/detectors/activation-event-risk.js +116 -116
  84. package/backend/vsix-scan/detectors/burst-publish.js +52 -52
  85. package/backend/vsix-scan/detectors/exfil-pattern.js +88 -88
  86. package/backend/vsix-scan/detectors/known-ioc.js +105 -105
  87. package/backend/vsix-scan/detectors/orphan-commit-fetch.js +69 -69
  88. package/backend/vsix-scan/detectors/publisher-anomaly.js +70 -70
  89. package/backend/vsix-scan/index.js +183 -183
  90. package/backend/vsix-scan/marketplace-client.js +145 -145
  91. package/backend/vsix-scan/vsix-iocs.json +31 -31
  92. package/cli/cli.js +458 -458
  93. package/package.json +74 -57
  94. package/.dockerignore +0 -20
  95. package/.husky/pre-commit +0 -1
  96. package/SECURITY.md +0 -73
  97. package/deploy/helm/npm-scan/Chart.yaml +0 -22
  98. package/deploy/helm/npm-scan/templates/_helpers.tpl +0 -9
  99. package/deploy/helm/npm-scan/templates/api.yaml +0 -94
  100. package/deploy/helm/npm-scan/templates/ingress.yaml +0 -28
  101. package/deploy/helm/npm-scan/templates/postgresql.yaml +0 -67
  102. package/deploy/helm/npm-scan/templates/secrets.yaml +0 -19
  103. package/deploy/helm/npm-scan/templates/worker.yaml +0 -32
  104. package/deploy/helm/npm-scan/values.byoc.yaml +0 -75
  105. package/deploy/helm/npm-scan/values.yaml +0 -103
  106. package/scripts/download-corpus.js +0 -30
  107. package/scripts/gen-mal-corpus.js +0 -35
  108. package/scripts/generate-campaign-fixtures.js +0 -170
  109. package/src/config/top-5000.json +0 -87
  110. package/test/fixtures/lockfiles/npm-lock.json +0 -69
  111. package/test/fixtures/lockfiles/pnpm-lock.yaml +0 -118
  112. package/test/fixtures/lockfiles/yarn.lock +0 -104
  113. package/test/fixtures/mock-data.js +0 -69
package/backend/pdf.js CHANGED
@@ -1,245 +1,245 @@
1
- import { PDFDocument, StandardFonts, rgb } from 'pdf-lib';
2
-
3
- const SEV_ORDER = ['critical', 'high', 'medium', 'low'];
4
- const SEV_COLORS = { critical: rgb(0.8, 0.2, 0.2), high: rgb(0.75, 0.15, 0.15), medium: rgb(0.9, 0.5, 0.1), low: rgb(0.8, 0.7, 0.1) };
5
-
6
- const NIST_SR_MAP = {
7
- 'ATK-001': { control: 'SR-3.1', title: 'Malicious code detection' },
8
- 'ATK-002': { control: 'SR-4.2', title: 'Code obfuscation analysis' },
9
- 'ATK-003': { control: 'SR-5.3', title: 'Credential protection' },
10
- 'ATK-004': { control: 'SR-6.4', title: 'Persistence monitoring' },
11
- 'ATK-005': { control: 'SR-7.5', title: 'Data exfiltration prevention' },
12
- 'ATK-006': { control: 'SR-2.2', title: 'Dependency validation' },
13
- 'ATK-007': { control: 'SR-2.1', title: 'Typosquatting prevention' },
14
- 'ATK-008': { control: 'SR-8.1', title: 'Integrity verification' },
15
- 'ATK-009': { control: 'SR-9.2', title: 'Conditional behavior analysis' },
16
- 'ATK-010': { control: 'SR-10.3', title: 'Anti-evasion detection' },
17
- 'ATK-011': { control: 'SR-11.4', title: 'Supply chain propagation monitoring' },
18
- };
19
-
20
- const MARGIN = 50;
21
- const PAGE_W = 612;
22
- const PAGE_H = 792;
23
- const CONTENT_W = PAGE_W - MARGIN * 2;
24
-
25
- function wrapText(text, font, size, maxWidth) {
26
- const words = text.split(' ');
27
- const lines = [];
28
- let current = '';
29
- for (const word of words) {
30
- const test = current ? current + ' ' + word : word;
31
- if (font.widthOfTextAtSize(test, size) > maxWidth) {
32
- if (current) lines.push(current);
33
- current = word;
34
- } else {
35
- current = test;
36
- }
37
- }
38
- if (current) lines.push(current);
39
- return lines;
40
- }
41
-
42
- function drawTableRow(page, font, columns, y, colWidths, fontSize, isHeader) {
43
- let x = MARGIN;
44
- const rowH = fontSize + 6;
45
- for (let i = 0; i < columns.length; i++) {
46
- const text = columns[i];
47
- const lines = wrapText(text, font, fontSize, colWidths[i] - 4);
48
- for (let j = 0; j < lines.length; j++) {
49
- page.drawText(lines[j], { x: x + 2, y: y - (j * fontSize) - 2, size: fontSize, font, color: rgb(0, 0, 0) });
50
- }
51
- x += colWidths[i];
52
- }
53
- return y - rowH;
54
- }
55
-
56
- function drawPageHeader(page, font, text, y) {
57
- page.drawText(text, { x: MARGIN, y, size: 14, font, color: rgb(0.2, 0.2, 0.2) });
58
- page.drawLine({ start: { x: MARGIN, y: y - 4 }, end: { x: PAGE_W - MARGIN, y: y - 4 }, thickness: 1, color: rgb(0.7, 0.7, 0.7) });
59
- return y - 20;
60
- }
61
-
62
- export async function generatePDF(scans) {
63
- const doc = await PDFDocument.create();
64
- const font = await doc.embedFont(StandardFonts.Helvetica);
65
- const boldFont = await doc.embedFont(StandardFonts.HelveticaBold);
66
- const version = process.env.npm_package_version || '0.0.0';
67
-
68
- const sevCounts = { critical: 0, high: 0, medium: 0, low: 0 };
69
- let totalFindings = 0;
70
- for (const s of scans) {
71
- for (const f of (s.findings || [])) {
72
- if (sevCounts[f.severity] !== undefined) sevCounts[f.severity]++;
73
- totalFindings++;
74
- }
75
- }
76
-
77
- // ─── Page 1: Title ──────────────────────────────────────────────────
78
- let page = doc.addPage([PAGE_W, PAGE_H]);
79
- let y = PAGE_H - MARGIN;
80
-
81
- page.drawText('npm-scan Report', { x: MARGIN, y, size: 24, font: boldFont, color: rgb(0, 0, 0) });
82
- y -= 30;
83
- page.drawText(`Generated: ${new Date().toISOString()}`, { x: MARGIN, y, size: 10, font, color: rgb(0.4, 0.4, 0.4) });
84
- y -= 14;
85
- page.drawText(`Version: ${version} | Packages scanned: ${scans.length} | Total findings: ${totalFindings}`, { x: MARGIN, y, size: 10, font, color: rgb(0.4, 0.4, 0.4) });
86
- y -= 30;
87
-
88
- // Severity summary
89
- page.drawText('Severity Summary', { x: MARGIN, y, size: 14, font: boldFont, color: rgb(0, 0, 0) });
90
- y -= 20;
91
-
92
- for (const sev of SEV_ORDER) {
93
- const count = sevCounts[sev] || 0;
94
- const color = SEV_COLORS[sev] || rgb(0, 0, 0);
95
- page.drawCircle({ x: MARGIN + 6, y: y - 4, size: 4, color });
96
- page.drawText(`${sev}: ${count}`, { x: MARGIN + 16, y: y - 8, size: 11, font, color: rgb(0, 0, 0) });
97
- y -= 18;
98
- }
99
-
100
- y -= 20;
101
-
102
- // Per-package summary
103
- for (const s of scans) {
104
- const findings = s.findings || [];
105
- if (y < MARGIN + 60) { page = doc.addPage([PAGE_W, PAGE_H]); y = PAGE_H - MARGIN; }
106
-
107
- page.drawText(`${s.package_name}@${s.version || 'unknown'}`, { x: MARGIN, y, size: 12, font: boldFont, color: rgb(0, 0, 0) });
108
- y -= 16;
109
- page.drawText(` ${findings.length} findings`, { x: MARGIN, y, size: 10, font, color: rgb(0.4, 0.4, 0.4) });
110
- y -= 14;
111
-
112
- for (const f of findings) {
113
- if (y < MARGIN + 20) { page = doc.addPage([PAGE_W, PAGE_H]); y = PAGE_H - MARGIN; }
114
- const sevColor = SEV_COLORS[f.severity] || rgb(0, 0, 0);
115
- page.drawCircle({ x: MARGIN + 3, y: y + 2, size: 3, color: sevColor });
116
- const line = `${f.atk_id || f.id} ${f.severity} ${(f.description || f.title || '').slice(0, 70)}`;
117
- page.drawText(line, { x: MARGIN + 12, y, size: 9, font, color: rgb(0.1, 0.1, 0.1) });
118
- y -= 13;
119
- }
120
- }
121
-
122
- // ─── Page: Findings Table ───────────────────────────────────────────
123
- page = doc.addPage([PAGE_W, PAGE_H]);
124
- y = PAGE_H - MARGIN;
125
- y = drawPageHeader(page, boldFont, 'All Findings', y);
126
- y -= 6;
127
-
128
- const colWidths = [60, 55, 140, CONTENT_W - 255];
129
- const headers = ['ATK ID', 'Severity', 'Title', 'Evidence'];
130
-
131
- // Draw header
132
- let x = MARGIN;
133
- for (let i = 0; i < headers.length; i++) {
134
- page.drawText(headers[i], { x: x + 2, y, size: 10, font: boldFont, color: rgb(0, 0, 0) });
135
- x += colWidths[i];
136
- }
137
- y -= 16;
138
-
139
- lineLoop: for (const s of scans) {
140
- for (const f of (s.findings || [])) {
141
- if (y < MARGIN + 20) {
142
- page = doc.addPage([PAGE_W, PAGE_H]);
143
- y = PAGE_H - MARGIN;
144
- y = drawPageHeader(page, boldFont, 'All Findings (continued)', y);
145
- y -= 6;
146
- }
147
-
148
- const rowData = [
149
- f.atk_id || f.id || '',
150
- f.severity || '',
151
- (f.title || '').slice(0, 30),
152
- (f.evidence || '').slice(0, 60),
153
- ];
154
-
155
- let rowY = y;
156
- let maxLines = 1;
157
- for (let i = 0; i < rowData.length; i++) {
158
- const lines = wrapText(rowData[i], font, 9, colWidths[i] - 4);
159
- if (lines.length > maxLines) maxLines = lines.length;
160
- }
161
-
162
- if (y - (maxLines * 11) < MARGIN) {
163
- page = doc.addPage([PAGE_W, PAGE_H]);
164
- y = PAGE_H - MARGIN;
165
- y = drawPageHeader(page, boldFont, 'All Findings (continued)', y);
166
- y -= 6;
167
- rowY = y;
168
- }
169
-
170
- x = MARGIN;
171
- for (let i = 0; i < rowData.length; i++) {
172
- const lines = wrapText(rowData[i], font, 9, colWidths[i] - 4);
173
- for (let j = 0; j < lines.length; j++) {
174
- const color = i === 1 && SEV_COLORS[f.severity] ? SEV_COLORS[f.severity] : rgb(0, 0, 0);
175
- page.drawText(lines[j], { x: x + 2, y: rowY - (j * 11) - 2, size: 9, font, color });
176
- }
177
- x += colWidths[i];
178
- }
179
-
180
- const lineY = rowY + 2;
181
- page.drawLine({ start: { x: MARGIN, y: lineY }, end: { x: PAGE_W - MARGIN, y: lineY }, thickness: 0.5, color: rgb(0.85, 0.85, 0.85) });
182
- y = rowY - (maxLines * 11) - 4;
183
- }
184
- }
185
-
186
- // ─── Page: NIST 800-161 Compliance Matrix ───────────────────────────
187
- page = doc.addPage([PAGE_W, PAGE_H]);
188
- y = PAGE_H - MARGIN;
189
- y = drawPageHeader(page, boldFont, 'NIST SP 800-161 Compliance Summary', y);
190
- y -= 6;
191
-
192
- const nistColWidths = [70, CONTENT_W - 180, 110];
193
- const nistHeaders = ['Control', 'Control Title', 'Status'];
194
- x = MARGIN;
195
- for (let i = 0; i < nistHeaders.length; i++) {
196
- page.drawText(nistHeaders[i], { x: x + 2, y, size: 10, font: boldFont, color: rgb(0, 0, 0) });
197
- x += nistColWidths[i];
198
- }
199
- y -= 16;
200
-
201
- const atkMap = {};
202
- for (const s of scans) {
203
- for (const f of (s.findings || [])) {
204
- const key = f.atk_id || f.id;
205
- if (!atkMap[key]) atkMap[key] = [];
206
- atkMap[key].push(f);
207
- }
208
- }
209
-
210
- for (const [atkId, { control, title }] of Object.entries(NIST_SR_MAP)) {
211
- if (y < MARGIN + 20) {
212
- page = doc.addPage([PAGE_W, PAGE_H]);
213
- y = PAGE_H - MARGIN;
214
- }
215
-
216
- const count = (atkMap[atkId] || []).length;
217
- const status = count > 0 ? `${count} finding(s)` : 'Pass';
218
- const statusColor = count > 0 ? rgb(0.8, 0.2, 0.2) : rgb(0.2, 0.6, 0.2);
219
-
220
- const rowData = [control, title, status];
221
- const rowWidths = nistColWidths;
222
-
223
- x = MARGIN;
224
- for (let i = 0; i < rowData.length; i++) {
225
- const color = i === 2 ? statusColor : rgb(0, 0, 0);
226
- const fnt = i === 0 ? boldFont : font;
227
- page.drawText(rowData[i], { x: x + 2, y: y - 2, size: 9, font: fnt, color });
228
- x += rowWidths[i];
229
- }
230
-
231
- page.drawLine({ start: { x: MARGIN, y: y + 4 }, end: { x: PAGE_W - MARGIN, y: y + 4 }, thickness: 0.5, color: rgb(0.85, 0.85, 0.85) });
232
- y -= 18;
233
- }
234
-
235
- // Footer
236
- const pages = doc.getPages();
237
- for (const p of pages) {
238
- const { width } = p.getSize();
239
- p.drawText(`npm-scan v${version} | Apache-2.0 + Commons Clause`, {
240
- x: MARGIN, y: 20, size: 8, font, color: rgb(0.6, 0.6, 0.6),
241
- });
242
- }
243
-
244
- return doc.save();
245
- }
1
+ import { PDFDocument, StandardFonts, rgb } from 'pdf-lib';
2
+
3
+ const SEV_ORDER = ['critical', 'high', 'medium', 'low'];
4
+ const SEV_COLORS = { critical: rgb(0.8, 0.2, 0.2), high: rgb(0.75, 0.15, 0.15), medium: rgb(0.9, 0.5, 0.1), low: rgb(0.8, 0.7, 0.1) };
5
+
6
+ const NIST_SR_MAP = {
7
+ 'ATK-001': { control: 'SR-3.1', title: 'Malicious code detection' },
8
+ 'ATK-002': { control: 'SR-4.2', title: 'Code obfuscation analysis' },
9
+ 'ATK-003': { control: 'SR-5.3', title: 'Credential protection' },
10
+ 'ATK-004': { control: 'SR-6.4', title: 'Persistence monitoring' },
11
+ 'ATK-005': { control: 'SR-7.5', title: 'Data exfiltration prevention' },
12
+ 'ATK-006': { control: 'SR-2.2', title: 'Dependency validation' },
13
+ 'ATK-007': { control: 'SR-2.1', title: 'Typosquatting prevention' },
14
+ 'ATK-008': { control: 'SR-8.1', title: 'Integrity verification' },
15
+ 'ATK-009': { control: 'SR-9.2', title: 'Conditional behavior analysis' },
16
+ 'ATK-010': { control: 'SR-10.3', title: 'Anti-evasion detection' },
17
+ 'ATK-011': { control: 'SR-11.4', title: 'Supply chain propagation monitoring' },
18
+ };
19
+
20
+ const MARGIN = 50;
21
+ const PAGE_W = 612;
22
+ const PAGE_H = 792;
23
+ const CONTENT_W = PAGE_W - MARGIN * 2;
24
+
25
+ function wrapText(text, font, size, maxWidth) {
26
+ const words = text.split(' ');
27
+ const lines = [];
28
+ let current = '';
29
+ for (const word of words) {
30
+ const test = current ? current + ' ' + word : word;
31
+ if (font.widthOfTextAtSize(test, size) > maxWidth) {
32
+ if (current) lines.push(current);
33
+ current = word;
34
+ } else {
35
+ current = test;
36
+ }
37
+ }
38
+ if (current) lines.push(current);
39
+ return lines;
40
+ }
41
+
42
+ function drawTableRow(page, font, columns, y, colWidths, fontSize, isHeader) {
43
+ let x = MARGIN;
44
+ const rowH = fontSize + 6;
45
+ for (let i = 0; i < columns.length; i++) {
46
+ const text = columns[i];
47
+ const lines = wrapText(text, font, fontSize, colWidths[i] - 4);
48
+ for (let j = 0; j < lines.length; j++) {
49
+ page.drawText(lines[j], { x: x + 2, y: y - (j * fontSize) - 2, size: fontSize, font, color: rgb(0, 0, 0) });
50
+ }
51
+ x += colWidths[i];
52
+ }
53
+ return y - rowH;
54
+ }
55
+
56
+ function drawPageHeader(page, font, text, y) {
57
+ page.drawText(text, { x: MARGIN, y, size: 14, font, color: rgb(0.2, 0.2, 0.2) });
58
+ page.drawLine({ start: { x: MARGIN, y: y - 4 }, end: { x: PAGE_W - MARGIN, y: y - 4 }, thickness: 1, color: rgb(0.7, 0.7, 0.7) });
59
+ return y - 20;
60
+ }
61
+
62
+ export async function generatePDF(scans) {
63
+ const doc = await PDFDocument.create();
64
+ const font = await doc.embedFont(StandardFonts.Helvetica);
65
+ const boldFont = await doc.embedFont(StandardFonts.HelveticaBold);
66
+ const version = process.env.npm_package_version || '0.0.0';
67
+
68
+ const sevCounts = { critical: 0, high: 0, medium: 0, low: 0 };
69
+ let totalFindings = 0;
70
+ for (const s of scans) {
71
+ for (const f of (s.findings || [])) {
72
+ if (sevCounts[f.severity] !== undefined) sevCounts[f.severity]++;
73
+ totalFindings++;
74
+ }
75
+ }
76
+
77
+ // ─── Page 1: Title ──────────────────────────────────────────────────
78
+ let page = doc.addPage([PAGE_W, PAGE_H]);
79
+ let y = PAGE_H - MARGIN;
80
+
81
+ page.drawText('npm-scan Report', { x: MARGIN, y, size: 24, font: boldFont, color: rgb(0, 0, 0) });
82
+ y -= 30;
83
+ page.drawText(`Generated: ${new Date().toISOString()}`, { x: MARGIN, y, size: 10, font, color: rgb(0.4, 0.4, 0.4) });
84
+ y -= 14;
85
+ page.drawText(`Version: ${version} | Packages scanned: ${scans.length} | Total findings: ${totalFindings}`, { x: MARGIN, y, size: 10, font, color: rgb(0.4, 0.4, 0.4) });
86
+ y -= 30;
87
+
88
+ // Severity summary
89
+ page.drawText('Severity Summary', { x: MARGIN, y, size: 14, font: boldFont, color: rgb(0, 0, 0) });
90
+ y -= 20;
91
+
92
+ for (const sev of SEV_ORDER) {
93
+ const count = sevCounts[sev] || 0;
94
+ const color = SEV_COLORS[sev] || rgb(0, 0, 0);
95
+ page.drawCircle({ x: MARGIN + 6, y: y - 4, size: 4, color });
96
+ page.drawText(`${sev}: ${count}`, { x: MARGIN + 16, y: y - 8, size: 11, font, color: rgb(0, 0, 0) });
97
+ y -= 18;
98
+ }
99
+
100
+ y -= 20;
101
+
102
+ // Per-package summary
103
+ for (const s of scans) {
104
+ const findings = s.findings || [];
105
+ if (y < MARGIN + 60) { page = doc.addPage([PAGE_W, PAGE_H]); y = PAGE_H - MARGIN; }
106
+
107
+ page.drawText(`${s.package_name}@${s.version || 'unknown'}`, { x: MARGIN, y, size: 12, font: boldFont, color: rgb(0, 0, 0) });
108
+ y -= 16;
109
+ page.drawText(` ${findings.length} findings`, { x: MARGIN, y, size: 10, font, color: rgb(0.4, 0.4, 0.4) });
110
+ y -= 14;
111
+
112
+ for (const f of findings) {
113
+ if (y < MARGIN + 20) { page = doc.addPage([PAGE_W, PAGE_H]); y = PAGE_H - MARGIN; }
114
+ const sevColor = SEV_COLORS[f.severity] || rgb(0, 0, 0);
115
+ page.drawCircle({ x: MARGIN + 3, y: y + 2, size: 3, color: sevColor });
116
+ const line = `${f.atk_id || f.id} ${f.severity} ${(f.description || f.title || '').slice(0, 70)}`;
117
+ page.drawText(line, { x: MARGIN + 12, y, size: 9, font, color: rgb(0.1, 0.1, 0.1) });
118
+ y -= 13;
119
+ }
120
+ }
121
+
122
+ // ─── Page: Findings Table ───────────────────────────────────────────
123
+ page = doc.addPage([PAGE_W, PAGE_H]);
124
+ y = PAGE_H - MARGIN;
125
+ y = drawPageHeader(page, boldFont, 'All Findings', y);
126
+ y -= 6;
127
+
128
+ const colWidths = [60, 55, 140, CONTENT_W - 255];
129
+ const headers = ['ATK ID', 'Severity', 'Title', 'Evidence'];
130
+
131
+ // Draw header
132
+ let x = MARGIN;
133
+ for (let i = 0; i < headers.length; i++) {
134
+ page.drawText(headers[i], { x: x + 2, y, size: 10, font: boldFont, color: rgb(0, 0, 0) });
135
+ x += colWidths[i];
136
+ }
137
+ y -= 16;
138
+
139
+ lineLoop: for (const s of scans) {
140
+ for (const f of (s.findings || [])) {
141
+ if (y < MARGIN + 20) {
142
+ page = doc.addPage([PAGE_W, PAGE_H]);
143
+ y = PAGE_H - MARGIN;
144
+ y = drawPageHeader(page, boldFont, 'All Findings (continued)', y);
145
+ y -= 6;
146
+ }
147
+
148
+ const rowData = [
149
+ f.atk_id || f.id || '',
150
+ f.severity || '',
151
+ (f.title || '').slice(0, 30),
152
+ (f.evidence || '').slice(0, 60),
153
+ ];
154
+
155
+ let rowY = y;
156
+ let maxLines = 1;
157
+ for (let i = 0; i < rowData.length; i++) {
158
+ const lines = wrapText(rowData[i], font, 9, colWidths[i] - 4);
159
+ if (lines.length > maxLines) maxLines = lines.length;
160
+ }
161
+
162
+ if (y - (maxLines * 11) < MARGIN) {
163
+ page = doc.addPage([PAGE_W, PAGE_H]);
164
+ y = PAGE_H - MARGIN;
165
+ y = drawPageHeader(page, boldFont, 'All Findings (continued)', y);
166
+ y -= 6;
167
+ rowY = y;
168
+ }
169
+
170
+ x = MARGIN;
171
+ for (let i = 0; i < rowData.length; i++) {
172
+ const lines = wrapText(rowData[i], font, 9, colWidths[i] - 4);
173
+ for (let j = 0; j < lines.length; j++) {
174
+ const color = i === 1 && SEV_COLORS[f.severity] ? SEV_COLORS[f.severity] : rgb(0, 0, 0);
175
+ page.drawText(lines[j], { x: x + 2, y: rowY - (j * 11) - 2, size: 9, font, color });
176
+ }
177
+ x += colWidths[i];
178
+ }
179
+
180
+ const lineY = rowY + 2;
181
+ page.drawLine({ start: { x: MARGIN, y: lineY }, end: { x: PAGE_W - MARGIN, y: lineY }, thickness: 0.5, color: rgb(0.85, 0.85, 0.85) });
182
+ y = rowY - (maxLines * 11) - 4;
183
+ }
184
+ }
185
+
186
+ // ─── Page: NIST 800-161 Compliance Matrix ───────────────────────────
187
+ page = doc.addPage([PAGE_W, PAGE_H]);
188
+ y = PAGE_H - MARGIN;
189
+ y = drawPageHeader(page, boldFont, 'NIST SP 800-161 Compliance Summary', y);
190
+ y -= 6;
191
+
192
+ const nistColWidths = [70, CONTENT_W - 180, 110];
193
+ const nistHeaders = ['Control', 'Control Title', 'Status'];
194
+ x = MARGIN;
195
+ for (let i = 0; i < nistHeaders.length; i++) {
196
+ page.drawText(nistHeaders[i], { x: x + 2, y, size: 10, font: boldFont, color: rgb(0, 0, 0) });
197
+ x += nistColWidths[i];
198
+ }
199
+ y -= 16;
200
+
201
+ const atkMap = {};
202
+ for (const s of scans) {
203
+ for (const f of (s.findings || [])) {
204
+ const key = f.atk_id || f.id;
205
+ if (!atkMap[key]) atkMap[key] = [];
206
+ atkMap[key].push(f);
207
+ }
208
+ }
209
+
210
+ for (const [atkId, { control, title }] of Object.entries(NIST_SR_MAP)) {
211
+ if (y < MARGIN + 20) {
212
+ page = doc.addPage([PAGE_W, PAGE_H]);
213
+ y = PAGE_H - MARGIN;
214
+ }
215
+
216
+ const count = (atkMap[atkId] || []).length;
217
+ const status = count > 0 ? `${count} finding(s)` : 'Pass';
218
+ const statusColor = count > 0 ? rgb(0.8, 0.2, 0.2) : rgb(0.2, 0.6, 0.2);
219
+
220
+ const rowData = [control, title, status];
221
+ const rowWidths = nistColWidths;
222
+
223
+ x = MARGIN;
224
+ for (let i = 0; i < rowData.length; i++) {
225
+ const color = i === 2 ? statusColor : rgb(0, 0, 0);
226
+ const fnt = i === 0 ? boldFont : font;
227
+ page.drawText(rowData[i], { x: x + 2, y: y - 2, size: 9, font: fnt, color });
228
+ x += rowWidths[i];
229
+ }
230
+
231
+ page.drawLine({ start: { x: MARGIN, y: y + 4 }, end: { x: PAGE_W - MARGIN, y: y + 4 }, thickness: 0.5, color: rgb(0.85, 0.85, 0.85) });
232
+ y -= 18;
233
+ }
234
+
235
+ // Footer
236
+ const pages = doc.getPages();
237
+ for (const p of pages) {
238
+ const { width } = p.getSize();
239
+ p.drawText(`npm-scan v${version} | Apache-2.0 + Commons Clause`, {
240
+ x: MARGIN, y: 20, size: 8, font, color: rgb(0.6, 0.6, 0.6),
241
+ });
242
+ }
243
+
244
+ return doc.save();
245
+ }