mask-privacy 1.0.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/.test_audit.db +0 -0
- package/README.md +250 -0
- package/dist/index.d.mts +257 -0
- package/dist/index.d.ts +257 -0
- package/dist/index.js +58820 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +58777 -0
- package/dist/index.mjs.map +1 -0
- package/examples/secure_vault/email_tool.ts +13 -0
- package/examples/test_agent.ts +50 -0
- package/jest.config.js +10 -0
- package/package.json +37 -0
- package/src/client.ts +135 -0
- package/src/core/crypto.ts +100 -0
- package/src/core/exceptions.ts +23 -0
- package/src/core/fpe.ts +185 -0
- package/src/core/key_provider.ts +158 -0
- package/src/core/scanner.ts +308 -0
- package/src/core/utils.ts +76 -0
- package/src/core/vault.ts +540 -0
- package/src/index.ts +85 -0
- package/src/integrations/adk_hooks.ts +56 -0
- package/src/integrations/langchain_hooks.ts +87 -0
- package/src/integrations/llamaindex_hooks.ts +80 -0
- package/src/telemetry/audit_logger.ts +168 -0
- package/tests/async.test.ts +47 -0
- package/tests/audit_logger.test.ts +55 -0
- package/tests/exceptions.test.ts +75 -0
- package/tests/fail_strategy.test.ts +84 -0
- package/tests/fpe.test.ts +126 -0
- package/tests/hooks.test.ts +107 -0
- package/tests/key_provider.test.ts +68 -0
- package/tests/langchain.test.ts +101 -0
- package/tests/llamaindex.test.ts +75 -0
- package/tests/scanner.test.ts +107 -0
- package/tests/smoke.test.ts +6 -0
- package/tests/substring.test.ts +59 -0
- package/tests/vault.test.ts +101 -0
- package/tests/vault_backends.test.ts +124 -0
- package/tsconfig.json +22 -0
- package/tsup.config.ts +11 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/** Interface every vault backend must implement. */
|
|
2
|
+
declare abstract class BaseVault {
|
|
3
|
+
/** Persist a token → plaintext mapping with a TTL. Optionally save a reverse lookup hash. */
|
|
4
|
+
abstract store(token: string, plaintext: string, ttlSeconds: number, ptHash?: string | null): Promise<void> | void;
|
|
5
|
+
/** Return the existing unexpired token for a given plaintext hash, or null. */
|
|
6
|
+
abstract getTokenByPlaintextHash(ptHash: string): Promise<string | null> | string | null;
|
|
7
|
+
/** Return the plaintext for token, or null if missing/expired. */
|
|
8
|
+
abstract retrieve(token: string): Promise<string | null> | string | null;
|
|
9
|
+
/** Delete a token and its reverse mapping. */
|
|
10
|
+
abstract delete(token: string): Promise<void> | void;
|
|
11
|
+
}
|
|
12
|
+
declare function getVault(): BaseVault;
|
|
13
|
+
/**
|
|
14
|
+
* Tokenise rawText, encrypt it, store in vault, return the FPE token.
|
|
15
|
+
*/
|
|
16
|
+
declare function encode(rawText: string, options?: {
|
|
17
|
+
ttl?: number;
|
|
18
|
+
}): Promise<string>;
|
|
19
|
+
/** Async wrapper for encode (parity with Python aencode). */
|
|
20
|
+
declare const aencode: typeof encode;
|
|
21
|
+
/**
|
|
22
|
+
* Detokenise token via vault lookup and decrypt it.
|
|
23
|
+
*/
|
|
24
|
+
declare function decode(token: string): Promise<string>;
|
|
25
|
+
/** Async wrapper for decode (parity with Python adecode). */
|
|
26
|
+
declare const adecode: typeof decode;
|
|
27
|
+
/**
|
|
28
|
+
* Find all Mask tokens within text and replace them with their original plaintext.
|
|
29
|
+
*/
|
|
30
|
+
declare function detokenizeText(text: string): Promise<string>;
|
|
31
|
+
/** Async wrapper for detokenizeText (parity with Python adetokenize_text). */
|
|
32
|
+
declare const adetokenizeText: typeof detokenizeText;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Format-Preserving Encryption (FPE) token generation.
|
|
36
|
+
*
|
|
37
|
+
* Generates structurally valid, **deterministic** tokens that preserve the
|
|
38
|
+
* format of the original data type so downstream tools, schemas, and
|
|
39
|
+
* validators continue to work without modification.
|
|
40
|
+
*
|
|
41
|
+
* Determinism is achieved via HMAC-SHA256 keyed with a master key, ensuring
|
|
42
|
+
* the same plaintext always produces the same token. This preserves entity
|
|
43
|
+
* relationships for LLMs (e.g. "John" is always [TKN-abc]) without leaking
|
|
44
|
+
* the identity.
|
|
45
|
+
*
|
|
46
|
+
* Supported formats:
|
|
47
|
+
* - Email → tkn-<hex>@email.com
|
|
48
|
+
* - Phone → +1-555-<7 digits>
|
|
49
|
+
* - SSN → 000-00-<4 digits>
|
|
50
|
+
* - CC → 4000-0000-0000-<4 digits>
|
|
51
|
+
* - Routing→ 000000<3 digits>
|
|
52
|
+
* - Default→ [TKN-<hex>]
|
|
53
|
+
*/
|
|
54
|
+
/** Clear the cached master key. Useful in tests. */
|
|
55
|
+
declare function resetMasterKey(): void;
|
|
56
|
+
/**
|
|
57
|
+
* Return a **deterministic**, format-preserving token for rawText.
|
|
58
|
+
*
|
|
59
|
+
* The token is structurally compatible with the original data type
|
|
60
|
+
* so that downstream schema validators, regex checks, and database
|
|
61
|
+
* constraints continue to pass.
|
|
62
|
+
*/
|
|
63
|
+
declare function generateFPEToken(rawText: string): string;
|
|
64
|
+
/**
|
|
65
|
+
* Heuristic: return true if value appears to be a Mask token.
|
|
66
|
+
*/
|
|
67
|
+
declare function looksLikeToken(value: string): boolean;
|
|
68
|
+
|
|
69
|
+
declare class PresidioScanner {
|
|
70
|
+
protected _supportedEntities: string[];
|
|
71
|
+
constructor();
|
|
72
|
+
setSupportedEntities(entities: string[]): void;
|
|
73
|
+
/** Validate a credit card number using the Luhn algorithm. */
|
|
74
|
+
protected static _luhnChecksum(ccNumber: string): boolean;
|
|
75
|
+
/** Validate a US ABA routing number using the checksum algorithm. */
|
|
76
|
+
protected static _abaChecksum(routingNumber: string): boolean;
|
|
77
|
+
protected _tier1Regex(text: string, encodeFn: (val: string) => Promise<string>, boostEntities: Set<string>, aggressive: boolean, confidenceThreshold: number): Promise<[string, any[]]>;
|
|
78
|
+
protected _tier2Nlp(text: string, encodeFn: (val: string) => Promise<string>, boostEntities: Set<string>, aggressive: boolean, confidenceThreshold: number): Promise<[string, any[]]>;
|
|
79
|
+
protected _resolveBoost(context?: string | null): Set<string>;
|
|
80
|
+
scanAndTokenize(text: string, options?: {
|
|
81
|
+
encodeFn?: (val: string) => Promise<string>;
|
|
82
|
+
pipeline?: string[];
|
|
83
|
+
confidenceThreshold?: number;
|
|
84
|
+
context?: string | null;
|
|
85
|
+
aggressive?: boolean;
|
|
86
|
+
}): Promise<string>;
|
|
87
|
+
scanAndReturnEntities(text: string, options?: {
|
|
88
|
+
encodeFn?: (val: string) => Promise<string>;
|
|
89
|
+
pipeline?: string[];
|
|
90
|
+
confidenceThreshold?: number;
|
|
91
|
+
context?: string | null;
|
|
92
|
+
aggressive?: boolean;
|
|
93
|
+
}): Promise<any[]>;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Scanner that calls a remote Presidio Analyzer endpoint.
|
|
97
|
+
*/
|
|
98
|
+
declare class RemotePresidioScanner extends PresidioScanner {
|
|
99
|
+
private endpointUrl;
|
|
100
|
+
constructor(endpointUrl: string);
|
|
101
|
+
protected _tier2Nlp(text: string, encodeFn: (val: string) => Promise<string>, boostEntities: Set<string>, aggressive: boolean, confidenceThreshold: number): Promise<[string, any[]]>;
|
|
102
|
+
}
|
|
103
|
+
declare function getScanner(): PresidioScanner;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Custom exception hierarchy for the Mask SDK.
|
|
107
|
+
*
|
|
108
|
+
* Provides specific exceptions so callers can implement targeted
|
|
109
|
+
* retry/fallback logic instead of catching generic Error.
|
|
110
|
+
*/
|
|
111
|
+
declare class MaskError extends Error {
|
|
112
|
+
constructor(message: string);
|
|
113
|
+
}
|
|
114
|
+
/** Raised when a vault backend (Redis, DynamoDB) is unreachable. */
|
|
115
|
+
declare class MaskVaultConnectionError extends MaskError {
|
|
116
|
+
}
|
|
117
|
+
/** Raised when CryptoEngine.decrypt() fails (bad key, corrupt data). */
|
|
118
|
+
declare class MaskDecryptionError extends MaskError {
|
|
119
|
+
}
|
|
120
|
+
/** Raised when spaCy / Presidio analysis exceeds the time budget. */
|
|
121
|
+
declare class MaskNLPTimeout extends MaskError {
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Core cryptography engine for Mask SDK.
|
|
126
|
+
*
|
|
127
|
+
* Provides a CryptoEngine singleton that handles Envelope Encryption,
|
|
128
|
+
* ensuring that plaintext PII is encrypted locally before being
|
|
129
|
+
* transmitted and stored in distributed vaults (Redis/Memcached/DynamoDB).
|
|
130
|
+
*
|
|
131
|
+
* Requires MASK_ENCRYPTION_KEY to be set in the environment.
|
|
132
|
+
*/
|
|
133
|
+
declare class CryptoEngine {
|
|
134
|
+
private static _instance;
|
|
135
|
+
private _fernet;
|
|
136
|
+
private constructor();
|
|
137
|
+
static getInstance(): CryptoEngine;
|
|
138
|
+
/** Clear the singleton instance to force re-initialization (useful for key rotation). */
|
|
139
|
+
static reset(): void;
|
|
140
|
+
private _init;
|
|
141
|
+
encrypt(plaintext: string): string;
|
|
142
|
+
decrypt(ciphertext: string): string;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Asynchronous audit logger for Mask Privacy SDK.
|
|
147
|
+
*
|
|
148
|
+
* Logs every tokenisation / detokenisation event *without* recording the
|
|
149
|
+
* plaintext PII. Events are batched and flushed to:
|
|
150
|
+
* - stdout / Console (default)
|
|
151
|
+
* - Customer SIEM via structured JSON log lines
|
|
152
|
+
*
|
|
153
|
+
* NOTE: This SDK is LOCAL-FIRST. Audits are stored in a local
|
|
154
|
+
* SQLite file (.mask_audit.db) and are NOT sent anywhere externally.
|
|
155
|
+
*/
|
|
156
|
+
declare class AuditLogger {
|
|
157
|
+
private static _instance;
|
|
158
|
+
private _dbPath;
|
|
159
|
+
private _flushInterval;
|
|
160
|
+
private _running;
|
|
161
|
+
private _timer;
|
|
162
|
+
private _buffer;
|
|
163
|
+
private _dbDisabled;
|
|
164
|
+
private _db;
|
|
165
|
+
private constructor();
|
|
166
|
+
static getInstance(): AuditLogger;
|
|
167
|
+
log(action: string, token: string, dataType?: string, agent?: string, tool?: string, extra?: Record<string, any>): void;
|
|
168
|
+
start(): void;
|
|
169
|
+
stop(): void;
|
|
170
|
+
private _flush;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Explicit Client initialization for the Mask SDK.
|
|
175
|
+
*
|
|
176
|
+
* Provides MaskClient — a unified, explicitly-configured client that
|
|
177
|
+
* bundles vault, crypto, scanner, and audit logger into a single object.
|
|
178
|
+
*/
|
|
179
|
+
|
|
180
|
+
declare class MaskClient {
|
|
181
|
+
vault: BaseVault;
|
|
182
|
+
crypto: CryptoEngine;
|
|
183
|
+
scanner: PresidioScanner;
|
|
184
|
+
auditLogger: AuditLogger;
|
|
185
|
+
/** backward compat alias */
|
|
186
|
+
logger: AuditLogger;
|
|
187
|
+
ttl: number;
|
|
188
|
+
/**
|
|
189
|
+
* Initialise the client with specific component instances.
|
|
190
|
+
*
|
|
191
|
+
* If an instance is not provided, the client will fall back to
|
|
192
|
+
* the standard environment-configured singleton for that component.
|
|
193
|
+
*/
|
|
194
|
+
constructor(options?: {
|
|
195
|
+
vault?: BaseVault;
|
|
196
|
+
crypto?: CryptoEngine;
|
|
197
|
+
scanner?: PresidioScanner;
|
|
198
|
+
auditLogger?: AuditLogger;
|
|
199
|
+
ttl?: number;
|
|
200
|
+
});
|
|
201
|
+
/**
|
|
202
|
+
* Tokenise rawText, encrypt it, and store it in the vault.
|
|
203
|
+
*
|
|
204
|
+
* Includes deduplication: if the same plaintext has been encoded
|
|
205
|
+
* before and the token is still active, the existing token is returned.
|
|
206
|
+
*/
|
|
207
|
+
encode(rawText: string): Promise<string>;
|
|
208
|
+
/** Retrieve token from vault and decrypt it. */
|
|
209
|
+
decode(token: string): Promise<string>;
|
|
210
|
+
/** Scan text using the Waterfall pipeline and replace PII with FPE tokens. */
|
|
211
|
+
scanAndTokenize(text: string): Promise<string>;
|
|
212
|
+
/** Async wrapper for encode (parity with Python aencode). */
|
|
213
|
+
aencode(rawText: string): Promise<string>;
|
|
214
|
+
/** Async wrapper for decode (parity with Python adecode). */
|
|
215
|
+
adecode(token: string): Promise<string>;
|
|
216
|
+
/** Async wrapper for scanAndTokenize (parity with Python ascan_and_tokenize). */
|
|
217
|
+
ascanAndTokenize(text: string): Promise<string>;
|
|
218
|
+
/** Find and replace all tokens within text with their plaintext. */
|
|
219
|
+
detokenizeText(text: string): Promise<string>;
|
|
220
|
+
/** Async wrapper for detokenizeText (parity with Python adetokenize_text). */
|
|
221
|
+
adetokenizeText(text: string): Promise<string>;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Mask Privacy SDK
|
|
226
|
+
* Just-In-Time Privacy Middleware for AI Agents.
|
|
227
|
+
*
|
|
228
|
+
* Provides format-preserving encryption, local/distributed vaulting,
|
|
229
|
+
* and framework-agnostic tool interception hooks.
|
|
230
|
+
*/
|
|
231
|
+
declare const VERSION = "1.0.0";
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Detect PII entities in text and return a list of objects with metadata.
|
|
235
|
+
*/
|
|
236
|
+
declare function detectEntitiesWithConfidence(text: string, options?: {
|
|
237
|
+
pipeline?: string[];
|
|
238
|
+
confidenceThreshold?: number;
|
|
239
|
+
context?: string | null;
|
|
240
|
+
aggressive?: boolean;
|
|
241
|
+
}): Promise<any[]>;
|
|
242
|
+
/**
|
|
243
|
+
* Async variant of getScanner().scanAndTokenize().
|
|
244
|
+
*/
|
|
245
|
+
declare function ascanAndTokenize(text: string, options?: {
|
|
246
|
+
pipeline?: string[];
|
|
247
|
+
confidenceThreshold?: number;
|
|
248
|
+
context?: string | null;
|
|
249
|
+
aggressive?: boolean;
|
|
250
|
+
}): Promise<string>;
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Drop-in decorator for LangChain tools with automatic PII protection.
|
|
254
|
+
*/
|
|
255
|
+
declare function secureTool(...args: any[]): any;
|
|
256
|
+
|
|
257
|
+
export { MaskClient, MaskDecryptionError, MaskError, MaskNLPTimeout, MaskVaultConnectionError, RemotePresidioScanner, VERSION, adecode, adetokenizeText, aencode, ascanAndTokenize, decode, detectEntitiesWithConfidence, detokenizeText, encode, generateFPEToken, getScanner, getVault, looksLikeToken, resetMasterKey, secureTool };
|