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.
Files changed (53) hide show
  1. package/CHANGELOG.md +91 -0
  2. package/README.md +107 -0
  3. package/SECURITY.md +36 -0
  4. package/bin/flare-gateway.mjs +6 -0
  5. package/dist/cli.d.ts.map +1 -1
  6. package/dist/cli.js +12 -0
  7. package/dist/cli.js.map +1 -1
  8. package/dist/detectors.d.ts +17 -0
  9. package/dist/detectors.d.ts.map +1 -1
  10. package/dist/detectors.js.map +1 -1
  11. package/dist/engine.d.ts +5 -0
  12. package/dist/engine.d.ts.map +1 -1
  13. package/dist/engine.js +50 -7
  14. package/dist/engine.js.map +1 -1
  15. package/dist/gateway/body.d.ts +90 -0
  16. package/dist/gateway/body.d.ts.map +1 -0
  17. package/dist/gateway/body.js +185 -0
  18. package/dist/gateway/body.js.map +1 -0
  19. package/dist/gateway/cli.d.ts +26 -0
  20. package/dist/gateway/cli.d.ts.map +1 -0
  21. package/dist/gateway/cli.js +230 -0
  22. package/dist/gateway/cli.js.map +1 -0
  23. package/dist/gateway/config.d.ts +110 -0
  24. package/dist/gateway/config.d.ts.map +1 -0
  25. package/dist/gateway/config.js +212 -0
  26. package/dist/gateway/config.js.map +1 -0
  27. package/dist/gateway/index.d.ts +21 -0
  28. package/dist/gateway/index.d.ts.map +1 -0
  29. package/dist/gateway/index.js +21 -0
  30. package/dist/gateway/index.js.map +1 -0
  31. package/dist/gateway/runtime.d.ts +80 -0
  32. package/dist/gateway/runtime.d.ts.map +1 -0
  33. package/dist/gateway/runtime.js +0 -0
  34. package/dist/gateway/runtime.js.map +1 -0
  35. package/dist/gateway/server.d.ts +37 -0
  36. package/dist/gateway/server.d.ts.map +1 -0
  37. package/dist/gateway/server.js +560 -0
  38. package/dist/gateway/server.js.map +1 -0
  39. package/dist/pack.d.ts +88 -0
  40. package/dist/pack.d.ts.map +1 -0
  41. package/dist/pack.js +350 -0
  42. package/dist/pack.js.map +1 -0
  43. package/docker/Dockerfile +47 -0
  44. package/docker/README.md +219 -0
  45. package/docker/docker-compose.yml +55 -0
  46. package/docker/flare-gateway.example.json +48 -0
  47. package/package.json +27 -4
  48. package/spec/SPEC.md +469 -0
  49. package/spec/conformance/README.md +60 -0
  50. package/spec/conformance/cases.json +149 -0
  51. package/spec/conformance/expected.json +1683 -0
  52. package/spec/detectors.json +1812 -0
  53. package/spec/detectors.schema.json +196 -0
