flare-redact 1.4.1 → 1.5.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/CHANGELOG.md +130 -0
- package/README.md +134 -2
- package/SECURITY.md +36 -0
- package/bin/flare-gateway.mjs +6 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +17 -61
- package/dist/cli.js.map +1 -1
- package/dist/detectors.d.ts +17 -0
- package/dist/detectors.d.ts.map +1 -1
- package/dist/detectors.js.map +1 -1
- package/dist/engine.d.ts +5 -0
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +50 -7
- package/dist/engine.js.map +1 -1
- package/dist/gateway/body.d.ts +90 -0
- package/dist/gateway/body.d.ts.map +1 -0
- package/dist/gateway/body.js +185 -0
- package/dist/gateway/body.js.map +1 -0
- package/dist/gateway/cli.d.ts +26 -0
- package/dist/gateway/cli.d.ts.map +1 -0
- package/dist/gateway/cli.js +237 -0
- package/dist/gateway/cli.js.map +1 -0
- package/dist/gateway/config.d.ts +110 -0
- package/dist/gateway/config.d.ts.map +1 -0
- package/dist/gateway/config.js +212 -0
- package/dist/gateway/config.js.map +1 -0
- package/dist/gateway/index.d.ts +21 -0
- package/dist/gateway/index.d.ts.map +1 -0
- package/dist/gateway/index.js +21 -0
- package/dist/gateway/index.js.map +1 -0
- package/dist/gateway/runtime.d.ts +80 -0
- package/dist/gateway/runtime.d.ts.map +1 -0
- package/dist/gateway/runtime.js +0 -0
- package/dist/gateway/runtime.js.map +1 -0
- package/dist/gateway/server.d.ts +37 -0
- package/dist/gateway/server.d.ts.map +1 -0
- package/dist/gateway/server.js +560 -0
- package/dist/gateway/server.js.map +1 -0
- package/dist/help.d.ts +32 -0
- package/dist/help.d.ts.map +1 -0
- package/dist/help.js +423 -0
- package/dist/help.js.map +1 -0
- package/dist/pack.d.ts +88 -0
- package/dist/pack.d.ts.map +1 -0
- package/dist/pack.js +350 -0
- package/dist/pack.js.map +1 -0
- package/docker/Dockerfile +47 -0
- package/docker/README.md +219 -0
- package/docker/docker-compose.yml +55 -0
- package/docker/flare-gateway.example.json +48 -0
- package/package.json +27 -7
- package/spec/SPEC.md +469 -0
- package/spec/conformance/README.md +60 -0
- package/spec/conformance/cases.json +149 -0
- package/spec/conformance/expected.json +1683 -0
- package/spec/detectors.json +1812 -0
- package/spec/detectors.schema.json +196 -0
|
@@ -0,0 +1,560 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The gateway itself: a reverse proxy that redacts what leaves your network, and
|
|
3
|
+
* a small HTTP API that any language can call.
|
|
4
|
+
*
|
|
5
|
+
* The proxy is the part that needs no code change. Point `OPENAI_BASE_URL` (or
|
|
6
|
+
* any vendor's base URL) at the sidecar, and the request body is scrubbed on the
|
|
7
|
+
* way out and the placeholders are put back on the way in — including in a
|
|
8
|
+
* streamed response. Your application keeps sending and receiving real data; the
|
|
9
|
+
* vendor never sees it.
|
|
10
|
+
*
|
|
11
|
+
* Three rules the implementation holds to:
|
|
12
|
+
*
|
|
13
|
+
* 1. **Credentials are forwarded, never redacted.** The `Authorization` header
|
|
14
|
+
* that authenticates you *to* the upstream is not the secret we are hiding
|
|
15
|
+
* *from* it. Header values are passed through untouched.
|
|
16
|
+
* 2. **Nothing sensitive is logged.** The audit trail records detector ids and
|
|
17
|
+
* counts. Bodies, headers and matched values never reach a log line.
|
|
18
|
+
* 3. **Failures are refusals.** An oversized body, an unreachable upstream or an
|
|
19
|
+
* unparseable request produces an error status, not a partially redacted
|
|
20
|
+
* request that quietly reaches the vendor.
|
|
21
|
+
*/
|
|
22
|
+
import { createServer } from 'node:http';
|
|
23
|
+
import { timingSafeEqual } from 'node:crypto';
|
|
24
|
+
import { readFileSync } from 'node:fs';
|
|
25
|
+
import { compilePolicy, createVault, DETECTORS, } from '../index.js';
|
|
26
|
+
import { matchRoute, upstreamUrl, } from './config.js';
|
|
27
|
+
import { isEventStream, parsePath, redactBody, restoreBody, streamRestorer, } from './body.js';
|
|
28
|
+
import { createLogger, Metrics, SessionStore } from './runtime.js';
|
|
29
|
+
const PACKAGE_VERSION = (() => {
|
|
30
|
+
try {
|
|
31
|
+
const pkg = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf8'));
|
|
32
|
+
return pkg.version ?? '0.0.0';
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return '0.0.0';
|
|
36
|
+
}
|
|
37
|
+
})();
|
|
38
|
+
/** Connection-scoped headers that must never be forwarded. RFC 9110 §7.6.1. */
|
|
39
|
+
const HOP_BY_HOP = new Set([
|
|
40
|
+
'connection',
|
|
41
|
+
'keep-alive',
|
|
42
|
+
'proxy-authenticate',
|
|
43
|
+
'proxy-authorization',
|
|
44
|
+
'proxy-connection',
|
|
45
|
+
'te',
|
|
46
|
+
'trailer',
|
|
47
|
+
'transfer-encoding',
|
|
48
|
+
'upgrade',
|
|
49
|
+
]);
|
|
50
|
+
/** Response headers the gateway recomputes because it may rewrite the body. */
|
|
51
|
+
const REWRITTEN_RESPONSE_HEADERS = new Set(['content-length', 'content-encoding']);
|
|
52
|
+
/** Options a client may never set on a gateway-enforced policy. */
|
|
53
|
+
const CLIENT_FORBIDDEN_OPTIONS = ['transformSecret', 'hashSalt', 'detectors', 'custom'];
|
|
54
|
+
class HttpError extends Error {
|
|
55
|
+
constructor(status, code, message,
|
|
56
|
+
// Set when the request body was deliberately left unread. The socket cannot
|
|
57
|
+
// be reused: the bytes still in flight would be read as the next request.
|
|
58
|
+
closeConnection = false) {
|
|
59
|
+
super(message);
|
|
60
|
+
this.status = status;
|
|
61
|
+
this.code = code;
|
|
62
|
+
this.closeConnection = closeConnection;
|
|
63
|
+
this.name = 'HttpError';
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// The gateway's own endpoints. Everything else — including the rest of `/v1/`,
|
|
67
|
+
// which is where every vendor puts `chat/completions`, `models` and `messages` —
|
|
68
|
+
// is proxied. Matching the whole `/v1/` prefix here would make the sidecar
|
|
69
|
+
// useless in front of the APIs it exists to protect.
|
|
70
|
+
const CONTROL_PATHS = new Set([
|
|
71
|
+
'/healthz',
|
|
72
|
+
'/readyz',
|
|
73
|
+
'/metrics',
|
|
74
|
+
'/v1/detectors',
|
|
75
|
+
'/v1/redact',
|
|
76
|
+
'/v1/scan',
|
|
77
|
+
'/v1/sessions',
|
|
78
|
+
]);
|
|
79
|
+
const SESSION_PATH = /^\/v1\/sessions\/([^/]+)(?:\/(redact|restore))?$/;
|
|
80
|
+
/** True for the gateway's own API, which shadows the upstream on these paths. */
|
|
81
|
+
function isControlPath(pathname) {
|
|
82
|
+
return CONTROL_PATHS.has(pathname) || SESSION_PATH.test(pathname);
|
|
83
|
+
}
|
|
84
|
+
/** Layer policies, later layers winning field by field. */
|
|
85
|
+
function layerPolicies(...layers) {
|
|
86
|
+
return layers.reduce((merged, layer) => (layer ? { ...merged, ...layer } : merged), {});
|
|
87
|
+
}
|
|
88
|
+
function sanitiseClientOptions(input) {
|
|
89
|
+
if (input === undefined || input === null)
|
|
90
|
+
return {};
|
|
91
|
+
if (typeof input !== 'object' || Array.isArray(input)) {
|
|
92
|
+
throw new HttpError(400, 'ERR_BAD_OPTIONS', 'options must be a JSON object');
|
|
93
|
+
}
|
|
94
|
+
const options = { ...input };
|
|
95
|
+
for (const forbidden of CLIENT_FORBIDDEN_OPTIONS) {
|
|
96
|
+
if (forbidden in options) {
|
|
97
|
+
throw new HttpError(400, 'ERR_FORBIDDEN_OPTION', `"${forbidden}" is configured on the gateway and cannot be set per request`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return options;
|
|
101
|
+
}
|
|
102
|
+
function readBody(req, maxBytes) {
|
|
103
|
+
return new Promise((resolve, reject) => {
|
|
104
|
+
const chunks = [];
|
|
105
|
+
let size = 0;
|
|
106
|
+
req.on('data', (chunk) => {
|
|
107
|
+
size += chunk.length;
|
|
108
|
+
if (size > maxBytes) {
|
|
109
|
+
// Stop reading, but answer with a 413 rather than resetting the socket:
|
|
110
|
+
// a caller that gets a connection reset cannot tell a refusal from a
|
|
111
|
+
// network failure, and a refusal is the whole point of the limit.
|
|
112
|
+
req.pause();
|
|
113
|
+
reject(new HttpError(413, 'ERR_BODY_TOO_LARGE', `request body exceeds ${maxBytes} bytes`, true));
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
chunks.push(chunk);
|
|
117
|
+
});
|
|
118
|
+
req.on('end', () => resolve(Buffer.concat(chunks)));
|
|
119
|
+
req.on('error', reject);
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
// `BodyInit` only admits ArrayBuffer-backed views, while a Node Buffer is typed
|
|
123
|
+
// over ArrayBufferLike. Node never allocates a Buffer on a SharedArrayBuffer, so
|
|
124
|
+
// this narrowing is exact, and the view shares memory rather than copying it.
|
|
125
|
+
function asBodyInit(buffer) {
|
|
126
|
+
return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
|
|
127
|
+
}
|
|
128
|
+
function sendJson(res, status, payload) {
|
|
129
|
+
const body = Buffer.from(JSON.stringify(payload), 'utf8');
|
|
130
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'content-length': body.length });
|
|
131
|
+
res.end(body);
|
|
132
|
+
}
|
|
133
|
+
function sendError(res, status, code, message) {
|
|
134
|
+
sendJson(res, status, { error: true, code, message });
|
|
135
|
+
}
|
|
136
|
+
function tokenMatches(expected, presented) {
|
|
137
|
+
const a = Buffer.from(expected, 'utf8');
|
|
138
|
+
const b = Buffer.from(presented, 'utf8');
|
|
139
|
+
// timingSafeEqual throws on a length mismatch, which would itself leak length.
|
|
140
|
+
if (a.length !== b.length) {
|
|
141
|
+
timingSafeEqual(a, a);
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
return timingSafeEqual(a, b);
|
|
145
|
+
}
|
|
146
|
+
function summarise(findings) {
|
|
147
|
+
const byDetector = {};
|
|
148
|
+
const byRisk = {};
|
|
149
|
+
for (const finding of findings) {
|
|
150
|
+
byDetector[finding.detector] = (byDetector[finding.detector] ?? 0) + 1;
|
|
151
|
+
byRisk[finding.risk] = (byRisk[finding.risk] ?? 0) + 1;
|
|
152
|
+
}
|
|
153
|
+
return { total: findings.length, byDetector, byRisk };
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Write one chunk, waiting for drain when the socket is full.
|
|
157
|
+
*
|
|
158
|
+
* All three outcomes have to be handled or a slow client can wedge the request
|
|
159
|
+
* forever: drain means carry on, error means give up, and close means the client
|
|
160
|
+
* left — which is not an error worth logging.
|
|
161
|
+
*/
|
|
162
|
+
function writeChunk(res, chunk) {
|
|
163
|
+
if (res.write(chunk))
|
|
164
|
+
return Promise.resolve();
|
|
165
|
+
return new Promise((resolve, reject) => {
|
|
166
|
+
const settle = (error) => {
|
|
167
|
+
res.off('drain', onDrain);
|
|
168
|
+
res.off('error', onError);
|
|
169
|
+
res.off('close', onClose);
|
|
170
|
+
if (error)
|
|
171
|
+
reject(error);
|
|
172
|
+
else
|
|
173
|
+
resolve();
|
|
174
|
+
};
|
|
175
|
+
const onDrain = () => settle();
|
|
176
|
+
const onClose = () => settle();
|
|
177
|
+
const onError = (error) => settle(error);
|
|
178
|
+
res.on('drain', onDrain);
|
|
179
|
+
res.on('error', onError);
|
|
180
|
+
res.on('close', onClose);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
export function createGateway(config) {
|
|
184
|
+
const logger = createLogger(config.log);
|
|
185
|
+
const metrics = new Metrics();
|
|
186
|
+
const sessions = new SessionStore(config.sessions);
|
|
187
|
+
// A route's own policy is the more specific one, so it wins over the
|
|
188
|
+
// gateway-wide default. Both are set by whoever runs the gateway.
|
|
189
|
+
const runtimes = config.routes.map((route) => {
|
|
190
|
+
const options = layerPolicies(config.policy, route.policy);
|
|
191
|
+
return {
|
|
192
|
+
route,
|
|
193
|
+
options,
|
|
194
|
+
policy: compilePolicy(options),
|
|
195
|
+
...(route.request.paths?.length ? { paths: route.request.paths.map(parsePath) } : {}),
|
|
196
|
+
};
|
|
197
|
+
});
|
|
198
|
+
const requireAuth = (req) => {
|
|
199
|
+
if (!config.token)
|
|
200
|
+
return;
|
|
201
|
+
const header = req.headers.authorization;
|
|
202
|
+
const presented = typeof header === 'string' && header.startsWith('Bearer ') ? header.slice(7) : '';
|
|
203
|
+
if (!presented || !tokenMatches(config.token, presented)) {
|
|
204
|
+
throw new HttpError(401, 'ERR_UNAUTHORIZED', 'a valid bearer token is required');
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
const parseJsonBody = async (req) => {
|
|
208
|
+
const raw = (await readBody(req, config.limits.maxBodyBytes)).toString('utf8');
|
|
209
|
+
if (!raw.trim())
|
|
210
|
+
return {};
|
|
211
|
+
try {
|
|
212
|
+
const parsed = JSON.parse(raw);
|
|
213
|
+
if (typeof parsed !== 'object' || parsed === null || Array.isArray(parsed)) {
|
|
214
|
+
throw new HttpError(400, 'ERR_BAD_REQUEST', 'request body must be a JSON object');
|
|
215
|
+
}
|
|
216
|
+
return parsed;
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
if (error instanceof HttpError)
|
|
220
|
+
throw error;
|
|
221
|
+
throw new HttpError(400, 'ERR_BAD_JSON', 'request body is not valid JSON');
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
// ---------------------------------------------------------------------- //
|
|
225
|
+
// The /v1 API
|
|
226
|
+
// ---------------------------------------------------------------------- //
|
|
227
|
+
const handleApi = async (req, res, pathname) => {
|
|
228
|
+
if (pathname === '/healthz' || pathname === '/readyz') {
|
|
229
|
+
sendJson(res, 200, {
|
|
230
|
+
status: 'ok',
|
|
231
|
+
version: PACKAGE_VERSION,
|
|
232
|
+
routes: config.routes.map((route) => route.name),
|
|
233
|
+
sessions: sessions.size,
|
|
234
|
+
});
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (pathname === '/metrics') {
|
|
238
|
+
if (!config.metrics)
|
|
239
|
+
throw new HttpError(404, 'ERR_NOT_FOUND', 'metrics are disabled');
|
|
240
|
+
const body = metrics.render(sessions.size);
|
|
241
|
+
res.writeHead(200, { 'content-type': 'text/plain; version=0.0.4; charset=utf-8' });
|
|
242
|
+
res.end(body);
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
requireAuth(req);
|
|
246
|
+
// The gateway's own policy is layered *over* the caller's, so a client
|
|
247
|
+
// cannot widen what the operator decided to protect.
|
|
248
|
+
const clientPolicy = async () => {
|
|
249
|
+
const payload = await parseJsonBody(req);
|
|
250
|
+
return { payload, options: layerPolicies(sanitiseClientOptions(payload.options), config.policy) };
|
|
251
|
+
};
|
|
252
|
+
if (pathname === '/v1/detectors' && req.method === 'GET') {
|
|
253
|
+
sendJson(res, 200, {
|
|
254
|
+
// The selectors are reported alongside the catalogue so a client can see
|
|
255
|
+
// which detectors this gateway actually runs without guessing.
|
|
256
|
+
policy: {
|
|
257
|
+
only: config.policy.only ?? null,
|
|
258
|
+
enable: config.policy.enable ?? null,
|
|
259
|
+
disable: config.policy.disable ?? null,
|
|
260
|
+
mode: config.policy.mode ?? 'mask',
|
|
261
|
+
},
|
|
262
|
+
detectors: DETECTORS.map((detector) => ({
|
|
263
|
+
id: detector.id,
|
|
264
|
+
label: detector.label,
|
|
265
|
+
why: detector.why,
|
|
266
|
+
default: detector.default,
|
|
267
|
+
tags: detector.tags ?? [],
|
|
268
|
+
risk: detector.risk ?? null,
|
|
269
|
+
})),
|
|
270
|
+
});
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (pathname === '/v1/redact' && req.method === 'POST') {
|
|
274
|
+
const { payload, options } = await clientPolicy();
|
|
275
|
+
const policy = compilePolicy(options);
|
|
276
|
+
const findings = config.auditFindings ? policy.scan(payload.input) : [];
|
|
277
|
+
for (const finding of findings)
|
|
278
|
+
metrics.finding(finding.detector);
|
|
279
|
+
sendJson(res, 200, {
|
|
280
|
+
output: policy.redact(payload.input),
|
|
281
|
+
...(config.auditFindings ? { summary: summarise(findings) } : {}),
|
|
282
|
+
});
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (pathname === '/v1/scan' && req.method === 'POST') {
|
|
286
|
+
const { payload, options } = await clientPolicy();
|
|
287
|
+
const findings = compilePolicy(options).scan(payload.input);
|
|
288
|
+
sendJson(res, 200, { findings, summary: summarise(findings) });
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
if (pathname === '/v1/sessions' && req.method === 'POST') {
|
|
292
|
+
const { options } = await clientPolicy();
|
|
293
|
+
const record = sessions.create(options);
|
|
294
|
+
sendJson(res, 201, { id: record.id, expiresAt: new Date(record.expiresAt).toISOString() });
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
const sessionRoute = SESSION_PATH.exec(pathname);
|
|
298
|
+
if (sessionRoute) {
|
|
299
|
+
const id = decodeURIComponent(sessionRoute[1]);
|
|
300
|
+
const action = sessionRoute[2];
|
|
301
|
+
if (!action && req.method === 'DELETE') {
|
|
302
|
+
const removed = sessions.delete(id);
|
|
303
|
+
if (!removed)
|
|
304
|
+
throw new HttpError(404, 'ERR_NO_SESSION', 'unknown session');
|
|
305
|
+
res.writeHead(204).end();
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
const record = sessions.get(id);
|
|
309
|
+
if (!record)
|
|
310
|
+
throw new HttpError(404, 'ERR_NO_SESSION', 'unknown or expired session');
|
|
311
|
+
if (req.method !== 'POST')
|
|
312
|
+
throw new HttpError(405, 'ERR_METHOD', 'use POST');
|
|
313
|
+
const payload = await parseJsonBody(req);
|
|
314
|
+
const output = action === 'restore'
|
|
315
|
+
? record.session.restore(payload.input)
|
|
316
|
+
: record.session.redact(payload.input);
|
|
317
|
+
sendJson(res, 200, { output, size: record.session.size });
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
throw new HttpError(404, 'ERR_NOT_FOUND', 'no such endpoint');
|
|
321
|
+
};
|
|
322
|
+
// ---------------------------------------------------------------------- //
|
|
323
|
+
// The reverse proxy
|
|
324
|
+
// ---------------------------------------------------------------------- //
|
|
325
|
+
const handleProxy = async (req, res, runtime, pathname, search) => {
|
|
326
|
+
const { route, options, policy } = runtime;
|
|
327
|
+
const started = Date.now();
|
|
328
|
+
const raw = await readBody(req, config.limits.maxBodyBytes);
|
|
329
|
+
metrics.bytesIn += raw.length;
|
|
330
|
+
const contentType = String(req.headers['content-type'] ?? '');
|
|
331
|
+
// One vault per request: placeholders must not leak between callers.
|
|
332
|
+
const vault = route.response.enabled ? createVault(options) : undefined;
|
|
333
|
+
let body = raw;
|
|
334
|
+
let findings = [];
|
|
335
|
+
if (route.request.enabled && raw.length) {
|
|
336
|
+
const text = raw.toString('utf8');
|
|
337
|
+
if (config.auditFindings) {
|
|
338
|
+
try {
|
|
339
|
+
findings = policy.scan(contentType && /json/i.test(contentType) ? JSON.parse(text) : text);
|
|
340
|
+
}
|
|
341
|
+
catch {
|
|
342
|
+
findings = policy.scan(text);
|
|
343
|
+
}
|
|
344
|
+
for (const finding of findings)
|
|
345
|
+
metrics.finding(finding.detector);
|
|
346
|
+
}
|
|
347
|
+
const result = redactBody(text, {
|
|
348
|
+
contentType,
|
|
349
|
+
nonJson: route.request.nonJson,
|
|
350
|
+
redact: vault ? (value) => vault.redact(value) : (value) => policy.redact(value),
|
|
351
|
+
...(runtime.paths ? { paths: runtime.paths } : {}),
|
|
352
|
+
});
|
|
353
|
+
body = Buffer.from(result.body, 'utf8');
|
|
354
|
+
}
|
|
355
|
+
const headers = new Headers();
|
|
356
|
+
for (const [name, value] of Object.entries(req.headers)) {
|
|
357
|
+
const lower = name.toLowerCase();
|
|
358
|
+
if (HOP_BY_HOP.has(lower) || lower === 'host' || lower === 'content-length' || lower === 'accept-encoding') {
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
if (Array.isArray(value)) {
|
|
362
|
+
for (const entry of value)
|
|
363
|
+
headers.append(name, entry);
|
|
364
|
+
}
|
|
365
|
+
else if (value !== undefined) {
|
|
366
|
+
headers.set(name, value);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
for (const [name, value] of Object.entries(route.headers ?? {}))
|
|
370
|
+
headers.set(name, value);
|
|
371
|
+
const method = req.method ?? 'GET';
|
|
372
|
+
// Content-Length is deliberately not set: the body changed length during
|
|
373
|
+
// redaction, and the fetch implementation recomputes it from the buffer.
|
|
374
|
+
const sendsBody = method !== 'GET' && method !== 'HEAD';
|
|
375
|
+
const target = upstreamUrl(route, pathname, search);
|
|
376
|
+
const controller = new AbortController();
|
|
377
|
+
const timeout = setTimeout(() => controller.abort(), config.limits.requestTimeoutMs);
|
|
378
|
+
res.on('close', () => controller.abort());
|
|
379
|
+
let upstream;
|
|
380
|
+
try {
|
|
381
|
+
upstream = await fetch(target, {
|
|
382
|
+
method,
|
|
383
|
+
headers,
|
|
384
|
+
...(sendsBody ? { body: asBodyInit(body) } : {}),
|
|
385
|
+
redirect: 'manual',
|
|
386
|
+
signal: controller.signal,
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
catch (error) {
|
|
390
|
+
metrics.upstreamFailures++;
|
|
391
|
+
clearTimeout(timeout);
|
|
392
|
+
logger.error({
|
|
393
|
+
msg: 'upstream request failed',
|
|
394
|
+
route: route.name,
|
|
395
|
+
method,
|
|
396
|
+
path: pathname,
|
|
397
|
+
reason: error.name,
|
|
398
|
+
});
|
|
399
|
+
if (!res.headersSent)
|
|
400
|
+
sendError(res, 502, 'ERR_UPSTREAM', 'the upstream service could not be reached');
|
|
401
|
+
return 502;
|
|
402
|
+
}
|
|
403
|
+
const responseHeaders = {};
|
|
404
|
+
upstream.headers.forEach((value, name) => {
|
|
405
|
+
const lower = name.toLowerCase();
|
|
406
|
+
if (HOP_BY_HOP.has(lower) || REWRITTEN_RESPONSE_HEADERS.has(lower) || lower === 'set-cookie')
|
|
407
|
+
return;
|
|
408
|
+
responseHeaders[name] = value;
|
|
409
|
+
});
|
|
410
|
+
// Set-Cookie is the one header that may legitimately repeat, and it is only
|
|
411
|
+
// recoverable through getSetCookie(); iterating Headers joins the values.
|
|
412
|
+
const cookies = upstream.headers.getSetCookie?.() ?? [];
|
|
413
|
+
if (cookies.length)
|
|
414
|
+
responseHeaders['set-cookie'] = cookies;
|
|
415
|
+
responseHeaders['x-flare-redact'] = `route=${route.name}; findings=${findings.length}; restored=${vault ? 'on' : 'off'}`;
|
|
416
|
+
const responseType = upstream.headers.get('content-type') ?? '';
|
|
417
|
+
const streaming = isEventStream(responseType);
|
|
418
|
+
const stream = upstream.body;
|
|
419
|
+
try {
|
|
420
|
+
if (!stream) {
|
|
421
|
+
res.writeHead(upstream.status, responseHeaders).end();
|
|
422
|
+
}
|
|
423
|
+
else if (vault && streaming) {
|
|
424
|
+
res.writeHead(upstream.status, responseHeaders);
|
|
425
|
+
const restorer = streamRestorer(vault, route.response.streamEscape);
|
|
426
|
+
const decoder = new TextDecoder();
|
|
427
|
+
const reader = stream.getReader();
|
|
428
|
+
for (;;) {
|
|
429
|
+
const { done, value } = await reader.read();
|
|
430
|
+
if (done)
|
|
431
|
+
break;
|
|
432
|
+
const text = restorer.push(decoder.decode(value, { stream: true }));
|
|
433
|
+
if (text) {
|
|
434
|
+
const chunk = Buffer.from(text, 'utf8');
|
|
435
|
+
metrics.bytesOut += chunk.length;
|
|
436
|
+
await writeChunk(res, chunk);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
const tail = restorer.flush() + decoder.decode();
|
|
440
|
+
if (tail) {
|
|
441
|
+
const chunk = Buffer.from(tail, 'utf8');
|
|
442
|
+
metrics.bytesOut += chunk.length;
|
|
443
|
+
await writeChunk(res, chunk);
|
|
444
|
+
}
|
|
445
|
+
res.end();
|
|
446
|
+
}
|
|
447
|
+
else if (vault) {
|
|
448
|
+
const text = await upstream.text();
|
|
449
|
+
const restored = restoreBody(text, responseType, vault);
|
|
450
|
+
const chunk = Buffer.from(restored, 'utf8');
|
|
451
|
+
metrics.bytesOut += chunk.length;
|
|
452
|
+
res.writeHead(upstream.status, { ...responseHeaders, 'content-length': chunk.length }).end(chunk);
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
res.writeHead(upstream.status, responseHeaders);
|
|
456
|
+
const reader = stream.getReader();
|
|
457
|
+
for (;;) {
|
|
458
|
+
const { done, value } = await reader.read();
|
|
459
|
+
if (done)
|
|
460
|
+
break;
|
|
461
|
+
const chunk = Buffer.from(value);
|
|
462
|
+
metrics.bytesOut += chunk.length;
|
|
463
|
+
await writeChunk(res, chunk);
|
|
464
|
+
}
|
|
465
|
+
res.end();
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
finally {
|
|
469
|
+
clearTimeout(timeout);
|
|
470
|
+
}
|
|
471
|
+
logger.info({
|
|
472
|
+
msg: 'proxied',
|
|
473
|
+
route: route.name,
|
|
474
|
+
method,
|
|
475
|
+
path: pathname,
|
|
476
|
+
status: upstream.status,
|
|
477
|
+
durationMs: Date.now() - started,
|
|
478
|
+
findings: summarise(findings).byDetector,
|
|
479
|
+
placeholders: vault?.size ?? 0,
|
|
480
|
+
});
|
|
481
|
+
return upstream.status;
|
|
482
|
+
};
|
|
483
|
+
// ---------------------------------------------------------------------- //
|
|
484
|
+
// Dispatch
|
|
485
|
+
// ---------------------------------------------------------------------- //
|
|
486
|
+
const server = createServer((req, res) => {
|
|
487
|
+
const url = new URL(req.url ?? '/', 'http://gateway.invalid');
|
|
488
|
+
const pathname = url.pathname;
|
|
489
|
+
const isApi = isControlPath(pathname);
|
|
490
|
+
const handle = async () => {
|
|
491
|
+
if (isApi) {
|
|
492
|
+
await handleApi(req, res, pathname);
|
|
493
|
+
return res.statusCode;
|
|
494
|
+
}
|
|
495
|
+
const runtime = runtimes.find((candidate) => matchRoute([candidate.route], pathname));
|
|
496
|
+
if (!runtime) {
|
|
497
|
+
sendError(res, 404, 'ERR_NO_ROUTE', 'no route matches this path');
|
|
498
|
+
return 404;
|
|
499
|
+
}
|
|
500
|
+
return handleProxy(req, res, runtime, pathname, url.search);
|
|
501
|
+
};
|
|
502
|
+
handle()
|
|
503
|
+
.then((status) => {
|
|
504
|
+
metrics.request(isApi ? 'api' : (matchRoute(config.routes, pathname)?.name ?? 'none'), status);
|
|
505
|
+
})
|
|
506
|
+
.catch((error) => {
|
|
507
|
+
const status = error instanceof HttpError ? error.status : 500;
|
|
508
|
+
const code = error instanceof HttpError ? error.code : 'ERR_INTERNAL';
|
|
509
|
+
const message = error instanceof HttpError ? error.message : 'the gateway failed to handle this request';
|
|
510
|
+
if (status >= 500) {
|
|
511
|
+
metrics.errors++;
|
|
512
|
+
logger.error({ msg: 'request failed', path: pathname, status, reason: error?.message });
|
|
513
|
+
}
|
|
514
|
+
metrics.request(isApi ? 'api' : 'proxy', status);
|
|
515
|
+
if (!res.headersSent) {
|
|
516
|
+
if (error instanceof HttpError && error.closeConnection)
|
|
517
|
+
res.setHeader('connection', 'close');
|
|
518
|
+
sendError(res, status, code, message);
|
|
519
|
+
}
|
|
520
|
+
else
|
|
521
|
+
res.end();
|
|
522
|
+
});
|
|
523
|
+
});
|
|
524
|
+
server.headersTimeout = config.limits.requestTimeoutMs + 5_000;
|
|
525
|
+
server.requestTimeout = config.limits.requestTimeoutMs + 10_000;
|
|
526
|
+
return {
|
|
527
|
+
config,
|
|
528
|
+
server,
|
|
529
|
+
metrics,
|
|
530
|
+
sessions,
|
|
531
|
+
listen() {
|
|
532
|
+
return new Promise((resolve, reject) => {
|
|
533
|
+
server.once('error', reject);
|
|
534
|
+
server.listen(config.listen.port, config.listen.host, () => {
|
|
535
|
+
server.off('error', reject);
|
|
536
|
+
const address = server.address();
|
|
537
|
+
const bound = typeof address === 'object' && address
|
|
538
|
+
? { host: address.address, port: address.port }
|
|
539
|
+
: { host: config.listen.host, port: config.listen.port };
|
|
540
|
+
logger.info({
|
|
541
|
+
msg: 'listening',
|
|
542
|
+
host: bound.host,
|
|
543
|
+
port: bound.port,
|
|
544
|
+
routes: config.routes.map((route) => `${route.prefix} -> ${route.upstream}`),
|
|
545
|
+
authenticated: Boolean(config.token),
|
|
546
|
+
});
|
|
547
|
+
resolve(bound);
|
|
548
|
+
});
|
|
549
|
+
});
|
|
550
|
+
},
|
|
551
|
+
close() {
|
|
552
|
+
sessions.clear();
|
|
553
|
+
return new Promise((resolve, reject) => {
|
|
554
|
+
server.close((error) => (error ? reject(error) : resolve()));
|
|
555
|
+
server.closeAllConnections?.();
|
|
556
|
+
});
|
|
557
|
+
},
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/gateway/server.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,YAAY,EAA0D,MAAM,WAAW,CAAC;AACjG,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEvC,OAAO,EACL,aAAa,EACb,WAAW,EACX,SAAS,GAIV,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,UAAU,EACV,WAAW,GAGZ,MAAM,aAAa,CAAC;AACrB,OAAO,EACL,aAAa,EACb,SAAS,EACT,UAAU,EACV,WAAW,EACX,cAAc,GAEf,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,EAAe,MAAM,cAAc,CAAC;AAEhF,MAAM,eAAe,GAAW,CAAC,GAAG,EAAE;IACpC,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,GAAG,CAAC,oBAAoB,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,CAAyB,CAAC;QACrH,OAAO,GAAG,CAAC,OAAO,IAAI,OAAO,CAAC;IAChC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,OAAO,CAAC;IACjB,CAAC;AACH,CAAC,CAAC,EA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,CAAC,IAAI,CAAC;wBACV,GAAG,EAAE,WAAW;wBAChB,IAAI,EAAE,KAAK,CAAC,IAAI;wBAChB,IAAI,EAAE,KAAK,CAAC,IAAI;wBAChB,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,KAAK,CAAC,MAAM,OAAO,KAAK,CAAC,QAAQ,EAAE,CAAC;wBAC5E,aAAa,EAAE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC;qBACrC,CAAC,CAAC;oBACH,OAAO,CAAC,KAAK,CAAC,CAAC;gBACjB,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC;QACD,KAAK;YACH,QAAQ,CAAC,KAAK,EAAE,CAAC;YACjB,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;gBACrC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;gBAC7D,MAAM,CAAC,mBAAmB,EAAE,EAAE,CAAC;YACjC,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/help.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The CLI's help system.
|
|
3
|
+
*
|
|
4
|
+
* `--help` prints the summary. `help <topic>` prints the part you actually
|
|
5
|
+
* needed, because a flag list tells you a flag exists and nothing about when to
|
|
6
|
+
* reach for it. Every topic here is written from the behaviour in this
|
|
7
|
+
* repository; if one disagrees with the code, the topic is the bug.
|
|
8
|
+
*/
|
|
9
|
+
export interface HelpTopic {
|
|
10
|
+
/** Topic name, as typed after `help`. */
|
|
11
|
+
readonly name: string;
|
|
12
|
+
/** One line, shown in the topic index. */
|
|
13
|
+
readonly summary: string;
|
|
14
|
+
/** The topic itself. */
|
|
15
|
+
readonly body: string;
|
|
16
|
+
}
|
|
17
|
+
export declare const HELP = "flare-redact \u2014 hide secrets & PII before they hit a log\n\nUSAGE\n flare-redact [options] [files...] stdin if no files\n flare-redact gateway [options] run the redaction sidecar proxy\n flare-redact help [topic] explain a topic in depth\n\nOPTIONS\n --scan list what would be redacted, and why (input unchanged)\n --format <f> scan output: pretty | json | sarif (default: pretty)\n --sarif shorthand for --scan --format sarif\n --summary print a count of findings per detector\n --json parse input as JSON and redact recursively\n --csv parse input as CSV and redact every cell\n --mode <m> mask | label | hash | pseudonym | surrogate\n (fpe is a deprecated alias for pseudonym)\n --secret-env <n> read the transform key from env var <n>\n (default: FLARE_REDACT_SECRET)\n --hash-salt <s> deprecated; prefer --secret-env\n --min-confidence <n>\n drop findings below this confidence (0-1)\n --refine-confidence\n use the learned classifier to refine confidence for\n generic detectors (fewer false positives on UUIDs,\n hashes, and slugs); pairs with --min-confidence\n --include-values include raw matched values in --scan output\n (unsafe for logs and reports)\n --only <ids> use only these detectors (comma-separated)\n --enable <ids> turn on extra detectors (e.g. ipv4,high_entropy)\n --disable <ids> turn off detectors (e.g. email)\n --mask <str> replace every secret with this string\n --allow <vals> never redact these exact values (comma-separated)\n --exclude <glob> skip matching paths during directory scans (repeatable)\n --max-file-size <size>\n largest directory-discovered file to scan (default: 1mb)\n accepts bytes or kb/mb/gb suffixes\n --no-default-excludes\n also walk .git, .hg, .svn, node_modules, and vendor\n --term <word> also catch this exact word/phrase (repeatable)\n --terms <file> also catch every word/phrase in this file (one per line)\n --vault <file> reversible: write an AES-GCM encrypted map to <file>\n --restore <file> restore using an encrypted map written by --vault\n --vault-password-env <n>\n vault password env var (default: FLARE_REDACT_VAULT_PASSWORD)\n --allow-plaintext-vault\n explicitly allow reading a legacy plaintext vault\n --list show all detectors and exit\n -h, --help show this help\n -v, --version show version\n\nSUBCOMMANDS\n gateway run a reverse proxy that redacts request bodies and\n restores the originals in the reply \u2014 no code changes\n in the calling application. See: flare-redact gateway help\n help explain a topic. Run 'flare-redact help topics' for the\n index, or 'flare-redact help <detector>' for one detector.\n\nEXAMPLES\n tail -f app.log | flare-redact\n flare-redact --scan config.env\n flare-redact --scan .\n flare-redact --scan . --exclude 'fixtures/**' --max-file-size 2mb\n flare-redact --scan --format json .env app.log\n flare-redact --sarif .env > flare-redact.sarif\n flare-redact --json --mode hash < event.json\n flare-redact --enable high_entropy,ipv4 < app.log\n flare-redact --scan --enable high_entropy --refine-confidence --min-confidence 0.5 app.log\n\nStuck? 'flare-redact help topics' lists everything this help can explain.\n";
|
|
18
|
+
export declare const TOPICS: readonly HelpTopic[];
|
|
19
|
+
/** The topic index, as printed by `help topics`. */
|
|
20
|
+
export declare function topicIndex(): string;
|
|
21
|
+
export interface HelpResult {
|
|
22
|
+
readonly text: string;
|
|
23
|
+
/** 0 when we answered the question, 2 when we could not. */
|
|
24
|
+
readonly code: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Resolve `help [topic]`. Returns the text and the exit code rather than
|
|
28
|
+
* writing, so the caller decides between stdout and stderr and the tests can
|
|
29
|
+
* read it back.
|
|
30
|
+
*/
|
|
31
|
+
export declare function renderHelp(topic?: string): HelpResult;
|
|
32
|
+
//# sourceMappingURL=help.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"help.d.ts","sourceRoot":"","sources":["../src/help.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,MAAM,WAAW,SAAS;IACxB,yCAAyC;IACzC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0CAA0C;IAC1C,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,wBAAwB;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,eAAO,MAAM,IAAI,2jHAqEhB,CAAC;AAoRF,eAAO,MAAM,MAAM,EAAE,SAAS,SAAS,EAAe,CAAC;AAEvD,oDAAoD;AACpD,wBAAgB,UAAU,IAAI,MAAM,CAWnC;AAoCD,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,4DAA4D;IAC5D,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,UAAU,CAiBrD"}
|