@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/llm-proxy.js
ADDED
|
@@ -0,0 +1,986 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* VibeGuard LLM Proxy — OpenAI-compatible runtime guard proxy.
|
|
5
|
+
*
|
|
6
|
+
* Any LLM client (OpenAI SDK, LangChain, Claude Code, Cursor, your own app)
|
|
7
|
+
* points base_url at http://127.0.0.1:<port>/v1. VibeGuard scans every prompt
|
|
8
|
+
* AND every response for prompt-injection, secrets, and PII, then either:
|
|
9
|
+
*
|
|
10
|
+
* block — reject violating requests with 403 (never forwarded upstream)
|
|
11
|
+
* redact — mask secrets/PII with [REDACTED:<ruleId>], forward everything
|
|
12
|
+
* report — never mutate; log every violation and pass through
|
|
13
|
+
*
|
|
14
|
+
* then forwards the (possibly mutated) request to the real upstream
|
|
15
|
+
* (e.g. https://api.openai.com/v1). This is the runtime "AI agent firewall"
|
|
16
|
+
* half of VibeGuard — Lakera Guard parity, 100% local, free, zero deps.
|
|
17
|
+
*
|
|
18
|
+
* Reuses VibeGuard's own detectors:
|
|
19
|
+
* - detectPII / redactText from ./pii (same as src/proxy.js)
|
|
20
|
+
* - secret.* rule regexes from ./rules (user prompt containing a hardcoded
|
|
21
|
+
* GitHub token is exfiltration)
|
|
22
|
+
* - the canonical prompt-injection regex from rules.js
|
|
23
|
+
* (ai.prompt-injection-marker) plus a few classic supplements.
|
|
24
|
+
*
|
|
25
|
+
* Zero runtime dependencies: only http/https from stdlib.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const http = require('http');
|
|
29
|
+
const https = require('https');
|
|
30
|
+
const { URL } = require('url');
|
|
31
|
+
const { detectPII } = require('./pii');
|
|
32
|
+
const rules = require('./rules');
|
|
33
|
+
|
|
34
|
+
const DEFAULT_PORT = 8443;
|
|
35
|
+
const DEFAULT_UPSTREAM = 'https://api.openai.com/v1';
|
|
36
|
+
const MODES = new Set(['block', 'redact', 'report']);
|
|
37
|
+
const IDLE_TIMEOUT_MS = 60000;
|
|
38
|
+
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Prompt-injection detection
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
// Extract prompt-injection regexes from src/rules.js: the canonical
|
|
44
|
+
// ai.prompt-injection-marker rule, plus any ai.* rule whose id mentions
|
|
45
|
+
// injection. (Code-oriented ai.* rules like ai.eval-llm-output are NOT
|
|
46
|
+
// text-injection patterns and are deliberately excluded.)
|
|
47
|
+
const INJECTION_FROM_RULES = [];
|
|
48
|
+
for (const r of rules.lineRules || []) {
|
|
49
|
+
const id = r.id || '';
|
|
50
|
+
if (!r.re || typeof r.re.test !== 'function') continue;
|
|
51
|
+
if (id === 'ai.prompt-injection-marker' || (id.startsWith('ai.') && /injection/i.test(id))) {
|
|
52
|
+
INJECTION_FROM_RULES.push({
|
|
53
|
+
ruleId: id,
|
|
54
|
+
severity: r.severity || 'high',
|
|
55
|
+
message: r.message || r.title || id,
|
|
56
|
+
re: r.re,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Classic supplemental patterns (system-prompt override attempts, output
|
|
62
|
+
// format manipulation). These must be global-capable regexes — detectInjection
|
|
63
|
+
// clones them with the 'g' flag so exec() can find every hit.
|
|
64
|
+
const INJECTION_PATTERNS = [
|
|
65
|
+
...INJECTION_FROM_RULES,
|
|
66
|
+
{
|
|
67
|
+
ruleId: 'ai.system-prompt-reveal',
|
|
68
|
+
severity: 'high',
|
|
69
|
+
message: 'Request to reveal the system/developer prompt',
|
|
70
|
+
re: /(?:reveal|show|print|display|output|leak|repeat)\s+(?:your|the)\s+(?:full\s+)?(?:system|developer|initial)\s+prompt/i,
|
|
71
|
+
},
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Detect prompt-injection / jailbreak phrases in free text.
|
|
76
|
+
* @param {string} text
|
|
77
|
+
* @returns {Array<{type:'prompt-injection', ruleId, severity, match, start, end, message}>}
|
|
78
|
+
*/
|
|
79
|
+
function detectInjection(text) {
|
|
80
|
+
const out = [];
|
|
81
|
+
if (typeof text !== 'string' || text.length === 0) return out;
|
|
82
|
+
for (const p of INJECTION_PATTERNS) {
|
|
83
|
+
if (!p._g) {
|
|
84
|
+
p._g = new RegExp(p.re.source, p.re.flags.includes('g') ? p.re.flags : p.re.flags + 'g');
|
|
85
|
+
}
|
|
86
|
+
p._g.lastIndex = 0;
|
|
87
|
+
let m;
|
|
88
|
+
while ((m = p._g.exec(text)) !== null) {
|
|
89
|
+
out.push({
|
|
90
|
+
type: 'prompt-injection',
|
|
91
|
+
ruleId: p.ruleId,
|
|
92
|
+
severity: p.severity || 'high',
|
|
93
|
+
match: m[0],
|
|
94
|
+
start: m.index,
|
|
95
|
+
end: m.index + m[0].length,
|
|
96
|
+
message: p.message || p.ruleId,
|
|
97
|
+
});
|
|
98
|
+
if (m.index === p._g.lastIndex) p._g.lastIndex++; // guard zero-width
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ---------------------------------------------------------------------------
|
|
105
|
+
// Secret detection (reuses secret.* regexes from src/rules.js, including
|
|
106
|
+
// their post-match filters)
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Scan text with every secret.* rule from rules.js.
|
|
111
|
+
* @param {string} text
|
|
112
|
+
* @returns {Array<{type:'secret', ruleId, severity, match, start, end, message}>}
|
|
113
|
+
*/
|
|
114
|
+
function scanSecrets(text) {
|
|
115
|
+
const out = [];
|
|
116
|
+
if (typeof text !== 'string' || text.length === 0) return out;
|
|
117
|
+
for (const rule of rules.secretRules || []) {
|
|
118
|
+
if (!rule.re) continue;
|
|
119
|
+
let re;
|
|
120
|
+
try {
|
|
121
|
+
re = new RegExp(rule.re.source, rule.re.flags.includes('g') ? rule.re.flags : rule.re.flags + 'g');
|
|
122
|
+
} catch {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
re.lastIndex = 0;
|
|
126
|
+
let m;
|
|
127
|
+
while ((m = re.exec(text)) !== null) {
|
|
128
|
+
const mm = { index: m.index, match: m[0], groups: m };
|
|
129
|
+
if (typeof rule.filter === 'function') {
|
|
130
|
+
try {
|
|
131
|
+
if (!rule.filter(text, mm)) {
|
|
132
|
+
if (m.index === re.lastIndex) re.lastIndex++;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
} catch {
|
|
136
|
+
/* keep the match — never let a filter crash the guard */
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
out.push({
|
|
140
|
+
type: 'secret',
|
|
141
|
+
ruleId: rule.id,
|
|
142
|
+
severity: rule.severity || 'critical',
|
|
143
|
+
match: m[0],
|
|
144
|
+
start: m.index,
|
|
145
|
+
end: m.index + m[0].length,
|
|
146
|
+
message: rule.message || rule.title || rule.id,
|
|
147
|
+
});
|
|
148
|
+
if (m.index === re.lastIndex) re.lastIndex++;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return out;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ---------------------------------------------------------------------------
|
|
155
|
+
// PII detection (reuses detectPII from ./pii)
|
|
156
|
+
// ---------------------------------------------------------------------------
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Scan text for PII via pii.js.
|
|
160
|
+
* @param {string} text
|
|
161
|
+
* @returns {Array<{type:'pii', ruleId, severity, match, start, end, token, message}>}
|
|
162
|
+
*/
|
|
163
|
+
function scanPII(text) {
|
|
164
|
+
const out = [];
|
|
165
|
+
if (typeof text !== 'string' || text.length === 0) return out;
|
|
166
|
+
const det = detectPII(text);
|
|
167
|
+
for (const m of det.matches) {
|
|
168
|
+
out.push({
|
|
169
|
+
type: 'pii',
|
|
170
|
+
ruleId: 'pii.' + m.type,
|
|
171
|
+
severity: m.severity,
|
|
172
|
+
match: m.value,
|
|
173
|
+
start: m.start,
|
|
174
|
+
end: m.end,
|
|
175
|
+
token: m.token,
|
|
176
|
+
message: 'PII detected: ' + m.type,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
return out;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* All violations in one pass: prompt-injection + secrets + PII.
|
|
184
|
+
* @param {string} text
|
|
185
|
+
* @returns {Array}
|
|
186
|
+
*/
|
|
187
|
+
function scanText(text) {
|
|
188
|
+
return detectInjection(text).concat(scanSecrets(text), scanPII(text));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Redact secrets/PII from text, replacing each full match with
|
|
193
|
+
* [REDACTED:<ruleId>] (pii.js-style). Injection is never redacted — it is
|
|
194
|
+
* detected and reported, but passes through (redacting injection is
|
|
195
|
+
* meaningless).
|
|
196
|
+
* @param {string} text
|
|
197
|
+
* @returns {{text: string, violations: Array}}
|
|
198
|
+
*/
|
|
199
|
+
function redactSensitive(text) {
|
|
200
|
+
const violations = [];
|
|
201
|
+
if (typeof text !== 'string' || text.length === 0) return { text: text || '', violations };
|
|
202
|
+
|
|
203
|
+
const matches = [];
|
|
204
|
+
for (const s of scanSecrets(text)) {
|
|
205
|
+
matches.push({ start: s.start, end: s.end, token: '[REDACTED:' + s.ruleId + ']', violation: s });
|
|
206
|
+
}
|
|
207
|
+
for (const p of scanPII(text)) {
|
|
208
|
+
matches.push({ start: p.start, end: p.end, token: '[REDACTED:' + p.ruleId + ']', violation: p });
|
|
209
|
+
}
|
|
210
|
+
if (matches.length === 0) return { text, violations };
|
|
211
|
+
|
|
212
|
+
// Non-overlapping, longest-first at equal start (same policy as pii.js).
|
|
213
|
+
matches.sort((a, b) => a.start - b.start || (b.end - b.start) - (a.end - a.start));
|
|
214
|
+
const kept = [];
|
|
215
|
+
let lastEnd = -1;
|
|
216
|
+
for (const mm of matches) {
|
|
217
|
+
if (mm.start < lastEnd) continue;
|
|
218
|
+
kept.push(mm);
|
|
219
|
+
lastEnd = mm.end;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
let out = text;
|
|
223
|
+
for (let i = kept.length - 1; i >= 0; i--) {
|
|
224
|
+
const mm = kept[i];
|
|
225
|
+
out = out.slice(0, mm.start) + mm.token + out.slice(mm.end);
|
|
226
|
+
violations.push(mm.violation);
|
|
227
|
+
}
|
|
228
|
+
return { text: out, violations };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// ---------------------------------------------------------------------------
|
|
232
|
+
// Structured scanning helpers (messages / tool-call arguments)
|
|
233
|
+
// ---------------------------------------------------------------------------
|
|
234
|
+
|
|
235
|
+
// Recursively collect every string value in a parsed JSON structure
|
|
236
|
+
// (tool-call arguments — indirect injection lives in the string values).
|
|
237
|
+
function collectStringValues(node, out) {
|
|
238
|
+
if (typeof node === 'string') {
|
|
239
|
+
out.push(node);
|
|
240
|
+
} else if (Array.isArray(node)) {
|
|
241
|
+
for (const item of node) collectStringValues(item, out);
|
|
242
|
+
} else if (node && typeof node === 'object') {
|
|
243
|
+
for (const k of Object.keys(node)) collectStringValues(node[k], out);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Scan (and in redact mode, mutate) a tool-call arguments JSON string.
|
|
249
|
+
* Falls back to scanning the raw string if it does not parse.
|
|
250
|
+
* @returns {{violations: Array, text: string}}
|
|
251
|
+
*/
|
|
252
|
+
function scanToolArguments(argStr, mode) {
|
|
253
|
+
const violations = [];
|
|
254
|
+
let obj = null;
|
|
255
|
+
try {
|
|
256
|
+
obj = JSON.parse(argStr);
|
|
257
|
+
} catch {
|
|
258
|
+
obj = null;
|
|
259
|
+
}
|
|
260
|
+
if (obj && typeof obj === 'object') {
|
|
261
|
+
const values = [];
|
|
262
|
+
collectStringValues(obj, values);
|
|
263
|
+
for (const v of values) {
|
|
264
|
+
const r = scanTextMode(v, mode);
|
|
265
|
+
violations.push(...r.violations);
|
|
266
|
+
}
|
|
267
|
+
if (mode === 'redact' && violations.length) {
|
|
268
|
+
// Re-walk and mutate in place (only secret/PII violations mutate).
|
|
269
|
+
mutateStrings(obj, mode);
|
|
270
|
+
return { violations, text: JSON.stringify(obj) };
|
|
271
|
+
}
|
|
272
|
+
return { violations, text: argStr };
|
|
273
|
+
}
|
|
274
|
+
const r = scanTextMode(argStr, mode);
|
|
275
|
+
return { violations: r.violations, text: r.text };
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// Mutate string leaves in place (redact mode).
|
|
279
|
+
function mutateStrings(node, mode) {
|
|
280
|
+
if (typeof node === 'string') {
|
|
281
|
+
return redactSensitive(node).text;
|
|
282
|
+
}
|
|
283
|
+
if (Array.isArray(node)) {
|
|
284
|
+
for (let i = 0; i < node.length; i++) node[i] = mutateStrings(node[i], mode);
|
|
285
|
+
return node;
|
|
286
|
+
}
|
|
287
|
+
if (node && typeof node === 'object') {
|
|
288
|
+
for (const k of Object.keys(node)) node[k] = mutateStrings(node[k], mode);
|
|
289
|
+
return node;
|
|
290
|
+
}
|
|
291
|
+
return node;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Scan one piece of text in the given mode.
|
|
296
|
+
* block/report: detect everything, no mutation.
|
|
297
|
+
* redact: detect everything, mutate secrets/PII in place.
|
|
298
|
+
* @returns {{violations: Array, text: string}}
|
|
299
|
+
*/
|
|
300
|
+
function scanTextMode(text, mode) {
|
|
301
|
+
const violations = detectInjection(text);
|
|
302
|
+
let out = text;
|
|
303
|
+
if (mode === 'redact') {
|
|
304
|
+
const r = redactSensitive(text);
|
|
305
|
+
violations.push(...r.violations);
|
|
306
|
+
out = r.text;
|
|
307
|
+
} else {
|
|
308
|
+
violations.push(...scanSecrets(text), ...scanPII(text));
|
|
309
|
+
}
|
|
310
|
+
return { violations, text: out };
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Scan a /v1/chat/completions request body. In redact mode the body is
|
|
315
|
+
* mutated in place (message content + tool-call arguments).
|
|
316
|
+
* @returns {Array} violations
|
|
317
|
+
*/
|
|
318
|
+
function scanChatCompletions(body, mode) {
|
|
319
|
+
const violations = [];
|
|
320
|
+
const messages = body && Array.isArray(body.messages) ? body.messages : [];
|
|
321
|
+
for (const msg of messages) {
|
|
322
|
+
if (!msg || typeof msg !== 'object') continue;
|
|
323
|
+
|
|
324
|
+
if (typeof msg.content === 'string') {
|
|
325
|
+
const r = scanTextMode(msg.content, mode);
|
|
326
|
+
violations.push(...r.violations);
|
|
327
|
+
if (mode === 'redact' && r.text !== msg.content) msg.content = r.text;
|
|
328
|
+
} else if (Array.isArray(msg.content)) {
|
|
329
|
+
// content as an array of parts ({type:'text', text:'...'})
|
|
330
|
+
for (const part of msg.content) {
|
|
331
|
+
if (part && typeof part.text === 'string') {
|
|
332
|
+
const r = scanTextMode(part.text, mode);
|
|
333
|
+
violations.push(...r.violations);
|
|
334
|
+
if (mode === 'redact' && r.text !== part.text) part.text = r.text;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// Assistant tool_calls carry JSON-string arguments — indirect injection.
|
|
340
|
+
if (Array.isArray(msg.tool_calls)) {
|
|
341
|
+
for (const tc of msg.tool_calls) {
|
|
342
|
+
const fn = tc && tc.function;
|
|
343
|
+
if (fn && typeof fn.arguments === 'string') {
|
|
344
|
+
const r = scanToolArguments(fn.arguments, mode);
|
|
345
|
+
violations.push(...r.violations);
|
|
346
|
+
if (mode === 'redact' && r.text !== fn.arguments) fn.arguments = r.text;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return violations;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Scan a string/array-of-strings field (prompt for /v1/completions, input
|
|
356
|
+
* for /v1/embeddings). Redact mode mutates in place. Numeric arrays (token
|
|
357
|
+
* ids) are ignored.
|
|
358
|
+
* @returns {Array} violations
|
|
359
|
+
*/
|
|
360
|
+
function scanTextField(body, field, mode) {
|
|
361
|
+
const violations = [];
|
|
362
|
+
if (!body || body[field] === undefined) return violations;
|
|
363
|
+
const value = body[field];
|
|
364
|
+
const targets = [];
|
|
365
|
+
if (typeof value === 'string') targets.push(value);
|
|
366
|
+
else if (Array.isArray(value)) {
|
|
367
|
+
for (const item of value) if (typeof item === 'string') targets.push(item);
|
|
368
|
+
}
|
|
369
|
+
if (mode === 'redact' && typeof value === 'string') {
|
|
370
|
+
const r = scanTextMode(value, mode);
|
|
371
|
+
violations.push(...r.violations);
|
|
372
|
+
if (r.text !== value) body[field] = r.text;
|
|
373
|
+
} else if (mode === 'redact' && Array.isArray(value)) {
|
|
374
|
+
for (let i = 0; i < value.length; i++) {
|
|
375
|
+
if (typeof value[i] === 'string') {
|
|
376
|
+
const r = scanTextMode(value[i], mode);
|
|
377
|
+
violations.push(...r.violations);
|
|
378
|
+
if (r.text !== value[i]) value[i] = r.text;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
} else {
|
|
382
|
+
for (const t of targets) violations.push(...scanTextMode(t, mode).violations);
|
|
383
|
+
}
|
|
384
|
+
return violations;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Scan a chat-completion RESPONSE body (choices[].message.content +
|
|
389
|
+
* tool_calls[].function.arguments — LLMs can leak secrets they were trained
|
|
390
|
+
* on or received in context). Redact mode mutates in place.
|
|
391
|
+
* @returns {Array} violations
|
|
392
|
+
*/
|
|
393
|
+
function scanChatResponse(body, mode, systemPromptLines) {
|
|
394
|
+
const violations = [];
|
|
395
|
+
const choices = body && Array.isArray(body.choices) ? body.choices : [];
|
|
396
|
+
for (const choice of choices) {
|
|
397
|
+
const message = choice && choice.message;
|
|
398
|
+
if (!message || typeof message !== 'object') continue;
|
|
399
|
+
if (typeof message.content === 'string') {
|
|
400
|
+
const r = scanTextMode(message.content, mode);
|
|
401
|
+
violations.push(...r.violations);
|
|
402
|
+
if (mode === 'redact' && r.text !== message.content) message.content = r.text;
|
|
403
|
+
violations.push(...detectSystemPromptLeak(message.content, systemPromptLines));
|
|
404
|
+
}
|
|
405
|
+
if (Array.isArray(message.tool_calls)) {
|
|
406
|
+
for (const tc of message.tool_calls) {
|
|
407
|
+
const fn = tc && tc.function;
|
|
408
|
+
if (fn && typeof fn.arguments === 'string') {
|
|
409
|
+
const r = scanToolArguments(fn.arguments, mode);
|
|
410
|
+
violations.push(...r.violations);
|
|
411
|
+
if (mode === 'redact' && r.text !== fn.arguments) fn.arguments = r.text;
|
|
412
|
+
violations.push(...detectSystemPromptLeak(fn.arguments, systemPromptLines));
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return violations;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* System-prompt leakage: the model echoed a line from YOUR system prompt
|
|
422
|
+
* (Lakera does this via custom guardrails; we do it deterministically).
|
|
423
|
+
* Any trimmed line >= 16 chars from the system-prompt file that appears in
|
|
424
|
+
* the output is a leak signal. FP-safe by construction (it's your own text).
|
|
425
|
+
*/
|
|
426
|
+
function detectSystemPromptLeak(text, systemPromptLines) {
|
|
427
|
+
const violations = [];
|
|
428
|
+
if (!Array.isArray(systemPromptLines) || systemPromptLines.length === 0) return violations;
|
|
429
|
+
if (typeof text !== 'string' || text.length === 0) return violations;
|
|
430
|
+
const textFlat = text.replace(/\s+/g, ' ');
|
|
431
|
+
for (const line of systemPromptLines) {
|
|
432
|
+
if (!line) continue;
|
|
433
|
+
// exact hit on raw text, or whitespace-drift tolerant hit (both sides collapsed)
|
|
434
|
+
const flat = line.replace(/\s+/g, ' ');
|
|
435
|
+
const hit = text.includes(line) || (flat.length >= 16 && textFlat.includes(flat));
|
|
436
|
+
if (hit) {
|
|
437
|
+
violations.push({
|
|
438
|
+
type: 'system-prompt-leak', ruleId: 'ai.system-prompt-leak', severity: 'high',
|
|
439
|
+
start: textFlat.indexOf(flat), match: maskSnippet(flat),
|
|
440
|
+
message: 'The model echoed a line from your system prompt — possible prompt-leakage exfiltration.',
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return violations;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
// ---------------------------------------------------------------------------
|
|
448
|
+
// Violation bookkeeping
|
|
449
|
+
// ---------------------------------------------------------------------------
|
|
450
|
+
|
|
451
|
+
function maskSnippet(s) {
|
|
452
|
+
const str = String(s);
|
|
453
|
+
if (str.length <= 12) return '***';
|
|
454
|
+
return str.slice(0, 4) + '…' + str.slice(-4);
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// ---------------------------------------------------------------------------
|
|
458
|
+
// The guard proxy server
|
|
459
|
+
// ---------------------------------------------------------------------------
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Create and start the guard proxy.
|
|
463
|
+
* @param {object} opts
|
|
464
|
+
* @param {number} [opts.port=0] listen port (0 = ephemeral)
|
|
465
|
+
* @param {string} [opts.upstream] upstream base URL (default https://api.openai.com/v1)
|
|
466
|
+
* @param {string} [opts.mode='block'] 'block' | 'redact' | 'report'
|
|
467
|
+
* @param {string} [opts.apiKey] Bearer key sent to upstream (default: forward client's)
|
|
468
|
+
* @param {Function} [opts.onViolation] callback(violation)
|
|
469
|
+
* @param {boolean} [opts.quiet] suppress console.error violation logs
|
|
470
|
+
* @returns {{server, port, close(), violations: Array}}
|
|
471
|
+
*/
|
|
472
|
+
function createGuardProxy(opts = {}) {
|
|
473
|
+
const mode = MODES.has(opts.mode) ? opts.mode : 'block';
|
|
474
|
+
const systemPromptLines = Array.isArray(opts.systemPromptLines) ? opts.systemPromptLines : [];
|
|
475
|
+
const upstreamBase = (opts.upstream || DEFAULT_UPSTREAM).replace(/\/+$/, '');
|
|
476
|
+
const apiKey = opts.apiKey || null;
|
|
477
|
+
const quiet = !!opts.quiet;
|
|
478
|
+
|
|
479
|
+
const violations = [];
|
|
480
|
+
const inflight = new Set(); // upstream requests in flight (for close())
|
|
481
|
+
|
|
482
|
+
function logViolation(base, location) {
|
|
483
|
+
const v = Object.assign({}, base, {
|
|
484
|
+
timestamp: new Date().toISOString(),
|
|
485
|
+
location: location || 'request',
|
|
486
|
+
snippet: maskSnippet(base.match),
|
|
487
|
+
});
|
|
488
|
+
violations.push(v);
|
|
489
|
+
if (typeof opts.onViolation === 'function') {
|
|
490
|
+
try {
|
|
491
|
+
opts.onViolation(v);
|
|
492
|
+
} catch {
|
|
493
|
+
/* callback must never break the guard */
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
if (!quiet) {
|
|
497
|
+
console.error('[llm-proxy] ' + v.timestamp + ' ' + v.type + ' ' + v.ruleId + ' ' + v.snippet);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
function blockedMessage(v) {
|
|
502
|
+
return 'vibeguard: blocked ' + v.type + ': ' + (v.ruleId || v.message || 'violation');
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// Resolve the upstream target for a client request path. Upstream base may
|
|
506
|
+
// end in /v1 (https://api.openai.com/v1) — in that case strip the client's
|
|
507
|
+
// leading /v1 so the final path is /v1/chat/completions, not /v1/v1/...
|
|
508
|
+
function upstreamTarget(clientPath) {
|
|
509
|
+
let up;
|
|
510
|
+
try {
|
|
511
|
+
up = new URL(upstreamBase);
|
|
512
|
+
} catch {
|
|
513
|
+
return null;
|
|
514
|
+
}
|
|
515
|
+
let path = clientPath;
|
|
516
|
+
if (up.pathname.replace(/\/+$/, '') === '/v1' && path.startsWith('/v1')) {
|
|
517
|
+
path = path.slice(3) || '/';
|
|
518
|
+
}
|
|
519
|
+
const targetPath = up.pathname.replace(/\/+$/, '') + path;
|
|
520
|
+
return {
|
|
521
|
+
hostname: up.hostname,
|
|
522
|
+
port: up.port ? Number(up.port) : up.protocol === 'https:' ? 443 : 80,
|
|
523
|
+
path: targetPath || '/',
|
|
524
|
+
transport: up.protocol === 'https:' ? https : http,
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
function forwardHeaders(headers, withBody) {
|
|
529
|
+
const out = {};
|
|
530
|
+
for (const k of Object.keys(headers || {})) {
|
|
531
|
+
const lk = k.toLowerCase();
|
|
532
|
+
if (lk === 'host' || lk === 'connection' || lk === 'content-length' ||
|
|
533
|
+
lk === 'transfer-encoding' || lk === 'accept-encoding' ||
|
|
534
|
+
lk === 'content-encoding' || lk === 'expect') {
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
out[k] = headers[k];
|
|
538
|
+
}
|
|
539
|
+
if (apiKey) out.authorization = 'Bearer ' + apiKey;
|
|
540
|
+
else if (!out.authorization) delete out.authorization;
|
|
541
|
+
if (withBody) out['content-type'] = 'application/json';
|
|
542
|
+
return out;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
function respondJson(res, status, obj) {
|
|
546
|
+
res.writeHead(status, { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' });
|
|
547
|
+
res.end(JSON.stringify(obj));
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// ── POST /v1/chat/completions | /v1/completions | /v1/embeddings ─────────
|
|
551
|
+
function handlePost(req, res, urlPath) {
|
|
552
|
+
const chunks = [];
|
|
553
|
+
req.on('data', (c) => chunks.push(c));
|
|
554
|
+
req.on('end', () => {
|
|
555
|
+
let body = null;
|
|
556
|
+
const raw = Buffer.concat(chunks).toString('utf8');
|
|
557
|
+
try {
|
|
558
|
+
body = JSON.parse(raw || '{}');
|
|
559
|
+
} catch {
|
|
560
|
+
respondJson(res, 400, { error: { message: 'vibeguard: invalid JSON body' } });
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
if (!body || typeof body !== 'object' || Array.isArray(body)) {
|
|
564
|
+
respondJson(res, 400, { error: { message: 'vibeguard: invalid JSON body' } });
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// ── REQUEST SCAN ──────────────────────────────────────────────────
|
|
569
|
+
let reqViolations = [];
|
|
570
|
+
if (urlPath === '/v1/chat/completions') reqViolations = scanChatCompletions(body, mode);
|
|
571
|
+
else if (urlPath === '/v1/completions') reqViolations = scanTextField(body, 'prompt', mode);
|
|
572
|
+
else if (urlPath === '/v1/embeddings') reqViolations = scanTextField(body, 'input', mode);
|
|
573
|
+
|
|
574
|
+
for (const v of reqViolations) logViolation(v, 'request');
|
|
575
|
+
|
|
576
|
+
if (mode === 'block' && reqViolations.length > 0) {
|
|
577
|
+
// DO NOT call upstream.
|
|
578
|
+
respondJson(res, 403, { error: { message: blockedMessage(reqViolations[0]) } });
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// ── FORWARD ───────────────────────────────────────────────────────
|
|
583
|
+
const target = upstreamTarget(req.url);
|
|
584
|
+
if (!target) {
|
|
585
|
+
respondJson(res, 502, { error: { message: 'vibeguard: invalid upstream URL' } });
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
const outBody = Buffer.from(JSON.stringify(body));
|
|
590
|
+
const upstreamReq = target.transport.request(
|
|
591
|
+
{
|
|
592
|
+
hostname: target.hostname,
|
|
593
|
+
port: target.port,
|
|
594
|
+
path: target.path,
|
|
595
|
+
method: req.method,
|
|
596
|
+
headers: forwardHeaders(req.headers, true),
|
|
597
|
+
agent: false, // one connection per request — no keep-alive reuse after stream destroys
|
|
598
|
+
},
|
|
599
|
+
(upstreamRes) => {
|
|
600
|
+
inflight.delete(upstreamReq);
|
|
601
|
+
const isSse = /text\/event-stream/i.test(upstreamRes.headers['content-type'] || '');
|
|
602
|
+
if ((body.stream === true || isSse) && upstreamRes.statusCode >= 200 && upstreamRes.statusCode < 300) {
|
|
603
|
+
handleStreamingResponse(upstreamReq, upstreamRes, res);
|
|
604
|
+
} else if (upstreamRes.statusCode >= 200 && upstreamRes.statusCode < 300) {
|
|
605
|
+
handleJsonResponse(upstreamReq, upstreamRes, res);
|
|
606
|
+
} else {
|
|
607
|
+
// Upstream error (4xx/5xx) — pass through untouched.
|
|
608
|
+
const h = {};
|
|
609
|
+
for (const k of Object.keys(upstreamRes.headers)) {
|
|
610
|
+
if (k.toLowerCase() !== 'content-length') h[k] = upstreamRes.headers[k];
|
|
611
|
+
}
|
|
612
|
+
res.writeHead(upstreamRes.statusCode || 502, h);
|
|
613
|
+
upstreamRes.pipe(res);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
);
|
|
617
|
+
|
|
618
|
+
upstreamReq.setTimeout(IDLE_TIMEOUT_MS, () => {
|
|
619
|
+
upstreamReq.destroy(new Error('vibeguard: upstream timeout'));
|
|
620
|
+
});
|
|
621
|
+
upstreamReq.on('error', (err) => {
|
|
622
|
+
inflight.delete(upstreamReq);
|
|
623
|
+
const timedOut = err && /timeout/i.test(err.message);
|
|
624
|
+
if (res.headersSent) {
|
|
625
|
+
res.destroy();
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
respondJson(res, timedOut ? 504 : 502, {
|
|
629
|
+
error: { message: 'vibeguard: ' + (timedOut ? 'upstream timeout' : 'upstream error: ' + (err && err.message ? err.message : err)) },
|
|
630
|
+
});
|
|
631
|
+
});
|
|
632
|
+
inflight.add(upstreamReq);
|
|
633
|
+
upstreamReq.end(outBody);
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// ── Non-streaming upstream response: buffer, scan, maybe redact ────────
|
|
638
|
+
function handleJsonResponse(upstreamReq, upstreamRes, res) {
|
|
639
|
+
const chunks = [];
|
|
640
|
+
upstreamRes.on('data', (c) => chunks.push(c));
|
|
641
|
+
upstreamRes.on('end', () => {
|
|
642
|
+
const raw = Buffer.concat(chunks);
|
|
643
|
+
const outHeaders = {};
|
|
644
|
+
for (const k of Object.keys(upstreamRes.headers)) {
|
|
645
|
+
const lk = k.toLowerCase();
|
|
646
|
+
if (lk !== 'content-length' && lk !== 'content-encoding' && lk !== 'transfer-encoding') {
|
|
647
|
+
outHeaders[k] = upstreamRes.headers[k];
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
outHeaders['Access-Control-Allow-Origin'] = '*';
|
|
651
|
+
outHeaders['Content-Type'] = upstreamRes.headers['content-type'] || 'application/json';
|
|
652
|
+
|
|
653
|
+
let outBody = raw;
|
|
654
|
+
let parsed = null;
|
|
655
|
+
try {
|
|
656
|
+
parsed = JSON.parse(raw.toString('utf8'));
|
|
657
|
+
} catch {
|
|
658
|
+
parsed = null;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
// ── RESPONSE SCAN ─────────────────────────────────────────────────
|
|
662
|
+
if (parsed && parsed.choices) {
|
|
663
|
+
const respViolations = scanChatResponse(parsed, mode, systemPromptLines);
|
|
664
|
+
for (const v of respViolations) logViolation(v, 'response');
|
|
665
|
+
|
|
666
|
+
if (mode === 'block' && respViolations.length > 0) {
|
|
667
|
+
respondJson(res, 403, { error: { message: blockedMessage(respViolations[0]) } });
|
|
668
|
+
return;
|
|
669
|
+
}
|
|
670
|
+
if (mode === 'redact' && respViolations.length > 0) {
|
|
671
|
+
outBody = Buffer.from(JSON.stringify(parsed));
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
outHeaders['Content-Length'] = outBody.length;
|
|
675
|
+
res.writeHead(upstreamRes.statusCode || 200, outHeaders);
|
|
676
|
+
res.end(outBody);
|
|
677
|
+
});
|
|
678
|
+
upstreamRes.on('error', () => {
|
|
679
|
+
if (!res.headersSent) respondJson(res, 502, { error: { message: 'vibeguard: upstream response error' } });
|
|
680
|
+
else res.destroy();
|
|
681
|
+
});
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// ── Streaming (SSE) upstream response: per-chunk scan + rewrite ────────
|
|
685
|
+
function handleStreamingResponse(upstreamReq, upstreamRes, res) {
|
|
686
|
+
const outHeaders = {};
|
|
687
|
+
for (const k of Object.keys(upstreamRes.headers)) {
|
|
688
|
+
const lk = k.toLowerCase();
|
|
689
|
+
if (lk !== 'content-length' && lk !== 'content-encoding' && lk !== 'transfer-encoding') {
|
|
690
|
+
outHeaders[k] = upstreamRes.headers[k];
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
outHeaders['Access-Control-Allow-Origin'] = '*';
|
|
694
|
+
outHeaders['Content-Type'] = upstreamRes.headers['content-type'] || 'text/event-stream';
|
|
695
|
+
res.writeHead(upstreamRes.statusCode || 200, outHeaders);
|
|
696
|
+
|
|
697
|
+
let buffer = '';
|
|
698
|
+
let stopped = false;
|
|
699
|
+
|
|
700
|
+
// Accumulated content (raw) + redacted output cursor, so secrets that
|
|
701
|
+
// span chunk boundaries still get redacted.
|
|
702
|
+
let accRaw = '';
|
|
703
|
+
let accRed = '';
|
|
704
|
+
let emittedLen = 0;
|
|
705
|
+
const toolAcc = new Map(); // tool_call index -> { raw, red, emittedLen }
|
|
706
|
+
const seen = new Set(); // violation keys already logged
|
|
707
|
+
|
|
708
|
+
function keyOf(v) {
|
|
709
|
+
return v.type + ':' + v.ruleId + ':' + v.start + ':' + v.match;
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
function scanDeltaText(deltaText) {
|
|
713
|
+
// Returns violations not yet logged.
|
|
714
|
+
const fresh = [];
|
|
715
|
+
const all = scanText(deltaText);
|
|
716
|
+
for (const v of all) {
|
|
717
|
+
const k = keyOf(v);
|
|
718
|
+
if (seen.has(k)) continue;
|
|
719
|
+
seen.add(k);
|
|
720
|
+
fresh.push(v);
|
|
721
|
+
}
|
|
722
|
+
return fresh;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
function blockOn(violation) {
|
|
726
|
+
if (stopped) return;
|
|
727
|
+
stopped = true;
|
|
728
|
+
logViolation(violation, 'stream');
|
|
729
|
+
try {
|
|
730
|
+
upstreamReq.destroy(); // cut the upstream socket
|
|
731
|
+
} catch {
|
|
732
|
+
/* already gone */
|
|
733
|
+
}
|
|
734
|
+
try {
|
|
735
|
+
res.write('data: [DONE]\n\n');
|
|
736
|
+
} catch {
|
|
737
|
+
/* client gone */
|
|
738
|
+
}
|
|
739
|
+
try {
|
|
740
|
+
res.end();
|
|
741
|
+
} catch {
|
|
742
|
+
/* ignore */
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
function handleLine(line) {
|
|
747
|
+
if (!line.startsWith('data:')) {
|
|
748
|
+
res.write(line + '\n');
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
const payload = line.slice(5).trim();
|
|
752
|
+
if (payload === '[DONE]') {
|
|
753
|
+
res.write(line + '\n\n');
|
|
754
|
+
return;
|
|
755
|
+
}
|
|
756
|
+
let evt = null;
|
|
757
|
+
try {
|
|
758
|
+
evt = JSON.parse(payload);
|
|
759
|
+
} catch {
|
|
760
|
+
res.write(line + '\n\n'); // unparseable — pass raw through
|
|
761
|
+
return;
|
|
762
|
+
}
|
|
763
|
+
const choice = evt.choices && evt.choices[0];
|
|
764
|
+
if (!choice || !choice.delta) {
|
|
765
|
+
res.write(line + '\n\n');
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
const delta = choice.delta;
|
|
769
|
+
let mutated = false;
|
|
770
|
+
|
|
771
|
+
// content deltas
|
|
772
|
+
if (typeof delta.content === 'string') {
|
|
773
|
+
accRaw += delta.content;
|
|
774
|
+
const fresh = scanDeltaText(accRaw);
|
|
775
|
+
if (mode === 'block' && fresh.length > 0) {
|
|
776
|
+
blockOn(fresh[0]);
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
for (const v of fresh) logViolation(v, 'stream');
|
|
780
|
+
if (mode === 'redact') {
|
|
781
|
+
accRed = redactSensitive(accRaw).text;
|
|
782
|
+
const emit = accRed.slice(emittedLen);
|
|
783
|
+
emittedLen = accRed.length;
|
|
784
|
+
if (emit !== delta.content) {
|
|
785
|
+
delta.content = emit;
|
|
786
|
+
mutated = true;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
// tool_call argument deltas (fragments per index)
|
|
792
|
+
if (Array.isArray(delta.tool_calls)) {
|
|
793
|
+
for (const tc of delta.tool_calls) {
|
|
794
|
+
const fn = tc && tc.function;
|
|
795
|
+
if (!fn || typeof fn.arguments !== 'string') continue;
|
|
796
|
+
const idx = tc.index !== undefined ? tc.index : 0;
|
|
797
|
+
let st = toolAcc.get(idx);
|
|
798
|
+
if (!st) {
|
|
799
|
+
st = { raw: '', red: '', emittedLen: 0 };
|
|
800
|
+
toolAcc.set(idx, st);
|
|
801
|
+
}
|
|
802
|
+
st.raw += fn.arguments;
|
|
803
|
+
const fresh = scanText(st.raw);
|
|
804
|
+
if (mode === 'block' && fresh.length > 0) {
|
|
805
|
+
blockOn(fresh[0]);
|
|
806
|
+
return;
|
|
807
|
+
}
|
|
808
|
+
for (const v of fresh) logViolation(v, 'stream');
|
|
809
|
+
if (mode === 'redact') {
|
|
810
|
+
st.red = redactSensitive(st.raw).text;
|
|
811
|
+
const emit = st.red.slice(st.emittedLen);
|
|
812
|
+
st.emittedLen = st.red.length;
|
|
813
|
+
if (emit !== fn.arguments) {
|
|
814
|
+
fn.arguments = emit;
|
|
815
|
+
mutated = true;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
if (mutated) res.write('data: ' + JSON.stringify(evt) + '\n\n');
|
|
822
|
+
else res.write(line + '\n\n');
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
upstreamRes.on('data', (chunk) => {
|
|
826
|
+
if (stopped) return;
|
|
827
|
+
buffer += chunk.toString('utf8');
|
|
828
|
+
let nl;
|
|
829
|
+
while ((nl = buffer.indexOf('\n')) !== -1) {
|
|
830
|
+
const line = buffer.slice(0, nl);
|
|
831
|
+
buffer = buffer.slice(nl + 1);
|
|
832
|
+
if (line === '') continue;
|
|
833
|
+
handleLine(line);
|
|
834
|
+
if (stopped) return;
|
|
835
|
+
}
|
|
836
|
+
});
|
|
837
|
+
const finish = () => {
|
|
838
|
+
if (stopped) return;
|
|
839
|
+
stopped = true;
|
|
840
|
+
// System-prompt leak check on the fully accumulated stream (report +
|
|
841
|
+
// block-mode kill; redaction of a leaked prompt line is meaningless).
|
|
842
|
+
if (accRaw) {
|
|
843
|
+
const leaked = detectSystemPromptLeak(accRaw, systemPromptLines);
|
|
844
|
+
for (const v of leaked) logViolation(v, 'stream');
|
|
845
|
+
if (mode === 'block' && leaked.length > 0) {
|
|
846
|
+
try {
|
|
847
|
+
res.write('data: [DONE]\n\n');
|
|
848
|
+
} catch {
|
|
849
|
+
/* client gone */
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
try {
|
|
854
|
+
res.end();
|
|
855
|
+
} catch {
|
|
856
|
+
/* ignore */
|
|
857
|
+
}
|
|
858
|
+
};
|
|
859
|
+
upstreamRes.on('end', finish);
|
|
860
|
+
upstreamRes.on('close', finish);
|
|
861
|
+
upstreamRes.on('error', finish);
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
// ── GET /v1/models (pass-through) ──────────────────────────────────────
|
|
865
|
+
function handleModels(req, res) {
|
|
866
|
+
const target = upstreamTarget(req.url);
|
|
867
|
+
if (!target) {
|
|
868
|
+
respondJson(res, 502, { error: { message: 'vibeguard: invalid upstream URL' } });
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
const upstreamReq = target.transport.request(
|
|
872
|
+
{
|
|
873
|
+
hostname: target.hostname,
|
|
874
|
+
port: target.port,
|
|
875
|
+
path: target.path,
|
|
876
|
+
method: 'GET',
|
|
877
|
+
headers: forwardHeaders(req.headers, false),
|
|
878
|
+
agent: false,
|
|
879
|
+
},
|
|
880
|
+
(upstreamRes) => {
|
|
881
|
+
inflight.delete(upstreamReq);
|
|
882
|
+
const h = {};
|
|
883
|
+
for (const k of Object.keys(upstreamRes.headers)) {
|
|
884
|
+
if (k.toLowerCase() !== 'content-length') h[k] = upstreamRes.headers[k];
|
|
885
|
+
}
|
|
886
|
+
h['Access-Control-Allow-Origin'] = '*';
|
|
887
|
+
res.writeHead(upstreamRes.statusCode || 200, h);
|
|
888
|
+
upstreamRes.pipe(res);
|
|
889
|
+
}
|
|
890
|
+
);
|
|
891
|
+
upstreamReq.setTimeout(IDLE_TIMEOUT_MS, () => upstreamReq.destroy(new Error('vibeguard: upstream timeout')));
|
|
892
|
+
upstreamReq.on('error', (err) => {
|
|
893
|
+
inflight.delete(upstreamReq);
|
|
894
|
+
if (res.headersSent) {
|
|
895
|
+
res.destroy();
|
|
896
|
+
return;
|
|
897
|
+
}
|
|
898
|
+
respondJson(res, 502, { error: { message: 'vibeguard: upstream error: ' + (err && err.message ? err.message : err) } });
|
|
899
|
+
});
|
|
900
|
+
inflight.add(upstreamReq);
|
|
901
|
+
upstreamReq.end();
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
// ── Server ─────────────────────────────────────────────────────────────
|
|
905
|
+
const server = http.createServer((req, res) => {
|
|
906
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
907
|
+
if (req.method === 'OPTIONS') {
|
|
908
|
+
res.writeHead(204, {
|
|
909
|
+
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
|
|
910
|
+
'Access-Control-Allow-Headers': req.headers['access-control-request-headers'] || 'Content-Type, Authorization',
|
|
911
|
+
'Access-Control-Max-Age': '86400',
|
|
912
|
+
});
|
|
913
|
+
res.end();
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
const urlPath = (req.url || '/').split('?')[0];
|
|
918
|
+
if (req.method === 'GET' && urlPath === '/v1/models') {
|
|
919
|
+
handleModels(req, res);
|
|
920
|
+
return;
|
|
921
|
+
}
|
|
922
|
+
if (req.method === 'POST' &&
|
|
923
|
+
(urlPath === '/v1/chat/completions' || urlPath === '/v1/completions' || urlPath === '/v1/embeddings')) {
|
|
924
|
+
handlePost(req, res, urlPath);
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
respondJson(res, 404, { error: { message: 'unknown endpoint' } });
|
|
928
|
+
});
|
|
929
|
+
|
|
930
|
+
server.requestTimeout = IDLE_TIMEOUT_MS;
|
|
931
|
+
server.headersTimeout = IDLE_TIMEOUT_MS;
|
|
932
|
+
server.keepAliveTimeout = IDLE_TIMEOUT_MS;
|
|
933
|
+
server.timeout = IDLE_TIMEOUT_MS;
|
|
934
|
+
|
|
935
|
+
server.listen(opts.port || 0, '127.0.0.1');
|
|
936
|
+
|
|
937
|
+
const proxy = {
|
|
938
|
+
server,
|
|
939
|
+
mode,
|
|
940
|
+
upstream: upstreamBase,
|
|
941
|
+
violations,
|
|
942
|
+
close() {
|
|
943
|
+
for (const r of inflight) {
|
|
944
|
+
try {
|
|
945
|
+
r.destroy();
|
|
946
|
+
} catch {
|
|
947
|
+
/* ignore */
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
inflight.clear();
|
|
951
|
+
return new Promise((resolve) => {
|
|
952
|
+
server.close(() => resolve());
|
|
953
|
+
if (typeof server.closeAllConnections === 'function') {
|
|
954
|
+
try {
|
|
955
|
+
server.closeAllConnections();
|
|
956
|
+
} catch {
|
|
957
|
+
/* ignore */
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
});
|
|
961
|
+
},
|
|
962
|
+
};
|
|
963
|
+
Object.defineProperty(proxy, 'port', {
|
|
964
|
+
get() {
|
|
965
|
+
const a = server.address();
|
|
966
|
+
return a ? a.port : null;
|
|
967
|
+
},
|
|
968
|
+
});
|
|
969
|
+
return proxy;
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
module.exports = {
|
|
973
|
+
createGuardProxy,
|
|
974
|
+
detectInjection,
|
|
975
|
+
scanSecrets,
|
|
976
|
+
scanPII,
|
|
977
|
+
scanText,
|
|
978
|
+
redactSensitive,
|
|
979
|
+
scanChatCompletions,
|
|
980
|
+
scanChatResponse,
|
|
981
|
+
detectSystemPromptLeak,
|
|
982
|
+
INJECTION_PATTERNS,
|
|
983
|
+
DEFAULT_PORT,
|
|
984
|
+
DEFAULT_UPSTREAM,
|
|
985
|
+
MODES,
|
|
986
|
+
};
|