@@ -0,0 +1,219 @@
1
+ # The flare-redact gateway
2
+
3
+ A redaction sidecar. Point a vendor SDK's base URL at it and the request body is
4
+ scrubbed on the way out and the originals are put back on the way in — streaming
5
+ included. Your application keeps sending and receiving real data; the vendor
6
+ never sees it.
7
+
8
+ ```bash
9
+ docker build -f docker/Dockerfile -t flare-redact-gateway .
10
+
11
+ docker run --rm -p 127.0.0.1:8787:8787 \
12
+ -e FLARE_REDACT_ENABLE=pii \
13
+ flare-redact-gateway --upstream https://api.openai.com
14
+ ```
15
+
16
+ ```diff
17
+ - OPENAI_BASE_URL=https://api.openai.com/v1
18
+ + OPENAI_BASE_URL=http://127.0.0.1:8787/v1
19
+ ```
20
+
21
+ That diff is the whole integration. No middleware, no wrapper, no import — which
22
+ is the point: the teams that most need this are the ones whose Python service,
23
+ PHP monolith and Node worker all call the same vendor and cannot each grow a new
24
+ dependency.
25
+
26
+ Without Docker: `npx flare-redact gateway --upstream https://api.openai.com`.
27
+
28
+ ## Contents
29
+
30
+ - [What it does to a request](#what-it-does-to-a-request)
31
+ - [Routes](#routes)
32
+ - [Which parts of the body](#which-parts-of-the-body)
33
+ - [Restoring the answer](#restoring-the-answer)
34
+ - [The redaction API](#the-redaction-api)
35
+ - [Configuration](#configuration)
36
+ - [Operating it](#operating-it)
37
+ - [Security model](#security-model)
38
+ - [What it deliberately does not do](#what-it-deliberately-does-not-do)
39
+
40
+ ## What it does to a request
41
+
42
+ ```
43
+ your app ──▶ gateway ──▶ vendor
44
+ real data │ placeholders
45
+ │
46
+ real data ◀──┘◀────────── placeholders
47
+ ```
48
+
49
+ 1. Read the body, up to `maxBodyBytes`.
50
+ 2. Redact it — the whole body by default, or only the JSON paths you name.
51
+ 3. Forward it with your headers untouched, including `Authorization`.
52
+ 4. Restore the originals in the response, buffered for JSON and incrementally
53
+ for `text/event-stream`.
54
+
55
+ ## Routes
56
+
57
+ One upstream:
58
+
59
+ ```bash
60
+ flare-redact gateway --upstream https://api.openai.com
61
+ ```
62
+
63
+ Several, by prefix:
64
+
65
+ ```bash
66
+ flare-redact gateway \
67
+ --route /openai=https://api.openai.com \
68
+ --route /anthropic=https://api.anthropic.com
69
+ ```
70
+
71
+ ```
72
+ OPENAI_BASE_URL=http://127.0.0.1:8787/openai/v1
73
+ ANTHROPIC_BASE_URL=http://127.0.0.1:8787/anthropic
74
+ ```
75
+
76
+ The longest matching prefix wins, so `/openai/v1` can be routed differently from
77
+ `/openai`. A route may not claim `/healthz`, `/readyz`, `/metrics` or `/v1/` —
78
+ the gateway answers those itself, and a config that would shadow one is rejected
79
+ at startup rather than mysteriously at runtime.
80
+
81
+ ## Which parts of the body
82
+
83
+ Redacting the entire body is the safe default and the one you get. For a chat
84
+ completion it also rewrites the model name and the tool schema, which is usually
85
+ harmless and occasionally not — so name the paths that hold what a human typed:
86
+
87
+ ```bash
88
+ --paths 'messages[*].content,input,prompt,system'
89
+ ```
90
+
91
+ `a.b`, `a[0]`, `a[*]` and `*` are supported. A path that does not exist in a
92
+ given request is a no-op, so one configuration covers several endpoint shapes.
93
+
94
+ Non-JSON bodies pass through untouched unless you add `--redact-text`, which
95
+ extends handling to `application/x-www-form-urlencoded` and `text/*`. A body that
96
+ claims to be JSON but does not parse is redacted as text rather than forwarded:
97
+ its framing is already broken, and the failure mode that matters is leaking.
98
+
99
+ ## Restoring the answer
100
+
101
+ Redaction uses a per-request vault, so `ada@example.com` becomes
102
+ `[FR_EMAIL_9f2c…]` on the way out and comes back on the way in. The model's reply
103
+ reads correctly to your user, and the vendor never held the address.
104
+
105
+ For a streamed response the restorer holds back the longest suffix that could
106
+ still be the start of a placeholder, so a token split across two SSE frames is
107
+ still restored exactly once. Substituted values are escaped for the JSON string
108
+ context those frames use, so a name containing a quote cannot break the client's
109
+ parser.
110
+
111
+ Turn it off with `--no-restore` when the upstream never echoes the prompt back
112
+ and you would rather save the work.
113
+
114
+ ## The redaction API
115
+
116
+ The gateway is also a service the [Python](../sdk/python/README.md) and
117
+ [Go](../sdk/go/README.md) clients talk to, for the cases where a native engine is
118
+ not what you want:
119
+
120
+ | | |
121
+ |---|---|
122
+ | `POST /v1/redact` | `{"input": …, "options": {…}}` → `{"output": …, "summary": {…}}` |
123
+ | `POST /v1/scan` | → `{"findings": [...], "summary": {…}}` |
124
+ | `GET /v1/detectors` | the catalogue, plus this gateway's selectors |
125
+ | `POST /v1/sessions` | open a server-side vault → `{"id", "expiresAt"}` |
126
+ | `POST /v1/sessions/:id/redact` | mask, remembering the mapping |
127
+ | `POST /v1/sessions/:id/restore` | put the originals back |
128
+ | `DELETE /v1/sessions/:id` | discard the mapping |
129
+ | `GET /healthz`, `GET /readyz` | liveness |
130
+ | `GET /metrics` | Prometheus text |
131
+
132
+ Set `FLARE_GATEWAY_TOKEN` and every `/v1` call needs
133
+ `Authorization: Bearer <token>`. Health and metrics stay open so a probe does not
134
+ need a credential.
135
+
136
+ Those eight paths are the only ones the gateway answers itself, and they are the
137
+ only ones that shadow your upstream. Everything else under `/v1` — `/v1/chat/
138
+ completions`, `/v1/models`, `/v1/messages` — is proxied like any other path, which
139
+ is the whole point of putting the sidecar in front of a vendor that owns that
140
+ namespace.
141
+
142
+ Sessions are held in memory, expire after `sessions.ttlMs` and are capped at
143
+ `sessions.max`. An unbounded map of secrets is a memory leak with a disclosure
144
+ attached. Because they are per-process, a replicated gateway needs sticky routing
145
+ for session traffic — the stateless `/v1/redact` path does not.
146
+
147
+ ## Configuration
148
+
149
+ Three layers, later winning: a config file, the environment, the command line.
150
+
151
+ ```bash
152
+ flare-redact gateway --config docker/flare-gateway.example.json --print-config
153
+ ```
154
+
155
+ | Environment variable | |
156
+ |---|---|
157
+ | `FLARE_UPSTREAM` | single-upstream shorthand |
158
+ | `FLARE_GATEWAY_HOST` / `FLARE_GATEWAY_PORT` | bind address (default `127.0.0.1:8787`) |
159
+ | `FLARE_GATEWAY_TOKEN` | bearer token for `/v1` |
160
+ | `FLARE_REDACT_ONLY` / `_ENABLE` / `_DISABLE` | detector selection |
161
+ | `FLARE_REDACT_MODE` | `mask`, `label`, `hash`, `pseudonym`, `surrogate` |
162
+ | `FLARE_REDACT_SECRET` | key for the keyed modes |
163
+ | `FLARE_GATEWAY_MAX_BODY_BYTES` | default 8 MiB |
164
+ | `FLARE_GATEWAY_TIMEOUT_MS` | default 120 s |
165
+ | `FLARE_GATEWAY_SESSION_TTL_MS` | default 30 min |
166
+ | `FLARE_GATEWAY_LOG` | `silent`, `info`, `debug` |
167
+
168
+ See [`flare-gateway.example.json`](./flare-gateway.example.json) for the full
169
+ shape, including per-route policies.
170
+
171
+ ## Operating it
172
+
173
+ The audit log is JSON lines and contains counts, never content:
174
+
175
+ ```json
176
+ {"time":"2026-08-05T09:12:44.031Z","level":"info","name":"flare-gateway","msg":"proxied",
177
+ "route":"openai","method":"POST","path":"/openai/v1/chat/completions","status":200,
178
+ "durationMs":412,"findings":{"email":2,"phone":1},"placeholders":3}
179
+ ```
180
+
181
+ `/metrics` exposes `flare_gateway_requests_total{route,status}`,
182
+ `flare_gateway_findings_total{detector}`, request and response byte counters,
183
+ error counters and an active-session gauge. Counting findings costs one extra
184
+ scan per request; set `auditFindings: false` if you would rather have the
185
+ throughput than the numbers.
186
+
187
+ Every response carries `X-Flare-Redact: route=…; findings=N; restored=on|off`,
188
+ which makes "is the proxy actually in the path?" answerable from a single curl.
189
+
190
+ ## Security model
191
+
192
+ - **Your credential is forwarded, never redacted.** The `Authorization` header
193
+ that authenticates you *to* the vendor is not the secret being hidden *from*
194
+ it. Header values pass through untouched.
195
+ - **Bodies, headers and matched values are never logged.**
196
+ - **The gateway's policy wins.** A client may pass options, but the configured
197
+ policy is layered over them, so no caller can send `{"disable":["email"]}` and
198
+ opt out. `transformSecret` from a client is rejected outright.
199
+ - **It binds to loopback by default.** In a container that has to be `0.0.0.0`;
200
+ publish it as `127.0.0.1:8787:8787` or keep it on an internal network. A
201
+ redaction proxy reachable from the internet is a proxy anyone can send data
202
+ through.
203
+ - **Failures are refusals.** An oversized body is a 413, an unreachable upstream
204
+ is a 502. Neither is a partially redacted request that quietly reaches the
205
+ vendor.
206
+ - **Hop-by-hop headers are stripped** in both directions, and redirects are not
207
+ followed — an upstream cannot bounce your data somewhere the config never
208
+ named.
209
+
210
+ ## What it deliberately does not do
211
+
212
+ - **No TLS interception.** This is a reverse proxy you point at, not a MITM that
213
+ forges certificates. Changing one base URL is a smaller, more auditable change
214
+ than installing a root CA on every host.
215
+ - **No shared session store.** Sessions are per-process. Distributing them would
216
+ mean replicating a map of plaintext secrets across your cluster.
217
+ - **No response redaction.** The gateway restores what it masked; it does not
218
+ scan what the vendor sent back. If the *upstream* is the untrusted party, redact
219
+ its response in your application with the library.
@@ -0,0 +1,55 @@
1
+ # A worked example: one application, one redaction sidecar, no application code
2
+ # changes. Run it from the repository root:
3
+ #
4
+ # docker compose -f docker/docker-compose.yml up --build
5
+ #
6
+ # The application talks to http://gateway:8787/openai/v1 instead of
7
+ # https://api.openai.com/v1. Everything else — the SDK, the API key, the request
8
+ # shape — stays exactly as it was.
9
+
10
+ services:
11
+ gateway:
12
+ build:
13
+ context: ..
14
+ dockerfile: docker/Dockerfile
15
+ # Bind to loopback on the host: a redaction proxy reachable from the network
16
+ # is a proxy anyone can send data through.
17
+ ports:
18
+ - "127.0.0.1:8787:8787"
19
+ environment:
20
+ FLARE_GATEWAY_HOST: 0.0.0.0
21
+ FLARE_GATEWAY_PORT: "8787"
22
+ # Detectors. `pii` turns on the personal-data set on top of the secrets
23
+ # that are always on.
24
+ FLARE_REDACT_ENABLE: pii
25
+ # Require a bearer token on /v1. The proxy routes stay open to the
26
+ # application network, which is what makes them drop-in.
27
+ FLARE_GATEWAY_TOKEN: ${FLARE_GATEWAY_TOKEN:-}
28
+ FLARE_GATEWAY_LOG: info
29
+ command:
30
+ - --route
31
+ - /openai=https://api.openai.com
32
+ - --route
33
+ - /anthropic=https://api.anthropic.com
34
+ # Only the parts a human typed. Leaving the model name and tool schemas
35
+ # alone keeps the request semantically intact.
36
+ - --paths
37
+ - messages[*].content,input,prompt,system
38
+ healthcheck:
39
+ test: ["CMD", "node", "-e", "fetch('http://127.0.0.1:8787/healthz').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
40
+ interval: 30s
41
+ timeout: 3s
42
+ retries: 3
43
+ restart: unless-stopped
44
+
45
+ app:
46
+ image: python:3.12-slim
47
+ depends_on:
48
+ gateway:
49
+ condition: service_healthy
50
+ environment:
51
+ # This is the whole integration.
52
+ OPENAI_BASE_URL: http://gateway:8787/openai/v1
53
+ ANTHROPIC_BASE_URL: http://gateway:8787/anthropic
54
+ OPENAI_API_KEY: ${OPENAI_API_KEY:-}
55
+ command: ["sleep", "infinity"]
@@ -0,0 +1,48 @@
1
+ {
2
+ "listen": { "host": "0.0.0.0", "port": 8787 },
3
+
4
+ "policy": {
5
+ "enable": ["pii"],
6
+ "disable": ["ipv4"],
7
+ "minConfidence": 0.6
8
+ },
9
+
10
+ "routes": [
11
+ {
12
+ "name": "openai",
13
+ "prefix": "/openai",
14
+ "upstream": "https://api.openai.com",
15
+ "request": {
16
+ "enabled": true,
17
+ "paths": ["messages[*].content", "input", "prompt", "instructions"],
18
+ "nonJson": false
19
+ },
20
+ "response": { "enabled": true, "streamEscape": "json" }
21
+ },
22
+ {
23
+ "name": "anthropic",
24
+ "prefix": "/anthropic",
25
+ "upstream": "https://api.anthropic.com",
26
+ "policy": { "enable": ["pii", "contextual"] },
27
+ "request": {
28
+ "enabled": true,
29
+ "paths": ["messages[*].content", "system"],
30
+ "nonJson": false
31
+ },
32
+ "response": { "enabled": true, "streamEscape": "json" }
33
+ },
34
+ {
35
+ "name": "analytics",
36
+ "prefix": "/analytics",
37
+ "upstream": "https://api.segment.io",
38
+ "request": { "enabled": true, "nonJson": true },
39
+ "response": { "enabled": false, "streamEscape": "none" }
40
+ }
41
+ ],
42
+
43
+ "sessions": { "ttlMs": 1800000, "max": 10000 },
44
+ "limits": { "maxBodyBytes": 8388608, "requestTimeoutMs": 120000 },
45
+ "auditFindings": true,
46
+ "metrics": true,
47
+ "log": "info"
48
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "flare-redact",
3
- "version": "1.4.1",
4
- "description": "Zero-dependency TypeScript secret and PII redaction for any SDK, logs, HTTP, LLM prompts, and scoped MCP tool boundaries.",
3
+ "version": "1.5.0",
4
+ "description": "Zero-dependency secret and PII redaction for any SDK, logs, HTTP, LLM prompts, and MCP tools — with a Docker sidecar gateway and Python, Go and Rust SDKs.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
7
7
  "main": "./dist/index.js",
@@ -54,14 +54,32 @@
54
54
  "./middleware": {
55
55
  "types": "./dist/middleware.d.ts",
56
56
  "default": "./dist/middleware.js"
57
- }
57
+ },
58
+ "./pack": {
59
+ "types": "./dist/pack.d.ts",
60
+ "default": "./dist/pack.js"
61
+ },
62
+ "./gateway": {
63
+ "types": "./dist/gateway/index.d.ts",
64
+ "default": "./dist/gateway/index.js"
65
+ },
66
+ "./spec/detectors.json": "./spec/detectors.json",
67
+ "./package.json": "./package.json"
58
68
  },
59
69
  "bin": {
60
- "flare-redact": "bin/flare-redact.mjs"
70
+ "flare-redact": "bin/flare-redact.mjs",
71
+ "flare-gateway": "bin/flare-gateway.mjs"
61
72
  },
62
73
  "files": [
63
74
  "dist",
64
75
  "bin",
76
+ "spec/SPEC.md",
77
+ "spec/detectors.json",
78
+ "spec/detectors.schema.json",
79
+ "spec/conformance",
80
+ "docker/Dockerfile",
81
+ "docker/docker-compose.yml",
82
+ "docker/flare-gateway.example.json",
65
83
  "bench/performance.mjs",
66
84
  "bench/adversarial-runtime.mjs",
67
85
  "bench/graph-vault.mjs",
@@ -74,6 +92,11 @@
74
92
  "scripts": {
75
93
  "build": "tsc",
76
94
  "test": "npm run build && node --test",
95
+ "test:python": "PYTHONPATH=sdk/python/src python3 -m unittest discover -s sdk/python/tests -t sdk/python",
96
+ "spec:sync": "node scripts/sync-spec.mjs",
97
+ "spec:check": "node scripts/sync-spec.mjs --check",
98
+ "spec:conformance": "npm run build && node scripts/build-conformance.mjs",
99
+ "gateway": "npm run build && node bin/flare-gateway.mjs",
77
100
  "benchmark": "npm run build && node bench/performance.mjs",
78
101
  "benchmark:adversarial": "npm run build && node bench/adversarial-runtime.mjs",
79
102
  "prepublishOnly": "npm run build",