-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.js
More file actions
292 lines (260 loc) · 11.7 KB
/
Copy pathproxy.js
File metadata and controls
292 lines (260 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import http from "node:http";
import { redact, restore } from "../preprocess/redact.js";
import { compress } from "../compress/compress.js";
import { getLogger } from "../utils/logger.js";
import { MapStore } from "./store.js";
// Hop-by-hop / length headers we must not forward verbatim.
const STRIP_REQUEST_HEADERS = new Set([
"host",
"connection",
"content-length",
"accept-encoding", // force identity so we can restore text in the response
"transfer-encoding",
]);
const STRIP_RESPONSE_HEADERS = new Set([
"content-length", // length changes after restore
"content-encoding",
"transfer-encoding",
"connection",
]);
/** All provider request formats the proxy knows how to redact. */
export const ALL_FORMATS = ["openai", "anthropic", "responses", "gemini"];
/**
* Redact PII (and optionally compress) text inside a parsed LLM request body.
* Format-aware: OpenAI Chat Completions, Anthropic Messages, OpenAI Responses API,
* and Gemini generateContent. `formats` is a Set selecting which to process
* (omit/undefined = all). Mutates bodyObj in place; the map lives in `state.map`.
*/
async function redactRequestBody(engine, bodyObj, redactOptions, state, compressOn, formats) {
const opts = { ...redactOptions, state };
const on = (f) => !formats || formats.has(f);
const doText = async (t) => {
let r = (await redact(engine, t, opts)).redacted;
if (compressOn) r = compress(r).compressed; // redact -> compress -> forward
return r;
};
// Redact text-bearing parts inside a content array (OpenAI/Anthropic/Responses blocks).
const TEXT_PART_TYPES = new Set(["text", "input_text", "output_text"]);
const doParts = async (parts) => {
for (const part of parts) {
if (part && TEXT_PART_TYPES.has(part.type) && typeof part.text === "string") {
part.text = await doText(part.text);
}
}
};
// OpenAI Chat Completions + Anthropic Messages both use messages[].
if ((on("openai") || on("anthropic")) && Array.isArray(bodyObj.messages)) {
for (const msg of bodyObj.messages) {
if (typeof msg.content === "string") msg.content = await doText(msg.content);
else if (Array.isArray(msg.content)) await doParts(msg.content);
}
}
// Anthropic system prompt: string OR array of text blocks.
if (on("anthropic")) {
if (typeof bodyObj.system === "string") bodyObj.system = await doText(bodyObj.system);
else if (Array.isArray(bodyObj.system)) await doParts(bodyObj.system);
}
// OpenAI Responses API: input (string | array of items) + instructions.
if (on("responses")) {
if (typeof bodyObj.input === "string") bodyObj.input = await doText(bodyObj.input);
else if (Array.isArray(bodyObj.input)) {
for (const item of bodyObj.input) {
if (typeof item?.content === "string") item.content = await doText(item.content);
else if (Array.isArray(item?.content)) await doParts(item.content);
}
}
if (typeof bodyObj.instructions === "string") bodyObj.instructions = await doText(bodyObj.instructions);
}
// Gemini generateContent: contents[].parts[].text + systemInstruction.
if (on("gemini")) {
const redactGeminiContents = async (arr) => {
for (const c of arr) {
if (Array.isArray(c?.parts)) {
for (const part of c.parts) {
if (typeof part?.text === "string") part.text = await doText(part.text);
}
}
}
};
if (Array.isArray(bodyObj.contents)) await redactGeminiContents(bodyObj.contents);
const sys = bodyObj.systemInstruction || bodyObj.system_instruction;
if (sys && Array.isArray(sys.parts)) {
for (const part of sys.parts) {
if (typeof part?.text === "string") part.text = await doText(part.text);
}
}
}
// Legacy OpenAI completions prompt.
if (on("openai") && typeof bodyObj.prompt === "string") bodyObj.prompt = await doText(bodyObj.prompt);
}
/** Sanitize (blur) base64 images embedded in a vision request body. One-way. */
async function sanitizeImagesInBody(bodyObj, sanitizer, opts = {}) {
if (!sanitizer || !Array.isArray(bodyObj.messages)) return;
const sanitizeDataUrl = async (url) => {
const m = /^data:(image\/[a-zA-Z0-9.+-]+);base64,(.*)$/s.exec(url);
if (!m) return url;
const { image } = await sanitizer.sanitize(Buffer.from(m[2], "base64"), opts);
const mime = opts.compress ? "image/jpeg" : m[1];
return `data:${mime};base64,${image.toString("base64")}`;
};
for (const msg of bodyObj.messages) {
if (!Array.isArray(msg.content)) continue;
for (const part of msg.content) {
// OpenAI vision: { type:'image_url', image_url:{ url:'data:image/...;base64,...' } }
if (part?.type === "image_url" && typeof part.image_url?.url === "string" && part.image_url.url.startsWith("data:image")) {
part.image_url.url = await sanitizeDataUrl(part.image_url.url);
}
// Anthropic vision: { type:'image', source:{ type:'base64', media_type, data } }
else if (part?.type === "image" && part.source?.type === "base64" && typeof part.source.data === "string") {
const { image } = await sanitizer.sanitize(Buffer.from(part.source.data, "base64"), opts);
part.source.data = image.toString("base64");
if (opts.compress) part.source.media_type = "image/jpeg";
}
}
}
}
/**
* Pipe an upstream (web) ReadableStream to a Node response, restoring PII
* placeholders as text flows through. Uses a look-back buffer so a placeholder
* split across two chunks is never emitted half-restored.
*/
async function pipeWithRestore(upstreamBody, res, map) {
if (!upstreamBody) {
res.end();
return;
}
let buffer = "";
const decoder = new TextDecoder();
for await (const chunk of upstreamBody) {
buffer += decoder.decode(chunk, { stream: true });
// Hold back a trailing fragment if it looks like an unfinished placeholder.
let lastBrace = buffer.lastIndexOf("{");
if (lastBrace > 0 && buffer[lastBrace - 1] === "{") lastBrace -= 1;
let emitStr = buffer;
let keepStr = "";
if (lastBrace !== -1 && lastBrace > buffer.length - 60) {
const remaining = buffer.substring(lastBrace);
if (!remaining.includes("}}")) {
emitStr = buffer.substring(0, lastBrace);
keepStr = remaining;
}
}
if (emitStr) res.write(restore(emitStr, map));
buffer = keepStr;
}
if (buffer) res.write(restore(buffer, map));
res.end();
}
/**
* Create a local PII-shielding reverse proxy.
*
* Point your LLM client's base URL at this server (e.g. http://localhost:8787).
* Outgoing prompts are redacted before leaving the machine; responses are
* restored locally. Redaction maps stay in a local MapStore (never persisted,
* never sent upstream).
*
* @param {Object} options
* @param {string} [options.upstream="https://api.openai.com"] - Provider base URL.
* @param {Object} [options.engine] - Optional Tier 2 engine (e.g. OllamaEngine).
* @param {Object} [options.redactOptions] - Passed through to redact().
* @param {MapStore} [options.store] - Map store (created if omitted).
* @param {number} [options.ttlMs] - TTL for stored maps.
* @param {Function} [options.fetch] - fetch impl (defaults to global fetch).
* @param {string[]} [options.formats] - Provider request formats to redact (see ALL_FORMATS); default all.
* @param {boolean} [options.compress] - Compress text after redaction.
* @param {Object} [options.imageSanitizer] - ImageSanitizer for vision requests (one-way blur).
* @param {Object} [options.logger]
* @returns {http.Server}
*/
export function createProxyServer(options = {}) {
const upstream = (options.upstream || "https://api.openai.com").replace(/\/+$/, "");
const engine = options.engine || null;
const logger = options.logger || getLogger(options.loggerOptions);
const store = options.store || new MapStore({ ttlMs: options.ttlMs });
const doFetch = options.fetch || globalThis.fetch;
const compressOn = !!options.compress;
const imageSanitizer = options.imageSanitizer || null;
const sanitizeOpts = options.sanitizeOpts || {};
// Which provider request formats to redact (default: all).
const formats = Array.isArray(options.formats) && options.formats.length ? new Set(options.formats) : null;
// Detection options (incl. `tier`, `ner`, `nerDetector`, `llm`) flow straight
// through to redact(). For backwards compat: if no explicit tier is set but a
// loaded LLM engine was supplied, default to the LLM tier.
const baseRedactOptions = { ...options.redactOptions };
if (!baseRedactOptions.tier && engine && typeof engine.isLoaded === "function" && engine.isLoaded()) {
baseRedactOptions.llm = { enabled: true, ...baseRedactOptions.llm };
}
const activeTier = baseRedactOptions.tier || (baseRedactOptions.llm?.enabled ? "llm" : "rules");
if (!doFetch) {
throw new Error("global fetch is not available. Use Node.js 18+ or pass options.fetch.");
}
const server = http.createServer(async (req, res) => {
try {
// Health / introspection endpoint.
if (req.url === "/__pii/health") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ ok: true, upstream, tier: activeTier }));
return;
}
// Read the full request body.
const chunks = [];
for await (const c of req) chunks.push(c);
const rawBody = Buffer.concat(chunks).toString("utf8");
const state = { map: {}, reverseMap: {}, placeholderCounts: {} };
let outgoingBody = rawBody;
const contentType = req.headers["content-type"] || "";
if (rawBody && contentType.includes("application/json")) {
try {
const bodyObj = JSON.parse(rawBody);
await redactRequestBody(engine, bodyObj, baseRedactOptions, state, compressOn, formats);
if (imageSanitizer) await sanitizeImagesInBody(bodyObj, imageSanitizer, sanitizeOpts);
outgoingBody = JSON.stringify(bodyObj);
} catch (e) {
logger.log("warn", "PROXY", "Body was not redactable JSON; forwarding as-is", { error: e.message });
}
}
// Build forwarded headers.
const headers = {};
for (const [k, v] of Object.entries(req.headers)) {
if (!STRIP_REQUEST_HEADERS.has(k.toLowerCase())) headers[k] = v;
}
headers["accept-encoding"] = "identity";
const target = `${upstream}${req.url}`;
const hasBody = req.method !== "GET" && req.method !== "HEAD";
const upstreamRes = await doFetch(target, {
method: req.method,
headers,
body: hasBody ? outgoingBody : undefined,
});
// Persist the map locally so it could be retrieved within the TTL.
const sessionId = store.put(state.map);
// Mirror status + headers (minus length/encoding which we change).
const resHeaders = { "x-pii-session": sessionId };
upstreamRes.headers.forEach((value, key) => {
if (!STRIP_RESPONSE_HEADERS.has(key.toLowerCase())) resHeaders[key] = value;
});
res.writeHead(upstreamRes.status, resHeaders);
await pipeWithRestore(upstreamRes.body, res, state.map);
} catch (error) {
logger.logError("proxy", error, { url: req.url, method: req.method });
if (!res.headersSent) {
res.writeHead(502, { "Content-Type": "application/json" });
}
res.end(JSON.stringify({ error: "redactkit proxy error", message: error.message }));
}
});
server.on("close", () => store.dispose?.());
return server;
}
/**
* Convenience: create the proxy and start listening.
* @returns {Promise<http.Server>}
*/
export function startProxy(options = {}) {
const port = options.port ?? 8787;
const host = options.host ?? "127.0.0.1";
const server = createProxyServer(options);
return new Promise((resolve) => {
server.listen(port, host, () => resolve(server));
});
}