@aisac/cli 0.4.0-beta.15 → 0.4.0-beta.18
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 +49 -2
- package/dist/assets/claude/hooks/context-compaction.ts +12 -4
- package/dist/assets/claude/hooks/native-host-contract-runtime.ts +1 -1
- package/dist/assets/claude/hooks/native-host-contract.test.ts +8 -4
- package/dist/assets/claude/hooks/provider-window.d.ts +83 -0
- package/dist/claude-compatibility.mjs +25 -15
- package/dist/claude-observation.mjs +2 -1
- package/dist/cli.mjs +76 -6
- package/dist/codex-compatibility.mjs +1 -0
- package/dist/codex-protocol-fingerprint.json +11 -11
- package/dist/engineering-pack.mjs +23 -3
- package/dist/engineering-scan-pins.json +42 -0
- package/dist/engineering-scan-report.mjs +1274 -0
- package/dist/engineering-scan-tools.mjs +1418 -0
- package/dist/engineering-scan.mjs +1265 -0
- package/dist/mcp.mjs +10 -2
- package/dist/package.json +1 -1
- package/dist/platform.mjs +21 -5
- package/dist/token-guide.mjs +27 -0
- package/package.json +1 -1
|
@@ -0,0 +1,1274 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* `aisac scan` reports (spec aisac-engineering-scan D5, D6, D7, D10, D12,
|
|
5
|
+
* FR5, FR6). Each tool's JSON becomes one finding shape: severity, OWASP ids,
|
|
6
|
+
* a location relative to the target, a redacted and bounded message and a
|
|
7
|
+
* fingerprint that never contains the matched value. The raw outputs are
|
|
8
|
+
* kept only as sanitized copies (matched code, metavariables and secret
|
|
9
|
+
* fields removed). `summary.json` is locale-free; `summary.md` and the
|
|
10
|
+
* terminal lines follow the pack locale.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { isRecord } from './engineering.mjs';
|
|
14
|
+
import {
|
|
15
|
+
GITLEAKS_REPORT_FIELDS,
|
|
16
|
+
GITLEAKS_VALUE_DIGEST,
|
|
17
|
+
SCAN_KINDS,
|
|
18
|
+
SCAN_TOOLS,
|
|
19
|
+
sha256Hex,
|
|
20
|
+
} from './engineering-scan-tools.mjs';
|
|
21
|
+
|
|
22
|
+
/** @typedef {import('./engineering-scan-tools.mjs').ScanKind} ScanKind */
|
|
23
|
+
/** @typedef {import('./engineering-scan-tools.mjs').ScanTool} ScanTool */
|
|
24
|
+
/** @typedef {import('./engineering-scan-tools.mjs').RulesetRecord} RulesetRecord */
|
|
25
|
+
/** @typedef {import('./engineering-pack.mjs').PackLocale} PackLocale */
|
|
26
|
+
|
|
27
|
+
export const SEVERITIES = /** @type {const} */ (['CRITICAL', 'HIGH', 'MEDIUM', 'LOW', 'INFO']);
|
|
28
|
+
export const FAIL_ON_LEVELS = /** @type {const} */ (['critical', 'high', 'medium', 'low', 'none']);
|
|
29
|
+
export const SUMMARY_SCHEMA_VERSION = 1;
|
|
30
|
+
|
|
31
|
+
/** @typedef {(typeof SEVERITIES)[number]} Severity */
|
|
32
|
+
/** @typedef {(typeof FAIL_ON_LEVELS)[number]} FailOn */
|
|
33
|
+
/**
|
|
34
|
+
* @typedef {{
|
|
35
|
+
* tool: ScanTool,
|
|
36
|
+
* kind: ScanKind,
|
|
37
|
+
* ruleId: string,
|
|
38
|
+
* severity: Severity,
|
|
39
|
+
* owasp: string[],
|
|
40
|
+
* path: string,
|
|
41
|
+
* line: number | null,
|
|
42
|
+
* message: string,
|
|
43
|
+
* fingerprint: string
|
|
44
|
+
* }} Finding
|
|
45
|
+
*/
|
|
46
|
+
/**
|
|
47
|
+
* What says two gitleaks findings are one secret (scan review F1): `key`
|
|
48
|
+
* hashes the rule, the path and the digest of the value (the line when there
|
|
49
|
+
* is no digest); `commit` is where history holds it. The digest never reaches
|
|
50
|
+
* the project or any report; the raw output in the private workspace is
|
|
51
|
+
* removed after the run.
|
|
52
|
+
* @typedef {{ key: string, commit: string | null }} SecretIdentity
|
|
53
|
+
*/
|
|
54
|
+
/** @typedef {{ findings: Finding[], sanitized: unknown, identities?: SecretIdentity[] }} ParsedOutput */
|
|
55
|
+
/** @typedef {{ prefixes: readonly string[] }} ParseContext */
|
|
56
|
+
/**
|
|
57
|
+
* @typedef {{
|
|
58
|
+
* tool: ScanTool,
|
|
59
|
+
* kind: ScanKind,
|
|
60
|
+
* mode: 'image' | 'binary' | 'skipped',
|
|
61
|
+
* reference: string | null,
|
|
62
|
+
* version: string | null,
|
|
63
|
+
* status: 'ran' | 'skipped' | 'failed' | 'timed_out',
|
|
64
|
+
* reason: string | null,
|
|
65
|
+
* startedAt: string,
|
|
66
|
+
* finishedAt: string,
|
|
67
|
+
* durationMs: number,
|
|
68
|
+
* exitCode: number | null,
|
|
69
|
+
* history: string | null
|
|
70
|
+
* }} ToolRecord
|
|
71
|
+
*/
|
|
72
|
+
/** @typedef {{ status: 'none' | 'loaded' | 'refused', reason: string | null, count: number }} AisacRulesRecord */
|
|
73
|
+
/** @typedef {{ tool: ScanTool, purpose: 'registry_rulesets' | 'advisory_lookup' }} NetworkUse */
|
|
74
|
+
/** @typedef {'clean' | 'findings' | 'no_scanner_available' | 'tool_failed' | 'interrupted'} ExitReason */
|
|
75
|
+
/**
|
|
76
|
+
* @typedef {{
|
|
77
|
+
* schemaVersion: 1,
|
|
78
|
+
* aisacClient: string,
|
|
79
|
+
* packVersion: string | null,
|
|
80
|
+
* startedAt: string,
|
|
81
|
+
* finishedAt: string,
|
|
82
|
+
* target: string,
|
|
83
|
+
* failOn: FailOn,
|
|
84
|
+
* exitCode: 0 | 1 | 3,
|
|
85
|
+
* exitReason: ExitReason,
|
|
86
|
+
* counts: Record<Severity, number>,
|
|
87
|
+
* tools: ToolRecord[],
|
|
88
|
+
* rulesets: RulesetRecord[],
|
|
89
|
+
* aisacRules: AisacRulesRecord,
|
|
90
|
+
* network: NetworkUse[],
|
|
91
|
+
* skippedPaths: string[],
|
|
92
|
+
* warnings: string[],
|
|
93
|
+
* findings: Finding[]
|
|
94
|
+
* }} ScanSummary
|
|
95
|
+
*/
|
|
96
|
+
/**
|
|
97
|
+
* @typedef {{
|
|
98
|
+
* kind: ScanKind,
|
|
99
|
+
* tool: ScanTool,
|
|
100
|
+
* mode: 'image' | 'binary' | 'skipped',
|
|
101
|
+
* reference: string | null,
|
|
102
|
+
* version: string | null,
|
|
103
|
+
* reason: string | null
|
|
104
|
+
* }} AvailabilityEntry
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
export const MAX_MESSAGE_LENGTH = 512;
|
|
108
|
+
const MAX_PATH_LENGTH = 1024;
|
|
109
|
+
const MAX_RULE_ID_LENGTH = 256;
|
|
110
|
+
const MAX_SANITIZED_MESSAGE_LENGTH = 4096;
|
|
111
|
+
export const MAX_FINDINGS_PER_TOOL = 2000;
|
|
112
|
+
const MIN_REDACTED_LENGTH = 4;
|
|
113
|
+
const REDACTED = '[REDACTED]';
|
|
114
|
+
const GITLEAKS_REDACTED = 'REDACTED';
|
|
115
|
+
const SEMGREP_LOGIN_SENTINEL = 'requires login';
|
|
116
|
+
const ELLIPSIS = '…';
|
|
117
|
+
/** C0, C1, line separators and the bidirectional embeddings, overrides and isolates. */
|
|
118
|
+
const CONTROL_CHARACTERS = /[\u0000-\u001f\u007f-\u009f\u2028\u2029\u202a-\u202e\u2066-\u2069]/gu;
|
|
119
|
+
const FENCE_RUN = /`{3,}/gu;
|
|
120
|
+
const SECRETS_OWASP = 'A07:2021';
|
|
121
|
+
const ADVISORY_OWASP = 'A06:2021';
|
|
122
|
+
const MISCONFIGURATION_OWASP = 'A05:2021';
|
|
123
|
+
/** gitleaks rules naming a cloud, code-host, payment or model-provider credential, or a private key. */
|
|
124
|
+
const CRITICAL_SECRET_RULE_PREFIXES = [
|
|
125
|
+
'aws-',
|
|
126
|
+
'gcp-',
|
|
127
|
+
'azure-',
|
|
128
|
+
'alibaba-',
|
|
129
|
+
'digitalocean-',
|
|
130
|
+
'heroku-',
|
|
131
|
+
'cloudflare-',
|
|
132
|
+
'github-',
|
|
133
|
+
'gitlab-',
|
|
134
|
+
'bitbucket-',
|
|
135
|
+
'stripe-',
|
|
136
|
+
'openai-',
|
|
137
|
+
'anthropic-',
|
|
138
|
+
'huggingface-',
|
|
139
|
+
'private-key',
|
|
140
|
+
];
|
|
141
|
+
/** Semgrep's own equivalence for its legacy levels (S2 note on D6); native levels map 1:1. */
|
|
142
|
+
/** @type {ReadonlyMap<string, Severity>} */
|
|
143
|
+
const SEMGREP_SEVERITY = new Map([
|
|
144
|
+
['ERROR', 'HIGH'],
|
|
145
|
+
['WARNING', 'MEDIUM'],
|
|
146
|
+
['INFO', 'LOW'],
|
|
147
|
+
['CRITICAL', 'CRITICAL'],
|
|
148
|
+
['HIGH', 'HIGH'],
|
|
149
|
+
['MEDIUM', 'MEDIUM'],
|
|
150
|
+
['LOW', 'LOW'],
|
|
151
|
+
['INVENTORY', 'INFO'],
|
|
152
|
+
['EXPERIMENT', 'INFO'],
|
|
153
|
+
]);
|
|
154
|
+
/** @type {Severity} */
|
|
155
|
+
const SEMGREP_UNKNOWN_SEVERITY = 'MEDIUM';
|
|
156
|
+
/** @type {ReadonlyMap<string, Severity>} */
|
|
157
|
+
const TRIVY_SEVERITY = new Map([
|
|
158
|
+
['CRITICAL', 'CRITICAL'],
|
|
159
|
+
['HIGH', 'HIGH'],
|
|
160
|
+
['MEDIUM', 'MEDIUM'],
|
|
161
|
+
['LOW', 'LOW'],
|
|
162
|
+
['UNKNOWN', 'INFO'],
|
|
163
|
+
]);
|
|
164
|
+
/** GitHub advisory levels, as osv-scanner reports them in `database_specific.severity`. */
|
|
165
|
+
/** @type {ReadonlyMap<string, Severity>} */
|
|
166
|
+
const ADVISORY_SEVERITY = new Map([
|
|
167
|
+
['CRITICAL', 'CRITICAL'],
|
|
168
|
+
['HIGH', 'HIGH'],
|
|
169
|
+
['MODERATE', 'MEDIUM'],
|
|
170
|
+
['MEDIUM', 'MEDIUM'],
|
|
171
|
+
['LOW', 'LOW'],
|
|
172
|
+
]);
|
|
173
|
+
/** @type {Severity} */
|
|
174
|
+
const DEFAULT_ADVISORY_SEVERITY = 'HIGH';
|
|
175
|
+
const CVSS_CRITICAL = 9;
|
|
176
|
+
const CVSS_HIGH = 7;
|
|
177
|
+
const CVSS_MEDIUM = 4;
|
|
178
|
+
const CVSS_SCORE = /^\d{1,2}(?:\.\d)?$/u;
|
|
179
|
+
|
|
180
|
+
const OWASP_WEB_COUNT = 10;
|
|
181
|
+
/** OWASP Top 10 2021 titles, to accept an id written without its edition. */
|
|
182
|
+
const OWASP_2021_TITLES = [
|
|
183
|
+
'broken access control',
|
|
184
|
+
'cryptographic failures',
|
|
185
|
+
'injection',
|
|
186
|
+
'insecure design',
|
|
187
|
+
'security misconfiguration',
|
|
188
|
+
'vulnerable and outdated components',
|
|
189
|
+
'identification and authentication failures',
|
|
190
|
+
'software and data integrity failures',
|
|
191
|
+
'security logging and monitoring failures',
|
|
192
|
+
'server-side request forgery',
|
|
193
|
+
];
|
|
194
|
+
const OWASP_2017_TITLES = [
|
|
195
|
+
'injection',
|
|
196
|
+
'broken authentication',
|
|
197
|
+
'sensitive data exposure',
|
|
198
|
+
'xml external entities',
|
|
199
|
+
'broken access control',
|
|
200
|
+
'security misconfiguration',
|
|
201
|
+
'cross-site scripting',
|
|
202
|
+
'insecure deserialization',
|
|
203
|
+
'using components with known vulnerabilities',
|
|
204
|
+
'insufficient logging',
|
|
205
|
+
];
|
|
206
|
+
/** Each 2017 category lands in exactly one 2021 category (OWASP's own mapping). */
|
|
207
|
+
const OWASP_2017_TO_2021 = [
|
|
208
|
+
'A03:2021',
|
|
209
|
+
'A07:2021',
|
|
210
|
+
'A02:2021',
|
|
211
|
+
'A05:2021',
|
|
212
|
+
'A01:2021',
|
|
213
|
+
'A05:2021',
|
|
214
|
+
'A03:2021',
|
|
215
|
+
'A08:2021',
|
|
216
|
+
'A06:2021',
|
|
217
|
+
'A09:2021',
|
|
218
|
+
];
|
|
219
|
+
const OWASP_WEB = /^A(\d{1,2})(?::(\d{4}))?(?!\d)\s*(?:[-:–—]\s*)?(.*)$/su;
|
|
220
|
+
const OWASP_LLM = /^LLM(0[1-9]|10)(?::2025)?(?!\d)/u;
|
|
221
|
+
const OWASP_AGENTIC = /^ASI(0[1-9]|10)(?!\d)/u;
|
|
222
|
+
const OWASP_FAMILY_ORDER = ['A', 'L', 'S'];
|
|
223
|
+
|
|
224
|
+
/** @param {Severity} severity */
|
|
225
|
+
export function severityRank(severity) {
|
|
226
|
+
return SEVERITIES.indexOf(severity);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Whether a finding of this severity reaches `--fail-on` (D6).
|
|
231
|
+
* @param {Severity} severity
|
|
232
|
+
* @param {FailOn} failOn
|
|
233
|
+
*/
|
|
234
|
+
export function meetsFailOn(severity, failOn) {
|
|
235
|
+
if (failOn === 'none') return false;
|
|
236
|
+
return severityRank(severity) <= severityRank(/** @type {Severity} */ (failOn.toUpperCase()));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** @param {unknown} value @returns {Severity} */
|
|
240
|
+
export function semgrepSeverity(value) {
|
|
241
|
+
return (
|
|
242
|
+
(typeof value === 'string' ? SEMGREP_SEVERITY.get(value.toUpperCase()) : undefined) ??
|
|
243
|
+
SEMGREP_UNKNOWN_SEVERITY
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** @param {unknown} value @returns {Severity} */
|
|
248
|
+
export function trivySeverity(value) {
|
|
249
|
+
return (
|
|
250
|
+
(typeof value === 'string' ? TRIVY_SEVERITY.get(value.toUpperCase()) : undefined) ?? 'INFO'
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/** CVSS v3 qualitative ratings. @param {number} score @returns {Severity} */
|
|
255
|
+
export function cvssSeverity(score) {
|
|
256
|
+
if (score >= CVSS_CRITICAL) return 'CRITICAL';
|
|
257
|
+
if (score >= CVSS_HIGH) return 'HIGH';
|
|
258
|
+
if (score >= CVSS_MEDIUM) return 'MEDIUM';
|
|
259
|
+
return score > 0 ? 'LOW' : 'INFO';
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** @param {string} ruleId @returns {Severity} */
|
|
263
|
+
export function gitleaksSeverity(ruleId) {
|
|
264
|
+
const id = ruleId.toLowerCase();
|
|
265
|
+
return CRITICAL_SECRET_RULE_PREFIXES.some((prefix) => id.startsWith(prefix))
|
|
266
|
+
? 'CRITICAL'
|
|
267
|
+
: 'HIGH';
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** @param {number} number */
|
|
271
|
+
function webId(number) {
|
|
272
|
+
return `A${String(number).padStart(2, '0')}:2021`;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* One OWASP id from a rule's metadata string, or null (D10). Web ids are
|
|
277
|
+
* 2021; a 2017 id is mapped and marked `legacy`; an id without an edition is
|
|
278
|
+
* kept only when its title says which edition it is; any other edition
|
|
279
|
+
* (2025 included, S2 note on D10) is dropped.
|
|
280
|
+
* @param {string} raw
|
|
281
|
+
* @returns {{ id: string, legacy: boolean } | null}
|
|
282
|
+
*/
|
|
283
|
+
function owaspIdOf(raw) {
|
|
284
|
+
const text = raw.trim();
|
|
285
|
+
const llm = OWASP_LLM.exec(text);
|
|
286
|
+
if (llm !== null) return { id: `LLM${llm[1]}:2025`, legacy: false };
|
|
287
|
+
const agentic = OWASP_AGENTIC.exec(text);
|
|
288
|
+
if (agentic !== null) return { id: `ASI${agentic[1]}`, legacy: false };
|
|
289
|
+
const web = OWASP_WEB.exec(text);
|
|
290
|
+
if (web === null) return null;
|
|
291
|
+
const number = Number(web[1]);
|
|
292
|
+
if (number < 1 || number > OWASP_WEB_COUNT) return null;
|
|
293
|
+
const edition = web[2];
|
|
294
|
+
const mapped = OWASP_2017_TO_2021[number - 1];
|
|
295
|
+
if (edition === '2021') return { id: webId(number), legacy: false };
|
|
296
|
+
if (edition === '2017') return mapped === undefined ? null : { id: mapped, legacy: true };
|
|
297
|
+
if (edition !== undefined) return null;
|
|
298
|
+
const title = (web[3] ?? '').trim().toLowerCase();
|
|
299
|
+
if (title.startsWith(OWASP_2021_TITLES[number - 1] ?? '\u0000'))
|
|
300
|
+
return { id: webId(number), legacy: false };
|
|
301
|
+
if (mapped !== undefined && title.startsWith(OWASP_2017_TITLES[number - 1] ?? '\u0000'))
|
|
302
|
+
return { id: mapped, legacy: true };
|
|
303
|
+
return null;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/** @param {string} id */
|
|
307
|
+
function owaspSortKey(id) {
|
|
308
|
+
const family = id.startsWith('LLM') ? 'L' : id.startsWith('ASI') ? 'S' : 'A';
|
|
309
|
+
const number = Number(/\d+/u.exec(id)?.[0] ?? '0');
|
|
310
|
+
return OWASP_FAMILY_ORDER.indexOf(family) * 100 + number;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Normalize metadata values (strings or lists of strings) into sorted,
|
|
315
|
+
* unique ids: Web, then LLM, then Agentic.
|
|
316
|
+
* @param {readonly unknown[]} values
|
|
317
|
+
* @returns {string[]}
|
|
318
|
+
*/
|
|
319
|
+
export function normalizeOwasp(values) {
|
|
320
|
+
const ids = new Set();
|
|
321
|
+
/** @type {Set<string>} */
|
|
322
|
+
const legacy = new Set();
|
|
323
|
+
for (const value of values) {
|
|
324
|
+
if (typeof value !== 'string') continue;
|
|
325
|
+
const parsed = owaspIdOf(value);
|
|
326
|
+
if (parsed === null) continue;
|
|
327
|
+
(parsed.legacy ? legacy : ids).add(parsed.id);
|
|
328
|
+
}
|
|
329
|
+
// A 2017 id stands in only for a rule that names no Web 2021 id itself.
|
|
330
|
+
const namesWeb2021 = [...ids].some((id) => id.endsWith(':2021'));
|
|
331
|
+
if (!namesWeb2021) for (const id of legacy) ids.add(id);
|
|
332
|
+
return [...ids].sort((left, right) => owaspSortKey(left) - owaspSortKey(right));
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** @param {unknown} value @returns {unknown[]} */
|
|
336
|
+
function listOf(value) {
|
|
337
|
+
if (Array.isArray(value)) return value;
|
|
338
|
+
return value === undefined || value === null ? [] : [value];
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** @param {string} text */
|
|
342
|
+
function cleanText(text) {
|
|
343
|
+
return text.replace(CONTROL_CHARACTERS, ' ');
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** @param {string} text @param {number} max */
|
|
347
|
+
function bounded(text, max) {
|
|
348
|
+
return text.length <= max ? text : `${text.slice(0, max - 1)}${ELLIPSIS}`;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** @param {unknown} value @returns {value is string} */
|
|
352
|
+
function isRedactable(value) {
|
|
353
|
+
return (
|
|
354
|
+
typeof value === 'string' &&
|
|
355
|
+
value.length >= MIN_REDACTED_LENGTH &&
|
|
356
|
+
value !== GITLEAKS_REDACTED &&
|
|
357
|
+
value !== REDACTED &&
|
|
358
|
+
value !== SEMGREP_LOGIN_SENTINEL
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* Replace each matched value (longest first) by `[REDACTED]`.
|
|
364
|
+
* @param {string} text
|
|
365
|
+
* @param {readonly string[]} secrets
|
|
366
|
+
*/
|
|
367
|
+
export function redact(text, secrets) {
|
|
368
|
+
let result = text;
|
|
369
|
+
for (const secret of [...secrets].sort((left, right) => right.length - left.length))
|
|
370
|
+
result = result.replaceAll(secret, REDACTED);
|
|
371
|
+
return result;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/** @param {string} text @param {readonly string[]} secrets */
|
|
375
|
+
function messageOf(text, secrets) {
|
|
376
|
+
return bounded(cleanText(redact(text, secrets)).trim(), MAX_MESSAGE_LENGTH);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** @param {unknown} value @param {string} fallback */
|
|
380
|
+
function textOr(value, fallback) {
|
|
381
|
+
return typeof value === 'string' && value.trim().length > 0 ? value : fallback;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** @param {unknown} value @param {string} fallback */
|
|
385
|
+
function ruleIdOf(value, fallback) {
|
|
386
|
+
return bounded(cleanText(textOr(value, fallback)).trim(), MAX_RULE_ID_LENGTH);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** @param {unknown} value */
|
|
390
|
+
function positiveLine(value) {
|
|
391
|
+
return Number.isSafeInteger(value) && /** @type {number} */ (value) >= 1
|
|
392
|
+
? /** @type {number} */ (value)
|
|
393
|
+
: null;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* The location relative to the target: the container mount or the host path
|
|
398
|
+
* is stripped; anything else is kept as reported (it is data, never opened).
|
|
399
|
+
* @param {unknown} file
|
|
400
|
+
* @param {readonly string[]} prefixes
|
|
401
|
+
*/
|
|
402
|
+
export function findingPath(file, prefixes) {
|
|
403
|
+
let path = typeof file === 'string' ? file : '';
|
|
404
|
+
for (const prefix of prefixes) {
|
|
405
|
+
if (path.startsWith(prefix)) {
|
|
406
|
+
path = path.slice(prefix.length);
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
path = path.replace(/^(?:\.\/)+/u, '');
|
|
411
|
+
return bounded(cleanText(path.length === 0 ? '.' : path), MAX_PATH_LENGTH);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* @param {{ tool: ScanTool, kind: ScanKind, ruleId: string, severity: Severity, owasp: string[],
|
|
416
|
+
* path: string, line: number | null, message: string, material: string | null }} input
|
|
417
|
+
* @returns {Finding}
|
|
418
|
+
*/
|
|
419
|
+
function finding(input) {
|
|
420
|
+
const materialHash = input.material === null ? '' : sha256Hex(input.material);
|
|
421
|
+
return {
|
|
422
|
+
tool: input.tool,
|
|
423
|
+
kind: input.kind,
|
|
424
|
+
ruleId: input.ruleId,
|
|
425
|
+
severity: input.severity,
|
|
426
|
+
owasp: input.owasp,
|
|
427
|
+
path: input.path,
|
|
428
|
+
line: input.line,
|
|
429
|
+
message: input.message,
|
|
430
|
+
fingerprint: sha256Hex(
|
|
431
|
+
`${input.tool}:${input.ruleId}:${input.path}:${input.line ?? 0}:${materialHash}`,
|
|
432
|
+
),
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Every string inside a JSON value with the matched values replaced.
|
|
438
|
+
* @param {unknown} value
|
|
439
|
+
* @param {readonly string[]} secrets
|
|
440
|
+
* @returns {unknown}
|
|
441
|
+
*/
|
|
442
|
+
function deepRedact(value, secrets) {
|
|
443
|
+
if (secrets.length === 0) return value;
|
|
444
|
+
if (typeof value === 'string') return redact(value, secrets);
|
|
445
|
+
if (Array.isArray(value)) return value.map((item) => deepRedact(item, secrets));
|
|
446
|
+
if (isRecord(value))
|
|
447
|
+
return Object.fromEntries(
|
|
448
|
+
Object.entries(value).map(([key, item]) => [key, deepRedact(item, secrets)]),
|
|
449
|
+
);
|
|
450
|
+
return value;
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/** gitleaks fields that hold the match, the line or a commit author. */
|
|
454
|
+
const GITLEAKS_SECRET_FIELDS = ['Secret', 'Match', 'Line'];
|
|
455
|
+
const SHA256_HEX = /^[0-9a-f]{64}$/u;
|
|
456
|
+
const COMMIT_ID = /^[0-9a-f]{7,64}$/u;
|
|
457
|
+
const SHORT_COMMIT_LENGTH = 7;
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* gitleaks, through the client's report template (`--report-format template`,
|
|
461
|
+
* `GITLEAKS_REPORT_FIELDS`): an array of leaks without the matched value, plus
|
|
462
|
+
* its sha256 (`ValueSha256`). Should a binary write the value anyway
|
|
463
|
+
* (`Secret`, `Match`, `Line`), it is redacted from every message here, and
|
|
464
|
+
* the sanitized copy keeps only the template's fields.
|
|
465
|
+
* @param {unknown} value
|
|
466
|
+
* @param {ParseContext} context
|
|
467
|
+
* @returns {ParsedOutput | null}
|
|
468
|
+
*/
|
|
469
|
+
export function parseGitleaks(value, context) {
|
|
470
|
+
if (!Array.isArray(value)) return null;
|
|
471
|
+
/** @type {string[]} */
|
|
472
|
+
const secrets = [];
|
|
473
|
+
/** @type {Finding[]} */
|
|
474
|
+
const findings = [];
|
|
475
|
+
/** @type {SecretIdentity[]} */
|
|
476
|
+
const identities = [];
|
|
477
|
+
for (const item of value) {
|
|
478
|
+
if (!isRecord(item) || typeof item.RuleID !== 'string' || typeof item.File !== 'string')
|
|
479
|
+
return null;
|
|
480
|
+
const digest = item[GITLEAKS_VALUE_DIGEST];
|
|
481
|
+
if (digest !== undefined && (typeof digest !== 'string' || !SHA256_HEX.test(digest)))
|
|
482
|
+
return null;
|
|
483
|
+
const own = GITLEAKS_SECRET_FIELDS.map((field) => item[field]).filter(isRedactable);
|
|
484
|
+
secrets.push(...own);
|
|
485
|
+
const ruleId = ruleIdOf(item.RuleID, 'unknown');
|
|
486
|
+
const found = finding({
|
|
487
|
+
tool: 'gitleaks',
|
|
488
|
+
kind: 'secrets',
|
|
489
|
+
ruleId,
|
|
490
|
+
severity: gitleaksSeverity(ruleId),
|
|
491
|
+
owasp: [SECRETS_OWASP],
|
|
492
|
+
path: findingPath(item.File, context.prefixes),
|
|
493
|
+
line: positiveLine(item.StartLine),
|
|
494
|
+
message: messageOf(textOr(item.Description, ruleId), own),
|
|
495
|
+
material: typeof item.Commit === 'string' && item.Commit.length > 0 ? item.Commit : null,
|
|
496
|
+
});
|
|
497
|
+
findings.push(found);
|
|
498
|
+
const same = digest === undefined ? `line:${found.line ?? 0}` : `value:${digest}`;
|
|
499
|
+
identities.push({
|
|
500
|
+
key: sha256Hex(`${ruleId}:${found.path}:${same}`),
|
|
501
|
+
commit: typeof item.Commit === 'string' && COMMIT_ID.test(item.Commit) ? item.Commit : null,
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
// Only the template's fields reach the copy, whatever shape the tool wrote:
|
|
505
|
+
// never the value, its digest (a weak value's hash can be guessed), the
|
|
506
|
+
// commit's author, date or message. The digest never reaches the project or
|
|
507
|
+
// any report; the raw output in the private workspace is removed after the run.
|
|
508
|
+
const sanitized = value.map((item) =>
|
|
509
|
+
isRecord(item)
|
|
510
|
+
? Object.fromEntries(
|
|
511
|
+
GITLEAKS_REPORT_FIELDS.filter((field) => field in item).map((field) => [
|
|
512
|
+
field,
|
|
513
|
+
item[field],
|
|
514
|
+
]),
|
|
515
|
+
)
|
|
516
|
+
: {},
|
|
517
|
+
);
|
|
518
|
+
return { findings, sanitized: deepRedact(sanitized, secrets), identities };
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* gitleaks' two passes as one list (scan review F1). A secret still in the
|
|
523
|
+
* tree is its tree row alone, wherever its line was in history. A secret
|
|
524
|
+
* found only in history is one row, at the first place gitleaks reported,
|
|
525
|
+
* with that commit and how many more hold it. Rows of the tree pass are kept
|
|
526
|
+
* as they are.
|
|
527
|
+
* @param {ParsedOutput} tree
|
|
528
|
+
* @param {ParsedOutput} history
|
|
529
|
+
* @returns {Finding[]}
|
|
530
|
+
*/
|
|
531
|
+
export function mergeSecretPasses(tree, history) {
|
|
532
|
+
const inTree = new Set((tree.identities ?? []).map((identity) => identity.key));
|
|
533
|
+
/** @type {Map<string, { finding: Finding, commits: string[] }>} */
|
|
534
|
+
const past = new Map();
|
|
535
|
+
history.findings.forEach((found, index) => {
|
|
536
|
+
const identity = history.identities?.[index] ?? { key: found.fingerprint, commit: null };
|
|
537
|
+
if (inTree.has(identity.key)) return;
|
|
538
|
+
const entry = past.get(identity.key) ?? { finding: found, commits: [] };
|
|
539
|
+
if (identity.commit !== null && !entry.commits.includes(identity.commit))
|
|
540
|
+
entry.commits.push(identity.commit);
|
|
541
|
+
past.set(identity.key, entry);
|
|
542
|
+
});
|
|
543
|
+
return [
|
|
544
|
+
...tree.findings,
|
|
545
|
+
...[...past.values()].map((entry) => withCommits(entry.finding, entry.commits)),
|
|
546
|
+
];
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/** The message names the commit and counts the others. @param {Finding} found @param {readonly string[]} commits */
|
|
550
|
+
function withCommits(found, commits) {
|
|
551
|
+
const [first] = commits;
|
|
552
|
+
if (first === undefined) return found;
|
|
553
|
+
const more = commits.length - 1;
|
|
554
|
+
const suffix = ` (commit ${first.slice(0, SHORT_COMMIT_LENGTH)}${more > 0 ? ` and ${more} more` : ''})`;
|
|
555
|
+
return {
|
|
556
|
+
...found,
|
|
557
|
+
message: `${bounded(found.message, MAX_MESSAGE_LENGTH - suffix.length)}${suffix}`,
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/** Matched code of a Semgrep result: its lines and metavariable values. @param {Record<string, unknown>} extra */
|
|
562
|
+
function semgrepMatches(extra) {
|
|
563
|
+
/** @type {string[]} */
|
|
564
|
+
const matches = [];
|
|
565
|
+
if (typeof extra.lines === 'string') {
|
|
566
|
+
if (isRedactable(extra.lines.trim())) matches.push(extra.lines.trim());
|
|
567
|
+
for (const line of extra.lines.split('\n'))
|
|
568
|
+
if (isRedactable(line.trim())) matches.push(line.trim());
|
|
569
|
+
}
|
|
570
|
+
if (isRecord(extra.metavars)) {
|
|
571
|
+
for (const metavariable of Object.values(extra.metavars)) {
|
|
572
|
+
if (isRecord(metavariable) && isRedactable(metavariable.abstract_content))
|
|
573
|
+
matches.push(metavariable.abstract_content);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
return matches;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/** Semgrep rules of the secrets class, when their metadata names no OWASP id. @param {string} ruleId @param {Record<string, unknown>} metadata */
|
|
580
|
+
function semgrepFallbackOwasp(ruleId, metadata) {
|
|
581
|
+
const cwe = listOf(metadata.cwe).filter((item) => typeof item === 'string');
|
|
582
|
+
const isSecret =
|
|
583
|
+
/(?:^|\.)secrets?(?:\.|$)/u.test(ruleId) || cwe.some((item) => item.startsWith('CWE-798'));
|
|
584
|
+
return isSecret ? [SECRETS_OWASP] : [];
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
const SEMGREP_CODE_FIELDS = ['metavars', 'fix', 'fix_regex', 'rendered_fix', 'dataflow_trace'];
|
|
588
|
+
const SEMGREP_ERROR_FIELDS = ['code', 'level', 'rule_id', 'path'];
|
|
589
|
+
|
|
590
|
+
/** @param {unknown} error */
|
|
591
|
+
function sanitizeSemgrepError(error) {
|
|
592
|
+
if (!isRecord(error)) return {};
|
|
593
|
+
/** @type {Record<string, unknown>} */
|
|
594
|
+
const copy = {};
|
|
595
|
+
for (const field of SEMGREP_ERROR_FIELDS) {
|
|
596
|
+
const item = error[field];
|
|
597
|
+
if (typeof item === 'string' || typeof item === 'number') copy[field] = item;
|
|
598
|
+
}
|
|
599
|
+
const type = Array.isArray(error.type) ? error.type[0] : error.type;
|
|
600
|
+
if (typeof type === 'string') copy.type = type;
|
|
601
|
+
return copy;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Semgrep (`--json`): `results[]` with `check_id`, `path`, `start.line` and
|
|
606
|
+
* `extra` (message, severity, metadata, lines, metavars).
|
|
607
|
+
* @param {unknown} value
|
|
608
|
+
* @param {ParseContext} context
|
|
609
|
+
* @returns {ParsedOutput | null}
|
|
610
|
+
*/
|
|
611
|
+
export function parseSemgrep(value, context) {
|
|
612
|
+
if (!isRecord(value) || !Array.isArray(value.results)) return null;
|
|
613
|
+
/** @type {string[]} */
|
|
614
|
+
const secrets = [];
|
|
615
|
+
/** @type {Finding[]} */
|
|
616
|
+
const findings = [];
|
|
617
|
+
/** @type {unknown[]} */
|
|
618
|
+
const results = [];
|
|
619
|
+
for (const result of value.results) {
|
|
620
|
+
if (
|
|
621
|
+
!isRecord(result) ||
|
|
622
|
+
typeof result.check_id !== 'string' ||
|
|
623
|
+
typeof result.path !== 'string' ||
|
|
624
|
+
!isRecord(result.extra)
|
|
625
|
+
)
|
|
626
|
+
return null;
|
|
627
|
+
const extra = result.extra;
|
|
628
|
+
const matches = semgrepMatches(extra);
|
|
629
|
+
secrets.push(...matches);
|
|
630
|
+
const metadata = isRecord(extra.metadata) ? extra.metadata : {};
|
|
631
|
+
const aisac = isRecord(metadata.aisac) ? metadata.aisac : {};
|
|
632
|
+
const ruleId = ruleIdOf(result.check_id, 'unknown');
|
|
633
|
+
const owasp = normalizeOwasp([
|
|
634
|
+
...listOf(metadata.owasp),
|
|
635
|
+
...listOf(aisac.owasp_llm),
|
|
636
|
+
...listOf(aisac.owasp_agentic),
|
|
637
|
+
]);
|
|
638
|
+
const start = isRecord(result.start) ? result.start : {};
|
|
639
|
+
const message = messageOf(textOr(extra.message, ruleId), matches);
|
|
640
|
+
findings.push(
|
|
641
|
+
finding({
|
|
642
|
+
tool: 'semgrep',
|
|
643
|
+
kind: 'sast',
|
|
644
|
+
ruleId,
|
|
645
|
+
severity: semgrepSeverity(extra.severity),
|
|
646
|
+
owasp: owasp.length > 0 ? owasp : semgrepFallbackOwasp(ruleId, metadata),
|
|
647
|
+
path: findingPath(result.path, context.prefixes),
|
|
648
|
+
line: positiveLine(start.line),
|
|
649
|
+
message,
|
|
650
|
+
material: typeof extra.lines === 'string' && isRedactable(extra.lines) ? extra.lines : null,
|
|
651
|
+
}),
|
|
652
|
+
);
|
|
653
|
+
const sanitizedExtra = { ...extra };
|
|
654
|
+
for (const field of SEMGREP_CODE_FIELDS) delete sanitizedExtra[field];
|
|
655
|
+
if (typeof sanitizedExtra.lines === 'string') sanitizedExtra.lines = REDACTED;
|
|
656
|
+
if (typeof sanitizedExtra.message === 'string')
|
|
657
|
+
sanitizedExtra.message = bounded(
|
|
658
|
+
redact(sanitizedExtra.message, matches),
|
|
659
|
+
MAX_SANITIZED_MESSAGE_LENGTH,
|
|
660
|
+
);
|
|
661
|
+
results.push({ ...result, extra: sanitizedExtra });
|
|
662
|
+
}
|
|
663
|
+
const sanitized = {
|
|
664
|
+
...value,
|
|
665
|
+
results,
|
|
666
|
+
errors: Array.isArray(value.errors) ? value.errors.map(sanitizeSemgrepError) : [],
|
|
667
|
+
};
|
|
668
|
+
return { findings, sanitized: deepRedact(sanitized, secrets) };
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
/**
|
|
672
|
+
* The highest advisory level among a group's advisories, from the group's
|
|
673
|
+
* CVSS score, else the advisory database level, else HIGH (S2 note on D6).
|
|
674
|
+
* @param {unknown} maxSeverity
|
|
675
|
+
* @param {readonly Record<string, unknown>[]} advisories
|
|
676
|
+
* @returns {Severity}
|
|
677
|
+
*/
|
|
678
|
+
function advisorySeverity(maxSeverity, advisories) {
|
|
679
|
+
if (typeof maxSeverity === 'string' && CVSS_SCORE.test(maxSeverity.trim()))
|
|
680
|
+
return cvssSeverity(Number(maxSeverity));
|
|
681
|
+
/** @type {Severity | null} */
|
|
682
|
+
let best = null;
|
|
683
|
+
for (const advisory of advisories) {
|
|
684
|
+
const database = isRecord(advisory.database_specific) ? advisory.database_specific : {};
|
|
685
|
+
const level =
|
|
686
|
+
typeof database.severity === 'string'
|
|
687
|
+
? ADVISORY_SEVERITY.get(database.severity.toUpperCase())
|
|
688
|
+
: undefined;
|
|
689
|
+
if (level !== undefined && (best === null || severityRank(level) < severityRank(best)))
|
|
690
|
+
best = level;
|
|
691
|
+
}
|
|
692
|
+
return best ?? DEFAULT_ADVISORY_SEVERITY;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* The advisory groups of one package: osv-scanner's `groups` (one per
|
|
697
|
+
* vulnerability, aliases folded), else one per listed vulnerability.
|
|
698
|
+
* @param {Record<string, unknown>} entry
|
|
699
|
+
* @param {Record<string, unknown>[]} advisories
|
|
700
|
+
*/
|
|
701
|
+
function advisoryGroups(entry, advisories) {
|
|
702
|
+
const groups = Array.isArray(entry.groups) ? entry.groups.filter(isRecord) : [];
|
|
703
|
+
if (groups.length > 0)
|
|
704
|
+
return groups.map((group) => ({
|
|
705
|
+
ids: listOf(group.ids).filter((id) => typeof id === 'string'),
|
|
706
|
+
maxSeverity: group.max_severity,
|
|
707
|
+
}));
|
|
708
|
+
return advisories.map((advisory) => ({
|
|
709
|
+
ids: typeof advisory.id === 'string' ? [advisory.id] : [],
|
|
710
|
+
maxSeverity: undefined,
|
|
711
|
+
}));
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* osv-scanner (`--format json`): `results[].packages[]` with
|
|
716
|
+
* `vulnerabilities` and `groups`. A lockfile has no line.
|
|
717
|
+
* @param {unknown} value
|
|
718
|
+
* @param {ParseContext} context
|
|
719
|
+
* @returns {ParsedOutput | null}
|
|
720
|
+
*/
|
|
721
|
+
export function parseOsvScanner(value, context) {
|
|
722
|
+
if (value === null) return { findings: [], sanitized: { results: [] } };
|
|
723
|
+
if (!isRecord(value)) return null;
|
|
724
|
+
const results = value.results ?? [];
|
|
725
|
+
if (!Array.isArray(results)) return null;
|
|
726
|
+
/** @type {Finding[]} */
|
|
727
|
+
const findings = [];
|
|
728
|
+
for (const result of results) {
|
|
729
|
+
if (!isRecord(result) || !isRecord(result.source) || typeof result.source.path !== 'string')
|
|
730
|
+
return null;
|
|
731
|
+
const path = findingPath(result.source.path, context.prefixes);
|
|
732
|
+
for (const entry of Array.isArray(result.packages) ? result.packages : []) {
|
|
733
|
+
if (!isRecord(entry) || !isRecord(entry.package)) return null;
|
|
734
|
+
const name = textOr(entry.package.name, 'unknown');
|
|
735
|
+
const version = textOr(entry.package.version, '');
|
|
736
|
+
const ecosystem = textOr(entry.package.ecosystem, '');
|
|
737
|
+
const advisories = Array.isArray(entry.vulnerabilities)
|
|
738
|
+
? entry.vulnerabilities.filter(isRecord)
|
|
739
|
+
: [];
|
|
740
|
+
for (const group of advisoryGroups(entry, advisories)) {
|
|
741
|
+
const primary = group.ids[0];
|
|
742
|
+
if (typeof primary !== 'string') continue;
|
|
743
|
+
const members = advisories.filter(
|
|
744
|
+
(advisory) => typeof advisory.id === 'string' && group.ids.includes(advisory.id),
|
|
745
|
+
);
|
|
746
|
+
const summary = members
|
|
747
|
+
.map((advisory) => advisory.summary)
|
|
748
|
+
.find((text) => typeof text === 'string');
|
|
749
|
+
findings.push(
|
|
750
|
+
finding({
|
|
751
|
+
tool: 'osv-scanner',
|
|
752
|
+
kind: 'sca',
|
|
753
|
+
ruleId: ruleIdOf(primary, 'unknown'),
|
|
754
|
+
severity: advisorySeverity(group.maxSeverity, members),
|
|
755
|
+
owasp: [ADVISORY_OWASP],
|
|
756
|
+
path,
|
|
757
|
+
line: null,
|
|
758
|
+
message: messageOf(
|
|
759
|
+
`${name}@${version} (${ecosystem}): ${textOr(summary, primary)}`,
|
|
760
|
+
[],
|
|
761
|
+
),
|
|
762
|
+
material: `${ecosystem}:${name}@${version}`,
|
|
763
|
+
}),
|
|
764
|
+
);
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
return { findings, sanitized: value };
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
/** The code lines Trivy quotes for a misconfiguration. @param {unknown} code */
|
|
772
|
+
function trivyCodeLines(code) {
|
|
773
|
+
if (!isRecord(code) || !Array.isArray(code.Lines)) return [];
|
|
774
|
+
return code.Lines.filter(isRecord)
|
|
775
|
+
.map((line) => line.Content)
|
|
776
|
+
.filter((content) => typeof content === 'string')
|
|
777
|
+
.map((content) => content.trim())
|
|
778
|
+
.filter(isRedactable);
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* Trivy (`fs --scanners misconfig --format json`): `Results[]` with
|
|
783
|
+
* `Misconfigurations[]`; only failed checks are findings.
|
|
784
|
+
* @param {unknown} value
|
|
785
|
+
* @param {ParseContext} context
|
|
786
|
+
* @returns {ParsedOutput | null}
|
|
787
|
+
*/
|
|
788
|
+
export function parseTrivyMisconfig(value, context) {
|
|
789
|
+
if (!isRecord(value)) return null;
|
|
790
|
+
const results = value.Results ?? [];
|
|
791
|
+
if (!Array.isArray(results)) return null;
|
|
792
|
+
/** @type {string[]} */
|
|
793
|
+
const secrets = [];
|
|
794
|
+
/** @type {Finding[]} */
|
|
795
|
+
const findings = [];
|
|
796
|
+
const sanitizedResults = [];
|
|
797
|
+
for (const result of results) {
|
|
798
|
+
if (!isRecord(result)) return null;
|
|
799
|
+
const misconfigurations = Array.isArray(result.Misconfigurations)
|
|
800
|
+
? result.Misconfigurations
|
|
801
|
+
: [];
|
|
802
|
+
const sanitizedMisconfigurations = [];
|
|
803
|
+
for (const item of misconfigurations) {
|
|
804
|
+
if (!isRecord(item)) return null;
|
|
805
|
+
const cause = isRecord(item.CauseMetadata) ? item.CauseMetadata : {};
|
|
806
|
+
const code = trivyCodeLines(cause.Code);
|
|
807
|
+
secrets.push(...code);
|
|
808
|
+
// The quoted source lines may hold a secret: the copy keeps everything else.
|
|
809
|
+
const causeWithoutCode = { ...cause };
|
|
810
|
+
delete causeWithoutCode.Code;
|
|
811
|
+
sanitizedMisconfigurations.push({ ...item, CauseMetadata: causeWithoutCode });
|
|
812
|
+
if (item.Status !== 'FAIL') continue;
|
|
813
|
+
const ruleId = ruleIdOf(item.ID, textOr(item.AVDID, 'unknown'));
|
|
814
|
+
findings.push(
|
|
815
|
+
finding({
|
|
816
|
+
tool: 'trivy',
|
|
817
|
+
kind: 'iac',
|
|
818
|
+
ruleId,
|
|
819
|
+
severity: trivySeverity(item.Severity),
|
|
820
|
+
owasp: [MISCONFIGURATION_OWASP],
|
|
821
|
+
path: findingPath(result.Target, context.prefixes),
|
|
822
|
+
line: positiveLine(cause.StartLine),
|
|
823
|
+
message: messageOf(textOr(item.Message, textOr(item.Title, ruleId)), code),
|
|
824
|
+
material: code.length > 0 ? code.join('\n') : null,
|
|
825
|
+
}),
|
|
826
|
+
);
|
|
827
|
+
}
|
|
828
|
+
sanitizedResults.push(
|
|
829
|
+
Array.isArray(result.Misconfigurations)
|
|
830
|
+
? { ...result, Misconfigurations: sanitizedMisconfigurations }
|
|
831
|
+
: result,
|
|
832
|
+
);
|
|
833
|
+
}
|
|
834
|
+
return { findings, sanitized: deepRedact({ ...value, Results: sanitizedResults }, secrets) };
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
/** @type {Readonly<Record<ScanTool, (value: unknown, context: ParseContext) => ParsedOutput | null>>} */
|
|
838
|
+
export const PARSERS = Object.freeze({
|
|
839
|
+
gitleaks: parseGitleaks,
|
|
840
|
+
semgrep: parseSemgrep,
|
|
841
|
+
'osv-scanner': parseOsvScanner,
|
|
842
|
+
trivy: parseTrivyMisconfig,
|
|
843
|
+
});
|
|
844
|
+
|
|
845
|
+
const TOOL_ORDER = new Map(SCAN_TOOLS.map((tool, index) => [tool, index]));
|
|
846
|
+
|
|
847
|
+
/** @param {string} left @param {string} right */
|
|
848
|
+
function compareText(left, right) {
|
|
849
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
/** Severity, tool, path, line, rule, fingerprint. @param {Finding} left @param {Finding} right */
|
|
853
|
+
export function compareFindings(left, right) {
|
|
854
|
+
return (
|
|
855
|
+
severityRank(left.severity) - severityRank(right.severity) ||
|
|
856
|
+
(TOOL_ORDER.get(left.tool) ?? 0) - (TOOL_ORDER.get(right.tool) ?? 0) ||
|
|
857
|
+
compareText(left.path, right.path) ||
|
|
858
|
+
(left.line ?? 0) - (right.line ?? 0) ||
|
|
859
|
+
compareText(left.ruleId, right.ruleId) ||
|
|
860
|
+
compareText(left.fingerprint, right.fingerprint)
|
|
861
|
+
);
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
/** @param {readonly Finding[]} findings @returns {Record<Severity, number>} */
|
|
865
|
+
export function countFindings(findings) {
|
|
866
|
+
/** @type {Record<Severity, number>} */
|
|
867
|
+
const counts = { CRITICAL: 0, HIGH: 0, MEDIUM: 0, LOW: 0, INFO: 0 };
|
|
868
|
+
for (const item of findings) counts[item.severity] += 1;
|
|
869
|
+
return counts;
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
/**
|
|
873
|
+
* Exit code and reason (D6). A failed or timed-out tool makes the report
|
|
874
|
+
* incomplete, so 3 comes before 1; a run where nothing ran is never green.
|
|
875
|
+
* @param {readonly ToolRecord[]} tools
|
|
876
|
+
* @param {readonly Finding[]} findings
|
|
877
|
+
* @param {FailOn} failOn
|
|
878
|
+
* @param {boolean} interrupted
|
|
879
|
+
* @returns {{ exitCode: 0 | 1 | 3, exitReason: ExitReason }}
|
|
880
|
+
*/
|
|
881
|
+
export function exitOutcome(tools, findings, failOn, interrupted) {
|
|
882
|
+
if (interrupted) return { exitCode: 3, exitReason: 'interrupted' };
|
|
883
|
+
if (tools.some((tool) => tool.status === 'failed' || tool.status === 'timed_out'))
|
|
884
|
+
return { exitCode: 3, exitReason: 'tool_failed' };
|
|
885
|
+
if (!tools.some((tool) => tool.status === 'ran'))
|
|
886
|
+
return { exitCode: 3, exitReason: 'no_scanner_available' };
|
|
887
|
+
if (findings.some((item) => meetsFailOn(item.severity, failOn)))
|
|
888
|
+
return { exitCode: 1, exitReason: 'findings' };
|
|
889
|
+
return { exitCode: 0, exitReason: 'clean' };
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
/**
|
|
893
|
+
* At most `MAX_FINDINGS_PER_TOOL` findings per tool, the most severe first;
|
|
894
|
+
* the counts and the exit code are computed before this cut.
|
|
895
|
+
* @param {readonly Finding[]} sorted
|
|
896
|
+
* @returns {{ findings: Finding[], truncated: ScanTool[] }}
|
|
897
|
+
*/
|
|
898
|
+
function keepPerTool(sorted) {
|
|
899
|
+
/** @type {Map<ScanTool, number>} */
|
|
900
|
+
const seen = new Map();
|
|
901
|
+
/** @type {Finding[]} */
|
|
902
|
+
const findings = [];
|
|
903
|
+
for (const item of sorted) {
|
|
904
|
+
const count = (seen.get(item.tool) ?? 0) + 1;
|
|
905
|
+
seen.set(item.tool, count);
|
|
906
|
+
if (count <= MAX_FINDINGS_PER_TOOL) findings.push(item);
|
|
907
|
+
}
|
|
908
|
+
const truncated = SCAN_TOOLS.filter((tool) => (seen.get(tool) ?? 0) > MAX_FINDINGS_PER_TOOL);
|
|
909
|
+
return { findings, truncated };
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
/**
|
|
913
|
+
* `summary.json` schema version 1 (docs/reference/aisac-scan-summary.schema.json),
|
|
914
|
+
* with a fixed key order so the bytes depend only on the run.
|
|
915
|
+
* @param {Omit<ScanSummary, 'schemaVersion' | 'exitCode' | 'exitReason' | 'counts'> & { interrupted: boolean }} input
|
|
916
|
+
* @returns {ScanSummary}
|
|
917
|
+
*/
|
|
918
|
+
export function buildSummary(input) {
|
|
919
|
+
const findings = [...input.findings].sort(compareFindings);
|
|
920
|
+
const outcome = exitOutcome(input.tools, findings, input.failOn, input.interrupted);
|
|
921
|
+
const kept = keepPerTool(findings);
|
|
922
|
+
return {
|
|
923
|
+
schemaVersion: SUMMARY_SCHEMA_VERSION,
|
|
924
|
+
aisacClient: input.aisacClient,
|
|
925
|
+
packVersion: input.packVersion,
|
|
926
|
+
startedAt: input.startedAt,
|
|
927
|
+
finishedAt: input.finishedAt,
|
|
928
|
+
target: input.target,
|
|
929
|
+
failOn: input.failOn,
|
|
930
|
+
exitCode: outcome.exitCode,
|
|
931
|
+
exitReason: outcome.exitReason,
|
|
932
|
+
counts: countFindings(findings),
|
|
933
|
+
tools: input.tools,
|
|
934
|
+
rulesets: input.rulesets,
|
|
935
|
+
aisacRules: input.aisacRules,
|
|
936
|
+
network: input.network,
|
|
937
|
+
skippedPaths: input.skippedPaths,
|
|
938
|
+
warnings: [...input.warnings, ...kept.truncated.map((tool) => `findings_truncated:${tool}`)],
|
|
939
|
+
findings: kept.findings,
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
/** @param {ScanSummary} summary */
|
|
944
|
+
export function summaryJson(summary) {
|
|
945
|
+
return `${JSON.stringify(summary, null, 2)}\n`;
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
export const GITIGNORE_LINE = '.security-reports/';
|
|
949
|
+
|
|
950
|
+
/** @type {Readonly<Record<PackLocale, Record<string, string>>>} */
|
|
951
|
+
const WORDS = Object.freeze({
|
|
952
|
+
en: {
|
|
953
|
+
title: 'aisac scan',
|
|
954
|
+
block: 'BLOCK RELEASE',
|
|
955
|
+
blockLine: 'BLOCK RELEASE: a CRITICAL finding is present.',
|
|
956
|
+
green: 'green: no findings; tools that ran:',
|
|
957
|
+
target: 'Target',
|
|
958
|
+
started: 'Started',
|
|
959
|
+
finished: 'Finished',
|
|
960
|
+
client: 'Client',
|
|
961
|
+
pack: 'Pack',
|
|
962
|
+
failOn: 'Fail on',
|
|
963
|
+
exit: 'Exit code',
|
|
964
|
+
findings: 'Findings',
|
|
965
|
+
tools: 'Tools',
|
|
966
|
+
ran: 'ran',
|
|
967
|
+
skipped: 'skipped',
|
|
968
|
+
failed: 'failed',
|
|
969
|
+
timedOut: 'timed out',
|
|
970
|
+
rulesets: 'Rulesets',
|
|
971
|
+
aisacRules: 'AISAC rules',
|
|
972
|
+
network: 'Network',
|
|
973
|
+
skippedPaths: 'Skipped paths',
|
|
974
|
+
skippedPath: 'symbolic link outside the target, not followed',
|
|
975
|
+
warnings: 'Warnings',
|
|
976
|
+
next: 'Next step',
|
|
977
|
+
nextText:
|
|
978
|
+
'open an `aisac claude` or `aisac codex` session in this folder and ask for the `scan-review` skill; it reads `.security-reports/aisac-scan/latest.json`.',
|
|
979
|
+
gitignore: 'is not ignored by git; add this line to .gitignore:',
|
|
980
|
+
noScanner:
|
|
981
|
+
'No scanner ran (no_scanner_available): install Docker or an allowlisted binary; `aisac scan --check-tools` lists what is missing.',
|
|
982
|
+
report: 'Report',
|
|
983
|
+
none: 'none',
|
|
984
|
+
pullRequired: 'pull required',
|
|
985
|
+
dockerAvailable: 'Docker: available',
|
|
986
|
+
dockerUnavailable: 'Docker: not available',
|
|
987
|
+
dockerDisabled: 'Docker: disabled (--no-docker)',
|
|
988
|
+
checkTitle: 'aisac scan --check-tools: what would run',
|
|
989
|
+
advisoryLookup: 'package names and versions sent to OSV.dev',
|
|
990
|
+
registryRulesets: 'rulesets downloaded from semgrep.dev',
|
|
991
|
+
secretsFix:
|
|
992
|
+
'Rotate the credential, remove it from the code and its history, and read it from a secret manager.',
|
|
993
|
+
sastFix:
|
|
994
|
+
'Review the flagged code; ask the scan-review skill for a fix under the method (spec and test first).',
|
|
995
|
+
scaFix: 'Upgrade the package to a version without the advisory and commit the lockfile.',
|
|
996
|
+
iacFix: "Apply the check's resolution to the configuration and run aisac scan again.",
|
|
997
|
+
containerFix: 'Rebuild the image on a patched base.',
|
|
998
|
+
},
|
|
999
|
+
'pt-BR': {
|
|
1000
|
+
title: 'aisac scan',
|
|
1001
|
+
block: 'BLOCK RELEASE',
|
|
1002
|
+
blockLine: 'BLOCK RELEASE: há um achado CRITICAL.',
|
|
1003
|
+
green: 'verde: nenhum achado; ferramentas que rodaram:',
|
|
1004
|
+
target: 'Alvo',
|
|
1005
|
+
started: 'Início',
|
|
1006
|
+
finished: 'Fim',
|
|
1007
|
+
client: 'Cliente',
|
|
1008
|
+
pack: 'Pacote',
|
|
1009
|
+
failOn: 'Falha em',
|
|
1010
|
+
exit: 'Código de saída',
|
|
1011
|
+
findings: 'Achados',
|
|
1012
|
+
tools: 'Ferramentas',
|
|
1013
|
+
ran: 'rodou',
|
|
1014
|
+
skipped: 'pulada',
|
|
1015
|
+
failed: 'falhou',
|
|
1016
|
+
timedOut: 'estourou o tempo',
|
|
1017
|
+
rulesets: 'Conjuntos de regras',
|
|
1018
|
+
aisacRules: 'Regras AISAC',
|
|
1019
|
+
network: 'Rede',
|
|
1020
|
+
skippedPaths: 'Caminhos pulados',
|
|
1021
|
+
skippedPath: 'link simbólico para fora do alvo, não seguido',
|
|
1022
|
+
warnings: 'Avisos',
|
|
1023
|
+
next: 'Próximo passo',
|
|
1024
|
+
nextText:
|
|
1025
|
+
'abra uma sessão `aisac claude` ou `aisac codex` nesta pasta e peça a skill `scan-review`; ela lê `.security-reports/aisac-scan/latest.json`.',
|
|
1026
|
+
gitignore: 'não está no .gitignore; adicione esta linha ao .gitignore:',
|
|
1027
|
+
noScanner:
|
|
1028
|
+
'Nenhum scanner rodou (no_scanner_available): instale o Docker ou um binário da lista permitida; `aisac scan --check-tools` mostra o que falta.',
|
|
1029
|
+
report: 'Relatório',
|
|
1030
|
+
none: 'nenhuma',
|
|
1031
|
+
pullRequired: 'download necessário',
|
|
1032
|
+
dockerAvailable: 'Docker: disponível',
|
|
1033
|
+
dockerUnavailable: 'Docker: indisponível',
|
|
1034
|
+
dockerDisabled: 'Docker: desativado (--no-docker)',
|
|
1035
|
+
checkTitle: 'aisac scan --check-tools: o que rodaria',
|
|
1036
|
+
advisoryLookup: 'nomes e versões de pacotes enviados ao OSV.dev',
|
|
1037
|
+
registryRulesets: 'conjuntos de regras baixados de semgrep.dev',
|
|
1038
|
+
secretsFix:
|
|
1039
|
+
'Revogue a credencial, remova-a do código e do histórico e leia-a de um gerenciador de segredos.',
|
|
1040
|
+
sastFix:
|
|
1041
|
+
'Revise o código apontado; peça à skill scan-review uma correção pelo método (spec e teste primeiro).',
|
|
1042
|
+
scaFix: 'Atualize o pacote para uma versão sem o aviso e faça commit do lockfile.',
|
|
1043
|
+
iacFix: 'Aplique a resolução da verificação na configuração e rode aisac scan de novo.',
|
|
1044
|
+
containerFix: 'Reconstrua a imagem sobre uma base corrigida.',
|
|
1045
|
+
},
|
|
1046
|
+
});
|
|
1047
|
+
|
|
1048
|
+
/** @param {PackLocale} locale @param {string} key */
|
|
1049
|
+
function word(locale, key) {
|
|
1050
|
+
return WORDS[locale][key] ?? key;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
/** @type {Readonly<Record<ScanKind, string>>} */
|
|
1054
|
+
const FIX_KEY = Object.freeze({
|
|
1055
|
+
secrets: 'secretsFix',
|
|
1056
|
+
sast: 'sastFix',
|
|
1057
|
+
sca: 'scaFix',
|
|
1058
|
+
iac: 'iacFix',
|
|
1059
|
+
container: 'containerFix',
|
|
1060
|
+
});
|
|
1061
|
+
/** @type {Readonly<Record<NetworkUse['purpose'], string>>} */
|
|
1062
|
+
const NETWORK_KEY = Object.freeze({
|
|
1063
|
+
registry_rulesets: 'registryRulesets',
|
|
1064
|
+
advisory_lookup: 'advisoryLookup',
|
|
1065
|
+
});
|
|
1066
|
+
|
|
1067
|
+
/** @param {string} text */
|
|
1068
|
+
function inlineCode(text) {
|
|
1069
|
+
return `\`${cleanText(text).replaceAll('`', "'")}\``;
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
/** Text inside the findings fence cannot close it. @param {string} text */
|
|
1073
|
+
function fenceSafe(text) {
|
|
1074
|
+
return text.replace(FENCE_RUN, "'''");
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
/** @param {Record<Severity, number>} counts */
|
|
1078
|
+
function countsText(counts) {
|
|
1079
|
+
return SEVERITIES.map((severity) => `${counts[severity]} ${severity}`).join(', ');
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
/** @param {ToolRecord} tool @param {PackLocale} locale */
|
|
1083
|
+
function toolLine(tool, locale) {
|
|
1084
|
+
const where =
|
|
1085
|
+
tool.mode === 'skipped'
|
|
1086
|
+
? ''
|
|
1087
|
+
: ` · ${tool.mode} ${inlineCode(tool.reference ?? '')}${tool.version === null ? '' : ` ${tool.version}`}`;
|
|
1088
|
+
const status = {
|
|
1089
|
+
ran: word(locale, 'ran'),
|
|
1090
|
+
skipped: word(locale, 'skipped'),
|
|
1091
|
+
failed: word(locale, 'failed'),
|
|
1092
|
+
timed_out: word(locale, 'timedOut'),
|
|
1093
|
+
}[tool.status];
|
|
1094
|
+
return `- ${status}: ${tool.tool} (${tool.kind})${where}${tool.reason === null ? '' : `: ${tool.reason}`}`;
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
/** @param {Finding} item @param {PackLocale} locale */
|
|
1098
|
+
function findingBlock(item, locale) {
|
|
1099
|
+
const location = item.line === null ? item.path : `${item.path}:${item.line}`;
|
|
1100
|
+
return [
|
|
1101
|
+
`[${item.severity}] ${item.tool} · ${location}`,
|
|
1102
|
+
`└─ rule: ${item.ruleId}`,
|
|
1103
|
+
`└─ owasp: ${item.owasp.length > 0 ? item.owasp.join(', ') : '-'}`,
|
|
1104
|
+
`└─ msg: ${item.message}`,
|
|
1105
|
+
`└─ fix: ${word(locale, FIX_KEY[item.kind])}`,
|
|
1106
|
+
]
|
|
1107
|
+
.map(fenceSafe)
|
|
1108
|
+
.join('\n');
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
/** @param {RulesetRecord} ruleset */
|
|
1112
|
+
function rulesetLine(ruleset) {
|
|
1113
|
+
return ruleset.status === 'loaded'
|
|
1114
|
+
? `- ${ruleset.name} (${ruleset.source}): loaded, sha256 ${inlineCode(ruleset.sha256)}`
|
|
1115
|
+
: `- ${ruleset.name} (${ruleset.source}): unavailable (${ruleset.reason})`;
|
|
1116
|
+
}
|
|
1117
|
+
|
|
1118
|
+
/** @param {AisacRulesRecord} rules */
|
|
1119
|
+
function aisacRulesText(rules) {
|
|
1120
|
+
if (rules.status === 'loaded') return `loaded (${rules.count})`;
|
|
1121
|
+
if (rules.status === 'refused') return `refused (${rules.reason ?? ''})`;
|
|
1122
|
+
return 'none';
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
/** @param {string} code @param {PackLocale} locale */
|
|
1126
|
+
function warningText(code, locale) {
|
|
1127
|
+
return code === 'gitignore_missing'
|
|
1128
|
+
? `${inlineCode(GITIGNORE_LINE)} ${word(locale, 'gitignore')} ${inlineCode(GITIGNORE_LINE)}`
|
|
1129
|
+
: inlineCode(code);
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
/**
|
|
1133
|
+
* `summary.md` in the `security-scan` skill's format (FR6): the release tag
|
|
1134
|
+
* at the top on a CRITICAL, findings in severity order, the tools that ran,
|
|
1135
|
+
* were skipped or failed, then rulesets, network use, skipped paths,
|
|
1136
|
+
* warnings and the next step.
|
|
1137
|
+
* @param {ScanSummary} summary
|
|
1138
|
+
* @param {PackLocale} locale
|
|
1139
|
+
*/
|
|
1140
|
+
export function renderSummaryMarkdown(summary, locale) {
|
|
1141
|
+
const lines = [`# ${word(locale, 'title')}`, ''];
|
|
1142
|
+
if (summary.counts.CRITICAL > 0) lines.push(`🚨 ${word(locale, 'block')}`, '');
|
|
1143
|
+
if (summary.findings.length === 0 && summary.exitCode === 0) {
|
|
1144
|
+
const ran = summary.tools.filter((tool) => tool.status === 'ran').map((tool) => tool.tool);
|
|
1145
|
+
lines.push(`✓ ${word(locale, 'green')} ${[...new Set(ran)].join(', ')}`, '');
|
|
1146
|
+
}
|
|
1147
|
+
lines.push(
|
|
1148
|
+
'| | |',
|
|
1149
|
+
'| --- | --- |',
|
|
1150
|
+
`| ${word(locale, 'target')} | ${inlineCode(summary.target)} |`,
|
|
1151
|
+
`| ${word(locale, 'started')} | ${summary.startedAt} |`,
|
|
1152
|
+
`| ${word(locale, 'finished')} | ${summary.finishedAt} |`,
|
|
1153
|
+
`| ${word(locale, 'client')} | ${inlineCode(summary.aisacClient)} |`,
|
|
1154
|
+
`| ${word(locale, 'pack')} | ${inlineCode(summary.packVersion ?? '-')} |`,
|
|
1155
|
+
`| ${word(locale, 'failOn')} | ${inlineCode(summary.failOn)} |`,
|
|
1156
|
+
`| ${word(locale, 'exit')} | ${inlineCode(`${summary.exitCode} ${summary.exitReason}`)} |`,
|
|
1157
|
+
'',
|
|
1158
|
+
`## ${word(locale, 'findings')}: ${countsText(summary.counts)}`,
|
|
1159
|
+
'',
|
|
1160
|
+
);
|
|
1161
|
+
if (summary.findings.length > 0) {
|
|
1162
|
+
lines.push('````text', summary.findings.map((item) => findingBlock(item, locale)).join('\n\n'));
|
|
1163
|
+
lines.push('````', '');
|
|
1164
|
+
}
|
|
1165
|
+
lines.push(
|
|
1166
|
+
`## ${word(locale, 'tools')}`,
|
|
1167
|
+
'',
|
|
1168
|
+
...summary.tools.map((tool) => toolLine(tool, locale)),
|
|
1169
|
+
);
|
|
1170
|
+
lines.push('', `## ${word(locale, 'rulesets')}`, '');
|
|
1171
|
+
lines.push(...summary.rulesets.map(rulesetLine));
|
|
1172
|
+
lines.push(`- ${word(locale, 'aisacRules')}: ${aisacRulesText(summary.aisacRules)}`, '');
|
|
1173
|
+
if (summary.network.length > 0) {
|
|
1174
|
+
lines.push(`## ${word(locale, 'network')}`, '');
|
|
1175
|
+
for (const use of summary.network)
|
|
1176
|
+
lines.push(`- ${use.tool}: ${word(locale, NETWORK_KEY[use.purpose])}`);
|
|
1177
|
+
lines.push('');
|
|
1178
|
+
}
|
|
1179
|
+
if (summary.skippedPaths.length > 0) {
|
|
1180
|
+
lines.push(`## ${word(locale, 'skippedPaths')}`, '');
|
|
1181
|
+
for (const path of summary.skippedPaths)
|
|
1182
|
+
lines.push(`- ${inlineCode(path)}: ${word(locale, 'skippedPath')}`);
|
|
1183
|
+
lines.push('');
|
|
1184
|
+
}
|
|
1185
|
+
if (summary.warnings.length > 0) {
|
|
1186
|
+
lines.push(`## ${word(locale, 'warnings')}`, '');
|
|
1187
|
+
for (const code of summary.warnings) lines.push(`- ${warningText(code, locale)}`);
|
|
1188
|
+
lines.push('');
|
|
1189
|
+
}
|
|
1190
|
+
lines.push(`## ${word(locale, 'next')}`, '', `${word(locale, 'nextText')}`, '');
|
|
1191
|
+
return lines.join('\n');
|
|
1192
|
+
}
|
|
1193
|
+
|
|
1194
|
+
/** @param {readonly ToolRecord[]} tools @param {ToolRecord['status']} status */
|
|
1195
|
+
function toolsWith(tools, status) {
|
|
1196
|
+
return tools.filter((tool) => tool.status === status);
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
/**
|
|
1200
|
+
* The terminal summary (FR6, D7): counts, tools, network use, warnings, the
|
|
1201
|
+
* run directory and the next step. Never a finding's message or value.
|
|
1202
|
+
* @param {ScanSummary} summary
|
|
1203
|
+
* @param {PackLocale} locale
|
|
1204
|
+
* @param {string} runDirectory
|
|
1205
|
+
*/
|
|
1206
|
+
export function scanReportLines(summary, locale, runDirectory) {
|
|
1207
|
+
const lines = [
|
|
1208
|
+
`aisac scan: ${countsText(summary.counts)} (fail-on ${summary.failOn}, exit ${summary.exitCode})`,
|
|
1209
|
+
];
|
|
1210
|
+
if (summary.counts.CRITICAL > 0) lines.push(word(locale, 'blockLine'));
|
|
1211
|
+
if (summary.exitReason === 'no_scanner_available') lines.push(word(locale, 'noScanner'));
|
|
1212
|
+
const ran = toolsWith(summary.tools, 'ran').map(
|
|
1213
|
+
(tool) =>
|
|
1214
|
+
`${tool.tool} (${tool.kind}, ${tool.mode}${tool.version === null ? '' : ` ${tool.version}`})`,
|
|
1215
|
+
);
|
|
1216
|
+
lines.push(`${word(locale, 'ran')}: ${ran.length > 0 ? ran.join(', ') : word(locale, 'none')}`);
|
|
1217
|
+
for (const status of /** @type {const} */ (['skipped', 'failed', 'timed_out'])) {
|
|
1218
|
+
const listed = toolsWith(summary.tools, status).map(
|
|
1219
|
+
(tool) => `${tool.tool} (${tool.kind}${tool.reason === null ? '' : `: ${tool.reason}`})`,
|
|
1220
|
+
);
|
|
1221
|
+
const label = word(locale, status === 'timed_out' ? 'timedOut' : status);
|
|
1222
|
+
if (listed.length > 0) lines.push(`${label}: ${listed.join(', ')}`);
|
|
1223
|
+
}
|
|
1224
|
+
if (summary.network.length > 0)
|
|
1225
|
+
lines.push(
|
|
1226
|
+
`${word(locale, 'network')}: ${summary.network.map((use) => `${use.tool} (${word(locale, NETWORK_KEY[use.purpose])})`).join(', ')}`,
|
|
1227
|
+
);
|
|
1228
|
+
if (summary.skippedPaths.length > 0)
|
|
1229
|
+
lines.push(
|
|
1230
|
+
`${word(locale, 'skippedPaths')}: ${summary.skippedPaths.length} (${word(locale, 'skippedPath')})`,
|
|
1231
|
+
);
|
|
1232
|
+
for (const code of summary.warnings) {
|
|
1233
|
+
lines.push(
|
|
1234
|
+
code === 'gitignore_missing'
|
|
1235
|
+
? `${word(locale, 'warnings')}: ${GITIGNORE_LINE} ${word(locale, 'gitignore')} ${GITIGNORE_LINE}`
|
|
1236
|
+
: `${word(locale, 'warnings')}: ${code}`,
|
|
1237
|
+
);
|
|
1238
|
+
}
|
|
1239
|
+
lines.push(`${word(locale, 'report')}: ${cleanText(runDirectory)}`);
|
|
1240
|
+
lines.push(`${word(locale, 'next')}: ${word(locale, 'nextText')}`);
|
|
1241
|
+
return lines;
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
/**
|
|
1245
|
+
* The `--check-tools` table (FR1, FR9): one line per kind, then Docker.
|
|
1246
|
+
* @param {readonly AvailabilityEntry[]} entries
|
|
1247
|
+
* @param {PackLocale} locale
|
|
1248
|
+
* @param {'available' | 'unavailable' | 'disabled'} docker
|
|
1249
|
+
*/
|
|
1250
|
+
export function availabilityLines(entries, locale, docker) {
|
|
1251
|
+
const kindWidth = Math.max(...SCAN_KINDS.map((kind) => kind.length));
|
|
1252
|
+
const toolWidth = Math.max(...SCAN_TOOLS.map((tool) => tool.length));
|
|
1253
|
+
const lines = [word(locale, 'checkTitle')];
|
|
1254
|
+
for (const entry of entries) {
|
|
1255
|
+
const detail =
|
|
1256
|
+
entry.mode === 'skipped'
|
|
1257
|
+
? (entry.reason ?? '')
|
|
1258
|
+
: `${entry.reference ?? ''} (${[entry.version, entry.reason === 'image_pull_required' ? word(locale, 'pullRequired') : null].filter((part) => part !== null).join(', ')})`;
|
|
1259
|
+
lines.push(
|
|
1260
|
+
`${entry.kind.padEnd(kindWidth)} ${entry.tool.padEnd(toolWidth)} ${entry.mode.padEnd(7)} ${detail}`,
|
|
1261
|
+
);
|
|
1262
|
+
}
|
|
1263
|
+
lines.push(
|
|
1264
|
+
word(
|
|
1265
|
+
locale,
|
|
1266
|
+
docker === 'available'
|
|
1267
|
+
? 'dockerAvailable'
|
|
1268
|
+
: docker === 'disabled'
|
|
1269
|
+
? 'dockerDisabled'
|
|
1270
|
+
: 'dockerUnavailable',
|
|
1271
|
+
),
|
|
1272
|
+
);
|
|
1273
|
+
return lines;
|
|
1274
|
+
}
|