@yagyeshvyas/vibeguard 1.3.0 → 1.11.1
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/.vibeguard-mcp.example.json +51 -0
- package/CHANGELOG.md +604 -216
- package/MCP-FIREWALL.md +296 -0
- package/README.md +355 -31
- package/bin/cli.js +355 -27
- package/bin/cmd-agent-monitor.js +45 -0
- package/bin/cmd-agent-risk.js +64 -0
- package/bin/cmd-gif.js +62 -0
- package/bin/cmd-git-scan.js +74 -0
- package/bin/cmd-llm-proxy.js +164 -0
- package/bin/cmd-mcp-firewall.js +191 -0
- package/bin/cmd-pentest.js +103 -0
- package/bin/cmd-platform.js +137 -0
- package/bin/cmd-pr-scan.js +135 -0
- package/bin/cmd-sbom-diff.js +38 -0
- package/bin/cmd-skill-scan.js +35 -0
- package/docs/AISVS.md +25 -0
- package/docs/ARCHITECTURE.md +81 -0
- package/docs/INSTALLATION.md +139 -0
- package/docs/WINDOWS.md +74 -0
- package/docs/security-policy.strict.example.json +134 -0
- package/docs/security-policy.strict.example.pub.pem +3 -0
- package/package.json +45 -7
- package/src/.vibeguard/cache/file-hashes.json +60 -0
- package/src/.vibeguard/latest-snapshot.json +36 -0
- package/src/.vibeguard/mcp-pins.json +1 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/auto.js +428 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/daemon.js +344 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/dashboard.js +279 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/engine.js +297 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/hook.js +151 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/install.js +299 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/manifest.json +65 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/mcp-server.js +2051 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/plugin.js +168 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/pre-deploy.js +362 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/repl.js +150 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/rules.js +1611 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/sbom.js +164 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/shell-guard.js +292 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/slopsquat.js +144 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/trace.js +220 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/auto.js +428 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/daemon.js +344 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/engine.js +297 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/install.js +299 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/manifest.json +37 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/mcp-server.js +2055 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/plugin.js +168 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/pre-deploy.js +362 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/trace.js +220 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/auto.js +428 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/daemon.js +344 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/dashboard.js +279 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/engine.js +297 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/hook.js +151 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/install.js +299 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/manifest.json +65 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/mcp-server.js +2055 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/plugin.js +168 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/pre-deploy.js +362 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/repl.js +150 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/rules.js +1611 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/sbom.js +164 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/shell-guard.js +292 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/slopsquat.js +144 -0
- package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/trace.js +220 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/auto.js +428 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/daemon.js +344 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/dashboard.js +279 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/engine.js +297 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/hook.js +151 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/install.js +299 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/manifest.json +65 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/mcp-server.js +2054 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/plugin.js +168 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/pre-deploy.js +362 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/repl.js +150 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/rules.js +1611 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/sbom.js +164 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/shell-guard.js +292 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/slopsquat.js +144 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/trace.js +220 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/auto.js +428 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/daemon.js +344 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/engine.js +297 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/install.js +299 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/manifest.json +37 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/mcp-server.js +2055 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/plugin.js +168 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/pre-deploy.js +362 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/trace.js +220 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/auto.js +428 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/daemon.js +344 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/engine.js +297 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/manifest.json +33 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/mcp-server.js +2055 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/rules.js +1611 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/shell-guard.js +292 -0
- package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/trace.js +220 -0
- package/src/.vibeguard-baseline.json +5226 -0
- package/src/action-guard.js +84 -6
- package/src/agent-monitor.js +262 -0
- package/src/agent-policy.js +642 -0
- package/src/agent-risk.js +271 -0
- package/src/agentic.js +1513 -0
- package/src/ast.js +165 -7
- package/src/audit-trail.js +212 -67
- package/src/auto.js +427 -427
- package/src/autofix.js +11 -4
- package/src/bundles.js +198 -0
- package/src/daemon.js +343 -343
- package/src/dashboard.js +278 -278
- package/src/engine.js +3 -0
- package/src/entropy.js +187 -0
- package/src/firewall.js +19 -3
- package/src/gif.js +134 -0
- package/src/gitscan.js +282 -0
- package/src/hook.js +151 -151
- package/src/injection-decode.js +136 -0
- package/src/install.js +298 -280
- package/src/integrity.js +16 -1
- package/src/integrity.json +43 -13
- package/src/interceptor.js +12 -9
- package/src/llm-proxy.js +986 -0
- package/src/mcp-audit.js +61 -5
- package/src/mcp-firewall.js +971 -0
- package/src/mcp-server.js +2500 -2047
- package/src/obfuscation.js +315 -0
- package/src/owasp-agentic.js +314 -0
- package/src/pentest.js +589 -0
- package/src/pii.js +4 -0
- package/src/platform/aisvs.js +38 -0
- package/src/platform/approval.js +187 -0
- package/src/platform/canonical.js +73 -0
- package/src/platform/contracts.js +158 -0
- package/src/platform/delegation.js +73 -0
- package/src/platform/http-gateway.js +177 -0
- package/src/platform/identity.js +253 -0
- package/src/platform/jwt.js +86 -0
- package/src/platform/mcp-http-bridge.js +199 -0
- package/src/platform/memory-guard.js +64 -0
- package/src/platform/os-sandbox.js +144 -0
- package/src/platform/policy.js +332 -0
- package/src/platform/sandbox-manager.js +92 -0
- package/src/platform/signed-audit.js +357 -0
- package/src/platform/ssrf.js +129 -0
- package/src/plugin.js +182 -168
- package/src/pre-deploy.js +170 -55
- package/src/privacy-audit.js +272 -272
- package/src/repl.js +150 -150
- package/src/report.js +7 -1
- package/src/routes.js +1210 -0
- package/src/rules-2026.js +1407 -0
- package/src/rules-pack.js +3068 -3019
- package/src/rules.js +376 -60
- package/src/sandbox.js +200 -10
- package/src/sbom.js +193 -164
- package/src/scanner.js +189 -33
- package/src/shell-guard.js +11 -0
- package/src/skill-scan.js +477 -0
- package/src/slopsquat.js +144 -144
- package/src/surface.js +408 -0
- package/src/taint-ast.js +204 -37
- package/src/taint.js +15 -3
- package/src/tokenlean.js +579 -0
- package/src/trace.js +220 -220
package/src/action-guard.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
/*
|
|
4
4
|
* src/action-guard.js — Agent Action Firewall.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
6
|
+
* Real-time exfiltration guard. Inspects an agent action BEFORE it runs
|
|
7
7
|
* and blocks it if it would exfiltrate secrets or personal data, run a
|
|
8
8
|
* dangerous command, or send sensitive data to an untrusted destination.
|
|
9
9
|
*
|
|
@@ -14,10 +14,12 @@
|
|
|
14
14
|
* { type: 'prompt', content, provider? } // text going to an LLM
|
|
15
15
|
* { type: 'mcp', tool, args } // an MCP tool call
|
|
16
16
|
*
|
|
17
|
-
* Core
|
|
18
|
-
* of personal data (email, SSN, credit card, phone)
|
|
19
|
-
* to an external host without an explicit allow. Secrets to any
|
|
20
|
-
* destination are
|
|
17
|
+
* Core rule (best-effort, not a sandbox): a hardcoded API key or a piece
|
|
18
|
+
* of personal data (email, SSN, credit card, phone) should not leave the
|
|
19
|
+
* machine to an external host without an explicit allow. Secrets to any
|
|
20
|
+
* external destination are blocked. This catches the common exfil paths
|
|
21
|
+
* (fetch, http, exec, fs) — it is not a guarantee against a determined
|
|
22
|
+
* attacker with arbitrary native code execution.
|
|
21
23
|
*
|
|
22
24
|
* 100% local. Zero network. Zero dependencies. Fail-closed on the block path.
|
|
23
25
|
*/
|
|
@@ -105,6 +107,82 @@ function scanForLeaks(text) {
|
|
|
105
107
|
return out;
|
|
106
108
|
}
|
|
107
109
|
|
|
110
|
+
function decodedCandidates(text) {
|
|
111
|
+
const out = new Set();
|
|
112
|
+
const source = String(text || '');
|
|
113
|
+
if (/%[0-9a-f]{2}/i.test(source)) {
|
|
114
|
+
try { out.add(decodeURIComponent(source)); } catch { /* ignore */ }
|
|
115
|
+
}
|
|
116
|
+
const b64 = source.match(/[A-Za-z0-9+/_-]{20,}={0,2}/g) || [];
|
|
117
|
+
for (const token of b64.slice(0, 8)) {
|
|
118
|
+
try {
|
|
119
|
+
const decoded = Buffer.from(token.replace(/-/g, '+').replace(/_/g, '/'), 'base64').toString('utf8');
|
|
120
|
+
if (decoded.length >= 8 && /^[\x09\x0a\x0d\x20-\x7e\u00a0-\uffff]+$/.test(decoded)) out.add(decoded);
|
|
121
|
+
} catch { /* ignore */ }
|
|
122
|
+
}
|
|
123
|
+
if (/\\x[0-9a-f]{2}/i.test(source)) {
|
|
124
|
+
out.add(source.replace(/\\x([0-9a-f]{2})/gi, (_, h) => String.fromCharCode(parseInt(h, 16))));
|
|
125
|
+
}
|
|
126
|
+
if (/\\u[0-9a-f]{4}/i.test(source)) {
|
|
127
|
+
out.add(source.replace(/\\u([0-9a-f]{4})/gi, (_, h) => String.fromCharCode(parseInt(h, 16))));
|
|
128
|
+
}
|
|
129
|
+
return [...out];
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function findEntropyTokens(text) {
|
|
133
|
+
const hits = [];
|
|
134
|
+
const source = String(text || '');
|
|
135
|
+
const tokens = source.match(/[A-Za-z0-9+/=_-]{20,}/g) || [];
|
|
136
|
+
for (const token of tokens.slice(0, 32)) {
|
|
137
|
+
if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(token)) continue;
|
|
138
|
+
if (/^[0-9a-f]{32,}$/i.test(token) && [32, 40, 64, 96, 128].includes(token.length)) continue;
|
|
139
|
+
if (!/[A-Za-z]/.test(token) || !/[0-9]/.test(token)) continue;
|
|
140
|
+
if (looksStructured(token)) continue;
|
|
141
|
+
if (shannonEntropy(token) < 3.5) continue;
|
|
142
|
+
hits.push(token);
|
|
143
|
+
}
|
|
144
|
+
return [...new Set(hits)];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function findEntropySecrets(text) {
|
|
148
|
+
return findEntropyTokens(text).map(() => 'high-entropy token');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Deep scan: known patterns + decoded payloads + entropy + named credentials.
|
|
152
|
+
// Used by the strict policy/gateway; scanForLeaks stays for compatibility.
|
|
153
|
+
function scanForLeaksDeep(text) {
|
|
154
|
+
const raw = scanForLeaks(text);
|
|
155
|
+
// Entropy/encoded candidates all require at least 20 characters. Avoid the
|
|
156
|
+
// regex/entropy work for short ordinary arguments (the common hot path).
|
|
157
|
+
if (typeof text !== 'string' || text.length < 20) {
|
|
158
|
+
return {
|
|
159
|
+
secrets: raw.secrets,
|
|
160
|
+
pii: raw.pii,
|
|
161
|
+
namedSecrets: raw.namedSecrets,
|
|
162
|
+
entropy: [],
|
|
163
|
+
entropyTokens: [],
|
|
164
|
+
hasLeak: raw.secrets.length > 0 || raw.namedSecrets.length > 0 || raw.pii.length > 0,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
const secrets = new Set(raw.secrets);
|
|
168
|
+
const named = new Set(raw.namedSecrets);
|
|
169
|
+
const entropy = new Set(findEntropySecrets(text));
|
|
170
|
+
for (const variant of decodedCandidates(text)) {
|
|
171
|
+
const next = scanForLeaks(variant);
|
|
172
|
+
for (const s of next.secrets) secrets.add(s);
|
|
173
|
+
for (const n of next.namedSecrets) named.add(n);
|
|
174
|
+
for (const e of findEntropySecrets(variant)) entropy.add(e);
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
secrets: [...secrets],
|
|
178
|
+
pii: raw.pii,
|
|
179
|
+
namedSecrets: [...named],
|
|
180
|
+
entropy: [...entropy],
|
|
181
|
+
entropyTokens: findEntropyTokens(text),
|
|
182
|
+
hasLeak: secrets.size > 0 || named.size > 0 || entropy.size > 0 || raw.pii.length > 0,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
108
186
|
const RANK = { block: 2, warn: 1, allow: 0 };
|
|
109
187
|
|
|
110
188
|
// Inspect a single action. `policy.trustedHosts` (array) explicitly allows
|
|
@@ -217,4 +295,4 @@ function sanitizeOutbound(text) {
|
|
|
217
295
|
return out;
|
|
218
296
|
}
|
|
219
297
|
|
|
220
|
-
module.exports = { inspectAction, scanForLeaks, sanitizeOutbound, isExternalHost, hostOf, findNamedSecrets, shannonEntropy };
|
|
298
|
+
module.exports = { inspectAction, scanForLeaks, scanForLeaksDeep, sanitizeOutbound, isExternalHost, hostOf, findNamedSecrets, shannonEntropy };
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* src/agent-monitor.js — runtime monitor for autonomous agent activity.
|
|
5
|
+
*
|
|
6
|
+
* Reads the hash-chained audit log the MCP firewall writes and turns it into
|
|
7
|
+
* an operator view: what the agent tried, what was blocked, which servers and
|
|
8
|
+
* tools were involved, whether the log is intact, and whether the sequence of
|
|
9
|
+
* events matches a known hostile pattern (trust building, data gathering
|
|
10
|
+
* before egress, slow exfiltration, repeated blocks, rapid fire, tampering).
|
|
11
|
+
*
|
|
12
|
+
* `--watch` tails the log live. This is monitoring, not prevention: prevention
|
|
13
|
+
* happens in src/mcp-firewall.js, before the call reaches the server.
|
|
14
|
+
*
|
|
15
|
+
* 100% local. Zero network. Zero dependencies.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const fs = require('fs');
|
|
19
|
+
const path = require('path');
|
|
20
|
+
const { createLogger } = require('./audit-trail');
|
|
21
|
+
const { createSession, recordEvent, analyzeSession } = require('./behavior');
|
|
22
|
+
const { DEFAULT_AUDIT_FILE } = require('./mcp-firewall');
|
|
23
|
+
|
|
24
|
+
function eventFor(entry) {
|
|
25
|
+
const detail = entry.detail || {};
|
|
26
|
+
switch (entry.type) {
|
|
27
|
+
case 'mcp_blocked': return { type: 'blocked', detail };
|
|
28
|
+
case 'mcp_tool_poisoned': return { type: 'tamper', detail };
|
|
29
|
+
case 'mcp_prompt_blocked': return { type: 'blocked', detail };
|
|
30
|
+
case 'mcp_warn': return { type: 'network', detail };
|
|
31
|
+
case 'mcp_result_scrubbed': return { type: 'secret_access', detail };
|
|
32
|
+
case 'mcp_result_injection': return { type: 'tamper', detail };
|
|
33
|
+
case 'mcp_call': return { type: 'file_read', detail };
|
|
34
|
+
case 'mcp_tool_pinned': return { type: 'file_read', detail };
|
|
35
|
+
default: return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function analyzeMonitorLog(file) {
|
|
40
|
+
const resolved = path.resolve(file || DEFAULT_AUDIT_FILE);
|
|
41
|
+
const report = {
|
|
42
|
+
file: resolved,
|
|
43
|
+
exists: false,
|
|
44
|
+
entries: 0,
|
|
45
|
+
allowed: 0,
|
|
46
|
+
blocked: 0,
|
|
47
|
+
warned: 0,
|
|
48
|
+
scrubbed: 0,
|
|
49
|
+
poisoned: 0,
|
|
50
|
+
resultInjections: 0,
|
|
51
|
+
pinned: 0,
|
|
52
|
+
servers: {},
|
|
53
|
+
tools: {},
|
|
54
|
+
blockedTools: {},
|
|
55
|
+
byType: {},
|
|
56
|
+
firstEvent: null,
|
|
57
|
+
lastEvent: null,
|
|
58
|
+
chainValid: true,
|
|
59
|
+
brokenAt: [],
|
|
60
|
+
anomalies: [],
|
|
61
|
+
behavior: null,
|
|
62
|
+
riskScore: 0,
|
|
63
|
+
riskLevel: 'NONE',
|
|
64
|
+
recent: [],
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
if (!fs.existsSync(resolved)) return report;
|
|
68
|
+
let logger;
|
|
69
|
+
try { logger = createLogger(resolved); } catch (err) {
|
|
70
|
+
report.error = err.message;
|
|
71
|
+
return report;
|
|
72
|
+
}
|
|
73
|
+
report.exists = true;
|
|
74
|
+
const entries = logger.entries;
|
|
75
|
+
report.entries = entries.length;
|
|
76
|
+
if (entries.length) {
|
|
77
|
+
report.firstEvent = entries[0].timestamp;
|
|
78
|
+
report.lastEvent = entries[entries.length - 1].timestamp;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const verification = logger.verify();
|
|
82
|
+
report.chainValid = verification.valid;
|
|
83
|
+
report.brokenAt = verification.brokenAt;
|
|
84
|
+
|
|
85
|
+
const session = createSession();
|
|
86
|
+
for (const entry of entries) {
|
|
87
|
+
report.byType[entry.type] = (report.byType[entry.type] || 0) + 1;
|
|
88
|
+
const detail = entry.detail || {};
|
|
89
|
+
if (detail.server) report.servers[detail.server] = (report.servers[detail.server] || 0) + 1;
|
|
90
|
+
if (detail.tool) report.tools[detail.tool] = (report.tools[detail.tool] || 0) + 1;
|
|
91
|
+
|
|
92
|
+
if (entry.type === 'mcp_call') report.allowed++;
|
|
93
|
+
if (entry.type === 'mcp_warn') report.warned++;
|
|
94
|
+
if (entry.type === 'mcp_blocked') {
|
|
95
|
+
report.blocked++;
|
|
96
|
+
if (detail.tool) report.blockedTools[detail.tool] = (report.blockedTools[detail.tool] || 0) + 1;
|
|
97
|
+
}
|
|
98
|
+
if (entry.type === 'mcp_result_scrubbed') report.scrubbed += Number(detail.redactions || 1);
|
|
99
|
+
if (entry.type === 'mcp_result_injection') report.resultInjections++;
|
|
100
|
+
if (entry.type === 'mcp_tool_poisoned') report.poisoned++;
|
|
101
|
+
if (entry.type === 'mcp_tool_pinned') report.pinned++;
|
|
102
|
+
|
|
103
|
+
const mapped = eventFor(entry);
|
|
104
|
+
if (mapped) recordEvent(session, mapped.type, mapped.detail);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
report.behavior = analyzeSession(session);
|
|
108
|
+
report.anomalies = report.behavior.patterns;
|
|
109
|
+
report.riskScore =
|
|
110
|
+
Math.min(30, report.blocked * 6) +
|
|
111
|
+
Math.min(30, report.poisoned * 15) +
|
|
112
|
+
Math.min(20, report.resultInjections * 10) +
|
|
113
|
+
Math.min(10, report.warned) +
|
|
114
|
+
Math.min(10, report.scrubbed * 2) +
|
|
115
|
+
Math.min(20, report.anomalies.reduce((sum, p) => sum + (p.severity === 'critical' ? 20 : p.severity === 'high' ? 10 : 4), 0)) +
|
|
116
|
+
(report.chainValid ? 0 : 15);
|
|
117
|
+
report.riskScore = Math.min(100, report.riskScore);
|
|
118
|
+
report.riskLevel =
|
|
119
|
+
report.riskScore === 0 ? 'NONE' :
|
|
120
|
+
report.riskScore < 20 ? 'LOW' :
|
|
121
|
+
report.riskScore < 45 ? 'MEDIUM' :
|
|
122
|
+
report.riskScore < 70 ? 'HIGH' : 'CRITICAL';
|
|
123
|
+
|
|
124
|
+
report.recent = entries.slice(-15).map((entry) => ({
|
|
125
|
+
at: entry.timestamp,
|
|
126
|
+
type: entry.type,
|
|
127
|
+
server: entry.detail && entry.detail.server,
|
|
128
|
+
tool: entry.detail && entry.detail.tool,
|
|
129
|
+
reason: entry.detail && entry.detail.reasons ? entry.detail.reasons[0] : (entry.detail && entry.detail.error) || null,
|
|
130
|
+
}));
|
|
131
|
+
|
|
132
|
+
return report;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function topEntries(map, n = 5) {
|
|
136
|
+
return Object.entries(map).sort((a, b) => b[1] - a[1]).slice(0, n);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function renderMonitor(report) {
|
|
140
|
+
const C = { red: '\x1b[31m', green: '\x1b[32m', yellow: '\x1b[33m', cyan: '\x1b[36m', dim: '\x1b[2m', bold: '\x1b[1m', reset: '\x1b[0m' };
|
|
141
|
+
const riskColor = { NONE: C.green, LOW: C.green, MEDIUM: C.yellow, HIGH: C.yellow, CRITICAL: C.red };
|
|
142
|
+
const color = riskColor[report.riskLevel] || C.dim;
|
|
143
|
+
const lines = [
|
|
144
|
+
`${C.bold}VibeGuard Agent Monitor${C.reset}`,
|
|
145
|
+
`${C.dim}${'─'.repeat(64)}${C.reset}`,
|
|
146
|
+
` Log: ${report.file}`,
|
|
147
|
+
];
|
|
148
|
+
if (!report.exists) {
|
|
149
|
+
lines.push(` ${C.yellow}No audit log yet.${C.reset} Start the firewall first:`);
|
|
150
|
+
lines.push(` vibeguard mcp-firewall --server <name>`);
|
|
151
|
+
return lines.join('\n');
|
|
152
|
+
}
|
|
153
|
+
lines.push(
|
|
154
|
+
` Events: ${report.entries}`,
|
|
155
|
+
` Runtime risk: ${color}${report.riskScore}/100 (${report.riskLevel})${C.reset}`,
|
|
156
|
+
` Chain: ${report.chainValid ? C.green + 'intact' : C.red + 'BROKEN at ' + report.brokenAt.join(', ')}${C.reset}`,
|
|
157
|
+
'',
|
|
158
|
+
`${C.bold}Decisions${C.reset}`,
|
|
159
|
+
` Allowed: ${C.green}${report.allowed}${C.reset}`,
|
|
160
|
+
` Warned: ${C.yellow}${report.warned}${C.reset}`,
|
|
161
|
+
` Blocked: ${C.red}${report.blocked}${C.reset}`,
|
|
162
|
+
` Redactions: ${report.scrubbed}`,
|
|
163
|
+
` Injections: ${C.red}${report.resultInjections}${C.reset} tool-output injection(s) withheld`,
|
|
164
|
+
` Poisoned: ${C.red}${report.poisoned}${C.reset}`,
|
|
165
|
+
` Tools pinned: ${report.pinned}`,
|
|
166
|
+
''
|
|
167
|
+
);
|
|
168
|
+
const servers = topEntries(report.servers);
|
|
169
|
+
if (servers.length) {
|
|
170
|
+
lines.push(`${C.bold}Servers${C.reset}`);
|
|
171
|
+
for (const [name, count] of servers) lines.push(` ${name.padEnd(28)} ${count}`);
|
|
172
|
+
lines.push('');
|
|
173
|
+
}
|
|
174
|
+
const blocked = topEntries(report.blockedTools);
|
|
175
|
+
if (blocked.length) {
|
|
176
|
+
lines.push(`${C.bold}Blocked tools${C.reset}`);
|
|
177
|
+
for (const [name, count] of blocked) lines.push(` ${C.red}${name.padEnd(28)}${C.reset} ${count}`);
|
|
178
|
+
lines.push('');
|
|
179
|
+
}
|
|
180
|
+
if (report.anomalies.length) {
|
|
181
|
+
lines.push(`${C.bold}Behavior anomalies${C.reset}`);
|
|
182
|
+
for (const a of report.anomalies) {
|
|
183
|
+
const sc = a.severity === 'critical' ? C.red : C.yellow;
|
|
184
|
+
lines.push(` ${sc}[${a.severity}]${C.reset} ${a.pattern}`);
|
|
185
|
+
lines.push(` ${a.message}`);
|
|
186
|
+
}
|
|
187
|
+
lines.push('');
|
|
188
|
+
} else {
|
|
189
|
+
lines.push(`${C.green}No behavioral anomalies.${C.reset}`, '');
|
|
190
|
+
}
|
|
191
|
+
if (report.recent.length) {
|
|
192
|
+
lines.push(`${C.bold}Recent events${C.reset}`);
|
|
193
|
+
for (const e of report.recent) {
|
|
194
|
+
const t = new Date(e.at).toISOString().slice(11, 19);
|
|
195
|
+
const sc = e.type === 'mcp_blocked' || e.type === 'mcp_tool_poisoned' ? C.red : e.type === 'mcp_warn' ? C.yellow : C.dim;
|
|
196
|
+
const where = [e.server, e.tool].filter(Boolean).join('/');
|
|
197
|
+
lines.push(` ${C.dim}${t}${C.reset} ${sc}${e.type.padEnd(20)}${C.reset} ${where}${e.reason ? ' — ' + String(e.reason).slice(0, 80) : ''}`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return lines.join('\n');
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Tail a JSONL audit log. Calls onEvent for every new complete line.
|
|
204
|
+
function watchAudit(file, opts = {}) {
|
|
205
|
+
const resolved = path.resolve(file || DEFAULT_AUDIT_FILE);
|
|
206
|
+
const interval = Math.max(200, Number(opts.interval || 1000));
|
|
207
|
+
let position = 0;
|
|
208
|
+
let carry = '';
|
|
209
|
+
try { position = fs.statSync(resolved).size; } catch { position = 0; }
|
|
210
|
+
let timer = null;
|
|
211
|
+
let stopped = false;
|
|
212
|
+
|
|
213
|
+
const tick = () => {
|
|
214
|
+
if (stopped) return;
|
|
215
|
+
let stat;
|
|
216
|
+
try { stat = fs.statSync(resolved); } catch { return; }
|
|
217
|
+
if (stat.size < position) { position = 0; carry = ''; } // log rotated
|
|
218
|
+
if (stat.size === position) return;
|
|
219
|
+
let chunk;
|
|
220
|
+
try {
|
|
221
|
+
const fd = fs.openSync(resolved, 'r');
|
|
222
|
+
const length = stat.size - position;
|
|
223
|
+
const buffer = Buffer.alloc(length);
|
|
224
|
+
fs.readSync(fd, buffer, 0, length, position);
|
|
225
|
+
fs.closeSync(fd);
|
|
226
|
+
position = stat.size;
|
|
227
|
+
chunk = carry + buffer.toString('utf8');
|
|
228
|
+
} catch {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const lines = chunk.split(/\r?\n/);
|
|
232
|
+
carry = lines.pop();
|
|
233
|
+
for (const line of lines) {
|
|
234
|
+
if (!line.trim()) continue;
|
|
235
|
+
try {
|
|
236
|
+
const entry = JSON.parse(line);
|
|
237
|
+
if (opts.onEvent) opts.onEvent(entry);
|
|
238
|
+
} catch { /* ignore partial/corrupt line */ }
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
timer = setInterval(tick, interval);
|
|
243
|
+
return {
|
|
244
|
+
stop() {
|
|
245
|
+
stopped = true;
|
|
246
|
+
if (timer) clearInterval(timer);
|
|
247
|
+
timer = null;
|
|
248
|
+
},
|
|
249
|
+
tick,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function formatEvent(entry) {
|
|
254
|
+
const C = { red: '\x1b[31m', green: '\x1b[32m', yellow: '\x1b[33m', dim: '\x1b[2m', reset: '\x1b[0m' };
|
|
255
|
+
const detail = entry.detail || {};
|
|
256
|
+
const t = new Date(entry.timestamp).toISOString().slice(11, 19);
|
|
257
|
+
const color = entry.type === 'mcp_blocked' || entry.type === 'mcp_tool_poisoned' ? C.red : entry.type === 'mcp_warn' ? C.yellow : C.green;
|
|
258
|
+
const where = [detail.server, detail.tool].filter(Boolean).join('/');
|
|
259
|
+
return `${C.dim}${t}${C.reset} ${color}${entry.type}${C.reset} ${where}${detail.reasons ? ' — ' + String(detail.reasons[0] || detail.reasons).slice(0, 100) : ''}`;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
module.exports = { analyzeMonitorLog, renderMonitor, watchAudit, formatEvent, eventFor };
|