flare-redact 1.4.1 → 1.5.0
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 +91 -0
- package/README.md +107 -0
- package/SECURITY.md +36 -0
- package/bin/flare-gateway.mjs +6 -0
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +12 -0
- 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 +230 -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/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 -4
- 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,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway configuration: a file, environment variables, and command-line flags,
|
|
3
|
+
* merged in that order of increasing precedence and then validated once.
|
|
4
|
+
*
|
|
5
|
+
* The defaults are the safe ones. The listener binds to loopback, the `/v1` API
|
|
6
|
+
* refuses unauthenticated calls as soon as a token exists, bodies are capped,
|
|
7
|
+
* and an upstream must be an explicit absolute `http(s)` URL — a redaction proxy
|
|
8
|
+
* that can be pointed anywhere by a header is an open relay.
|
|
9
|
+
*/
|
|
10
|
+
import type { RedactOptions } from '../index.js';
|
|
11
|
+
export interface ListenConfig {
|
|
12
|
+
host: string;
|
|
13
|
+
port: number;
|
|
14
|
+
}
|
|
15
|
+
export interface RequestRedactionConfig {
|
|
16
|
+
/**
|
|
17
|
+
* JSON paths to redact, e.g. `["messages[*].content", "input", "prompt"]`.
|
|
18
|
+
* Omit to redact every string in the body — safest, and the default.
|
|
19
|
+
*/
|
|
20
|
+
paths?: string[];
|
|
21
|
+
/** Set false to forward request bodies untouched (response restore still works). */
|
|
22
|
+
enabled: boolean;
|
|
23
|
+
/** Also redact `application/x-www-form-urlencoded` values and plain text. */
|
|
24
|
+
nonJson: boolean;
|
|
25
|
+
}
|
|
26
|
+
export interface ResponseRestorationConfig {
|
|
27
|
+
/**
|
|
28
|
+
* Put the originals back into the upstream's answer. This is what makes the
|
|
29
|
+
* proxy transparent: the model sees placeholders, your app sees real data.
|
|
30
|
+
*/
|
|
31
|
+
enabled: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* How a placeholder inside a streamed frame is replaced. `json` escapes the
|
|
34
|
+
* original for a JSON string context, which is what every LLM streaming API
|
|
35
|
+
* puts on the wire; `none` substitutes verbatim.
|
|
36
|
+
*/
|
|
37
|
+
streamEscape: 'json' | 'none';
|
|
38
|
+
}
|
|
39
|
+
export interface RouteConfig {
|
|
40
|
+
/** Stable name used in logs and metrics. Defaults to the prefix. */
|
|
41
|
+
name: string;
|
|
42
|
+
/** Path prefix this route claims, e.g. `/openai`. `/` is the catch-all. */
|
|
43
|
+
prefix: string;
|
|
44
|
+
/** Absolute upstream origin, e.g. `https://api.openai.com`. */
|
|
45
|
+
upstream: string;
|
|
46
|
+
/** Remove `prefix` before forwarding. Default: true for anything but `/`. */
|
|
47
|
+
stripPrefix: boolean;
|
|
48
|
+
/** Policy overrides layered on top of the gateway-wide policy. */
|
|
49
|
+
policy?: RedactOptions;
|
|
50
|
+
request: RequestRedactionConfig;
|
|
51
|
+
response: ResponseRestorationConfig;
|
|
52
|
+
/** Extra headers to set on the upstream request, e.g. a vendor API version. */
|
|
53
|
+
headers?: Record<string, string>;
|
|
54
|
+
}
|
|
55
|
+
export interface GatewayConfig {
|
|
56
|
+
listen: ListenConfig;
|
|
57
|
+
/** Bearer token required by `/v1`. Unset means the API is open — loopback only. */
|
|
58
|
+
token?: string;
|
|
59
|
+
policy: RedactOptions;
|
|
60
|
+
routes: RouteConfig[];
|
|
61
|
+
sessions: {
|
|
62
|
+
ttlMs: number;
|
|
63
|
+
max: number;
|
|
64
|
+
};
|
|
65
|
+
limits: {
|
|
66
|
+
maxBodyBytes: number;
|
|
67
|
+
requestTimeoutMs: number;
|
|
68
|
+
};
|
|
69
|
+
/** Count findings per detector for logs and metrics. Costs one extra scan. */
|
|
70
|
+
auditFindings: boolean;
|
|
71
|
+
metrics: boolean;
|
|
72
|
+
log: 'silent' | 'info' | 'debug';
|
|
73
|
+
}
|
|
74
|
+
export declare class GatewayConfigError extends Error {
|
|
75
|
+
constructor(message: string);
|
|
76
|
+
}
|
|
77
|
+
export declare const DEFAULT_PORT = 8787;
|
|
78
|
+
/** Paths the gateway answers itself; a route may not claim them. */
|
|
79
|
+
export declare const RESERVED_PREFIXES: string[];
|
|
80
|
+
/** A route as written in a config file: everything but `upstream` is optional. */
|
|
81
|
+
export type RawRoute = Partial<Omit<RouteConfig, 'request' | 'response'>> & {
|
|
82
|
+
request?: Partial<RequestRedactionConfig>;
|
|
83
|
+
response?: Partial<ResponseRestorationConfig>;
|
|
84
|
+
};
|
|
85
|
+
export interface RawConfig {
|
|
86
|
+
listen?: Partial<ListenConfig>;
|
|
87
|
+
token?: string;
|
|
88
|
+
policy?: RedactOptions;
|
|
89
|
+
routes?: RawRoute[];
|
|
90
|
+
upstream?: string;
|
|
91
|
+
sessions?: Partial<GatewayConfig['sessions']>;
|
|
92
|
+
limits?: Partial<GatewayConfig['limits']>;
|
|
93
|
+
auditFindings?: boolean;
|
|
94
|
+
metrics?: boolean;
|
|
95
|
+
log?: GatewayConfig['log'];
|
|
96
|
+
}
|
|
97
|
+
/** Environment overrides, for the container case where a file is inconvenient. */
|
|
98
|
+
export declare function configFromEnv(env: NodeJS.ProcessEnv): RawConfig;
|
|
99
|
+
/**
|
|
100
|
+
* Build a validated configuration from any number of partial layers, later
|
|
101
|
+
* layers winning. Throws {@link GatewayConfigError} with a message that names
|
|
102
|
+
* the offending field — a proxy that starts with a subtly wrong upstream is
|
|
103
|
+
* worse than one that refuses to start.
|
|
104
|
+
*/
|
|
105
|
+
export declare function resolveConfig(...layers: Array<RawConfig | undefined>): GatewayConfig;
|
|
106
|
+
/** The route claiming `pathname`, or undefined when the gateway is API-only. */
|
|
107
|
+
export declare function matchRoute(routes: RouteConfig[], pathname: string): RouteConfig | undefined;
|
|
108
|
+
/** The upstream URL a request is forwarded to. */
|
|
109
|
+
export declare function upstreamUrl(route: RouteConfig, pathname: string, search: string): string;
|
|
110
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/gateway/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,aAAa,E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AAChF,wBAAgB,UAAU,CAAC,MAAM,EAAE,WAAW,EAAE,EAAE,QAAQ,EAAE,MAAM,GAAG,WAAW,GAAG,SAAS,CAK3F;AAED,kDAAkD;AAClD,wBAAgB,WAAW,CAAC,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAIxF"}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway configuration: a file, environment variables, and command-line flags,
|
|
3
|
+
* merged in that order of increasing precedence and then validated once.
|
|
4
|
+
*
|
|
5
|
+
* The defaults are the safe ones. The listener binds to loopback, the `/v1` API
|
|
6
|
+
* refuses unauthenticated calls as soon as a token exists, bodies are capped,
|
|
7
|
+
* and an upstream must be an explicit absolute `http(s)` URL — a redaction proxy
|
|
8
|
+
* that can be pointed anywhere by a header is an open relay.
|
|
9
|
+
*/
|
|
10
|
+
export class GatewayConfigError extends Error {
|
|
11
|
+
constructor(message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = 'GatewayConfigError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export const DEFAULT_PORT = 8787;
|
|
17
|
+
const DEFAULTS = {
|
|
18
|
+
listen: { host: '127.0.0.1', port: DEFAULT_PORT },
|
|
19
|
+
policy: {},
|
|
20
|
+
routes: [],
|
|
21
|
+
sessions: { ttlMs: 30 * 60_000, max: 10_000 },
|
|
22
|
+
limits: { maxBodyBytes: 8 * 1024 * 1024, requestTimeoutMs: 120_000 },
|
|
23
|
+
auditFindings: true,
|
|
24
|
+
metrics: true,
|
|
25
|
+
log: 'info',
|
|
26
|
+
};
|
|
27
|
+
/** Paths the gateway answers itself; a route may not claim them. */
|
|
28
|
+
export const RESERVED_PREFIXES = ['/healthz', '/readyz', '/metrics', '/v1/'];
|
|
29
|
+
function normaliseUpstream(value, where) {
|
|
30
|
+
let url;
|
|
31
|
+
try {
|
|
32
|
+
url = new URL(value);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
throw new GatewayConfigError(`${where}: upstream must be an absolute URL, received ${JSON.stringify(value)}`);
|
|
36
|
+
}
|
|
37
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
|
38
|
+
throw new GatewayConfigError(`${where}: upstream must use http or https, received ${url.protocol}`);
|
|
39
|
+
}
|
|
40
|
+
if (url.search || url.hash) {
|
|
41
|
+
throw new GatewayConfigError(`${where}: upstream must not carry a query string or fragment`);
|
|
42
|
+
}
|
|
43
|
+
// Keep any base path, drop the trailing slash so joining is unambiguous.
|
|
44
|
+
return (url.origin + url.pathname).replace(/\/$/, '');
|
|
45
|
+
}
|
|
46
|
+
function normalisePrefix(value, where) {
|
|
47
|
+
if (!value.startsWith('/'))
|
|
48
|
+
throw new GatewayConfigError(`${where}: prefix must start with "/"`);
|
|
49
|
+
const trimmed = value.length > 1 ? value.replace(/\/$/, '') : '/';
|
|
50
|
+
for (const reserved of RESERVED_PREFIXES) {
|
|
51
|
+
if (reserved.startsWith(trimmed === '/' ? '/x' : trimmed)) {
|
|
52
|
+
throw new GatewayConfigError(`${where}: prefix ${JSON.stringify(trimmed)} would shadow the gateway's own ${reserved} endpoint`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return trimmed;
|
|
56
|
+
}
|
|
57
|
+
function normaliseRoute(raw, index) {
|
|
58
|
+
const where = `routes[${index}]`;
|
|
59
|
+
if (!raw.upstream)
|
|
60
|
+
throw new GatewayConfigError(`${where}: upstream is required`);
|
|
61
|
+
const prefix = normalisePrefix(raw.prefix ?? '/', where);
|
|
62
|
+
return {
|
|
63
|
+
name: raw.name ?? prefix,
|
|
64
|
+
prefix,
|
|
65
|
+
upstream: normaliseUpstream(raw.upstream, where),
|
|
66
|
+
stripPrefix: raw.stripPrefix ?? prefix !== '/',
|
|
67
|
+
...(raw.policy ? { policy: raw.policy } : {}),
|
|
68
|
+
request: {
|
|
69
|
+
enabled: raw.request?.enabled ?? true,
|
|
70
|
+
nonJson: raw.request?.nonJson ?? false,
|
|
71
|
+
...(raw.request?.paths?.length ? { paths: raw.request.paths } : {}),
|
|
72
|
+
},
|
|
73
|
+
response: {
|
|
74
|
+
enabled: raw.response?.enabled ?? true,
|
|
75
|
+
streamEscape: raw.response?.streamEscape ?? 'json',
|
|
76
|
+
},
|
|
77
|
+
...(raw.headers ? { headers: raw.headers } : {}),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function positiveInteger(value, where) {
|
|
81
|
+
if (value === undefined)
|
|
82
|
+
return undefined;
|
|
83
|
+
const parsed = Number(value);
|
|
84
|
+
if (!Number.isSafeInteger(parsed) || parsed <= 0) {
|
|
85
|
+
throw new GatewayConfigError(`${where} must be a positive integer, received ${JSON.stringify(value)}`);
|
|
86
|
+
}
|
|
87
|
+
return parsed;
|
|
88
|
+
}
|
|
89
|
+
function csv(value) {
|
|
90
|
+
if (!value)
|
|
91
|
+
return undefined;
|
|
92
|
+
const items = value.split(',').map((item) => item.trim()).filter(Boolean);
|
|
93
|
+
return items.length ? items : undefined;
|
|
94
|
+
}
|
|
95
|
+
/** Environment overrides, for the container case where a file is inconvenient. */
|
|
96
|
+
export function configFromEnv(env) {
|
|
97
|
+
const raw = {};
|
|
98
|
+
const host = env.FLARE_GATEWAY_HOST;
|
|
99
|
+
const port = positiveInteger(env.FLARE_GATEWAY_PORT, 'FLARE_GATEWAY_PORT');
|
|
100
|
+
if (host || port)
|
|
101
|
+
raw.listen = { ...(host ? { host } : {}), ...(port ? { port } : {}) };
|
|
102
|
+
if (env.FLARE_GATEWAY_TOKEN)
|
|
103
|
+
raw.token = env.FLARE_GATEWAY_TOKEN;
|
|
104
|
+
if (env.FLARE_UPSTREAM)
|
|
105
|
+
raw.upstream = env.FLARE_UPSTREAM;
|
|
106
|
+
const policy = {};
|
|
107
|
+
const only = csv(env.FLARE_REDACT_ONLY);
|
|
108
|
+
const enable = csv(env.FLARE_REDACT_ENABLE);
|
|
109
|
+
const disable = csv(env.FLARE_REDACT_DISABLE);
|
|
110
|
+
if (only)
|
|
111
|
+
policy.only = only;
|
|
112
|
+
if (enable)
|
|
113
|
+
policy.enable = enable;
|
|
114
|
+
if (disable)
|
|
115
|
+
policy.disable = disable;
|
|
116
|
+
if (env.FLARE_REDACT_MODE)
|
|
117
|
+
policy.mode = env.FLARE_REDACT_MODE;
|
|
118
|
+
if (env.FLARE_REDACT_SECRET)
|
|
119
|
+
policy.transformSecret = env.FLARE_REDACT_SECRET;
|
|
120
|
+
if (Object.keys(policy).length)
|
|
121
|
+
raw.policy = policy;
|
|
122
|
+
const maxBodyBytes = positiveInteger(env.FLARE_GATEWAY_MAX_BODY_BYTES, 'FLARE_GATEWAY_MAX_BODY_BYTES');
|
|
123
|
+
const requestTimeoutMs = positiveInteger(env.FLARE_GATEWAY_TIMEOUT_MS, 'FLARE_GATEWAY_TIMEOUT_MS');
|
|
124
|
+
if (maxBodyBytes || requestTimeoutMs) {
|
|
125
|
+
raw.limits = { ...(maxBodyBytes ? { maxBodyBytes } : {}), ...(requestTimeoutMs ? { requestTimeoutMs } : {}) };
|
|
126
|
+
}
|
|
127
|
+
const ttlMs = positiveInteger(env.FLARE_GATEWAY_SESSION_TTL_MS, 'FLARE_GATEWAY_SESSION_TTL_MS');
|
|
128
|
+
if (ttlMs)
|
|
129
|
+
raw.sessions = { ttlMs };
|
|
130
|
+
if (env.FLARE_GATEWAY_LOG)
|
|
131
|
+
raw.log = env.FLARE_GATEWAY_LOG;
|
|
132
|
+
if (env.FLARE_GATEWAY_METRICS === 'false')
|
|
133
|
+
raw.metrics = false;
|
|
134
|
+
if (env.FLARE_GATEWAY_AUDIT_FINDINGS === 'false')
|
|
135
|
+
raw.auditFindings = false;
|
|
136
|
+
return raw;
|
|
137
|
+
}
|
|
138
|
+
function mergeRaw(base, override) {
|
|
139
|
+
return {
|
|
140
|
+
...base,
|
|
141
|
+
...override,
|
|
142
|
+
listen: { ...base.listen, ...override.listen },
|
|
143
|
+
policy: { ...base.policy, ...override.policy },
|
|
144
|
+
sessions: { ...base.sessions, ...override.sessions },
|
|
145
|
+
limits: { ...base.limits, ...override.limits },
|
|
146
|
+
routes: override.routes ?? base.routes,
|
|
147
|
+
upstream: override.upstream ?? base.upstream,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Build a validated configuration from any number of partial layers, later
|
|
152
|
+
* layers winning. Throws {@link GatewayConfigError} with a message that names
|
|
153
|
+
* the offending field — a proxy that starts with a subtly wrong upstream is
|
|
154
|
+
* worse than one that refuses to start.
|
|
155
|
+
*/
|
|
156
|
+
export function resolveConfig(...layers) {
|
|
157
|
+
const raw = layers.filter((layer) => Boolean(layer)).reduce(mergeRaw, {});
|
|
158
|
+
const rawRoutes = raw.routes?.length
|
|
159
|
+
? raw.routes
|
|
160
|
+
: raw.upstream
|
|
161
|
+
? [{ prefix: '/', upstream: raw.upstream }]
|
|
162
|
+
: [];
|
|
163
|
+
const routes = rawRoutes.map(normaliseRoute);
|
|
164
|
+
const seen = new Set();
|
|
165
|
+
for (const route of routes) {
|
|
166
|
+
if (seen.has(route.prefix))
|
|
167
|
+
throw new GatewayConfigError(`duplicate route prefix ${JSON.stringify(route.prefix)}`);
|
|
168
|
+
seen.add(route.prefix);
|
|
169
|
+
}
|
|
170
|
+
// Longest prefix first, so `/openai/v1` wins over `/openai`.
|
|
171
|
+
routes.sort((a, b) => b.prefix.length - a.prefix.length);
|
|
172
|
+
const log = raw.log ?? DEFAULTS.log;
|
|
173
|
+
if (log !== 'silent' && log !== 'info' && log !== 'debug') {
|
|
174
|
+
throw new GatewayConfigError(`log must be silent, info or debug, received ${JSON.stringify(log)}`);
|
|
175
|
+
}
|
|
176
|
+
const port = raw.listen?.port ?? DEFAULTS.listen.port;
|
|
177
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535) {
|
|
178
|
+
throw new GatewayConfigError(`listen.port must be between 0 and 65535, received ${JSON.stringify(port)}`);
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
listen: { host: raw.listen?.host ?? DEFAULTS.listen.host, port },
|
|
182
|
+
...(raw.token ? { token: raw.token } : {}),
|
|
183
|
+
policy: raw.policy ?? DEFAULTS.policy,
|
|
184
|
+
routes,
|
|
185
|
+
sessions: {
|
|
186
|
+
ttlMs: raw.sessions?.ttlMs ?? DEFAULTS.sessions.ttlMs,
|
|
187
|
+
max: raw.sessions?.max ?? DEFAULTS.sessions.max,
|
|
188
|
+
},
|
|
189
|
+
limits: {
|
|
190
|
+
maxBodyBytes: raw.limits?.maxBodyBytes ?? DEFAULTS.limits.maxBodyBytes,
|
|
191
|
+
requestTimeoutMs: raw.limits?.requestTimeoutMs ?? DEFAULTS.limits.requestTimeoutMs,
|
|
192
|
+
},
|
|
193
|
+
auditFindings: raw.auditFindings ?? DEFAULTS.auditFindings,
|
|
194
|
+
metrics: raw.metrics ?? DEFAULTS.metrics,
|
|
195
|
+
log,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
/** The route claiming `pathname`, or undefined when the gateway is API-only. */
|
|
199
|
+
export function matchRoute(routes, pathname) {
|
|
200
|
+
return routes.find((route) => {
|
|
201
|
+
if (route.prefix === '/')
|
|
202
|
+
return true;
|
|
203
|
+
return pathname === route.prefix || pathname.startsWith(`${route.prefix}/`);
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
/** The upstream URL a request is forwarded to. */
|
|
207
|
+
export function upstreamUrl(route, pathname, search) {
|
|
208
|
+
const remainder = route.stripPrefix && route.prefix !== '/' ? pathname.slice(route.prefix.length) : pathname;
|
|
209
|
+
const path = remainder.startsWith('/') ? remainder : `/${remainder}`;
|
|
210
|
+
return `${route.upstream}${path}${search}`;
|
|
211
|
+
}
|
|
212
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/gateway/config.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAkEH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kDAAkD;AAClD,MAAM,UAAU,WAAW,CAAC,KAAkB,EAAE,QAAgB,EAAE,MAAc;IAC9E,MAAM,SAAS,GAAG,KAAK,CAAC,WAAW,IAAI,KAAK,CAAC,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;IAC7G,MAAM,IAAI,GAAG,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,SAAS,EAAE,CAAC;IACrE,OAAO,GAAG,KAAK,CAAC,QAAQ,GAAG,IAAI,GAAG,MAAM,EAAE,CAAC;AAC7C,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `flare-redact/gateway` — the sidecar, as a library.
|
|
3
|
+
*
|
|
4
|
+
* Import this to embed the proxy in an existing Node service, to write tests
|
|
5
|
+
* against it, or to build a differently-shaped deployment. The CLI in
|
|
6
|
+
* `flare-redact gateway` is a thin wrapper over exactly these exports.
|
|
7
|
+
*
|
|
8
|
+
* import { resolveConfig, createGateway } from 'flare-redact/gateway';
|
|
9
|
+
*
|
|
10
|
+
* const gateway = createGateway(resolveConfig({
|
|
11
|
+
* upstream: 'https://api.openai.com',
|
|
12
|
+
* policy: { enable: ['pii'] },
|
|
13
|
+
* }));
|
|
14
|
+
* await gateway.listen();
|
|
15
|
+
*/
|
|
16
|
+
export { resolveConfig, configFromEnv, matchRoute, upstreamUrl, GatewayConfigError, DEFAULT_PORT, RESERVED_PREFIXES, type GatewayConfig, type RawConfig, type RawRoute, type RouteConfig, type ListenConfig, type RequestRedactionConfig, type ResponseRestorationConfig, } from './config.js';
|
|
17
|
+
export { parsePath, transformAtPath, transformAtPaths, redactBody, restoreBody, streamRestorer, jsonEscapedEntries, isJsonType, isFormType, isTextType, isEventStream, PathSyntaxError, type ParsedPath, type PathSegment, type BodyRedactionOptions, type BodyRedactionResult, } from './body.js';
|
|
18
|
+
export { SessionStore, Metrics, createLogger, type Logger, type LogLevel, type SessionRecord, type SessionStoreOptions, } from './runtime.js';
|
|
19
|
+
export { createGateway, type Gateway } from './server.js';
|
|
20
|
+
export { main as runGateway, buildConfig, parseGatewayArgs, GATEWAY_HELP } from './cli.js';
|
|
21
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/gateway/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EACL,aAAa,EACb,aAAa,EACb,UAAU,EACV,WAAW,EACX,kBAAkB,EAClB,YAAY,EACZ,iBAAiB,EACjB,KAAK,aAAa,EAClB,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,sBAAsB,EAC3B,KAAK,yBAAyB,GAC/B,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,UAAU,EACV,WAAW,EACX,cAAc,EACd,kBAAkB,EAClB,UAAU,EACV,UAAU,EACV,UAAU,EACV,aAAa,EACb,eAAe,EACf,KAAK,UAAU,EACf,KAAK,WAAW,EAChB,KAAK,oBAAoB,EACzB,KAAK,mBAAmB,GACzB,MAAM,WAAW,CAAC;AAEnB,OAAO,EACL,YAAY,EACZ,OAAO,EACP,YAAY,EACZ,KAAK,MAAM,EACX,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,mBAAmB,GACzB,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAE,KAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AAE1D,OAAO,EAAE,IAAI,IAAI,UAAU,EAAE,WAAW,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `flare-redact/gateway` — the sidecar, as a library.
|
|
3
|
+
*
|
|
4
|
+
* Import this to embed the proxy in an existing Node service, to write tests
|
|
5
|
+
* against it, or to build a differently-shaped deployment. The CLI in
|
|
6
|
+
* `flare-redact gateway` is a thin wrapper over exactly these exports.
|
|
7
|
+
*
|
|
8
|
+
* import { resolveConfig, createGateway } from 'flare-redact/gateway';
|
|
9
|
+
*
|
|
10
|
+
* const gateway = createGateway(resolveConfig({
|
|
11
|
+
* upstream: 'https://api.openai.com',
|
|
12
|
+
* policy: { enable: ['pii'] },
|
|
13
|
+
* }));
|
|
14
|
+
* await gateway.listen();
|
|
15
|
+
*/
|
|
16
|
+
export { resolveConfig, configFromEnv, matchRoute, upstreamUrl, GatewayConfigError, DEFAULT_PORT, RESERVED_PREFIXES, } from './config.js';
|
|
17
|
+
export { parsePath, transformAtPath, transformAtPaths, redactBody, restoreBody, streamRestorer, jsonEscapedEntries, isJsonType, isFormType, isTextType, isEventStream, PathSyntaxError, } from './body.js';
|
|
18
|
+
export { SessionStore, Metrics, createLogger, } from './runtime.js';
|
|
19
|
+
export { createGateway } from './server.js';
|
|
20
|
+
export { main as runGateway, buildConfig, parseGatewayArgs, GATEWAY_HELP } from './cli.js';
|
|
21
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/gateway/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EACL,aAAa,EACb,aAAa,EACb,UAAU,EACV,WAAW,EACX,kBAAkB,EAClB,YAAY,EACZ,iBAAiB,GAQlB,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,SAAS,EACT,eAAe,EACf,gBAAgB,EAChB,UAAU,EACV,WAAW,EACX,cAAc,EACd,kBAAkB,EAClB,UAAU,EACV,UAAU,EACV,UAAU,EACV,aAAa,EACb,eAAe,GAKhB,MAAM,WAAW,CAAC;AAEnB,OAAO,EACL,YAAY,EACZ,OAAO,EACP,YAAY,GAKb,MAAM,cAAc,CAAC;AAEtB,OAAO,EAAE,aAAa,EAAgB,MAAM,aAAa,CAAC;AAE1D,OAAO,EAAE,IAAI,IAAI,UAAU,EAAE,WAAW,EAAE,gBAAgB,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway runtime services: sessions, metrics and the audit log.
|
|
3
|
+
*
|
|
4
|
+
* All three exist to answer the question a security team will ask on day two —
|
|
5
|
+
* *what did this thing actually remove?* — without ever writing a secret to
|
|
6
|
+
* disk. The audit log carries counts and detector ids; it never carries a value,
|
|
7
|
+
* a body, or a placeholder mapping.
|
|
8
|
+
*/
|
|
9
|
+
import { type RedactOptions, type Session } from '../index.js';
|
|
10
|
+
export interface SessionRecord {
|
|
11
|
+
id: string;
|
|
12
|
+
session: Session;
|
|
13
|
+
expiresAt: number;
|
|
14
|
+
}
|
|
15
|
+
export interface SessionStoreOptions {
|
|
16
|
+
ttlMs: number;
|
|
17
|
+
max: number;
|
|
18
|
+
/** Injectable for tests. */
|
|
19
|
+
now?: () => number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* In-memory, TTL-bounded session store.
|
|
23
|
+
*
|
|
24
|
+
* Sessions hold placeholder→original mappings, which are exactly as sensitive as
|
|
25
|
+
* the data they protect. They therefore live in memory only, expire, and are
|
|
26
|
+
* capped: an unbounded map of secrets is a memory leak with a disclosure
|
|
27
|
+
* attached. Because they are per-process, a replicated gateway needs sticky
|
|
28
|
+
* routing for session traffic — the stateless `/v1/redact` path does not.
|
|
29
|
+
*/
|
|
30
|
+
export declare class SessionStore {
|
|
31
|
+
private readonly records;
|
|
32
|
+
private readonly ttlMs;
|
|
33
|
+
private readonly max;
|
|
34
|
+
private readonly now;
|
|
35
|
+
constructor(options: SessionStoreOptions);
|
|
36
|
+
get size(): number;
|
|
37
|
+
create(options: RedactOptions): SessionRecord;
|
|
38
|
+
/** Fetch and renew a session, or undefined when it is unknown or expired. */
|
|
39
|
+
get(id: string): SessionRecord | undefined;
|
|
40
|
+
delete(id: string): boolean;
|
|
41
|
+
sweep(): number;
|
|
42
|
+
clear(): void;
|
|
43
|
+
}
|
|
44
|
+
export interface MetricsSnapshot {
|
|
45
|
+
requests: Map<string, number>;
|
|
46
|
+
findings: Map<string, number>;
|
|
47
|
+
bytesIn: number;
|
|
48
|
+
bytesOut: number;
|
|
49
|
+
errors: number;
|
|
50
|
+
upstreamFailures: number;
|
|
51
|
+
}
|
|
52
|
+
/** Counters exposed at `/metrics` in Prometheus text format. */
|
|
53
|
+
export declare class Metrics {
|
|
54
|
+
readonly requests: Map<string, number>;
|
|
55
|
+
readonly findings: Map<string, number>;
|
|
56
|
+
bytesIn: number;
|
|
57
|
+
bytesOut: number;
|
|
58
|
+
errors: number;
|
|
59
|
+
upstreamFailures: number;
|
|
60
|
+
private static bump;
|
|
61
|
+
request(route: string, status: number): void;
|
|
62
|
+
finding(detector: string, count?: number): void;
|
|
63
|
+
render(sessions: number): string;
|
|
64
|
+
}
|
|
65
|
+
export type LogLevel = 'silent' | 'info' | 'debug';
|
|
66
|
+
export interface LogEvent {
|
|
67
|
+
msg: string;
|
|
68
|
+
[key: string]: unknown;
|
|
69
|
+
}
|
|
70
|
+
export interface Logger {
|
|
71
|
+
info(event: LogEvent): void;
|
|
72
|
+
debug(event: LogEvent): void;
|
|
73
|
+
error(event: LogEvent): void;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Structured JSON-lines logging. Bodies, headers and matched values never reach
|
|
77
|
+
* this — the gateway logs what it removed, not what was removed.
|
|
78
|
+
*/
|
|
79
|
+
export declare function createLogger(level: LogLevel, write?: (line: string) => void): Logger;
|
|
80
|
+
//# sourceMappingURL=runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.d.ts","sourceRoot":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ACf,IAAI,CAAC,MAAM;gBAAE,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QACpC,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
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 { type Server } from 'node:http';
|
|
23
|
+
import { type GatewayConfig } from './config.js';
|
|
24
|
+
import { Metrics, SessionStore } from './runtime.js';
|
|
25
|
+
export interface Gateway {
|
|
26
|
+
readonly config: GatewayConfig;
|
|
27
|
+
readonly server: Server;
|
|
28
|
+
readonly metrics: Metrics;
|
|
29
|
+
readonly sessions: SessionStore;
|
|
30
|
+
listen(): Promise<{
|
|
31
|
+
host: string;
|
|
32
|
+
port: number;
|
|
33
|
+
}>;
|
|
34
|
+
close(): Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
export declare function createGateway(config: GatewayConfig): Gateway;
|
|
37
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/gateway/server.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAsC,KAAK,MAAM,EAAuB,MAAM,WAAW,CAAC;AAYjG,OAAO,EAGL,KAAK,aAAa,EAEnB,MAAM,aAAa,CAAC;AASrB,OAAO,EAAgB,OAAO,EAAE,YAAY,EAAe,MAAM,cAAc,CAAC;AA8BhF,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;IAC/B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;IAChC,MAAM,IAAI,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAClD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAyJD,wBAAgB,aAAa,CAAC,MAAM,EAAE,aAAa,GAAG,OAAO,CAoY5D"}
|