@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
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* src/platform/identity.js — local identity and capability-token primitives.
|
|
5
|
+
*
|
|
6
|
+
* Tokens are JWS Compact Serialization with EdDSA (Ed25519). No external
|
|
7
|
+
* dependency is used; Node's crypto implements Ed25519.
|
|
8
|
+
*
|
|
9
|
+
* VGAT claims:
|
|
10
|
+
* iss, sub (principal), aud, iat, nbf, exp, jti, tenant, kind,
|
|
11
|
+
* capabilities, policyVersion, scope (optional), delegation (optional).
|
|
12
|
+
*
|
|
13
|
+
* The gateway verifies issuer, audience, expiry, capability subset, replay
|
|
14
|
+
* (jti) and (when configured) the caller's tenant. Tokens are short-lived and
|
|
15
|
+
* audience-bound; they are not a substitute for human approval.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const crypto = require('crypto');
|
|
19
|
+
const { CAPABILITIES, ERROR_CODES, VibeGuardError, assertPrincipal } = require('./contracts');
|
|
20
|
+
|
|
21
|
+
const B64 = (buf) => Buffer.from(buf).toString('base64url');
|
|
22
|
+
const UNB64 = (s) => Buffer.from(String(s), 'base64url');
|
|
23
|
+
const MAX_TTL_SECONDS = 15 * 60;
|
|
24
|
+
const DEFAULT_SKEW_SECONDS = 30;
|
|
25
|
+
|
|
26
|
+
function generateKeyPair() {
|
|
27
|
+
return crypto.generateKeyPairSync('ed25519');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function publicKeyToPem(publicKey) {
|
|
31
|
+
return publicKey.export({ type: 'spki', format: 'pem' });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function privateKeyToPem(privateKey) {
|
|
35
|
+
return privateKey.export({ type: 'pkcs8', format: 'pem' });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function publicKeyToJwk(publicKey, kid) {
|
|
39
|
+
const jwk = publicKey.export({ format: 'jwk' });
|
|
40
|
+
return { ...jwk, kid, use: 'sig', alg: 'EdDSA' };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function loadPublicKey(value) {
|
|
44
|
+
if (value && typeof value === 'object' && value.type === 'public') return value;
|
|
45
|
+
return crypto.createPublicKey(value);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function loadPrivateKey(value) {
|
|
49
|
+
if (value && typeof value === 'object' && value.type === 'private') return value;
|
|
50
|
+
return crypto.createPrivateKey(value);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function signJws(header, payload, privateKey) {
|
|
54
|
+
const h = B64(JSON.stringify(header));
|
|
55
|
+
const p = B64(JSON.stringify(payload));
|
|
56
|
+
const input = Buffer.from(`${h}.${p}`, 'utf8');
|
|
57
|
+
const sig = crypto.sign(null, input, privateKey);
|
|
58
|
+
return `${h}.${p}.${sig.toString('base64url')}`;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function decodeJws(token) {
|
|
62
|
+
if (typeof token !== 'string') throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'token must be a string');
|
|
63
|
+
const parts = token.split('.');
|
|
64
|
+
if (parts.length !== 3) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'malformed token');
|
|
65
|
+
// Reject non-canonical base64url. Without this check, changing unused padding
|
|
66
|
+
// bits can preserve the decoded signature bytes while altering the token
|
|
67
|
+
// string (JWS canonicalization/malleability).
|
|
68
|
+
for (const part of parts) {
|
|
69
|
+
let canonical;
|
|
70
|
+
try { canonical = UNB64(part).toString('base64url'); } catch { canonical = null; }
|
|
71
|
+
if (!canonical || canonical !== part) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'invalid non-canonical token encoding');
|
|
72
|
+
}
|
|
73
|
+
let header;
|
|
74
|
+
let payload;
|
|
75
|
+
try {
|
|
76
|
+
header = JSON.parse(UNB64(parts[0]).toString('utf8'));
|
|
77
|
+
payload = JSON.parse(UNB64(parts[1]).toString('utf8'));
|
|
78
|
+
} catch {
|
|
79
|
+
throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'malformed token JSON');
|
|
80
|
+
}
|
|
81
|
+
return { header, payload, input: `${parts[0]}.${parts[1]}`, signature: parts[2] };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function verifyJws(token, publicKey) {
|
|
85
|
+
const decoded = decodeJws(token);
|
|
86
|
+
const key = loadPublicKey(publicKey);
|
|
87
|
+
const ok = crypto.verify(null, Buffer.from(decoded.input, 'utf8'), key, UNB64(decoded.signature));
|
|
88
|
+
if (!ok) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'invalid token signature');
|
|
89
|
+
return decoded;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
class ReplayCache {
|
|
93
|
+
constructor(clock) {
|
|
94
|
+
this.clock = clock || (() => Date.now());
|
|
95
|
+
this.seen = new Map();
|
|
96
|
+
this.max = 10000;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
prune() {
|
|
100
|
+
const now = this.clock();
|
|
101
|
+
for (const [jti, exp] of this.seen) {
|
|
102
|
+
if (exp <= now) this.seen.delete(jti);
|
|
103
|
+
}
|
|
104
|
+
while (this.seen.size > this.max) {
|
|
105
|
+
const first = this.seen.keys().next().value;
|
|
106
|
+
this.seen.delete(first);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
checkAndStore(jti, expMs) {
|
|
111
|
+
if (!jti) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'token jti is required for replay protection');
|
|
112
|
+
this.prune();
|
|
113
|
+
if (this.seen.has(jti)) throw new VibeGuardError(ERROR_CODES.REPLAY_DETECTED, 'token replay detected', { status: 401 });
|
|
114
|
+
this.seen.set(jti, expMs);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
class IdentityAuthority {
|
|
119
|
+
constructor(opts) {
|
|
120
|
+
if (!opts || !opts.issuer || !opts.tenant) throw new Error('IdentityAuthority requires issuer and tenant');
|
|
121
|
+
let privateKey;
|
|
122
|
+
let publicKey;
|
|
123
|
+
if (opts.privateKey) {
|
|
124
|
+
privateKey = loadPrivateKey(opts.privateKey);
|
|
125
|
+
publicKey = opts.publicKey ? loadPublicKey(opts.publicKey) : crypto.createPublicKey(privateKey);
|
|
126
|
+
} else if (opts.publicKey) {
|
|
127
|
+
privateKey = null;
|
|
128
|
+
publicKey = loadPublicKey(opts.publicKey);
|
|
129
|
+
} else {
|
|
130
|
+
const pair = generateKeyPair();
|
|
131
|
+
privateKey = pair.privateKey;
|
|
132
|
+
publicKey = pair.publicKey;
|
|
133
|
+
}
|
|
134
|
+
this.issuer = opts.issuer;
|
|
135
|
+
this.tenant = opts.tenant;
|
|
136
|
+
this.keyId = opts.keyId || `key_${crypto.randomBytes(4).toString('hex')}`;
|
|
137
|
+
this.privateKey = privateKey;
|
|
138
|
+
this.publicKey = publicKey;
|
|
139
|
+
this.now = opts.now || (() => new Date());
|
|
140
|
+
this.replay = opts.replay || new ReplayCache(() => this.now().getTime());
|
|
141
|
+
this.maxTtlSeconds = opts.maxTtlSeconds || MAX_TTL_SECONDS;
|
|
142
|
+
this.clockSkewSeconds = opts.clockSkewSeconds === undefined ? DEFAULT_SKEW_SECONDS : opts.clockSkewSeconds;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
publicJwk() {
|
|
146
|
+
return publicKeyToJwk(this.publicKey, this.keyId);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
issue(request = {}) {
|
|
150
|
+
if (!this.privateKey) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'identity authority is verify-only');
|
|
151
|
+
const principal = assertPrincipal(request.principal || `agent:${this.tenant}/${request.agent || 'default'}`);
|
|
152
|
+
const tenant = principal.split(':')[1].split('/')[0];
|
|
153
|
+
if (tenant !== this.tenant) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'principal tenant does not match authority');
|
|
154
|
+
const ttl = Number(request.ttlSeconds || 300);
|
|
155
|
+
if (!Number.isFinite(ttl) || ttl <= 0 || ttl > this.maxTtlSeconds) {
|
|
156
|
+
throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, `ttlSeconds must be 1..${this.maxTtlSeconds}`);
|
|
157
|
+
}
|
|
158
|
+
const capabilities = Array.isArray(request.capabilities) ? [...new Set(request.capabilities)] : [];
|
|
159
|
+
for (const cap of capabilities) {
|
|
160
|
+
if (!CAPABILITIES.includes(cap)) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, `unknown capability: ${cap}`);
|
|
161
|
+
}
|
|
162
|
+
const now = Math.floor(this.now().getTime() / 1000);
|
|
163
|
+
const payload = {
|
|
164
|
+
iss: this.issuer,
|
|
165
|
+
sub: principal,
|
|
166
|
+
aud: request.audience || 'vibeguard-gateway',
|
|
167
|
+
iat: now,
|
|
168
|
+
nbf: now,
|
|
169
|
+
exp: now + ttl,
|
|
170
|
+
jti: request.jti || crypto.randomBytes(12).toString('base64url'),
|
|
171
|
+
tenant,
|
|
172
|
+
kind: request.kind || 'agent',
|
|
173
|
+
capabilities,
|
|
174
|
+
policyVersion: request.policyVersion || null,
|
|
175
|
+
scope: request.scope || null,
|
|
176
|
+
delegation: request.delegation || null,
|
|
177
|
+
};
|
|
178
|
+
const header = { alg: 'EdDSA', typ: 'VGAT', kid: this.keyId };
|
|
179
|
+
const token = signJws(header, payload, this.privateKey);
|
|
180
|
+
return { token, payload, publicJwk: this.publicJwk() };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
verify(token, opts = {}) {
|
|
184
|
+
const decoded = decodeJws(token);
|
|
185
|
+
if (decoded.header.alg !== 'EdDSA' || decoded.header.typ !== 'VGAT') {
|
|
186
|
+
throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'unsupported token type/algorithm');
|
|
187
|
+
}
|
|
188
|
+
if (decoded.header.kid !== this.keyId) {
|
|
189
|
+
throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'unknown key id');
|
|
190
|
+
}
|
|
191
|
+
verifyJws(token, this.publicKey);
|
|
192
|
+
const p = decoded.payload;
|
|
193
|
+
const now = Math.floor(this.now().getTime() / 1000);
|
|
194
|
+
const skew = this.clockSkewSeconds;
|
|
195
|
+
if (p.iss !== this.issuer) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'invalid issuer');
|
|
196
|
+
if (p.aud !== (opts.audience || 'vibeguard-gateway')) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'invalid audience');
|
|
197
|
+
if (!Number.isInteger(p.exp) || p.exp <= now - skew) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'token expired');
|
|
198
|
+
if (!Number.isInteger(p.nbf) || p.nbf > now + skew) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'token not yet valid');
|
|
199
|
+
if (!Number.isInteger(p.iat) || p.iat > now + skew) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'invalid issued-at');
|
|
200
|
+
if (p.exp - p.iat > this.maxTtlSeconds + skew) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'token ttl exceeds maximum');
|
|
201
|
+
if (!p.jti) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'missing jti');
|
|
202
|
+
if (opts.tenant && p.tenant !== opts.tenant) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'tenant mismatch');
|
|
203
|
+
if (opts.principal && p.sub !== opts.principal) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'principal mismatch');
|
|
204
|
+
if (!Array.isArray(p.capabilities)) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'missing capabilities');
|
|
205
|
+
for (const cap of p.capabilities) {
|
|
206
|
+
if (!CAPABILITIES.includes(cap)) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, `unknown capability: ${cap}`);
|
|
207
|
+
}
|
|
208
|
+
if (opts.requiredCapabilities) {
|
|
209
|
+
for (const cap of opts.requiredCapabilities) {
|
|
210
|
+
if (!p.capabilities.includes(cap)) throw new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, `missing capability: ${cap}`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (opts.replay !== false) this.replay.checkAndStore(p.jti, p.exp * 1000);
|
|
214
|
+
return {
|
|
215
|
+
principal: {
|
|
216
|
+
principalId: p.sub,
|
|
217
|
+
kind: p.kind,
|
|
218
|
+
tenantId: p.tenant,
|
|
219
|
+
displayName: p.sub.split('/').slice(1).join('/'),
|
|
220
|
+
status: 'active',
|
|
221
|
+
capabilities: p.capabilities,
|
|
222
|
+
},
|
|
223
|
+
claims: p,
|
|
224
|
+
header: decoded.header,
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function verifyExternalJws(token, opts = {}) {
|
|
230
|
+
const decoded = decodeJws(token);
|
|
231
|
+
if (decoded.header.alg !== 'EdDSA') {
|
|
232
|
+
throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'unsupported JWS algorithm');
|
|
233
|
+
}
|
|
234
|
+
if (!opts.publicKey) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'no verification key');
|
|
235
|
+
verifyJws(token, opts.publicKey);
|
|
236
|
+
return decoded;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
module.exports = {
|
|
240
|
+
generateKeyPair,
|
|
241
|
+
publicKeyToPem,
|
|
242
|
+
privateKeyToPem,
|
|
243
|
+
publicKeyToJwk,
|
|
244
|
+
loadPublicKey,
|
|
245
|
+
loadPrivateKey,
|
|
246
|
+
signJws,
|
|
247
|
+
decodeJws,
|
|
248
|
+
verifyJws,
|
|
249
|
+
verifyExternalJws,
|
|
250
|
+
ReplayCache,
|
|
251
|
+
IdentityAuthority,
|
|
252
|
+
MAX_TTL_SECONDS,
|
|
253
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* src/platform/jwt.js — offline OAuth/JWT access-token validation.
|
|
5
|
+
*
|
|
6
|
+
* Supports EdDSA (Ed25519) JWTs with a local JWKS. RS256 is intentionally not
|
|
7
|
+
* implemented in the first baseline to avoid an unverified crypto path; add it
|
|
8
|
+
* with a reviewed implementation if needed.
|
|
9
|
+
*
|
|
10
|
+
* Validates signature, issuer, audience, exp/nbf/iat, key id, tenant/scope and
|
|
11
|
+
* replay. No network; JWKS must be supplied from local/offline configuration.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const crypto = require('crypto');
|
|
15
|
+
const { ERROR_CODES, VibeGuardError } = require('./contracts');
|
|
16
|
+
const { ReplayCache } = require('./identity');
|
|
17
|
+
const defaultReplayCache = new ReplayCache();
|
|
18
|
+
|
|
19
|
+
function b64json(part) {
|
|
20
|
+
let text;
|
|
21
|
+
try { text = Buffer.from(part, 'base64url').toString('utf8'); } catch { throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'invalid JWT encoding'); }
|
|
22
|
+
try { return JSON.parse(text); } catch { throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'invalid JWT JSON'); }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function decodeJwt(token) {
|
|
26
|
+
if (typeof token !== 'string') throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'JWT must be a string');
|
|
27
|
+
const parts = token.split('.');
|
|
28
|
+
if (parts.length !== 3) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'malformed JWT');
|
|
29
|
+
for (const part of parts) {
|
|
30
|
+
let bytes;
|
|
31
|
+
try { bytes = Buffer.from(part, 'base64url'); } catch { throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'invalid JWT base64url'); }
|
|
32
|
+
if (bytes.toString('base64url') !== part) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'non-canonical JWT encoding');
|
|
33
|
+
}
|
|
34
|
+
return { header: b64json(parts[0]), payload: b64json(parts[1]), input: `${parts[0]}.${parts[1]}`, signature: parts[2] };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function keyForJwks(jwks, kid) {
|
|
38
|
+
const keys = (jwks && jwks.keys) || [];
|
|
39
|
+
const jwk = keys.find((k) => k.kid === kid) || (keys.length === 1 ? keys[0] : null);
|
|
40
|
+
if (!jwk) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, `no JWKS key for kid ${kid || 'unknown'}`);
|
|
41
|
+
if (jwk.alg && jwk.alg !== 'EdDSA') throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'JWKS key algorithm not allowed');
|
|
42
|
+
if (jwk.use && jwk.use !== 'sig') throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'JWKS key is not a signing key');
|
|
43
|
+
try { return crypto.createPublicKey({ key: jwk, format: 'jwk' }); } catch (err) {
|
|
44
|
+
throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, `invalid JWKS key: ${err.message}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function verifyJwt(token, opts = {}) {
|
|
49
|
+
const decoded = decodeJwt(token);
|
|
50
|
+
if (decoded.header.alg !== 'EdDSA') throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'unsupported JWT alg');
|
|
51
|
+
if (opts.jwks) {
|
|
52
|
+
const key = keyForJwks(opts.jwks, decoded.header.kid);
|
|
53
|
+
const ok = crypto.verify(null, Buffer.from(decoded.input, 'utf8'), key, Buffer.from(decoded.signature, 'base64url'));
|
|
54
|
+
if (!ok) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'JWT signature invalid');
|
|
55
|
+
} else if (opts.publicKey) {
|
|
56
|
+
const ok = crypto.verify(null, Buffer.from(decoded.input, 'utf8'), opts.publicKey, Buffer.from(decoded.signature, 'base64url'));
|
|
57
|
+
if (!ok) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'JWT signature invalid');
|
|
58
|
+
} else {
|
|
59
|
+
throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'no JWT verification key');
|
|
60
|
+
}
|
|
61
|
+
const p = decoded.payload;
|
|
62
|
+
const now = Math.floor((opts.now ? (opts.now instanceof Date ? opts.now : new Date(opts.now)) : new Date()).getTime() / 1000);
|
|
63
|
+
const skew = opts.clockSkewSeconds === undefined ? 30 : Number(opts.clockSkewSeconds);
|
|
64
|
+
if (p.iss !== opts.issuer) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'JWT issuer mismatch');
|
|
65
|
+
const aud = Array.isArray(p.aud) ? p.aud : [p.aud];
|
|
66
|
+
if (opts.audience && !aud.includes(opts.audience)) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'JWT audience mismatch');
|
|
67
|
+
if (!Number.isInteger(p.exp) || p.exp <= now - skew) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'JWT expired');
|
|
68
|
+
if (p.nbf != null && (!Number.isInteger(p.nbf) || p.nbf > now + skew)) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'JWT not yet valid');
|
|
69
|
+
if (p.iat != null && (!Number.isInteger(p.iat) || p.iat > now + skew)) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'JWT issued-at invalid');
|
|
70
|
+
if (opts.maxTtlSeconds && Number.isInteger(p.iat) && p.exp - p.iat > Number(opts.maxTtlSeconds) + skew) {
|
|
71
|
+
throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'JWT ttl exceeds maximum');
|
|
72
|
+
}
|
|
73
|
+
if (opts.scopes && opts.scopes.length) {
|
|
74
|
+
const granted = String(p.scope || '').split(/\s+/).filter(Boolean);
|
|
75
|
+
for (const required of opts.scopes) {
|
|
76
|
+
if (!granted.includes(required)) throw new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, `missing scope: ${required}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (opts.replay !== false) {
|
|
80
|
+
const cache = opts.replayCache || defaultReplayCache;
|
|
81
|
+
cache.checkAndStore(p.jti, p.exp * 1000);
|
|
82
|
+
}
|
|
83
|
+
return { header: decoded.header, claims: p };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
module.exports = { decodeJwt, verifyJwt };
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* src/platform/mcp-http-bridge.js — Streamable HTTP <-> stdio MCP bridge.
|
|
5
|
+
*
|
|
6
|
+
* The bridge owns one upstream stdio MCP child. It accepts authenticated JSON-
|
|
7
|
+
* RPC POSTs through createHttpGateway, forwards requests to the child, returns
|
|
8
|
+
* responses, and exposes server-to-client notifications/requests on an SSE
|
|
9
|
+
* stream (`GET /mcp` with `Accept: text/event-stream`).
|
|
10
|
+
*
|
|
11
|
+
* It is intentionally local/loopback-first. TLS/remote OAuth termination and
|
|
12
|
+
* multi-session persistence are deployment concerns and are not claimed here.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const { spawn } = require('child_process');
|
|
16
|
+
const { createHttpGateway } = require('./http-gateway');
|
|
17
|
+
const { createEngine } = require('../mcp-firewall');
|
|
18
|
+
const { ERROR_CODES, VibeGuardError } = require('./contracts');
|
|
19
|
+
|
|
20
|
+
class StdioMcpBridge {
|
|
21
|
+
constructor(opts = {}) {
|
|
22
|
+
if (!opts.command) throw new Error('stdio bridge requires a command');
|
|
23
|
+
this.command = opts.command;
|
|
24
|
+
this.args = opts.args || [];
|
|
25
|
+
this.cwd = opts.cwd || process.cwd();
|
|
26
|
+
this.env = opts.env || {};
|
|
27
|
+
this.serverName = opts.serverName || 'http-upstream';
|
|
28
|
+
this.timeoutMs = opts.timeoutMs || 30000;
|
|
29
|
+
this.engine = opts.engine || null;
|
|
30
|
+
this.onEvent = opts.onEvent || (() => {});
|
|
31
|
+
this.child = null;
|
|
32
|
+
this.buffer = '';
|
|
33
|
+
this.pending = new Map();
|
|
34
|
+
this.nextId = 1;
|
|
35
|
+
this.closed = false;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
start() {
|
|
39
|
+
return new Promise((resolve, reject) => {
|
|
40
|
+
const child = spawn(this.command, this.args, {
|
|
41
|
+
cwd: this.cwd,
|
|
42
|
+
env: { ...process.env, ...this.env },
|
|
43
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
44
|
+
windowsHide: true,
|
|
45
|
+
shell: false,
|
|
46
|
+
});
|
|
47
|
+
this.child = child;
|
|
48
|
+
let started = false;
|
|
49
|
+
child.once('spawn', () => { started = true; resolve(this); });
|
|
50
|
+
child.once('error', (err) => { if (!started) reject(err); this.failAll(err); });
|
|
51
|
+
child.stdout.on('data', (chunk) => this._onData(chunk));
|
|
52
|
+
child.stderr.on('data', (chunk) => {
|
|
53
|
+
if (this.onStderr) this.onStderr(chunk);
|
|
54
|
+
});
|
|
55
|
+
child.once('exit', (code, signal) => this.failAll(new Error(`upstream exit ${code || signal || 'unknown'}`)));
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
_onData(chunk) {
|
|
60
|
+
this.buffer += chunk.toString('utf8');
|
|
61
|
+
if (this.buffer.length > 8 * 1024 * 1024) {
|
|
62
|
+
this.failAll(new Error('upstream message exceeds 8 MiB'));
|
|
63
|
+
try { this.child.kill(); } catch { /* ignore */ }
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const lines = this.buffer.split(/\r?\n/);
|
|
67
|
+
this.buffer = lines.pop();
|
|
68
|
+
for (const line of lines) {
|
|
69
|
+
const trimmed = line.trim();
|
|
70
|
+
if (!trimmed) continue;
|
|
71
|
+
let message;
|
|
72
|
+
try { message = JSON.parse(trimmed); } catch {
|
|
73
|
+
this.onEvent({ raw: trimmed });
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (message && message.id != null && this.pending.has(message.id)) {
|
|
77
|
+
const pending = this.pending.get(message.id);
|
|
78
|
+
this.pending.delete(message.id);
|
|
79
|
+
clearTimeout(pending.timer);
|
|
80
|
+
let inspected = { forward: true, message };
|
|
81
|
+
try { if (this.engine) inspected = this.engine.onServerMessage(this.serverName, message); } catch (err) {
|
|
82
|
+
pending.reject(err);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (inspected.forward === false) {
|
|
86
|
+
pending.reject(new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, 'upstream response blocked by content inspection'));
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
pending.resolve(inspected.message || message);
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
this.onEvent(message);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
request(message) {
|
|
97
|
+
if (this.closed || !this.child) return Promise.reject(new VibeGuardError(ERROR_CODES.SANDBOX_UNAVAILABLE, 'bridge is closed'));
|
|
98
|
+
const request = { ...message };
|
|
99
|
+
if (request.id == null) request.id = this.nextId++;
|
|
100
|
+
const id = request.id;
|
|
101
|
+
return new Promise((resolve, reject) => {
|
|
102
|
+
const timer = setTimeout(() => {
|
|
103
|
+
this.pending.delete(id);
|
|
104
|
+
reject(new VibeGuardError(ERROR_CODES.LIMIT_EXCEEDED, 'upstream request timed out'));
|
|
105
|
+
}, this.timeoutMs);
|
|
106
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
107
|
+
try {
|
|
108
|
+
this.child.stdin.write(JSON.stringify(request) + '\n');
|
|
109
|
+
} catch (err) {
|
|
110
|
+
clearTimeout(timer);
|
|
111
|
+
this.pending.delete(id);
|
|
112
|
+
reject(err);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
send(message) {
|
|
118
|
+
if (!this.child) return false;
|
|
119
|
+
try { this.child.stdin.write(JSON.stringify(message) + '\n'); return true; } catch { return false; }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
failAll(err) {
|
|
123
|
+
for (const [id, p] of this.pending) {
|
|
124
|
+
clearTimeout(p.timer);
|
|
125
|
+
p.reject(err);
|
|
126
|
+
this.pending.delete(id);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
close() {
|
|
131
|
+
this.closed = true;
|
|
132
|
+
this.failAll(new Error('bridge closed'));
|
|
133
|
+
try { this.child && this.child.kill(); } catch { /* ignore */ }
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function createMcpHttpBridge(opts = {}) {
|
|
138
|
+
const engine = opts.engine || createEngine({
|
|
139
|
+
root: opts.root || process.cwd(),
|
|
140
|
+
policy: {},
|
|
141
|
+
logger: opts.logger || { log() {} },
|
|
142
|
+
});
|
|
143
|
+
const bridge = new StdioMcpBridge({ ...opts, engine });
|
|
144
|
+
await bridge.start();
|
|
145
|
+
const clients = new Set();
|
|
146
|
+
const onStream = opts.onStream || ((req, res) => {
|
|
147
|
+
res.writeHead(200, {
|
|
148
|
+
'content-type': 'text/event-stream',
|
|
149
|
+
'cache-control': 'no-cache',
|
|
150
|
+
connection: 'keep-alive',
|
|
151
|
+
});
|
|
152
|
+
res.write(': connected\n\n');
|
|
153
|
+
clients.add(res);
|
|
154
|
+
req.on('close', () => clients.delete(res));
|
|
155
|
+
});
|
|
156
|
+
bridge.onEvent = (message) => {
|
|
157
|
+
// Inspect server-originated requests/notifications before they reach an
|
|
158
|
+
// SSE client. A blocked event is replaced with an error/removal.
|
|
159
|
+
let inspected = { forward: true, message };
|
|
160
|
+
try { if (engine && message) inspected = engine.onServerMessage(bridge.serverName, message); } catch { inspected = { forward: false }; }
|
|
161
|
+
if (inspected.forward === false) return;
|
|
162
|
+
const payload = JSON.stringify(inspected.message || message);
|
|
163
|
+
for (const client of clients) {
|
|
164
|
+
try { client.write(`event: message\ndata: ${payload}\n\n`); } catch { clients.delete(client); }
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
const gateway = await createHttpGateway({
|
|
168
|
+
host: opts.host,
|
|
169
|
+
port: opts.port,
|
|
170
|
+
authority: opts.authority,
|
|
171
|
+
jwt: opts.jwt,
|
|
172
|
+
policy: opts.policy,
|
|
173
|
+
handler: async (message) => {
|
|
174
|
+
if (!message.method && message.id != null) {
|
|
175
|
+
bridge.send(message);
|
|
176
|
+
return undefined;
|
|
177
|
+
}
|
|
178
|
+
const response = await bridge.request(message);
|
|
179
|
+
if (response.error) {
|
|
180
|
+
throw new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, response.error.message || 'upstream error', { status: 502 });
|
|
181
|
+
}
|
|
182
|
+
return response.result;
|
|
183
|
+
},
|
|
184
|
+
onStream,
|
|
185
|
+
});
|
|
186
|
+
return {
|
|
187
|
+
bridge,
|
|
188
|
+
gateway,
|
|
189
|
+
url: gateway.url,
|
|
190
|
+
close: async () => {
|
|
191
|
+
for (const client of clients) { try { client.end(); } catch { /* ignore */ } }
|
|
192
|
+
clients.clear();
|
|
193
|
+
bridge.close();
|
|
194
|
+
await gateway.close();
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
module.exports = { StdioMcpBridge, createMcpHttpBridge };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* src/platform/memory-guard.js — provenance-aware local memory primitive.
|
|
5
|
+
*
|
|
6
|
+
* This is not a persistent memory server. It is a small guard that any memory
|
|
7
|
+
* integration can call before storing/retrieving agent context: content is
|
|
8
|
+
* scanned for injection/secret material, source-tagged and hash-verified.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const { sha256Hex } = require('./canonical');
|
|
12
|
+
const { scanTextForInjection } = require('../mcp-audit');
|
|
13
|
+
const { scanForLeaksDeep } = require('../action-guard');
|
|
14
|
+
const { ERROR_CODES, VibeGuardError, assertPrincipal } = require('./contracts');
|
|
15
|
+
|
|
16
|
+
class MemoryGuard {
|
|
17
|
+
constructor(opts = {}) {
|
|
18
|
+
this.trustedSources = new Set(opts.trustedSources || ['human', 'system']);
|
|
19
|
+
this.entries = new Map();
|
|
20
|
+
this.clock = opts.clock || (() => new Date());
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
ingest({ id, content, source, principal, labels = [] } = {}) {
|
|
24
|
+
if (!id || typeof content !== 'string') throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'memory id and string content are required');
|
|
25
|
+
if (principal) assertPrincipal(principal);
|
|
26
|
+
if (!this.trustedSources.has(source)) throw new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, `untrusted memory source: ${source}`);
|
|
27
|
+
const injection = scanTextForInjection(content);
|
|
28
|
+
if (injection.length) throw new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, `memory content contains prompt injection: ${injection.join(', ')}`);
|
|
29
|
+
const leaks = scanForLeaksDeep(content);
|
|
30
|
+
if (leaks.secrets.length || leaks.entropy.length) throw new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, 'memory content contains credential material');
|
|
31
|
+
const entry = {
|
|
32
|
+
id,
|
|
33
|
+
content,
|
|
34
|
+
source,
|
|
35
|
+
principal: principal || null,
|
|
36
|
+
labels: [...labels],
|
|
37
|
+
hash: sha256Hex({ id, content, source, principal: principal || null }),
|
|
38
|
+
createdAt: this.clock().toISOString(),
|
|
39
|
+
};
|
|
40
|
+
this.entries.set(id, entry);
|
|
41
|
+
return this.get(id);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get(id) {
|
|
45
|
+
const e = this.entries.get(id);
|
|
46
|
+
if (!e) return null;
|
|
47
|
+
return { ...e, labels: e.labels.slice() };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
retrieve(id, principal) {
|
|
51
|
+
const e = this.entries.get(id);
|
|
52
|
+
if (!e) return null;
|
|
53
|
+
if (e.principal && principal && e.principal !== principal) throw new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, 'memory entry belongs to another principal');
|
|
54
|
+
if (sha256Hex({ id: e.id, content: e.content, source: e.source, principal: e.principal }) !== e.hash) {
|
|
55
|
+
throw new VibeGuardError(ERROR_CODES.AUDIT_INVALID, 'memory entry integrity check failed');
|
|
56
|
+
}
|
|
57
|
+
return { ...e, labels: e.labels.slice() };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
revoke(id) { return this.entries.delete(id); }
|
|
61
|
+
list() { return [...this.entries.values()].map((e) => ({ ...e, labels: e.labels.slice() })); }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = { MemoryGuard };
|