@clear-capabilities/agentic-security-scanner 0.75.0 → 0.77.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 (68) hide show
  1. package/CHANGELOG.md +57 -0
  2. package/bin/agentic-security.js +2 -2
  3. package/dist/838.index.js +152 -0
  4. package/dist/{634.index.js → 985.index.js} +21 -144
  5. package/dist/agentic-security.mjs +8 -8
  6. package/dist/agentic-security.mjs.sha256 +1 -1
  7. package/package.json +6 -6
  8. package/src/mcp/tools.js +17 -2
  9. package/src/sca/base-images.json +1 -1
  10. package/bin/.agentic-security/findings.json +0 -1596
  11. package/bin/.agentic-security/last-scan.json +0 -1596
  12. package/bin/.agentic-security/last-scan.json.sig +0 -1
  13. package/bin/.agentic-security/scan-history.json +0 -470
  14. package/bin/.agentic-security/streak.json +0 -25
  15. package/dist/218.index.js +0 -793
  16. package/dist/601.index.js +0 -1038
  17. package/src/.agentic-security/findings.json +0 -80844
  18. package/src/.agentic-security/last-scan.json +0 -80844
  19. package/src/.agentic-security/last-scan.json.sig +0 -1
  20. package/src/.agentic-security/scan-history.json +0 -8408
  21. package/src/.agentic-security/streak.json +0 -26
  22. package/src/dataflow/.agentic-security/findings.json +0 -3487
  23. package/src/dataflow/.agentic-security/last-scan.json +0 -3487
  24. package/src/dataflow/.agentic-security/last-scan.json.sig +0 -1
  25. package/src/dataflow/.agentic-security/scan-history.json +0 -735
  26. package/src/dataflow/.agentic-security/streak.json +0 -24
  27. package/src/integrations/.agentic-security/findings.json +0 -1504
  28. package/src/integrations/.agentic-security/last-scan.json +0 -1504
  29. package/src/integrations/.agentic-security/scan-history.json +0 -40
  30. package/src/integrations/.agentic-security/streak.json +0 -21
  31. package/src/ir/.agentic-security/findings.json +0 -3036
  32. package/src/ir/.agentic-security/last-scan.json +0 -3036
  33. package/src/ir/.agentic-security/last-scan.json.sig +0 -1
  34. package/src/ir/.agentic-security/scan-history.json +0 -364
  35. package/src/ir/.agentic-security/streak.json +0 -23
  36. package/src/llm-validator/.agentic-security/findings.json +0 -1891
  37. package/src/llm-validator/.agentic-security/last-scan.json +0 -1891
  38. package/src/llm-validator/.agentic-security/last-scan.json.sig +0 -1
  39. package/src/llm-validator/.agentic-security/scan-history.json +0 -168
  40. package/src/llm-validator/.agentic-security/streak.json +0 -20
  41. package/src/lsp/.agentic-security/findings.json +0 -28
  42. package/src/lsp/.agentic-security/last-scan.json +0 -28
  43. package/src/lsp/.agentic-security/scan-history.json +0 -79
  44. package/src/lsp/.agentic-security/streak.json +0 -22
  45. package/src/mcp/.agentic-security/findings.json +0 -8358
  46. package/src/mcp/.agentic-security/last-scan.json +0 -8358
  47. package/src/mcp/.agentic-security/last-scan.json.sig +0 -1
  48. package/src/mcp/.agentic-security/scan-history.json +0 -1125
  49. package/src/mcp/.agentic-security/streak.json +0 -22
  50. package/src/posture/.agentic-security/findings.json +0 -51239
  51. package/src/posture/.agentic-security/last-scan.json +0 -51239
  52. package/src/posture/.agentic-security/last-scan.json.sig +0 -1
  53. package/src/posture/.agentic-security/scan-history.json +0 -5557
  54. package/src/posture/.agentic-security/streak.json +0 -24
  55. package/src/report/.agentic-security/findings.json +0 -79
  56. package/src/report/.agentic-security/last-scan.json +0 -79
  57. package/src/report/.agentic-security/last-scan.json.sig +0 -1
  58. package/src/report/.agentic-security/scan-history.json +0 -332
  59. package/src/report/.agentic-security/streak.json +0 -23
  60. package/src/sast/.agentic-security/findings.json +0 -5051
  61. package/src/sast/.agentic-security/last-scan.json +0 -5051
  62. package/src/sast/.agentic-security/last-scan.json.sig +0 -1
  63. package/src/sast/.agentic-security/scan-history.json +0 -788
  64. package/src/sast/.agentic-security/streak.json +0 -23
  65. package/src/sast/bench-shape/.agentic-security/findings.json +0 -28
  66. package/src/sast/bench-shape/.agentic-security/last-scan.json +0 -28
  67. package/src/sast/bench-shape/.agentic-security/scan-history.json +0 -24
  68. package/src/sast/bench-shape/.agentic-security/streak.json +0 -22
