@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,187 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* src/platform/approval.js — signed, single-use human approvals.
|
|
5
|
+
*
|
|
6
|
+
* An approval ticket binds a canonical action digest to a requesting agent.
|
|
7
|
+
* A human approver (a different principal in the same tenant) signs a VGAPPROVAL
|
|
8
|
+
* JWS. The gateway verifies the signature, digest, ticket, identity, expiry and
|
|
9
|
+
* nonce before executing the action. Tokens are single-use and cannot be
|
|
10
|
+
* self-approved.
|
|
11
|
+
*
|
|
12
|
+
* This module is intentionally in-memory for the first platform baseline.
|
|
13
|
+
* Production persistence/replication belongs behind the ApprovalStore port.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const crypto = require('crypto');
|
|
17
|
+
const { ERROR_CODES, VibeGuardError, newId } = require('./contracts');
|
|
18
|
+
const { signJws, verifyJws, loadPrivateKey, loadPublicKey, generateKeyPair } = require('./identity');
|
|
19
|
+
const { actionDigest } = require('./canonical');
|
|
20
|
+
|
|
21
|
+
class ApprovalBroker {
|
|
22
|
+
constructor(opts = {}) {
|
|
23
|
+
if (!opts.issuer || !opts.tenant) throw new Error('ApprovalBroker requires issuer and tenant');
|
|
24
|
+
let privateKey;
|
|
25
|
+
let publicKey;
|
|
26
|
+
if (opts.privateKey) {
|
|
27
|
+
privateKey = loadPrivateKey(opts.privateKey);
|
|
28
|
+
publicKey = opts.publicKey ? loadPublicKey(opts.publicKey) : crypto.createPublicKey(privateKey);
|
|
29
|
+
} else if (opts.publicKey) {
|
|
30
|
+
privateKey = null;
|
|
31
|
+
publicKey = loadPublicKey(opts.publicKey);
|
|
32
|
+
} else {
|
|
33
|
+
const pair = generateKeyPair();
|
|
34
|
+
privateKey = pair.privateKey;
|
|
35
|
+
publicKey = pair.publicKey;
|
|
36
|
+
}
|
|
37
|
+
this.issuer = opts.issuer;
|
|
38
|
+
this.tenant = opts.tenant;
|
|
39
|
+
this.keyId = opts.keyId || `approval_${crypto.randomBytes(4).toString('hex')}`;
|
|
40
|
+
this.privateKey = privateKey;
|
|
41
|
+
this.publicKey = publicKey;
|
|
42
|
+
this.now = opts.now || (() => new Date());
|
|
43
|
+
this.defaultTtlSeconds = opts.defaultTtlSeconds || 300;
|
|
44
|
+
this.tickets = new Map();
|
|
45
|
+
this.usedTokens = new Set();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
request(request = {}) {
|
|
49
|
+
if (!this.privateKey) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'approval broker is verify-only');
|
|
50
|
+
if (!request.principal || !String(request.principal).startsWith('agent:')) {
|
|
51
|
+
throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'approval requester must be an agent principal');
|
|
52
|
+
}
|
|
53
|
+
const requesterTenant = String(request.principal).split(':')[1].split('/')[0];
|
|
54
|
+
if (requesterTenant !== this.tenant) {
|
|
55
|
+
throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'requester tenant mismatch');
|
|
56
|
+
}
|
|
57
|
+
if (!request.action || typeof request.action !== 'object') {
|
|
58
|
+
throw new VibeGuardError(ERROR_CODES.SCHEMA_INVALID, 'approval action is required');
|
|
59
|
+
}
|
|
60
|
+
const ttl = Math.min(Number(request.ttlSeconds || this.defaultTtlSeconds), 900);
|
|
61
|
+
if (!Number.isFinite(ttl) || ttl <= 0) throw new VibeGuardError(ERROR_CODES.SCHEMA_INVALID, 'invalid approval ttl');
|
|
62
|
+
const now = this.now();
|
|
63
|
+
const expires = new Date(now.getTime() + ttl * 1000);
|
|
64
|
+
const ticket = {
|
|
65
|
+
ticketId: newId('tkt'),
|
|
66
|
+
principal: request.principal,
|
|
67
|
+
action: request.action,
|
|
68
|
+
actionDigest: actionDigest(request.action),
|
|
69
|
+
nonce: crypto.randomBytes(16).toString('base64url'),
|
|
70
|
+
reason: String(request.reason || '').slice(0, 500),
|
|
71
|
+
createdAt: now.toISOString(),
|
|
72
|
+
expiresAt: expires.toISOString(),
|
|
73
|
+
status: 'pending',
|
|
74
|
+
approver: null,
|
|
75
|
+
approvalToken: null,
|
|
76
|
+
};
|
|
77
|
+
this.tickets.set(ticket.ticketId, ticket);
|
|
78
|
+
return { ticket: publicTicket(ticket) };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
decide(ticketId, decision = {}) {
|
|
82
|
+
const ticket = this.tickets.get(ticketId);
|
|
83
|
+
if (!ticket) throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approval ticket not found', { status: 404 });
|
|
84
|
+
if (ticket.status !== 'pending') throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'ticket already decided');
|
|
85
|
+
if (new Date(ticket.expiresAt).getTime() <= this.now().getTime()) {
|
|
86
|
+
ticket.status = 'expired';
|
|
87
|
+
throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approval ticket expired');
|
|
88
|
+
}
|
|
89
|
+
const approver = String(decision.approver || '');
|
|
90
|
+
if (!approver.startsWith('human:')) {
|
|
91
|
+
throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approver must be a human principal');
|
|
92
|
+
}
|
|
93
|
+
const tenant = (x) => String(x).split(':')[1].split('/')[0];
|
|
94
|
+
if (tenant(approver) !== this.tenant) throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approver tenant mismatch');
|
|
95
|
+
if (approver === ticket.principal) throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'self-approval is not allowed');
|
|
96
|
+
|
|
97
|
+
if (decision.approve !== true) {
|
|
98
|
+
ticket.status = 'denied';
|
|
99
|
+
ticket.approver = approver;
|
|
100
|
+
ticket.decidedAt = this.now().toISOString();
|
|
101
|
+
return { status: 'denied', ticket: publicTicket(ticket) };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const payload = {
|
|
105
|
+
iss: this.issuer,
|
|
106
|
+
sub: approver,
|
|
107
|
+
aud: 'vibeguard-approval',
|
|
108
|
+
iat: Math.floor(this.now().getTime() / 1000),
|
|
109
|
+
exp: Math.floor(new Date(ticket.expiresAt).getTime() / 1000),
|
|
110
|
+
jti: crypto.randomBytes(12).toString('base64url'),
|
|
111
|
+
typ: 'VGAPPROVAL',
|
|
112
|
+
ticketId: ticket.ticketId,
|
|
113
|
+
actionDigest: ticket.actionDigest,
|
|
114
|
+
nonce: ticket.nonce,
|
|
115
|
+
requestedBy: ticket.principal,
|
|
116
|
+
};
|
|
117
|
+
const token = signJws({ alg: 'EdDSA', typ: 'VGAPPROVAL', kid: this.keyId }, payload, this.privateKey);
|
|
118
|
+
ticket.status = 'approved';
|
|
119
|
+
ticket.approver = approver;
|
|
120
|
+
ticket.approvalToken = token;
|
|
121
|
+
ticket.decidedAt = this.now().toISOString();
|
|
122
|
+
return { status: 'approved', ticket: publicTicket(ticket), approvalToken: token };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
verify(ticketId, approvalToken, expectedActionDigest) {
|
|
126
|
+
const ticket = this.tickets.get(ticketId);
|
|
127
|
+
if (!ticket) throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approval ticket not found');
|
|
128
|
+
if (ticket.status !== 'approved') throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'ticket is not approved');
|
|
129
|
+
if (ticket.actionDigest !== expectedActionDigest) {
|
|
130
|
+
throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approval action digest mismatch');
|
|
131
|
+
}
|
|
132
|
+
if (ticket.usedAt) throw new VibeGuardError(ERROR_CODES.REPLAY_DETECTED, 'approval replay detected (already used)');
|
|
133
|
+
const decoded = verifyJws(approvalToken, this.publicKey);
|
|
134
|
+
if (decoded.header.typ !== 'VGAPPROVAL' || decoded.header.alg !== 'EdDSA') {
|
|
135
|
+
throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'invalid approval token type');
|
|
136
|
+
}
|
|
137
|
+
const p = decoded.payload;
|
|
138
|
+
const now = Math.floor(this.now().getTime() / 1000);
|
|
139
|
+
if (p.iss !== this.issuer || p.aud !== 'vibeguard-approval') throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approval issuer/audience mismatch');
|
|
140
|
+
if (p.ticketId !== ticketId) throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approval ticket mismatch');
|
|
141
|
+
if (p.actionDigest !== expectedActionDigest) throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approval digest mismatch');
|
|
142
|
+
if (p.nonce !== ticket.nonce) throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approval nonce mismatch');
|
|
143
|
+
if (!Number.isInteger(p.exp) || p.exp <= now) throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approval expired');
|
|
144
|
+
if (p.sub !== ticket.approver) throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approval approver mismatch');
|
|
145
|
+
if (this.usedTokens.has(p.jti)) throw new VibeGuardError(ERROR_CODES.REPLAY_DETECTED, 'approval replay');
|
|
146
|
+
this.usedTokens.add(p.jti);
|
|
147
|
+
ticket.usedAt = this.now().toISOString();
|
|
148
|
+
return { approved: true, approver: p.sub, ticket: publicTicket(ticket) };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
listPending() {
|
|
152
|
+
return [...this.tickets.values()].filter((t) => t.status === 'pending').map(publicTicket);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
verifyStandalone(approvalToken, expectedActionDigest) {
|
|
156
|
+
if (!expectedActionDigest) throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'action digest required');
|
|
157
|
+
const decoded = verifyJws(approvalToken, this.publicKey);
|
|
158
|
+
if (decoded.header.typ !== 'VGAPPROVAL' || decoded.header.alg !== 'EdDSA') {
|
|
159
|
+
throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'invalid approval token type');
|
|
160
|
+
}
|
|
161
|
+
const p = decoded.payload;
|
|
162
|
+
const now = Math.floor(this.now().getTime() / 1000);
|
|
163
|
+
if (p.iss !== this.issuer || p.aud !== 'vibeguard-approval') throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approval issuer/audience mismatch');
|
|
164
|
+
if (p.actionDigest !== expectedActionDigest) throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approval digest mismatch');
|
|
165
|
+
if (!Number.isInteger(p.exp) || p.exp <= now) throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approval expired');
|
|
166
|
+
if (!p.sub || !String(p.sub).startsWith('human:')) throw new VibeGuardError(ERROR_CODES.APPROVAL_INVALID, 'approval approver is not human');
|
|
167
|
+
if (this.usedTokens.has(p.jti)) throw new VibeGuardError(ERROR_CODES.REPLAY_DETECTED, 'approval replay');
|
|
168
|
+
this.usedTokens.add(p.jti);
|
|
169
|
+
return { approved: true, approver: p.sub, actionDigest: p.actionDigest };
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function publicTicket(t) {
|
|
174
|
+
return {
|
|
175
|
+
ticketId: t.ticketId,
|
|
176
|
+
principal: t.principal,
|
|
177
|
+
actionDigest: t.actionDigest,
|
|
178
|
+
reason: t.reason,
|
|
179
|
+
createdAt: t.createdAt,
|
|
180
|
+
expiresAt: t.expiresAt,
|
|
181
|
+
status: t.status,
|
|
182
|
+
approver: t.approver,
|
|
183
|
+
used: !!t.usedAt,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
module.exports = { ApprovalBroker };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* src/platform/canonical.js — deterministic JSON canonicalization and hashing.
|
|
5
|
+
*
|
|
6
|
+
* This is a dependency-free subset of RFC 8785 (JCS): object keys are sorted,
|
|
7
|
+
* strings use JSON escaping, numbers use ECMAScript JSON number formatting,
|
|
8
|
+
* arrays preserve order, and undefined/function/symbol/BigInt/cycles are
|
|
9
|
+
* rejected. It is used for policy hashes, audit events, approvals and action
|
|
10
|
+
* digests. Unknown values fail closed rather than silently producing a hash.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const crypto = require('crypto');
|
|
14
|
+
|
|
15
|
+
function canonicalize(value) {
|
|
16
|
+
const seen = new WeakSet();
|
|
17
|
+
return serialize(value, seen);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function serialize(value, seen) {
|
|
21
|
+
if (value === null) return 'null';
|
|
22
|
+
const t = typeof value;
|
|
23
|
+
if (t === 'string') return JSON.stringify(value);
|
|
24
|
+
if (t === 'number') {
|
|
25
|
+
if (!Number.isFinite(value)) throw new Error('canonical JSON: non-finite number');
|
|
26
|
+
return JSON.stringify(value);
|
|
27
|
+
}
|
|
28
|
+
if (t === 'boolean') return value ? 'true' : 'false';
|
|
29
|
+
if (t === 'undefined' || t === 'function' || t === 'symbol' || t === 'bigint') {
|
|
30
|
+
throw new Error(`canonical JSON: unsupported ${t}`);
|
|
31
|
+
}
|
|
32
|
+
if (t !== 'object') throw new Error(`canonical JSON: unsupported type ${t}`);
|
|
33
|
+
if (seen.has(value)) throw new Error('canonical JSON: cycle detected');
|
|
34
|
+
seen.add(value);
|
|
35
|
+
let out;
|
|
36
|
+
if (Array.isArray(value)) {
|
|
37
|
+
out = '[' + value.map((v) => serialize(v, seen)).join(',') + ']';
|
|
38
|
+
} else {
|
|
39
|
+
if (value instanceof Date) {
|
|
40
|
+
out = JSON.stringify(value.toISOString());
|
|
41
|
+
} else {
|
|
42
|
+
const keys = Object.keys(value).sort();
|
|
43
|
+
out = '{' + keys.map((k) => JSON.stringify(k) + ':' + serialize(value[k], seen)).join(',') + '}';
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
seen.delete(value);
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function sha256Hex(value) {
|
|
51
|
+
const text = typeof value === 'string' ? value : canonicalize(value);
|
|
52
|
+
return crypto.createHash('sha256').update(text, 'utf8').digest('hex');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function sha256Base64url(value) {
|
|
56
|
+
const text = typeof value === 'string' ? value : canonicalize(value);
|
|
57
|
+
return crypto.createHash('sha256').update(text, 'utf8').digest('base64url');
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function bodyHash(value) {
|
|
61
|
+
return 'sha256:' + sha256Hex(value);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function actionDigest(action) {
|
|
65
|
+
return bodyHash({
|
|
66
|
+
server: action && action.server,
|
|
67
|
+
tool: action && action.tool,
|
|
68
|
+
capability: action && action.capability,
|
|
69
|
+
arguments: action && action.arguments !== undefined ? action.arguments : null,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
module.exports = { canonicalize, sha256Hex, sha256Base64url, bodyHash, actionDigest };
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* src/platform/contracts.js — shared platform contracts for the v2 security
|
|
5
|
+
* baseline. These are deliberately small, dependency-free primitives used by
|
|
6
|
+
* the policy, identity, approval, audit and gateway modules.
|
|
7
|
+
*
|
|
8
|
+
* The legacy v1 modules keep their public APIs; this file is additive.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const crypto = require('crypto');
|
|
12
|
+
|
|
13
|
+
const DECISION = Object.freeze({
|
|
14
|
+
ALLOW: 'allow',
|
|
15
|
+
DENY: 'deny',
|
|
16
|
+
APPROVAL_REQUIRED: 'approval_required',
|
|
17
|
+
QUARANTINE: 'quarantine',
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const ISOLATION = Object.freeze({
|
|
21
|
+
NONE: 'none',
|
|
22
|
+
PROCESS: 'process',
|
|
23
|
+
OS: 'os',
|
|
24
|
+
CONTAINER: 'container',
|
|
25
|
+
MICROVM: 'microvm',
|
|
26
|
+
HARDWARE_VM: 'hardware-vm',
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const TRUST = Object.freeze({
|
|
30
|
+
UNKNOWN: 'unknown',
|
|
31
|
+
PENDING: 'pending',
|
|
32
|
+
TRUSTED: 'trusted',
|
|
33
|
+
REVOKED: 'revoked',
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const CAPABILITIES = Object.freeze([
|
|
37
|
+
'mcp.tool.invoke',
|
|
38
|
+
'mcp.resource.read',
|
|
39
|
+
'mcp.prompt.get',
|
|
40
|
+
'mcp.server.manage',
|
|
41
|
+
'mcp.protocol.unknown',
|
|
42
|
+
'fs.read',
|
|
43
|
+
'fs.write',
|
|
44
|
+
'net.egress',
|
|
45
|
+
'proc.exec',
|
|
46
|
+
'secret.read',
|
|
47
|
+
'secret.use',
|
|
48
|
+
'sandbox.exec',
|
|
49
|
+
'package.install',
|
|
50
|
+
'skill.load',
|
|
51
|
+
'memory.write',
|
|
52
|
+
'agent.delegate',
|
|
53
|
+
'policy.admin',
|
|
54
|
+
'audit.read',
|
|
55
|
+
'trust.admin',
|
|
56
|
+
'incident.admin',
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
const DATA_LABELS = Object.freeze([
|
|
60
|
+
'public',
|
|
61
|
+
'internal',
|
|
62
|
+
'confidential',
|
|
63
|
+
'restricted',
|
|
64
|
+
'pii',
|
|
65
|
+
'secret',
|
|
66
|
+
'credential',
|
|
67
|
+
'untrusted',
|
|
68
|
+
]);
|
|
69
|
+
|
|
70
|
+
const ERROR_CODES = Object.freeze({
|
|
71
|
+
AUTH_REQUIRED: 'VG_AUTH_REQUIRED',
|
|
72
|
+
AUTH_INVALID: 'VG_AUTH_INVALID',
|
|
73
|
+
AUTHORIZATION_DENIED: 'VG_AUTHORIZATION_DENIED',
|
|
74
|
+
APPROVAL_REQUIRED: 'VG_APPROVAL_REQUIRED',
|
|
75
|
+
APPROVAL_INVALID: 'VG_APPROVAL_INVALID',
|
|
76
|
+
POLICY_INVALID: 'VG_POLICY_INVALID',
|
|
77
|
+
POLICY_UNAVAILABLE: 'VG_POLICY_UNAVAILABLE',
|
|
78
|
+
POLICY_EVALUATION_ERROR: 'VG_POLICY_EVALUATION_ERROR',
|
|
79
|
+
AUDIT_UNAVAILABLE: 'VG_AUDIT_UNAVAILABLE',
|
|
80
|
+
AUDIT_INVALID: 'VG_AUDIT_INVALID',
|
|
81
|
+
SCHEMA_INVALID: 'VG_SCHEMA_INVALID',
|
|
82
|
+
LIMIT_EXCEEDED: 'VG_LIMIT_EXCEEDED',
|
|
83
|
+
SANDBOX_UNAVAILABLE: 'VG_SANDBOX_UNAVAILABLE',
|
|
84
|
+
SANDBOX_FAILED: 'VG_SANDBOX_FAILED',
|
|
85
|
+
TRUST_UNKNOWN: 'VG_TRUST_UNKNOWN',
|
|
86
|
+
POSTURE_DEGRADED: 'VG_POSTURE_DEGRADED',
|
|
87
|
+
DETECTOR_ERROR: 'VG_DETECTOR_ERROR',
|
|
88
|
+
REPLAY_DETECTED: 'VG_REPLAY_DETECTED',
|
|
89
|
+
SSRF_BLOCKED: 'VG_SSRF_BLOCKED',
|
|
90
|
+
INTERNAL: 'VG_INTERNAL',
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
class VibeGuardError extends Error {
|
|
94
|
+
constructor(code, message, opts = {}) {
|
|
95
|
+
super(message || code);
|
|
96
|
+
this.name = 'VibeGuardError';
|
|
97
|
+
this.code = code || ERROR_CODES.INTERNAL;
|
|
98
|
+
this.status = opts.status || 500;
|
|
99
|
+
this.retryable = !!opts.retryable;
|
|
100
|
+
this.details = opts.details || null;
|
|
101
|
+
this.expose = opts.expose !== false;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
toJSON() {
|
|
105
|
+
return {
|
|
106
|
+
code: this.code,
|
|
107
|
+
message: this.expose ? this.message : 'internal error',
|
|
108
|
+
status: this.status,
|
|
109
|
+
retryable: this.retryable,
|
|
110
|
+
details: this.details,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function failClosed(code, message, opts) {
|
|
116
|
+
return new VibeGuardError(code, message, { status: 403, ...(opts || {}) });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function nowIso(clock) {
|
|
120
|
+
const d = clock && typeof clock.now === 'function' ? clock.now() : new Date();
|
|
121
|
+
return (d instanceof Date ? d : new Date(d)).toISOString();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function newId(prefix = 'id') {
|
|
125
|
+
return `${prefix}_${crypto.randomBytes(16).toString('hex')}`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function safeEqual(a, b) {
|
|
129
|
+
const ab = Buffer.from(String(a));
|
|
130
|
+
const bb = Buffer.from(String(b));
|
|
131
|
+
if (ab.length !== bb.length) return false;
|
|
132
|
+
return crypto.timingSafeEqual(ab, bb);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function assertPrincipal(principal) {
|
|
136
|
+
if (!principal || typeof principal !== 'string') {
|
|
137
|
+
throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'principal is required');
|
|
138
|
+
}
|
|
139
|
+
if (!/^(agent|human|service|mcp-server):[A-Za-z0-9._-]+\/[A-Za-z0-9._@-]+$/.test(principal)) {
|
|
140
|
+
throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, `invalid principal: ${principal}`);
|
|
141
|
+
}
|
|
142
|
+
return principal;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
module.exports = {
|
|
146
|
+
DECISION,
|
|
147
|
+
ISOLATION,
|
|
148
|
+
TRUST,
|
|
149
|
+
CAPABILITIES,
|
|
150
|
+
DATA_LABELS,
|
|
151
|
+
ERROR_CODES,
|
|
152
|
+
VibeGuardError,
|
|
153
|
+
failClosed,
|
|
154
|
+
nowIso,
|
|
155
|
+
newId,
|
|
156
|
+
safeEqual,
|
|
157
|
+
assertPrincipal,
|
|
158
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* src/platform/delegation.js — signed, attenuated multi-agent delegation.
|
|
5
|
+
*
|
|
6
|
+
* A parent can delegate a subset of its capabilities to a child, up to a
|
|
7
|
+
* maximum chain depth. Tokens are Ed25519 JWS with a delegation claim and are
|
|
8
|
+
* verified offline. This is a library primitive; no multi-agent runtime graph
|
|
9
|
+
* or cross-session store is claimed.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const crypto = require('crypto');
|
|
13
|
+
const { CAPABILITIES, ERROR_CODES, VibeGuardError, assertPrincipal } = require('./contracts');
|
|
14
|
+
const { generateKeyPair, loadPrivateKey, loadPublicKey, signJws, verifyJws } = require('./identity');
|
|
15
|
+
|
|
16
|
+
class DelegationGraph {
|
|
17
|
+
constructor(opts = {}) {
|
|
18
|
+
if (!opts.privateKey && !opts.publicKey) {
|
|
19
|
+
const pair = generateKeyPair();
|
|
20
|
+
this.privateKey = pair.privateKey;
|
|
21
|
+
this.publicKey = pair.publicKey;
|
|
22
|
+
} else {
|
|
23
|
+
this.privateKey = opts.privateKey ? loadPrivateKey(opts.privateKey) : null;
|
|
24
|
+
this.publicKey = opts.publicKey ? loadPublicKey(opts.publicKey) : crypto.createPublicKey(this.privateKey);
|
|
25
|
+
}
|
|
26
|
+
this.issuer = opts.issuer || 'human:local/operator';
|
|
27
|
+
this.tenant = opts.tenant || 'local';
|
|
28
|
+
this.keyId = opts.keyId || `delegation_${crypto.randomBytes(4).toString('hex')}`;
|
|
29
|
+
this.maxDepth = opts.maxDepth || 3;
|
|
30
|
+
this.now = opts.now || (() => new Date());
|
|
31
|
+
this.edges = new Map();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
delegate({ parent, child, capabilities, depth = 0, parentCapabilities = [] } = {}) {
|
|
35
|
+
if (!this.privateKey) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'delegation graph is verify-only');
|
|
36
|
+
assertPrincipal(parent);
|
|
37
|
+
assertPrincipal(child);
|
|
38
|
+
if (parent === child) throw new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, 'self-delegation is not allowed');
|
|
39
|
+
if (!Array.isArray(capabilities) || !capabilities.length) throw new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, 'capabilities are required');
|
|
40
|
+
for (const c of capabilities) {
|
|
41
|
+
if (!CAPABILITIES.includes(c)) throw new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, `unknown capability: ${c}`);
|
|
42
|
+
if (Array.isArray(parentCapabilities) && parentCapabilities.length && !parentCapabilities.includes(c)) {
|
|
43
|
+
throw new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, `capability escalation: ${c}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (depth + 1 > this.maxDepth) throw new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, 'delegation depth exceeded');
|
|
47
|
+
const now = Math.floor(this.now().getTime() / 1000);
|
|
48
|
+
const payload = {
|
|
49
|
+
iss: this.issuer, sub: child, parent, aud: 'vibeguard-delegation', iat: now, exp: now + 300,
|
|
50
|
+
jti: crypto.randomBytes(12).toString('base64url'), tenant: this.tenant, capabilities: [...new Set(capabilities)], depth: depth + 1,
|
|
51
|
+
};
|
|
52
|
+
const token = signJws({ alg: 'EdDSA', typ: 'VGDEL', kid: this.keyId }, payload, this.privateKey);
|
|
53
|
+
this.edges.set(`${parent}->${child}`, { parent, child, capabilities: [...capabilities], depth: depth + 1 });
|
|
54
|
+
return { token, child, capabilities: payload.capabilities, depth: payload.depth };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
verify(token, { requiredCapabilities = [], expectedIssuer } = {}) {
|
|
58
|
+
const decoded = verifyJws(token, this.publicKey);
|
|
59
|
+
if (decoded.header.typ !== 'VGDEL' || decoded.header.alg !== 'EdDSA') throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'invalid delegation token');
|
|
60
|
+
const p = decoded.payload;
|
|
61
|
+
const now = Math.floor(this.now().getTime() / 1000);
|
|
62
|
+
if (expectedIssuer && p.iss !== expectedIssuer) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'delegation issuer mismatch');
|
|
63
|
+
if (p.aud !== 'vibeguard-delegation') throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'delegation audience mismatch');
|
|
64
|
+
if (!Number.isInteger(p.exp) || p.exp <= now) throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'delegation expired');
|
|
65
|
+
if (p.depth > this.maxDepth) throw new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, 'delegation depth exceeded');
|
|
66
|
+
for (const cap of requiredCapabilities) if (!p.capabilities.includes(cap)) throw new VibeGuardError(ERROR_CODES.AUTHORIZATION_DENIED, `delegation lacks capability ${cap}`);
|
|
67
|
+
return { child: p.sub, parent: p.parent, capabilities: p.capabilities, depth: p.depth, claims: p };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
edgesFor(principal) { return [...this.edges.values()].filter((e) => e.parent === principal || e.child === principal); }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
module.exports = { DelegationGraph };
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* src/platform/http-gateway.js — minimal authenticated Streamable HTTP MCP
|
|
5
|
+
* gateway. Accepts JSON-RPC POST bodies, authenticates VGAT or JWT, evaluates
|
|
6
|
+
* policy for tools/call, and delegates allowed messages to an injected handler.
|
|
7
|
+
*
|
|
8
|
+
* This is the first HTTP slice; full SSE/streamable event framing is a later
|
|
9
|
+
* adapter. It is deliberately loopback-first, size-limited and fail-closed.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const http = require('http');
|
|
13
|
+
const { ERROR_CODES, VibeGuardError } = require('./contracts');
|
|
14
|
+
|
|
15
|
+
const MAX_BODY = 1024 * 1024;
|
|
16
|
+
|
|
17
|
+
function readBody(req) {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
let size = 0;
|
|
20
|
+
const chunks = [];
|
|
21
|
+
req.on('data', (chunk) => {
|
|
22
|
+
size += chunk.length;
|
|
23
|
+
if (size > MAX_BODY) {
|
|
24
|
+
reject(new VibeGuardError(ERROR_CODES.LIMIT_EXCEEDED, 'request body exceeds 1 MiB', { status: 413 }));
|
|
25
|
+
req.destroy();
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
chunks.push(chunk);
|
|
29
|
+
});
|
|
30
|
+
req.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
|
|
31
|
+
req.on('error', reject);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function json(res, status, body) {
|
|
36
|
+
const payload = JSON.stringify(body);
|
|
37
|
+
res.writeHead(status, { 'content-type': 'application/json', 'content-length': Buffer.byteLength(payload) });
|
|
38
|
+
res.end(payload);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function authError(res, err) {
|
|
42
|
+
return json(res, err.status || 401, { jsonrpc: '2.0', error: { code: -32001, message: err.message }, id: null });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function createHttpGateway(opts = {}) {
|
|
46
|
+
const host = opts.host || '127.0.0.1';
|
|
47
|
+
const port = Number(opts.port || 0);
|
|
48
|
+
const authority = opts.authority;
|
|
49
|
+
const jwt = opts.jwt; // { jwks, issuer, audience, scopes }
|
|
50
|
+
const policy = opts.policy;
|
|
51
|
+
const handler = opts.handler;
|
|
52
|
+
if (typeof handler !== 'function') throw new Error('http gateway requires a message handler');
|
|
53
|
+
if (!policy) throw new Error('http gateway requires an authorization policy; refusing an unprotected listener');
|
|
54
|
+
if (typeof policy.evaluate !== 'function') throw new Error('http gateway policy must implement evaluate()');
|
|
55
|
+
|
|
56
|
+
async function authenticate(req) {
|
|
57
|
+
const auth = req.headers.authorization || '';
|
|
58
|
+
const token = auth.startsWith('Bearer ') ? auth.slice(7) : '';
|
|
59
|
+
if (!token) throw new VibeGuardError(ERROR_CODES.AUTH_REQUIRED, 'bearer token required', { status: 401 });
|
|
60
|
+
if (authority) {
|
|
61
|
+
const verified = authority.verify(token, opts.verifyOpts || {});
|
|
62
|
+
return verified.principal;
|
|
63
|
+
}
|
|
64
|
+
if (jwt) {
|
|
65
|
+
const { verifyJwt } = require('./jwt');
|
|
66
|
+
const verified = verifyJwt(token, jwt);
|
|
67
|
+
return { principalId: verified.claims.sub, claims: verified.claims };
|
|
68
|
+
}
|
|
69
|
+
throw new VibeGuardError(ERROR_CODES.AUTH_INVALID, 'no authenticator configured', { status: 500 });
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const server = http.createServer(async (req, res) => {
|
|
73
|
+
try {
|
|
74
|
+
if (req.method === 'GET' && req.url === '/health') {
|
|
75
|
+
return json(res, 200, { status: 'ok', gateway: 'vibeguard', version: 1 });
|
|
76
|
+
}
|
|
77
|
+
if (req.method === 'GET' && req.url === '/mcp' && typeof opts.onStream === 'function') {
|
|
78
|
+
const principal = await authenticate(req);
|
|
79
|
+
return opts.onStream(req, res, principal);
|
|
80
|
+
}
|
|
81
|
+
if (req.method !== 'POST' || req.url !== '/mcp') {
|
|
82
|
+
return json(res, 404, { error: 'not found' });
|
|
83
|
+
}
|
|
84
|
+
const principal = await authenticate(req);
|
|
85
|
+
let message;
|
|
86
|
+
try { message = JSON.parse(await readBody(req)); } catch {
|
|
87
|
+
return json(res, 400, { jsonrpc: '2.0', id: null, error: { code: -32700, message: 'invalid JSON' } });
|
|
88
|
+
}
|
|
89
|
+
if (Array.isArray(message)) {
|
|
90
|
+
return json(res, 400, { jsonrpc: '2.0', id: null, error: { code: -32600, message: 'batch messages are not supported by this gateway version' } });
|
|
91
|
+
}
|
|
92
|
+
if (message.method === 'tools/call' && policy) {
|
|
93
|
+
const params = message.params || {};
|
|
94
|
+
const decision = policy.evaluate({
|
|
95
|
+
principal: principal.principalId,
|
|
96
|
+
server: params.server || opts.server || 'http',
|
|
97
|
+
tool: params.name,
|
|
98
|
+
capability: params.capability || 'mcp.tool.invoke',
|
|
99
|
+
arguments: params.arguments || {},
|
|
100
|
+
sessionId: params.sessionId,
|
|
101
|
+
});
|
|
102
|
+
if (decision.decision === 'deny' || decision.decision === 'quarantine') {
|
|
103
|
+
return json(res, 403, {
|
|
104
|
+
jsonrpc: '2.0',
|
|
105
|
+
id: message.id,
|
|
106
|
+
error: { code: -32003, message: decision.reasonCode || 'authorization denied', data: { decision } },
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (decision.decision === 'approval_required') {
|
|
110
|
+
return json(res, 202, {
|
|
111
|
+
jsonrpc: '2.0',
|
|
112
|
+
id: message.id,
|
|
113
|
+
result: { approvalRequired: true, actionDigest: decision.actionDigest, reason: decision.reasonCode },
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
if (policy && message.method !== 'tools/call' && message.method !== 'initialize' && message.method !== 'ping') {
|
|
118
|
+
const caps = {
|
|
119
|
+
'resources/read': 'mcp.resource.read',
|
|
120
|
+
'prompts/get': 'mcp.prompt.get',
|
|
121
|
+
};
|
|
122
|
+
const capability = caps[message.method] || (message.method === 'tools/list' ? null : 'mcp.protocol.unknown');
|
|
123
|
+
if (capability) {
|
|
124
|
+
const params = message.params || {};
|
|
125
|
+
const decision = policy.evaluate({
|
|
126
|
+
principal: principal.principalId,
|
|
127
|
+
server: params.server || opts.server || 'http',
|
|
128
|
+
tool: params.name || message.method,
|
|
129
|
+
capability,
|
|
130
|
+
arguments: params.arguments || {},
|
|
131
|
+
sessionId: params.sessionId,
|
|
132
|
+
});
|
|
133
|
+
if (decision.decision === 'deny' || decision.decision === 'quarantine') {
|
|
134
|
+
return json(res, 403, {
|
|
135
|
+
jsonrpc: '2.0',
|
|
136
|
+
id: message.id,
|
|
137
|
+
error: { code: -32003, message: decision.reasonCode || 'authorization denied', data: { decision } },
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
if (decision.decision === 'approval_required') {
|
|
141
|
+
return json(res, 202, { jsonrpc: '2.0', id: message.id, result: { approvalRequired: true, actionDigest: decision.actionDigest } });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const result = await handler(message, principal, { req, res });
|
|
146
|
+
if (result !== undefined) return json(res, 200, { jsonrpc: '2.0', id: message.id, result });
|
|
147
|
+
if (!res.writableEnded) return json(res, 200, { jsonrpc: '2.0', id: message.id, result: null });
|
|
148
|
+
} catch (err) {
|
|
149
|
+
if (err instanceof VibeGuardError) {
|
|
150
|
+
if (err.code === ERROR_CODES.AUTH_REQUIRED || err.code === ERROR_CODES.AUTH_INVALID || err.code === ERROR_CODES.REPLAY_DETECTED) {
|
|
151
|
+
return authError(res, err);
|
|
152
|
+
}
|
|
153
|
+
return json(res, err.status || 400, { jsonrpc: '2.0', id: null, error: { code: -32000, message: err.message } });
|
|
154
|
+
}
|
|
155
|
+
return json(res, 500, { jsonrpc: '2.0', id: null, error: { code: -32603, message: 'internal error' } });
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
return new Promise((resolve, reject) => {
|
|
160
|
+
server.once('error', reject);
|
|
161
|
+
server.listen(port, host, () => {
|
|
162
|
+
const addr = server.address();
|
|
163
|
+
resolve({
|
|
164
|
+
server,
|
|
165
|
+
host: addr.address,
|
|
166
|
+
port: addr.port,
|
|
167
|
+
url: `http://${addr.address}:${addr.port}`,
|
|
168
|
+
close: () => new Promise((res2) => {
|
|
169
|
+
try { if (typeof server.closeAllConnections === 'function') server.closeAllConnections(); } catch { /* ignore */ }
|
|
170
|
+
server.close(res2);
|
|
171
|
+
}),
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
module.exports = { createHttpGateway, MAX_BODY };
|