@abhishekvardanbotta/mask 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +142 -3
- package/dist/crypto.d.ts +23 -0
- package/dist/crypto.js +45 -0
- package/dist/detectors/recognizers.d.ts +24 -0
- package/dist/detectors/recognizers.js +274 -0
- package/dist/detectors/validators.d.ts +29 -0
- package/dist/detectors/validators.js +209 -0
- package/dist/fields.d.ts +39 -0
- package/dist/fields.js +219 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +8 -0
- package/dist/policy.d.ts +36 -0
- package/dist/policy.js +116 -0
- package/dist/privacy.d.ts +152 -0
- package/dist/privacy.js +596 -0
- package/dist/types.d.ts +98 -0
- package/dist/types.js +8 -0
- package/dist/vault.d.ts +30 -0
- package/dist/vault.js +29 -0
- package/package.json +52 -6
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { type CompiledPolicy } from "./policy.js";
|
|
2
|
+
import { type AuditEvent, type Detection, type EntityType, type PolicyConfig, type PrivacySummary, type ProtectOptions, type RestoreOptions } from "./types.js";
|
|
3
|
+
import { type VaultStore } from "./vault.js";
|
|
4
|
+
export interface PrivacyOptions extends PolicyConfig {
|
|
5
|
+
/**
|
|
6
|
+
* 256-bit master key (32 bytes, or base64 of 32 bytes). Omitted, a random key is generated per process: tokens then
|
|
7
|
+
* cannot be restored after a restart, which is the right default for an in-memory vault. Set it (for example from
|
|
8
|
+
* `KILN_PRIVACY_KEY`) when the store outlives the process.
|
|
9
|
+
*/
|
|
10
|
+
key?: string | Uint8Array;
|
|
11
|
+
/** Where encrypted vault records live. Default: in-memory, process-lifetime. */
|
|
12
|
+
store?: VaultStore;
|
|
13
|
+
/** How long an idle scope stays restorable, in ms. Default one hour. Records get a matching `expiresAt`. */
|
|
14
|
+
retentionMs?: number;
|
|
15
|
+
/**
|
|
16
|
+
* How token ids look.
|
|
17
|
+
* - `"sequential"` (default): `[EMAIL_1]`, `[PERSON_2]` — numbered per type within the scope. Small models copy
|
|
18
|
+
* these reliably; with random ids they were measured to splice one token's id onto another's type. The number says
|
|
19
|
+
* nothing about the value, and scopes stay unlinkable, but ids are guessable within a scope.
|
|
20
|
+
* - `"opaque"`: `[EMAIL_3f9a1c0b7e]` — 40 bits of the keyed HMAC, so a model can only reference tokens it was shown.
|
|
21
|
+
*/
|
|
22
|
+
tokenStyle?: "sequential" | "opaque";
|
|
23
|
+
}
|
|
24
|
+
interface Entry {
|
|
25
|
+
token: string;
|
|
26
|
+
type: EntityType;
|
|
27
|
+
classification: "PII" | "SENSITIVE";
|
|
28
|
+
value: string;
|
|
29
|
+
kind: "string" | "number";
|
|
30
|
+
fp: string;
|
|
31
|
+
}
|
|
32
|
+
/** Everything one scope knows: shared by every policy view of that scope. */
|
|
33
|
+
declare class ScopeState {
|
|
34
|
+
readonly scope: string;
|
|
35
|
+
readonly byToken: Map<string, Entry>;
|
|
36
|
+
readonly byFingerprint: Map<string, string>;
|
|
37
|
+
/** Exact plaintexts already tokenized in this scope -> token, so later free-text mentions are caught too. */
|
|
38
|
+
readonly known: Map<string, string>;
|
|
39
|
+
knownVersion: number;
|
|
40
|
+
/** Last sequential id issued per type. */
|
|
41
|
+
readonly sequence: Map<string, number>;
|
|
42
|
+
private knownRegexCache?;
|
|
43
|
+
readonly pending: Promise<void>[];
|
|
44
|
+
readonly audit: AuditEvent[];
|
|
45
|
+
blocked: number;
|
|
46
|
+
restored: number;
|
|
47
|
+
denied: number;
|
|
48
|
+
lastUsed: number;
|
|
49
|
+
constructor(scope: string);
|
|
50
|
+
record(event: Omit<AuditEvent, "at">): void;
|
|
51
|
+
/** Two alternations over the known plaintexts: case-sensitive, and case-folded for emails. */
|
|
52
|
+
knownRegexes(): {
|
|
53
|
+
exact?: RegExp;
|
|
54
|
+
folded?: RegExp;
|
|
55
|
+
foldedMap: Map<string, string>;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The privacy engine: holds the keys, the vault store and the per-scope state. Create one per process (or per
|
|
60
|
+
* tenant, if tenants need separate keys) and open a session per conversation, run or request.
|
|
61
|
+
*/
|
|
62
|
+
export declare class Privacy {
|
|
63
|
+
private readonly keys;
|
|
64
|
+
private readonly store;
|
|
65
|
+
private readonly retentionMs;
|
|
66
|
+
private readonly tokenStyle;
|
|
67
|
+
private readonly baseConfig;
|
|
68
|
+
private readonly basePolicy;
|
|
69
|
+
private readonly scopes;
|
|
70
|
+
private readonly hydrating;
|
|
71
|
+
constructor(options?: PrivacyOptions);
|
|
72
|
+
/** Compiles a policy layered over this engine's own. Compile once and reuse it across sessions. */
|
|
73
|
+
policy(overrides?: PolicyConfig): CompiledPolicy;
|
|
74
|
+
/**
|
|
75
|
+
* A session over `scope`, without loading previously persisted records. Tokens are deterministic within a scope
|
|
76
|
+
* and unlinkable across scopes. Use `open()` instead when the scope may have records from an earlier process.
|
|
77
|
+
*/
|
|
78
|
+
session(scope: string, policy?: CompiledPolicy | PolicyConfig): PrivacySession;
|
|
79
|
+
/** Like `session()`, but first loads and decrypts the scope's persisted records from the store. */
|
|
80
|
+
open(scope: string, policy?: CompiledPolicy | PolicyConfig): Promise<PrivacySession>;
|
|
81
|
+
/** Forgets a scope: drops its in-memory state and deletes its records from the store. Its tokens become unrestorable. */
|
|
82
|
+
destroy(scope: string): Promise<void>;
|
|
83
|
+
/** Runs detection only — what would be found in `text`, with confidences — without tokenizing anything. */
|
|
84
|
+
detect(text: string, policy?: CompiledPolicy | PolicyConfig): Detection[];
|
|
85
|
+
/** @internal */
|
|
86
|
+
tokenFor(state: ScopeState, type: EntityType, value: string, classification: "PII" | "SENSITIVE", kind: "string" | "number"): string;
|
|
87
|
+
private remember;
|
|
88
|
+
private persist;
|
|
89
|
+
private hydrate;
|
|
90
|
+
private resolvePolicy;
|
|
91
|
+
/** Drops in-memory scopes idle for longer than the retention period. Their store records expire on the same clock. */
|
|
92
|
+
private sweep;
|
|
93
|
+
}
|
|
94
|
+
export type ProtectResult<T> = T extends string ? string : unknown;
|
|
95
|
+
/**
|
|
96
|
+
* A view over one scope, under one policy. `protect` replaces personal data with tokens before data leaves your
|
|
97
|
+
* trust boundary; `restore` puts it back, for exactly the types the principal is granted.
|
|
98
|
+
*/
|
|
99
|
+
export declare class PrivacySession {
|
|
100
|
+
private readonly engine;
|
|
101
|
+
private readonly state;
|
|
102
|
+
readonly policy: CompiledPolicy;
|
|
103
|
+
private readonly memo;
|
|
104
|
+
/** @internal — use `Privacy.session()` / `Privacy.open()`. */
|
|
105
|
+
constructor(engine: Privacy, state: ScopeState, policy: CompiledPolicy);
|
|
106
|
+
get scope(): string;
|
|
107
|
+
/**
|
|
108
|
+
* Replaces personal data in `value` (a string, or any JSON-shaped value) with vault tokens. Field classifications
|
|
109
|
+
* come from `options.schema`, the policy's `fields`, and key names; free text runs through every detector,
|
|
110
|
+
* including asynchronous ones. Idempotent: protecting already-protected data changes nothing.
|
|
111
|
+
*/
|
|
112
|
+
protect<T>(value: T, options?: ProtectOptions): Promise<T extends string ? string : unknown>;
|
|
113
|
+
/** `protect()` without asynchronous detectors — for hot paths such as log lines. Values they found earlier in this scope are still caught. */
|
|
114
|
+
protectSync<T>(value: T, options?: ProtectOptions): T extends string ? string : unknown;
|
|
115
|
+
/**
|
|
116
|
+
* Replaces tokens in `value` with the original values the principal may see. Unknown tokens (another scope's, or
|
|
117
|
+
* made up by a model) are left exactly as they are. BLOCKed values were never stored and cannot come back.
|
|
118
|
+
*/
|
|
119
|
+
restore<T>(value: T, options?: RestoreOptions): T extends string ? string : unknown;
|
|
120
|
+
/** Whether `text` contains a token issued in this scope. */
|
|
121
|
+
hasTokens(text: string): boolean;
|
|
122
|
+
/** Restores a streamed response chunk by chunk, holding back a possible partial token at each chunk's end. */
|
|
123
|
+
streamRestorer(options?: RestoreOptions): StreamRestorer;
|
|
124
|
+
/** Waits for vault writes to reach the store; rejects if any failed. */
|
|
125
|
+
flush(): Promise<void>;
|
|
126
|
+
summary(): PrivacySummary;
|
|
127
|
+
/** What happened in this scope — tokenizations, blocks, restorations, denials. Holds tokens and types, never plaintext. */
|
|
128
|
+
audit(): AuditEvent[];
|
|
129
|
+
private walk;
|
|
130
|
+
private fieldClass;
|
|
131
|
+
private classificationOf;
|
|
132
|
+
/** Tokenizes a field's entire value as one entity. */
|
|
133
|
+
private whole;
|
|
134
|
+
private block;
|
|
135
|
+
private protectText;
|
|
136
|
+
private restoreValue;
|
|
137
|
+
private restoreText;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Restores a stream without ever emitting half a token: text after an unclosed `[`, or a trailing run of token
|
|
141
|
+
* characters, is held back until the next chunk (or `flush()`) decides it.
|
|
142
|
+
*/
|
|
143
|
+
export declare class StreamRestorer {
|
|
144
|
+
private readonly restoreText;
|
|
145
|
+
private buffer;
|
|
146
|
+
constructor(restoreText: (text: string) => string);
|
|
147
|
+
push(delta: string): string;
|
|
148
|
+
flush(): string;
|
|
149
|
+
}
|
|
150
|
+
/** Creates a privacy engine. See `PrivacyOptions`. */
|
|
151
|
+
export declare function createPrivacy(options?: PrivacyOptions): Privacy;
|
|
152
|
+
export {};
|