package/dist/218.index.js DELETED
@@ -1,793 +0,0 @@
1
- export const id = 218;
2
- export const ids = [218];
3
- export const modules = {
4
-
5
- /***/ 6218:
6
- /***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => {
7
-
8
-
9
- // EXPORTS
10
- __webpack_require__.d(__webpack_exports__, {
11
- runStdio: () => (/* binding */ runStdio)
12
- });
13
-
14
- // EXTERNAL MODULE: external "node:fs"
15
- var external_node_fs_ = __webpack_require__(3024);
16
- // EXTERNAL MODULE: external "node:crypto"
17
- var external_node_crypto_ = __webpack_require__(7598);
18
- // EXTERNAL MODULE: external "node:path"
19
- var external_node_path_ = __webpack_require__(6760);
20
- // EXTERNAL MODULE: external "node:url"
21
- var external_node_url_ = __webpack_require__(3136);
22
- // EXTERNAL MODULE: external "node:fs/promises"
23
- var promises_ = __webpack_require__(1455);
24
- // EXTERNAL MODULE: ./src/runScan.js + 1 modules
25
- var runScan = __webpack_require__(454);
26
- // EXTERNAL MODULE: ./src/posture/fix-history.js
27
- var fix_history = __webpack_require__(4407);
28
- // EXTERNAL MODULE: ./src/posture/integrity.js
29
- var integrity = __webpack_require__(1130);
30
- ;// CONCATENATED MODULE: ./src/mcp/redact.js
31
- // Secret redactor for MCP tool outputs and audit log argument summaries.
32
- //
33
- // OWASP MCP01 + MCP10: the scanner reads source code, and findings carry
34
- // `snippet` / `description` / `trace` strings that may contain hardcoded
35
- // credentials, API keys, JWTs, private keys, etc. When those flow back to
36
- // the agent through tools/call responses they land in the agent's context
37
- // — exposing the secret to model logs, transcripts, and any downstream tool
38
- // the agent passes them to.
39
- //
40
- // We replace high-confidence secret shapes with [REDACTED:<kind>] before
41
- // emitting them. The original full content is still on disk (scanner
42
- // findings); the MCP surface is the bottleneck we control.
43
- //
44
- // Patterns deliberately stay narrow: high-precision so we don't garble
45
- // non-secret long strings (UUIDs, SHAs, base64-encoded scan IDs).
46
-
47
- const PATTERNS = [
48
- // Provider-specific high-entropy keys (anchored prefixes give very low FP)
49
- [/AKIA[0-9A-Z]{16}/g, 'aws-access-key'],
50
- [/ASIA[0-9A-Z]{16}/g, 'aws-temp-key'],
51
- [/gh[pousr]_[A-Za-z0-9]{36,255}/g, 'github-token'],
52
- [/xox[abprs]-[A-Za-z0-9-]{10,}/g, 'slack-token'],
53
- [/sk-ant-[A-Za-z0-9_-]{20,}/g, 'anthropic-key'],
54
- [/sk-proj-[A-Za-z0-9_-]{20,}/g, 'openai-project-key'],
55
- [/sk-[A-Za-z0-9]{32,}/g, 'openai-or-stripe-key'],
56
- [/sk_(?:live|test)_[A-Za-z0-9]{20,}/g, 'stripe-key'],
57
- [/rk_(?:live|test)_[A-Za-z0-9]{20,}/g, 'stripe-restricted-key'],
58
- [/SG\.[A-Za-z0-9_-]{22}\.[A-Za-z0-9_-]{43}/g, 'sendgrid-key'],
59
- [/AIza[0-9A-Za-z_-]{35}/g, 'google-api-key'],
60
- // JWT — three dot-separated b64url segments starting with eyJ
61
- [/eyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}/g, 'jwt'],
62
- // PEM-encoded private keys
63
- [/-----BEGIN (?:RSA |DSA |EC |OPENSSH |PGP )?PRIVATE KEY-----[\s\S]*?-----END (?:RSA |DSA |EC |OPENSSH |PGP )?PRIVATE KEY-----/g, 'private-key-block'],
64
- // Authorization headers — common copy-paste shape
65
- [/(?:Authorization|authorization)\s*:\s*Bearer\s+[A-Za-z0-9._~+/-]{20,}={0,2}/g, 'bearer-token'],
66
- // Hardcoded password literals — assignment shape with quoted value
67
- [/(password|passwd|secret|api[_-]?key|access[_-]?token)\s*[:=]\s*["'][^"'\n]{6,}["']/gi, 'hardcoded-credential'],
68
- ];
69
-
70
- const SNIPPET_MAX = 2000;
71
- // OWASP A03 — cap input before running 14 regex patterns over it. A forged
72
- // last-scan.json could plant a 50MB description string; without this cap a
73
- // single explain_finding/query_taint call would peg CPU. After truncation
74
- // the snippet still gets the final SNIPPET_MAX trim downstream.
75
- const INPUT_MAX = 100_000;
76
-
77
- function redactString(s) {
78
- if (typeof s !== 'string') return s;
79
- let out = s;
80
- if (out.length > INPUT_MAX) out = out.slice(0, INPUT_MAX) + `…(+${out.length - INPUT_MAX})`;
81
- for (const [re, kind] of PATTERNS) {
82
- out = out.replace(re, `[REDACTED:${kind}]`);
83
- }
84
- if (out.length > SNIPPET_MAX) out = out.slice(0, SNIPPET_MAX) + `…(+${out.length - SNIPPET_MAX})`;
85
- return out;
86
- }
87
-
88
- // Deep-redact every string in a finding-like object (mutates returned copy).
89
- function redactFinding(f) {
90
- if (!f || typeof f !== 'object') return f;
91
- const out = { ...f };
92
- for (const k of ['snippet', 'description', 'remediation', 'title', 'vuln', 'message']) {
93
- if (typeof out[k] === 'string') out[k] = redactString(out[k]);
94
- }
95
- if (out.trace) {
96
- try { out.trace = JSON.parse(redactString(JSON.stringify(out.trace))); }
97
- catch { /* keep as-is if not round-trippable */ }
98
- }
99
- return out;
100
- }
101
-
102
- // Redact a freeform JSON-stringified argument blob (used by audit log).
103
- function redactArgsBlob(s) {
104
- return redactString(s);
105
- }
106
-
107
- ;// CONCATENATED MODULE: ./src/mcp/tools.js
108
- // MCP tool implementations — PRD Feature 2, hardened against the OWASP MCP
109
- // Top 10 (see ./redact.js, ./audit.js, ./server.js for sibling controls).
110
- //
111
- // Trust model:
112
- // - Session root fixed at server boot. No per-call retargeting.
113
- // - Path arguments lstat-checked (symlinks refused, OWASP MCP05) and
114
- // realpath-confined to session root.
115
- // - Tool outputs marked _meta.untrusted_excerpts:true (OWASP MCP03/MCP06)
116
- // because they may contain text from scanned files, which is adversary-
117
- // controlled in any context where the agent might read malicious code.
118
- // - Secret-shaped strings redacted on the way out (OWASP MCP01/MCP10).
119
- // - `apply_fix` requires confirm:true, valid HMAC signature on
120
- // last-scan.json, non-shadow finding, and confined file path.
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
- const MAX_FILES_PER_SCAN = 1024;
131
- const MAX_FILE_BYTES = 500_000;
132
- const MAX_TOTAL_SCAN_BYTES = 50_000_000;
133
- const META = { source: 'agentic-security-mcp', untrusted_excerpts: true };
134
-
135
- // OWASP A01 — refuse writes to paths that could subvert the security tool
136
- // itself or the host's source-control / dependency state. A forged finding
137
- // could otherwise tell apply_fix to overwrite our own rules.yml, our audit
138
- // log, a .git/hooks/post-commit payload, or a node_modules package.
139
- const RESERVED_WRITE_PREFIXES = [
140
- '.git/',
141
- '.agentic-security/',
142
- 'node_modules/',
143
- ];
144
- function _isReservedWritePath(sessionRoot, absFile) {
145
- // Resolve sessionRoot symlinks so the relative path is computed against
146
- // the same canonical root as `absFile` (which _confine already realpath'd).
147
- // On macOS /tmp → /private/tmp; without this normalization the relative
148
- // would contain "../" and the prefix check would miss the reserved path.
149
- const rootReal = external_node_fs_.realpathSync(external_node_path_.resolve(sessionRoot));
150
- const rel = external_node_path_.relative(rootReal, absFile).replace(/\\/g, '/');
151
- return RESERVED_WRITE_PREFIXES.some(p => rel === p.replace(/\/$/, '') || rel.startsWith(p));
152
- }
153
-
154
- // ─── Path confinement ────────────────────────────────────────────────────────
155
- // Lexical check + lstat symlink reject + realpath re-check. OWASP MCP05.
156
- //
157
- // For non-existent paths (apply_fix to a new file is a possible legitimate
158
- // case; in practice we re-check existence at the use-site) we walk up the
159
- // deepest existing ancestor and realpath that, so a parent-symlink can't
160
- // silently relocate writes.
161
- function _confine(sessionRoot, candidate, label) {
162
- if (typeof candidate !== 'string' || !candidate) throw new Error(`${label}: not a string`);
163
- const rootReal = external_node_fs_.realpathSync(external_node_path_.resolve(sessionRoot));
164
- const abs = external_node_path_.isAbsolute(candidate) ? candidate : external_node_path_.resolve(rootReal, candidate);
165
-
166
- // Lexical pre-check: rejects "../../etc/passwd" before any fs call.
167
- const relLex = external_node_path_.relative(rootReal, external_node_path_.resolve(abs));
168
- if (relLex === '' || relLex.startsWith('..') || external_node_path_.isAbsolute(relLex)) {
169
- throw new Error(`${label}: path "${candidate}" escapes session root`);
170
- }
171
-
172
- // If the path exists, the leaf must not be a symlink and its realpath
173
- // must still be under rootReal.
174
- if (external_node_fs_.existsSync(abs)) {
175
- if (external_node_fs_.lstatSync(abs).isSymbolicLink()) {
176
- throw new Error(`${label}: path "${candidate}" is a symbolic link (refused)`);
177
- }
178
- const real = external_node_fs_.realpathSync(abs);
179
- if (external_node_path_.relative(rootReal, real).startsWith('..')) {
180
- throw new Error(`${label}: path "${candidate}" resolves outside session root via symlink`);
181
- }
182
- return real;
183
- }
184
-
185
- // Path doesn't exist — walk up to the deepest existing ancestor and
186
- // realpath that. If a parent dir is a symlink pointing outside rootReal
187
- // we catch it here.
188
- let parent = external_node_path_.dirname(abs);
189
- while (parent !== external_node_path_.dirname(parent) && !external_node_fs_.existsSync(parent)) {
190
- parent = external_node_path_.dirname(parent);
191
- }
192
- const parentReal = external_node_fs_.realpathSync(parent);
193
- if (external_node_path_.relative(rootReal, parentReal).startsWith('..')) {
194
- throw new Error(`${label}: path "${candidate}" parent resolves outside session root`);
195
- }
196
- const suffix = external_node_path_.relative(parent, abs);
197
- return external_node_path_.resolve(parentReal, suffix);
198
- }
199
-
200
- function _readLastScanVerified(sessionRoot, { allowUnsigned = false } = {}) {
201
- const stateDir = external_node_path_.join(sessionRoot, '.agentic-security');
202
- const scanFile = external_node_path_.join(stateDir, 'last-scan.json');
203
- const sigFile = scanFile + '.sig';
204
- if (!external_node_fs_.existsSync(scanFile)) return { scan: null, status: 'missing' };
205
- const body = external_node_fs_.readFileSync(scanFile, 'utf8');
206
- const ok = (0,integrity/* verifyLastScan */.E)(body, sigFile);
207
- if (ok === false) return { scan: null, status: 'tampered' };
208
- if (ok === null && !allowUnsigned) return { scan: null, status: 'unsigned' };
209
- let parsed;
210
- try { parsed = JSON.parse(body); }
211
- catch { return { scan: null, status: 'unparseable' }; }
212
- return { scan: parsed, status: ok ? 'verified' : 'unsigned' };
213
- }
214
-
215
- function _findById(scan, id) {
216
- if (!scan) return null;
217
- return (scan.findings || []).find(f => f.id === id)
218
- || (scan.secrets || []).find(f => f.id === id)
219
- || null;
220
- }
221
-
222
- // ─── scan_diff ───────────────────────────────────────────────────────────────
223
- const scan_diff = {
224
- name: 'scan_diff',
225
- description: 'Scan a list of files for security findings. Use BEFORE writing a Write/Edit to disk so the agent can self-correct. Returns findings with severity, file:line, title, remediation. Snippets are redacted of obvious secret patterns. Paths confined to the session root; symlinks are refused.',
226
- inputSchema: {
227
- type: 'object',
228
- additionalProperties: false,
229
- properties: {
230
- files: {
231
- type: 'array', minItems: 1, maxItems: MAX_FILES_PER_SCAN,
232
- items: { type: 'string', minLength: 1, maxLength: 4096 },
233
- },
234
- severity: { type: 'string', enum: ['critical', 'high', 'medium', 'low', 'info'] },
235
- },
236
- required: ['files'],
237
- },
238
- async handler({ files, severity }, ctx) {
239
- const sessionRoot = ctx.sessionRoot;
240
- const abs = files.map(f => _confine(sessionRoot, f, 'files[]'));
241
-
242
- const fileContents = {};
243
- let totalBytes = 0;
244
- for (const a of abs) {
245
- let stat;
246
- try { stat = external_node_fs_.statSync(a); } catch { continue; }
247
- if (!stat.isFile()) continue;
248
- if (stat.size > MAX_FILE_BYTES) continue;
249
- totalBytes += stat.size;
250
- if (totalBytes > MAX_TOTAL_SCAN_BYTES) {
251
- throw new Error(`scan_diff: total scan size exceeds ${MAX_TOTAL_SCAN_BYTES} bytes`);
252
- }
253
- let content;
254
- try { content = external_node_fs_.readFileSync(a, 'utf8'); } catch { continue; }
255
- const rel = external_node_path_.relative(sessionRoot, a).replace(/\\/g, '/');
256
- fileContents[rel] = content;
257
- }
258
-
259
- const result = await (0,runScan.runScan)(sessionRoot, { network: false, fileContents });
260
- const wantSet = new Set(Object.keys(fileContents));
261
- const sevRank = { info: 0, low: 1, medium: 2, high: 3, critical: 4 };
262
- const min = sevRank[severity] ?? 0;
263
- const findings = (result.scan.findings || [])
264
- .filter(f => wantSet.has(String(f.file || '').replace(/\\/g, '/')) && (sevRank[f.severity] ?? 0) >= min)
265
- .map(f => redactFinding({
266
- id: f.id, severity: f.severity, file: f.file, line: f.line,
267
- title: f.title || f.vuln, cwe: f.cwe,
268
- description: f.description, remediation: f.remediation,
269
- }));
270
- return {
271
- _meta: META,
272
- scannedFiles: Object.keys(fileContents).length,
273
- findingCount: findings.length,
274
- findings,
275
- };
276
- },
277
- };
278
-
279
- // ─── query_taint ─────────────────────────────────────────────────────────────
280
- const query_taint = {
281
- name: 'query_taint',
282
- description: 'Query whether the last verified scan found a taint path involving a given source and sink.',
283
- inputSchema: {
284
- type: 'object',
285
- additionalProperties: false,
286
- properties: {
287
- source: { type: 'string', minLength: 1, maxLength: 256 },
288
- sink: { type: 'string', minLength: 1, maxLength: 256 },
289
- },
290
- required: ['source', 'sink'],
291
- },
292
- async handler({ source, sink }, ctx) {
293
- const { scan, status } = _readLastScanVerified(ctx.sessionRoot, { allowUnsigned: true });
294
- if (!scan) {
295
- return { _meta: META, hasResult: false, status, message: `No usable scan state (${status}).` };
296
- }
297
- const srcL = String(source).toLowerCase();
298
- const sinkL = String(sink).toLowerCase();
299
- const matches = (scan.findings || []).filter(f => {
300
- const hay = [f.description, f.title, f.vuln, f.snippet, JSON.stringify(f.trace || '')].join(' ').toLowerCase();
301
- return hay.includes(srcL) && hay.includes(sinkL);
302
- }).map(f => redactFinding({
303
- id: f.id, severity: f.severity, file: f.file, line: f.line,
304
- title: f.title || f.vuln, description: f.description,
305
- trace: f.trace || null,
306
- }));
307
- return {
308
- _meta: META,
309
- hasResult: true,
310
- integrity: status,
311
- scanStartedAt: scan.startedAt || scan.meta?.startedAt || null,
312
- matchCount: matches.length,
313
- matches,
314
- };
315
- },
316
- };
317
-
318
- // ─── explain_finding ─────────────────────────────────────────────────────────
319
- const explain_finding = {
320
- name: 'explain_finding',
321
- description: 'Return full details for a single finding from the last verified scan. Snippet/description redacted of secret patterns.',
322
- inputSchema: {
323
- type: 'object',
324
- additionalProperties: false,
325
- properties: {
326
- finding_id: { type: 'string', minLength: 1, maxLength: 256 },
327
- },
328
- required: ['finding_id'],
329
- },
330
- async handler({ finding_id }, ctx) {
331
- const { scan, status } = _readLastScanVerified(ctx.sessionRoot, { allowUnsigned: true });
332
- if (!scan) throw new Error(`No usable scan state (${status}).`);
333
- const f = _findById(scan, finding_id);
334
- if (!f) throw new Error(`Finding not found: ${finding_id}`);
335
- const redacted = redactFinding({
336
- id: f.id, severity: f.severity, file: f.file, line: f.line,
337
- title: f.title || f.vuln, cwe: f.cwe,
338
- description: f.description, remediation: f.remediation,
339
- snippet: f.snippet || null,
340
- trace: f.trace || null,
341
- });
342
- return {
343
- _meta: META,
344
- ...redacted,
345
- confidence: f.confidence ?? null,
346
- hasReplacementFix: typeof f.fix?.replacement === 'string',
347
- integrity: status,
348
- };
349
- },
350
- };
351
-
352
- // ─── apply_fix ───────────────────────────────────────────────────────────────
353
- const apply_fix = {
354
- name: 'apply_fix',
355
- description: 'Apply the stored replacement fix for a finding. Refuses if last-scan.json fails its HMAC check, if the finding is shadow-marked, or if its file path escapes the session root via lexical traversal OR a symlink. Requires confirm:true. Supports dry_run:true to preview without writing.',
356
- inputSchema: {
357
- type: 'object',
358
- additionalProperties: false,
359
- properties: {
360
- finding_id: { type: 'string', minLength: 1, maxLength: 256 },
361
- confirm: { type: 'boolean' },
362
- dry_run: { type: 'boolean' },
363
- },
364
- required: ['finding_id', 'confirm'],
365
- },
366
- async handler({ finding_id, confirm, dry_run = false }, ctx) {
367
- if (confirm !== true) {
368
- return { _meta: META, applied: false, reason: 'apply_fix requires confirm: true.' };
369
- }
370
- const { scan, status } = _readLastScanVerified(ctx.sessionRoot, { allowUnsigned: false });
371
- if (!scan) {
372
- return { _meta: META, applied: false, reason: `last-scan.json failed integrity check: ${status}. Run a fresh scan.` };
373
- }
374
- const f = _findById(scan, finding_id);
375
- if (!f) return { _meta: META, applied: false, reason: `Finding not found: ${finding_id}` };
376
- if (f._shadow === true) {
377
- return { _meta: META, applied: false, reason: 'shadow findings cannot be auto-applied' };
378
- }
379
- if (typeof f.fix?.replacement !== 'string') {
380
- return {
381
- _meta: META, applied: false,
382
- reason: 'No full replacement available — only a template. Apply the template manually.',
383
- template: redactString(f.fix?.code || ''),
384
- file: f.file, line: f.line,
385
- };
386
- }
387
- let absFile;
388
- try { absFile = _confine(ctx.sessionRoot, f.file, 'finding.file'); }
389
- catch (e) {
390
- return { _meta: META, applied: false, reason: `path-escape refused: ${e.message}` };
391
- }
392
- if (_isReservedWritePath(ctx.sessionRoot, absFile)) {
393
- return { _meta: META, applied: false, reason: `reserved path refused: writes to .git/, .agentic-security/, or node_modules/ are not permitted via apply_fix` };
394
- }
395
- if (!external_node_fs_.existsSync(absFile)) {
396
- return { _meta: META, applied: false, reason: `File not found: ${absFile}` };
397
- }
398
- const originalContent = await promises_.readFile(absFile, 'utf8');
399
-
400
- if (dry_run) {
401
- return {
402
- _meta: META,
403
- applied: false, dryRun: true,
404
- file: f.file,
405
- originalSize: originalContent.length,
406
- newSize: f.fix.replacement.length,
407
- diffSummary: `${originalContent.length} → ${f.fix.replacement.length} bytes`,
408
- };
409
- }
410
-
411
- const entry = await (0,fix_history/* applyFix */.oM)({
412
- scanRoot: ctx.sessionRoot,
413
- file: f.file,
414
- originalContent,
415
- newContent: f.fix.replacement,
416
- findingId: f.id,
417
- ruleId: f.rule || null,
418
- vuln: f.vuln || f.title || null,
419
- });
420
- return { _meta: META, applied: true, historyId: entry.id, file: f.file, backupPath: entry.backupPath, integrity: status };
421
- },
422
- };
423
-
424
- const ALL_TOOLS = [scan_diff, query_taint, explain_finding, apply_fix];
425
-
426
- ;// CONCATENATED MODULE: ./src/mcp/validate.js
427
- // Minimal JSON Schema validator — just the subset our tool schemas use.
428
- // No deps. Throws on invalid input with a path-prefixed error message.
429
- //
430
- // Supported keywords: type (object/array/string/boolean/number),
431
- // required, properties, items, enum, minItems, maxItems, maxLength,
432
- // minLength, additionalProperties (only as `false` — strict).
433
-
434
- const TYPE_OF = (v) => {
435
- if (v === null) return 'null';
436
- if (Array.isArray(v)) return 'array';
437
- return typeof v;
438
- };
439
-
440
- function validate(schema, value, path = 'arguments') {
441
- if (!schema) return;
442
- const t = schema.type;
443
- if (t === 'object') {
444
- if (TYPE_OF(value) !== 'object') throw new Error(`${path}: expected object, got ${TYPE_OF(value)}`);
445
- for (const req of schema.required || []) {
446
- if (!(req in value)) throw new Error(`${path}: missing required property "${req}"`);
447
- }
448
- if (schema.additionalProperties === false) {
449
- const allowed = new Set(Object.keys(schema.properties || {}));
450
- for (const k of Object.keys(value)) {
451
- if (!allowed.has(k)) throw new Error(`${path}: unexpected property "${k}"`);
452
- }
453
- }
454
- for (const [k, sub] of Object.entries(schema.properties || {})) {
455
- if (k in value) validate(sub, value[k], `${path}.${k}`);
456
- }
457
- } else if (t === 'array') {
458
- if (!Array.isArray(value)) throw new Error(`${path}: expected array, got ${TYPE_OF(value)}`);
459
- if (schema.minItems != null && value.length < schema.minItems) throw new Error(`${path}: minItems=${schema.minItems}, got length=${value.length}`);
460
- if (schema.maxItems != null && value.length > schema.maxItems) throw new Error(`${path}: maxItems=${schema.maxItems}, got length=${value.length}`);
461
- if (schema.items) for (let i = 0; i < value.length; i++) validate(schema.items, value[i], `${path}[${i}]`);
462
- } else if (t === 'string') {
463
- if (typeof value !== 'string') throw new Error(`${path}: expected string, got ${TYPE_OF(value)}`);
464
- if (schema.enum && !schema.enum.includes(value)) throw new Error(`${path}: must be one of [${schema.enum.join(', ')}]`);
465
- if (schema.maxLength != null && value.length > schema.maxLength) throw new Error(`${path}: maxLength=${schema.maxLength}, got length=${value.length}`);
466
- if (schema.minLength != null && value.length < schema.minLength) throw new Error(`${path}: minLength=${schema.minLength}, got length=${value.length}`);
467
- } else if (t === 'boolean') {
468
- if (typeof value !== 'boolean') throw new Error(`${path}: expected boolean, got ${TYPE_OF(value)}`);
469
- } else if (t === 'number' || t === 'integer') {
470
- if (typeof value !== 'number') throw new Error(`${path}: expected number, got ${TYPE_OF(value)}`);
471
- if (t === 'integer' && !Number.isInteger(value)) throw new Error(`${path}: expected integer`);
472
- if (schema.minimum != null && value < schema.minimum) throw new Error(`${path}: < minimum (${schema.minimum})`);
473
- if (schema.maximum != null && value > schema.maximum) throw new Error(`${path}: > maximum (${schema.maximum})`);
474
- }
475
- }
476
-
477
- ;// CONCATENATED MODULE: ./src/mcp/audit.js
478
- // Append-only audit log of MCP tool calls — OWASP MCP08.
479
- //
480
- // Format: one JSON object per line (NDJSON) at
481
- // <sessionRoot>/.agentic-security/mcp-audit.log
482
- //
483
- // Each entry carries `prev` — the SHA-256 of the previous entry's serialized
484
- // form. The first entry's prev is "GENESIS". Tampering with any line breaks
485
- // the chain from that point forward; a reader can detect partial truncation
486
- // or in-place edits. (Cannot prevent total deletion of the file — for that
487
- // you need write-once storage or a remote sink, out of scope for v1.)
488
- //
489
- // Argument blobs are redacted (OWASP MCP01/MCP10) so credentials passed in
490
- // arguments cannot leak via the audit trail.
491
-
492
-
493
-
494
-
495
-
496
-
497
- const MAX_ARG_BYTES = 1024;
498
- const GENESIS = 'GENESIS';
499
-
500
- function _summarize(args) {
501
- let s;
502
- try { s = JSON.stringify(args); } catch { s = '<unserializable>'; }
503
- s = redactArgsBlob(s);
504
- if (s.length > MAX_ARG_BYTES) s = s.slice(0, MAX_ARG_BYTES) + `…(+${s.length - MAX_ARG_BYTES})`;
505
- return s;
506
- }
507
-
508
- function _sha(s) { return external_node_crypto_.createHash('sha256').update(s).digest('hex'); }
509
-
510
- function _readLastEntryHash(logFile) {
511
- if (!external_node_fs_.existsSync(logFile)) return GENESIS;
512
- try {
513
- const all = external_node_fs_.readFileSync(logFile, 'utf8');
514
- const lines = all.split('\n').filter(Boolean);
515
- if (!lines.length) return GENESIS;
516
- return _sha(lines[lines.length - 1]);
517
- } catch { return GENESIS; }
518
- }
519
-
520
- function auditCall({ sessionRoot, tool, args, outcome, reason }) {
521
- if (!sessionRoot) return;
522
- try {
523
- const dir = external_node_path_.join(sessionRoot, '.agentic-security');
524
- external_node_fs_.mkdirSync(dir, { recursive: true });
525
- const logFile = external_node_path_.join(dir, 'mcp-audit.log');
526
- const entry = {
527
- ts: new Date().toISOString(),
528
- tool,
529
- outcome,
530
- ...(reason ? { reason } : {}),
531
- args: _summarize(args),
532
- prev: _readLastEntryHash(logFile),
533
- };
534
- external_node_fs_.appendFileSync(logFile, JSON.stringify(entry) + '\n');
535
- } catch { /* audit failure must never break a tool call */ }
536
- }
537
-
538
- // Verify the chain from start to end. Returns
539
- // { ok: true, entries: N } if intact
540
- // { ok: false, brokenAt: <line-index>, expected, got } if any link breaks
541
- // Reader/operator-facing tool.
542
- function verifyAuditLog(logFile) {
543
- if (!fs.existsSync(logFile)) return { ok: true, entries: 0 };
544
- const text = fs.readFileSync(logFile, 'utf8');
545
- const lines = text.split('\n').filter(Boolean);
546
- let expectedPrev = GENESIS;
547
- for (let i = 0; i < lines.length; i++) {
548
- let entry;
549
- try { entry = JSON.parse(lines[i]); }
550
- catch { return { ok: false, brokenAt: i, reason: 'not JSON' }; }
551
- if (entry.prev !== expectedPrev) {
552
- return { ok: false, brokenAt: i, expected: expectedPrev, got: entry.prev };
553
- }
554
- expectedPrev = _sha(lines[i]);
555
- }
556
- return { ok: true, entries: lines.length };
557
- }
558
-
559
- ;// CONCATENATED MODULE: ./src/mcp/server.js
560
- // MCP server core — JSON-RPC 2.0 handler for the Model Context Protocol.
561
- //
562
- // Hardening posture (mapped to OWASP MCP Top 10):
563
- // - Session root chosen at server boot, no per-call retargeting (MCP02)
564
- // - Every tools/call argument validated against the tool's inputSchema (MCP02/MCP05)
565
- // - Every tools/call audited with a hash-chained log (MCP08)
566
- // - serverInfo.codeFingerprint = SHA-256 of MCP source files (MCP04/MCP09)
567
- // so a fleet can detect tampered or unauthorized server deployments
568
- // - AGENTIC_SECURITY_MCP_DISABLED=1 hard-disables all tool calls (MCP09)
569
- // - Stdio transport caps line/buffer size (./stdio.js) (MCP05 DoS)
570
-
571
-
572
-
573
-
574
-
575
-
576
-
577
-
578
-
579
- const PROTOCOL_VERSION = '2025-03-26';
580
- const SERVER_NAME = 'agentic-security';
581
- const SERVER_VERSION = '0.39.2';
582
-
583
- const TOOLS_BY_NAME = Object.fromEntries(ALL_TOOLS.map(t => [t.name, t]));
584
-
585
- // Code fingerprint — SHA-256 of the MCP source files concatenated in a
586
- // stable order. Embedded in `initialize` response so a fleet operator can
587
- // detect when an unapproved build is running (OWASP MCP04/MCP09).
588
- function _codeFingerprint() {
589
- try {
590
- const here = external_node_path_.dirname((0,external_node_url_.fileURLToPath)(import.meta.url));
591
- const files = ['server.js', 'tools.js', 'stdio.js', 'audit.js', 'validate.js', 'redact.js'];
592
- const h = external_node_crypto_.createHash('sha256');
593
- for (const f of files) {
594
- try { h.update(f); h.update(external_node_fs_.readFileSync(external_node_path_.join(here, f))); } catch {}
595
- }
596
- return h.digest('hex');
597
- } catch { return null; }
598
- }
599
- const CODE_FINGERPRINT = _codeFingerprint();
600
-
601
- function _err(id, code, message, data) {
602
- const out = { jsonrpc: '2.0', id, error: { code, message } };
603
- if (data !== undefined) out.error.data = data;
604
- return out;
605
- }
606
-
607
- function _ok(id, result) {
608
- return { jsonrpc: '2.0', id, result };
609
- }
610
-
611
- function createServer({ sessionRoot = process.cwd() } = {}) {
612
- const ctx = { sessionRoot };
613
-
614
- async function handleRequest(msg) {
615
- if (!msg || typeof msg !== 'object') return _err(null, -32600, 'Invalid Request');
616
- if (msg.jsonrpc !== '2.0') return _err(msg.id ?? null, -32600, 'Invalid Request: jsonrpc must be "2.0"');
617
-
618
- const isNotification = msg.id === undefined || msg.id === null;
619
- const id = msg.id ?? null;
620
- const disabled = process.env.AGENTIC_SECURITY_MCP_DISABLED === '1';
621
-
622
- switch (msg.method) {
623
- case 'initialize':
624
- return _ok(id, {
625
- protocolVersion: PROTOCOL_VERSION,
626
- capabilities: { tools: {} },
627
- serverInfo: {
628
- name: SERVER_NAME,
629
- version: SERVER_VERSION,
630
- codeFingerprint: CODE_FINGERPRINT,
631
- disabled,
632
- },
633
- });
634
-
635
- case 'notifications/initialized':
636
- return null;
637
-
638
- case 'ping':
639
- return _ok(id, {});
640
-
641
- case 'tools/list':
642
- return _ok(id, {
643
- tools: ALL_TOOLS.map(t => ({
644
- name: t.name,
645
- description: t.description,
646
- inputSchema: t.inputSchema,
647
- })),
648
- });
649
-
650
- case 'tools/call': {
651
- const name = msg.params?.name;
652
- const args = msg.params?.arguments ?? {};
653
- if (disabled) {
654
- auditCall({ sessionRoot, tool: name, args, outcome: 'rejected', reason: 'server-disabled' });
655
- return _ok(id, {
656
- content: [{ type: 'text', text: 'MCP server is disabled (AGENTIC_SECURITY_MCP_DISABLED=1).' }],
657
- isError: true,
658
- });
659
- }
660
- const tool = TOOLS_BY_NAME[name];
661
- if (!tool) {
662
- auditCall({ sessionRoot, tool: name, args, outcome: 'rejected', reason: 'unknown-tool' });
663
- return _err(id, -32602, `Unknown tool: ${name}`);
664
- }
665
- try { validate(tool.inputSchema, args); }
666
- catch (e) {
667
- auditCall({ sessionRoot, tool: name, args, outcome: 'rejected', reason: `invalid-args: ${e.message}` });
668
- return _ok(id, {
669
- content: [{ type: 'text', text: `Invalid arguments: ${e.message}` }],
670
- isError: true,
671
- });
672
- }
673
- try {
674
- const result = await tool.handler(args, ctx);
675
- auditCall({ sessionRoot, tool: name, args, outcome: 'ok' });
676
- return _ok(id, {
677
- content: [{ type: 'text', text: JSON.stringify(result, null, 2) }],
678
- isError: false,
679
- });
680
- } catch (e) {
681
- auditCall({ sessionRoot, tool: name, args, outcome: 'error', reason: e.message });
682
- return _ok(id, {
683
- content: [{ type: 'text', text: `Error: ${e.message}` }],
684
- isError: true,
685
- });
686
- }
687
- }
688
-
689
- default:
690
- if (isNotification) return null;
691
- return _err(id, -32601, `Method not found: ${msg.method}`);
692
- }
693
- }
694
-
695
- return { handleRequest, sessionRoot };
696
- }
697
-
698
- // NOTE: no default-singleton export. Callers must use createServer({...})
699
- // with an explicit sessionRoot. Removed because the prior default was bound
700
- // to process.cwd() at module-load time — a footgun for any caller that
701
- // imported `handleRequest` directly (OWASP A05).
702
-
703
-
704
-
705
- ;// CONCATENATED MODULE: ./src/mcp/stdio.js
706
- // Stdio transport for the MCP server — newline-delimited JSON in/out.
707
- //
708
- // MCP's stdio transport is NDJSON: one JSON-RPC message per line on stdin,
709
- // one response per line on stdout. stderr is reserved for logging.
710
- //
711
- // Hardening:
712
- // - Per-message line cap (MAX_LINE_BYTES). A line over the cap is dropped
713
- // and the buffer state is reset so a long oversize payload can't peg
714
- // the parser via `buf += chunk` growth.
715
- // - Buffer hard cap (MAX_BUFFER_BYTES). Reached if input arrives with no
716
- // newlines (e.g., a peer streaming a 4GB stream of `a`). On overflow we
717
- // emit a parse-error response and reset.
718
-
719
-
720
-
721
- const MAX_LINE_BYTES = 4 * 1024 * 1024; // 4 MB per JSON-RPC message
722
- const MAX_BUFFER_BYTES = 8 * 1024 * 1024; // 8 MB sliding buffer
723
-
724
- function runStdio({
725
- stdin = process.stdin,
726
- stdout = process.stdout,
727
- stderr = process.stderr,
728
- sessionRoot = process.cwd(),
729
- } = {}) {
730
- const server = createServer({ sessionRoot });
731
- let buf = '';
732
- let overflowSkip = false; // true while we are dropping bytes until the next newline
733
-
734
- stdin.setEncoding('utf8');
735
-
736
- stdin.on('data', async (chunk) => {
737
- if (overflowSkip) {
738
- const nl = chunk.indexOf('\n');
739
- if (nl === -1) return;
740
- // Resume after the next newline.
741
- chunk = chunk.slice(nl + 1);
742
- overflowSkip = false;
743
- }
744
-
745
- buf += chunk;
746
-
747
- // Hard buffer cap — only triggers if a peer is streaming without newlines.
748
- if (buf.length > MAX_BUFFER_BYTES) {
749
- stderr.write(`mcp: input buffer exceeded ${MAX_BUFFER_BYTES} bytes — dropping until next newline\n`);
750
- const errResponse = { jsonrpc: '2.0', id: null, error: { code: -32700, message: 'Parse error: input too large' } };
751
- stdout.write(JSON.stringify(errResponse) + '\n');
752
- buf = '';
753
- overflowSkip = true;
754
- return;
755
- }
756
-
757
- let nl;
758
- while ((nl = buf.indexOf('\n')) !== -1) {
759
- const line = buf.slice(0, nl).trim();
760
- buf = buf.slice(nl + 1);
761
- if (!line) continue;
762
- if (line.length > MAX_LINE_BYTES) {
763
- stderr.write(`mcp: dropped oversize line (${line.length} > ${MAX_LINE_BYTES} bytes)\n`);
764
- const errResponse = { jsonrpc: '2.0', id: null, error: { code: -32700, message: 'Parse error: line too large' } };
765
- stdout.write(JSON.stringify(errResponse) + '\n');
766
- continue;
767
- }
768
- let msg;
769
- try { msg = JSON.parse(line); }
770
- catch (e) {
771
- stderr.write(`mcp: failed to parse line as JSON: ${e.message}\n`);
772
- const errResponse = { jsonrpc: '2.0', id: null, error: { code: -32700, message: 'Parse error' } };
773
- stdout.write(JSON.stringify(errResponse) + '\n');
774
- continue;
775
- }
776
- try {
777
- const response = await server.handleRequest(msg);
778
- if (response !== null) stdout.write(JSON.stringify(response) + '\n');
779
- } catch (e) {
780
- stderr.write(`mcp: handler threw: ${e.message}\n`);
781
- const errResponse = { jsonrpc: '2.0', id: msg.id ?? null, error: { code: -32603, message: 'Internal error', data: e.message } };
782
- stdout.write(JSON.stringify(errResponse) + '\n');
783
- }
784
- }
785
- });
786
-
787
- stdin.on('end', () => { process.exit(0); });
788
- }
789
-
790
-
791
- /***/ })
792
-
793
- };