flare-redact 0.6.1 → 0.6.2
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/README.md +50 -7
- package/dist/cli.js +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/session.d.ts +44 -0
- package/dist/session.d.ts.map +1 -0
- package/dist/session.js +59 -0
- package/dist/session.js.map +1 -0
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -51,10 +51,7 @@ Nothing to configure. No list of field paths to maintain. No native build step.
|
|
|
51
51
|
> [Jump to it ↓](#redact-prompts-before-they-reach-an-llm)
|
|
52
52
|
|
|
53
53
|
<p align="center">
|
|
54
|
-
<
|
|
55
|
-
<source media="(prefers-color-scheme: dark)" srcset="assets/coverage-dark.svg">
|
|
56
|
-
<img src="assets/coverage-light.svg" alt="One policy applied across app, logs, HTTP, LLM, datasets, and egress" width="820">
|
|
57
|
-
</picture>
|
|
54
|
+
<img src="assets/demo.svg" alt="Before and after — secrets and PII masked in a log line, across languages" width="820">
|
|
58
55
|
</p>
|
|
59
56
|
|
|
60
57
|
| | | |
|
|
@@ -69,6 +66,7 @@ Nothing to configure. No list of field paths to maintain. No native build step.
|
|
|
69
66
|
- [Redact prompts before they reach an LLM](#redact-prompts-before-they-reach-an-llm)
|
|
70
67
|
- [Ways to hide a value](#ways-to-hide-a-value)
|
|
71
68
|
- [Reversible redaction](#reversible-redaction)
|
|
69
|
+
- [Build a private chat app](#build-a-private-chat-app)
|
|
72
70
|
- [See what leaks, and why](#see-what-leaks-and-why)
|
|
73
71
|
- [Guard your logger in one line](#guard-your-logger-in-one-line)
|
|
74
72
|
- [One policy, everywhere](#one-policy-everywhere)
|
|
@@ -191,6 +189,40 @@ The same value always gets the same placeholder, so references survive the round
|
|
|
191
189
|
trip. Works on objects too, and `restore()` also takes a plain placeholder→value
|
|
192
190
|
map if you persisted one.
|
|
193
191
|
|
|
192
|
+
## Build a private chat app
|
|
193
|
+
|
|
194
|
+
If you're building a chat interface — over your own local model or any API — a
|
|
195
|
+
**session** is the drop-in layer. One session holds one vault, so a value keeps
|
|
196
|
+
the same placeholder across every turn: mask the user's message on the way in,
|
|
197
|
+
restore the model's reply on the way out. It's model-agnostic, synchronous, and
|
|
198
|
+
fast enough that the cost vanishes next to inference (a 13 KB message redacts in
|
|
199
|
+
~0.3 ms).
|
|
200
|
+
|
|
201
|
+
```js
|
|
202
|
+
import { createSession } from 'flare-redact';
|
|
203
|
+
|
|
204
|
+
const session = createSession({ enable: ['pii'] });
|
|
205
|
+
|
|
206
|
+
// on the way in — the model only ever sees placeholders
|
|
207
|
+
const prompt = session.redact(userMessage);
|
|
208
|
+
const reply = await myModel.generate(prompt);
|
|
209
|
+
|
|
210
|
+
// on the way out — the user sees the real values back
|
|
211
|
+
show(session.restore(reply));
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
Streaming? Restore token by token, even when a placeholder is split across chunks:
|
|
215
|
+
|
|
216
|
+
```js
|
|
217
|
+
const out = session.stream();
|
|
218
|
+
for await (const chunk of modelStream) process(out.push(chunk.text));
|
|
219
|
+
process(out.flush());
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
`session.redactMessages([{ role, content }])` masks a whole chat array at once,
|
|
223
|
+
and `session.reset()` starts a fresh conversation. The model never sees the real
|
|
224
|
+
data; your app still works end to end.
|
|
225
|
+
|
|
194
226
|
## See what leaks, and why
|
|
195
227
|
|
|
196
228
|
`scan()` finds secrets without changing the input, and explains every hit in
|
|
@@ -401,9 +433,20 @@ Plus object values whose **key name** is sensitive (`password`, `token`,
|
|
|
401
433
|
## Every language, every country
|
|
402
434
|
|
|
403
435
|
Secrets like API keys and card numbers don't care what language your app is in.
|
|
404
|
-
Neither does this — but the word-based checks do, so
|
|
405
|
-
|
|
406
|
-
|
|
436
|
+
Neither does this — but the word-based checks do, so words like *password*,
|
|
437
|
+
*secret*, and *token* are recognized as assignments and as object keys in all
|
|
438
|
+
**24 languages** below:
|
|
439
|
+
|
|
440
|
+
| | | |
|
|
441
|
+
|---|---|---|
|
|
442
|
+
| 🇬🇧 English `password` | 🇨🇳 Chinese `密码` | 🇮🇳 Hindi `पासवर्ड` |
|
|
443
|
+
| 🇪🇸 Spanish `contraseña` | 🇸🇦 Arabic `كلمة المرور` | 🇫🇷 French `mot de passe` |
|
|
444
|
+
| 🇵🇹 Portuguese `senha` | 🇷🇺 Russian `пароль` | 🇯🇵 Japanese `パスワード` |
|
|
445
|
+
| 🇩🇪 German `passwort` | 🇰🇷 Korean `비밀번호` | 🇹🇷 Turkish `şifre` |
|
|
446
|
+
| 🇮🇹 Italian `segreto` | 🇮🇷 Persian `رمز عبور` | 🇵🇱 Polish `hasło` |
|
|
447
|
+
| 🇺🇦 Ukrainian `пароль` | 🇳🇱 Dutch `wachtwoord` | 🇻🇳 Vietnamese `mật khẩu` |
|
|
448
|
+
| 🇮🇩 Indonesian `kata sandi` | 🇹🇭 Thai `รหัสผ่าน` | 🇬🇷 Greek `κωδικός` |
|
|
449
|
+
| 🇮🇱 Hebrew `סיסמה` | 🇦🇿 Azerbaijani `şifrə` | 🇷🇴 Romanian `parolă` |
|
|
407
450
|
|
|
408
451
|
National IDs are opt-in and **checksum-validated**, so a random run of digits is
|
|
409
452
|
never mistaken for one. Enable a whole group or a single country by tag:
|
package/dist/cli.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { readFileSync } from 'node:fs';
|
|
2
2
|
import { redact, scan, summary, DETECTORS } from './index.js';
|
|
3
3
|
import { redactCsv } from './csv.js';
|
|
4
|
-
const VERSION = '0.6.
|
|
4
|
+
const VERSION = '0.6.2';
|
|
5
5
|
const HELP = `flare-redact — hide secrets & PII before they hit a log
|
|
6
6
|
|
|
7
7
|
USAGE
|
package/dist/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export type { Mode, Finding, RedactOptions } from './engine.js';
|
|
|
3
3
|
export type { Detector } from './detectors.js';
|
|
4
4
|
export { DETECTORS, SENSITIVE_KEY_RE, keepPrefix, keepLast, luhn, entropy, fnv1a, fpe, } from './detectors.js';
|
|
5
5
|
export { createVault, restore, buildRestore, type Vault, type VaultOptions } from './vault.js';
|
|
6
|
+
export { createSession, type Session, type SessionOptions, type StreamRestorer } from './session.js';
|
|
6
7
|
export declare function redact<T>(input: T, opts?: RedactOptions): T;
|
|
7
8
|
export declare function scan(input: unknown, opts?: RedactOptions): Finding[];
|
|
8
9
|
export declare function isClean(input: unknown, opts?: RedactOptions): boolean;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAOL,KAAK,aAAa,EAClB,KAAK,OAAO,EAEb,MAAM,aAAa,CAAC;AAErB,YAAY,EAAE,IAAI,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAChE,YAAY,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,UAAU,EACV,QAAQ,EACR,IAAI,EACJ,OAAO,EACP,KAAK,EACL,GAAG,GACJ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,YAAY,EAAE,KAAK,KAAK,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAOL,KAAK,aAAa,EAClB,KAAK,OAAO,EAEb,MAAM,aAAa,CAAC;AAErB,YAAY,EAAE,IAAI,EAAE,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAChE,YAAY,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,UAAU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makeReplacer, scanString, redactString, } from './engine.js';
|
|
4
4
|
export { DETECTORS, SENSITIVE_KEY_RE, keepPrefix, keepLast, luhn, entropy, fnv1a, fpe, } from './detectors.js';
|
|
5
5
|
export { createVault, restore, buildRestore } from './vault.js';
|
|
6
|
+
export { createSession } from './session.js';
|
|
6
7
|
export function redact(input, opts = {}) {
|
|
7
8
|
const dets = resolveDetectors(opts);
|
|
8
9
|
const allow = allowMatcher(opts);
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EACL,gBAAgB,EAChB,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,YAAY,GAIb,MAAM,aAAa,CAAC;AAIrB,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,UAAU,EACV,QAAQ,EACR,IAAI,EACJ,OAAO,EACP,KAAK,EACL,GAAG,GACJ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,YAAY,EAAiC,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,EACL,gBAAgB,EAChB,UAAU,EACV,YAAY,EACZ,YAAY,EACZ,UAAU,EACV,YAAY,GAIb,MAAM,aAAa,CAAC;AAIrB,OAAO,EACL,SAAS,EACT,gBAAgB,EAChB,UAAU,EACV,QAAQ,EACR,IAAI,EACJ,OAAO,EACP,KAAK,EACL,GAAG,GACJ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,WA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,MAAM,CAAC,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC;IAC/E,CAAC;IACD,OAAO,GAAG,EAAE;QACV,KAAK,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,QAAQ;YAAE,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;IACrD,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type Vault, type VaultOptions } from './vault.js';
|
|
2
|
+
export interface SessionOptions extends VaultOptions {
|
|
3
|
+
}
|
|
4
|
+
interface ChatMessage {
|
|
5
|
+
role?: string;
|
|
6
|
+
content: unknown;
|
|
7
|
+
[k: string]: unknown;
|
|
8
|
+
}
|
|
9
|
+
export interface StreamRestorer {
|
|
10
|
+
/** Feed a streamed chunk, get back the safe-to-display text so far. */
|
|
11
|
+
push(chunk: string): string;
|
|
12
|
+
/** Flush any held-back tail once the stream ends. */
|
|
13
|
+
flush(): string;
|
|
14
|
+
}
|
|
15
|
+
export interface Session {
|
|
16
|
+
/** Mask a user message before it reaches the model. */
|
|
17
|
+
redact<T>(input: T): T;
|
|
18
|
+
/** Restore the model's reply before showing it to the user. */
|
|
19
|
+
restore<T>(input: T): T;
|
|
20
|
+
/** Mask a whole `[{ role, content }]` chat array in one call. */
|
|
21
|
+
redactMessages<M extends ChatMessage>(messages: M[]): M[];
|
|
22
|
+
/** A restorer for streamed replies — rebuilds originals even across chunk splits. */
|
|
23
|
+
stream(): StreamRestorer;
|
|
24
|
+
/** The underlying vault (placeholder ↔ original map). */
|
|
25
|
+
readonly vault: Vault;
|
|
26
|
+
/** How many distinct values have been masked this session. */
|
|
27
|
+
readonly size: number;
|
|
28
|
+
/** Start a fresh conversation — a new vault, no carried-over mappings. */
|
|
29
|
+
reset(): void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A conversation-scoped redactor for chat/AI apps. One session keeps one vault,
|
|
33
|
+
* so the same value maps to the same placeholder across every turn — mask the
|
|
34
|
+
* user's message on the way in, restore the model's answer on the way out. Model
|
|
35
|
+
* agnostic (works with a local model or any API), synchronous, and fast enough
|
|
36
|
+
* that the cost disappears next to inference.
|
|
37
|
+
*
|
|
38
|
+
* const session = createSession({ enable: ['pii'] });
|
|
39
|
+
* const safe = session.redact(userMessage); // → send `safe` to the model
|
|
40
|
+
* const reply = session.restore(modelReply); // → show `reply` to the user
|
|
41
|
+
*/
|
|
42
|
+
export declare function createSession(opts?: SessionOptions): Session;
|
|
43
|
+
export {};
|
|
44
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,KAAK,KAAK,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAEtF,MAAM,WAAW,cAAe,SAAQ,YAAY;CAAG;AAEvD,UAAU,WAAW;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,OAAO,CAAC;IACjB,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,cAAc;IAC7B,uEAAuE;IACvE,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;IAC5B,qDAAqD;IACrD,KAAK,IAAI,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,OAAO;IACtB,uDAAuD;IACvD,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IACvB,+DAA+D;IAC/D,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC;IACxB,iEAAiE;IACjE,cAAc,CAAC,CAAC,SAAS,WAAW,EAAE,QAAQ,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC;IAC1D,qFAAqF;IACrF,MAAM,IAAI,cAAc,CAAC;IACzB,yDAAyD;IACzD,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;IACtB,8DAA8D;IAC9D,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,0EAA0E;IAC1E,KAAK,IAAI,IAAI,CAAC;CACf;AAcD;;;;;;;;;;GAUG;AACH,wBAAgB,aAAa,CAAC,IAAI,GAAE,cAAmB,GAAG,OAAO,CAqChE"}
|
package/dist/session.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { createVault, buildRestore } from './vault.js';
|
|
2
|
+
function redactContent(content, vault) {
|
|
3
|
+
if (typeof content === 'string')
|
|
4
|
+
return vault.redact(content);
|
|
5
|
+
if (Array.isArray(content)) {
|
|
6
|
+
return content.map((part) => part && typeof part === 'object' && typeof part.text === 'string'
|
|
7
|
+
? { ...part, text: vault.redact(part.text) }
|
|
8
|
+
: part);
|
|
9
|
+
}
|
|
10
|
+
return content;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A conversation-scoped redactor for chat/AI apps. One session keeps one vault,
|
|
14
|
+
* so the same value maps to the same placeholder across every turn — mask the
|
|
15
|
+
* user's message on the way in, restore the model's answer on the way out. Model
|
|
16
|
+
* agnostic (works with a local model or any API), synchronous, and fast enough
|
|
17
|
+
* that the cost disappears next to inference.
|
|
18
|
+
*
|
|
19
|
+
* const session = createSession({ enable: ['pii'] });
|
|
20
|
+
* const safe = session.redact(userMessage); // → send `safe` to the model
|
|
21
|
+
* const reply = session.restore(modelReply); // → show `reply` to the user
|
|
22
|
+
*/
|
|
23
|
+
export function createSession(opts = {}) {
|
|
24
|
+
let vault = createVault(opts);
|
|
25
|
+
return {
|
|
26
|
+
redact: (input) => vault.redact(input),
|
|
27
|
+
restore: (input) => vault.restore(input),
|
|
28
|
+
redactMessages: (messages) => messages.map((m) => ({ ...m, content: redactContent(m.content, vault) })),
|
|
29
|
+
stream() {
|
|
30
|
+
const restore = buildRestore(vault.entries());
|
|
31
|
+
let buf = '';
|
|
32
|
+
return {
|
|
33
|
+
push(chunk) {
|
|
34
|
+
buf += chunk;
|
|
35
|
+
const open = buf.lastIndexOf('[');
|
|
36
|
+
const emitEnd = open !== -1 && buf.indexOf(']', open) === -1 ? open : buf.length;
|
|
37
|
+
const emit = buf.slice(0, emitEnd);
|
|
38
|
+
buf = buf.slice(emitEnd);
|
|
39
|
+
return restore(emit);
|
|
40
|
+
},
|
|
41
|
+
flush() {
|
|
42
|
+
const out = restore(buf);
|
|
43
|
+
buf = '';
|
|
44
|
+
return out;
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
},
|
|
48
|
+
get vault() {
|
|
49
|
+
return vault;
|
|
50
|
+
},
|
|
51
|
+
get size() {
|
|
52
|
+
return vault.size;
|
|
53
|
+
},
|
|
54
|
+
reset() {
|
|
55
|
+
vault = createVault(opts);
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session.js","sourceRoot":"","sources":["../src/session.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAiC,MAAM,YAAY,CAAC;AAkCtF,SAAS,aAAa,CAAC,OAAgB,EAAE,KAAY;IACnD,IAAI,OAAO,OAAO,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC9D,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3B,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAC1B,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,OAAQ,IAA2B,CAAC,IAAI,KAAK,QAAQ;YACvF,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,IAAI,EAAE,KAAK,CAAC,MAAM,CAAE,IAAyB,CAAC,IAAI,CAAC,EAAE;YAClE,CAAC,CAAC,IAAI,CACT,CAAC;IACJ,CAAC;IACD,OAAO,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G,CAAC,CAAC;oBACzB,GAAG,GAAG,EAAE,CAAC;oBACT,OAAO,GAAG,CAAC;gBACb,CAAC;aACF,CAAC;QACJ,CAAC;QACD,IAAI,KAAK;YACP,OAAO,KAAK,CAAC;QACf,CAAC;QACD,IAAI,IAAI;YACN,OAAO,KAAK,CAAC,IAAI,CAAC;QACpB,CAAC;QACD,KAAK;YACH,KAAK,GAAG,WAAW,CAAC,IAAI,CAAC,CAAC;QAC5B,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flare-redact",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Hide secrets & PII in logs, prompts, and text before they leak — with a reversible layer that keeps them out of your OpenAI/Anthropic calls. Zero dependencies, ReDoS-safe.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -19,6 +19,10 @@
|
|
|
19
19
|
"types": "./dist/llm.d.ts",
|
|
20
20
|
"default": "./dist/llm.js"
|
|
21
21
|
},
|
|
22
|
+
"./session": {
|
|
23
|
+
"types": "./dist/session.d.ts",
|
|
24
|
+
"default": "./dist/session.js"
|
|
25
|
+
},
|
|
22
26
|
"./pino": {
|
|
23
27
|
"types": "./dist/pino.d.ts",
|
|
24
28
|
"default": "./dist/pino.js"
|