@waswrongassembled/pii-shield 0.1.1 → 0.3.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 +36 -1
- package/README.md +135 -21
- package/dist/cli.js +37 -5
- package/dist/create-pii-shield.d.ts +91 -0
- package/dist/create-pii-shield.js +124 -0
- package/dist/entity-extractor.d.ts +7 -0
- package/dist/entity-extractor.js +6 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.js +4 -3
- package/dist/internal/background.d.ts +27 -0
- package/dist/internal/background.js +47 -0
- package/dist/learning/custom-entity-type.d.ts +14 -4
- package/dist/learning/custom-entity-type.js +23 -5
- package/dist/learning/entity-learner.d.ts +33 -2
- package/dist/learning/entity-learner.js +48 -5
- package/dist/learning/store.d.ts +26 -5
- package/dist/learning/store.js +4 -2
- package/dist/pii-masker.js +2 -1
- package/dist/pii-shield.js +2 -1
- package/dist/postgres/index.d.ts +103 -0
- package/dist/postgres/index.js +204 -0
- package/dist/redaction.d.ts +16 -2
- package/dist/redaction.js +22 -10
- package/dist/server/config.d.ts +16 -7
- package/dist/server/config.js +78 -126
- package/dist/server/connections.d.ts +34 -0
- package/dist/server/connections.js +99 -0
- package/dist/server/doctor.d.ts +28 -0
- package/dist/server/doctor.js +438 -0
- package/dist/server/env.d.ts +62 -0
- package/dist/server/env.js +197 -0
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +1 -0
- package/dist/testing/conformance.d.ts +64 -0
- package/dist/testing/conformance.js +348 -0
- package/dist/testing/index.d.ts +6 -0
- package/dist/testing/index.js +6 -0
- package/dist/vault/encrypted-vault.d.ts +39 -2
- package/dist/vault/encrypted-vault.js +96 -13
- package/dist/vault/memory-vault-storage.d.ts +8 -1
- package/dist/vault/memory-vault-storage.js +20 -8
- package/dist/vault/vault.d.ts +18 -2
- package/dist/vault-redis/index.d.ts +14 -0
- package/dist/vault-redis/index.js +8 -0
- package/docs/openapi.yaml +8 -2
- package/package.json +18 -3
|
@@ -3,8 +3,17 @@ import { type Vault, type VaultStorage } from './vault.js';
|
|
|
3
3
|
export interface EncryptedVaultOptions {
|
|
4
4
|
/** Where the ciphertext goes; it never sees clear text. */
|
|
5
5
|
storage: VaultStorage;
|
|
6
|
-
/**
|
|
6
|
+
/**
|
|
7
|
+
* 32 raw bytes, see vaultKeyFromEnv() and parseVaultKey(). Encrypts every new entry and decrypts.
|
|
8
|
+
* The vault keeps its own copy.
|
|
9
|
+
*/
|
|
7
10
|
key: Uint8Array;
|
|
11
|
+
/**
|
|
12
|
+
* Keys of earlier rotations (32 raw bytes each, see previousVaultKeysFromEnv()): only for reading
|
|
13
|
+
* entries written before the rotation, tried in order after `key`. Drop a key once the longest TTL
|
|
14
|
+
* since the rotation has passed. The vault keeps its own copies. Node only, PHP has a single key.
|
|
15
|
+
*/
|
|
16
|
+
previousKeys?: readonly Uint8Array[] | undefined;
|
|
8
17
|
}
|
|
9
18
|
/**
|
|
10
19
|
* Encrypts mappings with XChaCha20-Poly1305 before they reach the storage. Byte-compatible with PHP's
|
|
@@ -13,6 +22,10 @@ export interface EncryptedVaultOptions {
|
|
|
13
22
|
*
|
|
14
23
|
* The vault id is bound as associated data, so a ciphertext copied to another id fails to decrypt.
|
|
15
24
|
* This is the only supported way to persist mappings.
|
|
25
|
+
*
|
|
26
|
+
* Key rotation (Node only): pass the new `key` and the old one in `previousKeys`. New entries are
|
|
27
|
+
* encrypted with `key`; reading tries `key`, then each previous key. Payloads stay PHP's format, so PHP
|
|
28
|
+
* reads every entry written with the key it has.
|
|
16
29
|
*/
|
|
17
30
|
export declare class EncryptedVault implements Vault {
|
|
18
31
|
#private;
|
|
@@ -21,13 +34,36 @@ export declare class EncryptedVault implements Vault {
|
|
|
21
34
|
fetch(id: string): Promise<PlaceholderMapping | null>;
|
|
22
35
|
delete(id: string): Promise<void>;
|
|
23
36
|
}
|
|
24
|
-
/**
|
|
37
|
+
/**
|
|
38
|
+
* A new random key (32 raw bytes), for creating a key once (`npx @waswrongassembled/pii-shield generate-key`
|
|
39
|
+
* prints one base64-encoded). Keep it as a secret and read it with vaultKeyFromEnv() or parseVaultKey(); a
|
|
40
|
+
* key generated at run time cannot read entries after a restart or in another process.
|
|
41
|
+
*/
|
|
25
42
|
export declare function generateVaultKey(): Uint8Array;
|
|
26
43
|
/**
|
|
27
44
|
* Decodes a base64 key (e.g. from an environment variable or a secret file; surrounding whitespace is
|
|
28
45
|
* ignored). Throws without echoing the input.
|
|
29
46
|
*/
|
|
30
47
|
export declare function parseVaultKey(base64: string): Uint8Array;
|
|
48
|
+
type Env = Readonly<Record<string, string | undefined>>;
|
|
49
|
+
/**
|
|
50
|
+
* Reads the vault key (32 bytes, base64) from an environment variable, default `PII_SHIELD_VAULT_KEY`
|
|
51
|
+
* (the service reads `PII_SERVICE_VAULT_KEY`). Create a key once with
|
|
52
|
+
* `npx @waswrongassembled/pii-shield generate-key`.
|
|
53
|
+
*
|
|
54
|
+
* @throws TypeError if the variable is missing, empty or not a 32-byte base64 key; the message names the
|
|
55
|
+
* variable, never its value
|
|
56
|
+
*/
|
|
57
|
+
export declare function vaultKeyFromEnv(name?: string, env?: Env): Uint8Array;
|
|
58
|
+
/**
|
|
59
|
+
* Reads the keys of earlier rotations (comma-separated, each 32 bytes base64) for
|
|
60
|
+
* `EncryptedVault`'s `previousKeys`, default variable `PII_SHIELD_VAULT_PREVIOUS_KEYS`. Missing or empty
|
|
61
|
+
* gives `[]`.
|
|
62
|
+
*
|
|
63
|
+
* @throws TypeError if an entry is not a 32-byte base64 key; the message names the variable and the
|
|
64
|
+
* entry's position, never a value
|
|
65
|
+
*/
|
|
66
|
+
export declare function previousVaultKeysFromEnv(name?: string, env?: Env): Uint8Array[];
|
|
31
67
|
/**
|
|
32
68
|
* Encrypts a mapping with a given nonce: the seam for byte-parity tests with PHP payloads. Not public API:
|
|
33
69
|
* reusing a nonce breaks XChaCha20-Poly1305.
|
|
@@ -35,3 +71,4 @@ export declare function parseVaultKey(base64: string): Uint8Array;
|
|
|
35
71
|
* @internal
|
|
36
72
|
*/
|
|
37
73
|
export declare function encryptMapping(key: Uint8Array, id: string, mapping: PlaceholderMapping, nonce: Uint8Array): Uint8Array;
|
|
74
|
+
export {};
|
|
@@ -11,20 +11,31 @@ const NONCE_BYTES = 24;
|
|
|
11
11
|
*
|
|
12
12
|
* The vault id is bound as associated data, so a ciphertext copied to another id fails to decrypt.
|
|
13
13
|
* This is the only supported way to persist mappings.
|
|
14
|
+
*
|
|
15
|
+
* Key rotation (Node only): pass the new `key` and the old one in `previousKeys`. New entries are
|
|
16
|
+
* encrypted with `key`; reading tries `key`, then each previous key. Payloads stay PHP's format, so PHP
|
|
17
|
+
* reads every entry written with the key it has.
|
|
14
18
|
*/
|
|
15
19
|
export class EncryptedVault {
|
|
16
20
|
#storage;
|
|
17
21
|
#key;
|
|
22
|
+
/** `key` first, then the previous keys. */
|
|
23
|
+
#decryptionKeys;
|
|
18
24
|
constructor(options) {
|
|
19
25
|
const { storage, key } = options;
|
|
20
|
-
|
|
21
|
-
|
|
26
|
+
const previousKeys = options.previousKeys ?? [];
|
|
27
|
+
assertKey(key, 'The key');
|
|
28
|
+
if (!Array.isArray(previousKeys)) {
|
|
29
|
+
throw new TypeError('The previous keys must be an array of keys.');
|
|
22
30
|
}
|
|
23
|
-
|
|
24
|
-
|
|
31
|
+
const previous = [];
|
|
32
|
+
for (const [i, candidate] of previousKeys.entries()) {
|
|
33
|
+
assertKey(candidate, `Previous key ${String(i + 1)}`);
|
|
34
|
+
previous.push(candidate);
|
|
25
35
|
}
|
|
26
36
|
this.#storage = storage;
|
|
27
37
|
this.#key = key.slice();
|
|
38
|
+
this.#decryptionKeys = Object.freeze([this.#key, ...previous.map((k) => k.slice())]);
|
|
28
39
|
}
|
|
29
40
|
async store(id, mapping, ttlSeconds) {
|
|
30
41
|
assertTtl(ttlSeconds);
|
|
@@ -36,14 +47,9 @@ export class EncryptedVault {
|
|
|
36
47
|
if (payload === null) {
|
|
37
48
|
return null;
|
|
38
49
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
throw new RangeError('payload too short');
|
|
43
|
-
}
|
|
44
|
-
plaintext = xchacha20poly1305(this.#key, payload.subarray(0, NONCE_BYTES), utf8(id)).decrypt(payload.subarray(NONCE_BYTES));
|
|
45
|
-
}
|
|
46
|
-
catch {
|
|
50
|
+
const plaintext = decryptWithAny(this.#decryptionKeys, id, payload);
|
|
51
|
+
if (plaintext === null) {
|
|
52
|
+
// Same error whichever key failed, without a cause: nothing about the keys leaves the vault.
|
|
47
53
|
throw new VaultError('Vault entry cannot be decrypted (wrong key or tampered data).', 'VAULT_DECRYPTION_FAILED');
|
|
48
54
|
}
|
|
49
55
|
try {
|
|
@@ -57,7 +63,37 @@ export class EncryptedVault {
|
|
|
57
63
|
await this.#storage.remove(id);
|
|
58
64
|
}
|
|
59
65
|
}
|
|
60
|
-
/**
|
|
66
|
+
/** The plaintext, or null if no key authenticates the payload (or it is too short). */
|
|
67
|
+
function decryptWithAny(keys, id, payload) {
|
|
68
|
+
if (payload.length <= NONCE_BYTES) {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
const nonce = payload.subarray(0, NONCE_BYTES);
|
|
72
|
+
const ciphertext = payload.subarray(NONCE_BYTES);
|
|
73
|
+
const associatedData = utf8(id);
|
|
74
|
+
for (const key of keys) {
|
|
75
|
+
try {
|
|
76
|
+
return xchacha20poly1305(key, nonce, associatedData).decrypt(ciphertext);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// wrong key or tampered data: try the next key
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
function assertKey(key, what) {
|
|
85
|
+
if (!(key instanceof Uint8Array)) {
|
|
86
|
+
throw new TypeError(`${what} must be a Uint8Array of 32 raw bytes, see parseVaultKey().`);
|
|
87
|
+
}
|
|
88
|
+
if (key.length !== KEY_BYTES) {
|
|
89
|
+
throw new RangeError(`${what} must be ${String(KEY_BYTES)} bytes long.`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* A new random key (32 raw bytes), for creating a key once (`npx @waswrongassembled/pii-shield generate-key`
|
|
94
|
+
* prints one base64-encoded). Keep it as a secret and read it with vaultKeyFromEnv() or parseVaultKey(); a
|
|
95
|
+
* key generated at run time cannot read entries after a restart or in another process.
|
|
96
|
+
*/
|
|
61
97
|
export function generateVaultKey() {
|
|
62
98
|
return globalThis.crypto.getRandomValues(new Uint8Array(KEY_BYTES));
|
|
63
99
|
}
|
|
@@ -81,6 +117,53 @@ export function parseVaultKey(base64) {
|
|
|
81
117
|
}
|
|
82
118
|
return key;
|
|
83
119
|
}
|
|
120
|
+
/**
|
|
121
|
+
* Reads the vault key (32 bytes, base64) from an environment variable, default `PII_SHIELD_VAULT_KEY`
|
|
122
|
+
* (the service reads `PII_SERVICE_VAULT_KEY`). Create a key once with
|
|
123
|
+
* `npx @waswrongassembled/pii-shield generate-key`.
|
|
124
|
+
*
|
|
125
|
+
* @throws TypeError if the variable is missing, empty or not a 32-byte base64 key; the message names the
|
|
126
|
+
* variable, never its value
|
|
127
|
+
*/
|
|
128
|
+
export function vaultKeyFromEnv(name = 'PII_SHIELD_VAULT_KEY', env = process.env) {
|
|
129
|
+
const value = env[name];
|
|
130
|
+
if (value === undefined || value.trim() === '') {
|
|
131
|
+
throw new TypeError(`${name} is not set. Set it to a vault key (32 bytes, base64), e.g. from "npx @waswrongassembled/pii-shield generate-key".`);
|
|
132
|
+
}
|
|
133
|
+
try {
|
|
134
|
+
return parseVaultKey(value);
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
throw new TypeError(`${name} must be a vault key of 32 bytes, base64-encoded.`);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Reads the keys of earlier rotations (comma-separated, each 32 bytes base64) for
|
|
142
|
+
* `EncryptedVault`'s `previousKeys`, default variable `PII_SHIELD_VAULT_PREVIOUS_KEYS`. Missing or empty
|
|
143
|
+
* gives `[]`.
|
|
144
|
+
*
|
|
145
|
+
* @throws TypeError if an entry is not a 32-byte base64 key; the message names the variable and the
|
|
146
|
+
* entry's position, never a value
|
|
147
|
+
*/
|
|
148
|
+
export function previousVaultKeysFromEnv(name = 'PII_SHIELD_VAULT_PREVIOUS_KEYS', env = process.env) {
|
|
149
|
+
const entries = (env[name] ?? '')
|
|
150
|
+
.split(',')
|
|
151
|
+
.map((entry) => entry.trim())
|
|
152
|
+
.filter((entry) => entry !== '');
|
|
153
|
+
const keys = [];
|
|
154
|
+
for (const [i, entry] of entries.entries()) {
|
|
155
|
+
try {
|
|
156
|
+
keys.push(parseVaultKey(entry));
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
for (const key of keys) {
|
|
160
|
+
key.fill(0);
|
|
161
|
+
}
|
|
162
|
+
throw new TypeError(`${name} must be a comma-separated list of vault keys (32 bytes, base64-encoded); entry ${String(i + 1)} is not.`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return keys;
|
|
166
|
+
}
|
|
84
167
|
/**
|
|
85
168
|
* Encrypts a mapping with a given nonce: the seam for byte-parity tests with PHP payloads. Not public API:
|
|
86
169
|
* reusing a nonce breaks XChaCha20-Poly1305.
|
|
@@ -5,7 +5,7 @@ export interface MemoryVaultStorageOptions {
|
|
|
5
5
|
}
|
|
6
6
|
/**
|
|
7
7
|
* Process-local VaultStorage with expiry, for tests and single-process apps that want EncryptedVault's
|
|
8
|
-
* behaviour without a server. Expired entries are purged on every write. Payloads are copied in and out.
|
|
8
|
+
* behaviour without a server. Expired entries are purged on every write and by purgeExpired(). Payloads are copied in and out.
|
|
9
9
|
*/
|
|
10
10
|
export declare class MemoryVaultStorage implements VaultStorage {
|
|
11
11
|
#private;
|
|
@@ -13,4 +13,11 @@ export declare class MemoryVaultStorage implements VaultStorage {
|
|
|
13
13
|
put(id: string, payload: Uint8Array, ttlSeconds: number): Promise<void>;
|
|
14
14
|
get(id: string): Promise<Uint8Array | null>;
|
|
15
15
|
remove(id: string): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Deletes expired entries (writes do it too). createPiiShield() calls it on its purge timer, so an idle
|
|
18
|
+
* process does not hold expired mappings.
|
|
19
|
+
*
|
|
20
|
+
* @returns the number of deleted entries
|
|
21
|
+
*/
|
|
22
|
+
purgeExpired(): Promise<number>;
|
|
16
23
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { assertTtl } from './vault.js';
|
|
3
3
|
/**
|
|
4
4
|
* Process-local VaultStorage with expiry, for tests and single-process apps that want EncryptedVault's
|
|
5
|
-
* behaviour without a server. Expired entries are purged on every write. Payloads are copied in and out.
|
|
5
|
+
* behaviour without a server. Expired entries are purged on every write and by purgeExpired(). Payloads are copied in and out.
|
|
6
6
|
*/
|
|
7
7
|
export class MemoryVaultStorage {
|
|
8
8
|
#entries = new Map();
|
|
@@ -12,13 +12,8 @@ export class MemoryVaultStorage {
|
|
|
12
12
|
}
|
|
13
13
|
async put(id, payload, ttlSeconds) {
|
|
14
14
|
assertTtl(ttlSeconds);
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
if (entry.expiresAt <= now) {
|
|
18
|
-
this.#entries.delete(key);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
this.#entries.set(id, { payload: payload.slice(), expiresAt: now + ttlSeconds * 1000 });
|
|
15
|
+
await this.purgeExpired();
|
|
16
|
+
this.#entries.set(id, { payload: payload.slice(), expiresAt: this.#clock() + ttlSeconds * 1000 });
|
|
22
17
|
}
|
|
23
18
|
async get(id) {
|
|
24
19
|
const entry = this.#entries.get(id);
|
|
@@ -34,4 +29,21 @@ export class MemoryVaultStorage {
|
|
|
34
29
|
async remove(id) {
|
|
35
30
|
this.#entries.delete(id);
|
|
36
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Deletes expired entries (writes do it too). createPiiShield() calls it on its purge timer, so an idle
|
|
34
|
+
* process does not hold expired mappings.
|
|
35
|
+
*
|
|
36
|
+
* @returns the number of deleted entries
|
|
37
|
+
*/
|
|
38
|
+
async purgeExpired() {
|
|
39
|
+
const now = this.#clock();
|
|
40
|
+
let deleted = 0;
|
|
41
|
+
for (const [key, entry] of this.#entries) {
|
|
42
|
+
if (entry.expiresAt <= now) {
|
|
43
|
+
this.#entries.delete(key);
|
|
44
|
+
deleted++;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return deleted;
|
|
48
|
+
}
|
|
37
49
|
}
|
package/dist/vault/vault.d.ts
CHANGED
|
@@ -19,13 +19,29 @@ export interface Vault {
|
|
|
19
19
|
delete(id: string): Promise<void>;
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
22
|
-
* Raw byte storage behind EncryptedVault (PHP's VaultStorageInterface).
|
|
23
|
-
*
|
|
22
|
+
* Raw byte storage behind EncryptedVault (PHP's VaultStorageInterface). Implement this to plug in any
|
|
23
|
+
* backend (Redis, a database, files, ...), and check it with `verifyVaultStorage()` from
|
|
24
|
+
* `@waswrongassembled/pii-shield/testing`.
|
|
25
|
+
*
|
|
26
|
+
* Behind EncryptedVault it only ever receives ciphertext: never use a storage on its own for mappings.
|
|
27
|
+
*
|
|
28
|
+
* Contract every implementation keeps:
|
|
29
|
+
* - Ids are arbitrary strings that may come from outside (a client's `vaultId`): map them to keys
|
|
30
|
+
* safely, e.g. by hashing, never by building a path or a query from them.
|
|
31
|
+
* - Payloads are binary-safe (every byte value, any length) and copied: later changes to the array
|
|
32
|
+
* passed to `put()` or returned by `get()` do not change the stored entry.
|
|
33
|
+
* - `put()` overwrites an existing entry, payload and expiry.
|
|
34
|
+
* - `get()` of an unknown or expired id gives `null`. Expiry is checked on read, not left to a cleanup
|
|
35
|
+
* job; expired entries must still be deleted eventually (storage limitation, Art. 5(1)(e) GDPR).
|
|
36
|
+
* - `remove()` of an unknown id is a no-op.
|
|
37
|
+
* - A TTL that is not a positive integer rejects the Promise (see assertTtl()) before anything is stored.
|
|
24
38
|
*/
|
|
25
39
|
export interface VaultStorage {
|
|
26
40
|
/** @param ttlSeconds positive integer */
|
|
27
41
|
put(id: string, payload: Uint8Array, ttlSeconds: number): Promise<void>;
|
|
42
|
+
/** @returns a copy of the payload, or null if the id is unknown or expired */
|
|
28
43
|
get(id: string): Promise<Uint8Array | null>;
|
|
44
|
+
/** Deletes the entry; resolves also if there is none. */
|
|
29
45
|
remove(id: string): Promise<void>;
|
|
30
46
|
}
|
|
31
47
|
/** Current time in milliseconds, like `Date.now` (PHP takes a PSR-20 clock). Inject one in tests. */
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { VaultSetup } from '../create-pii-shield.js';
|
|
1
2
|
import { type VaultStorage } from '../vault/vault.js';
|
|
2
3
|
/**
|
|
3
4
|
* The ioredis methods RedisVaultStorage calls. An ioredis `Redis` or `Cluster` instance satisfies it;
|
|
@@ -26,3 +27,16 @@ export declare class RedisVaultStorage implements VaultStorage {
|
|
|
26
27
|
get(id: string): Promise<Uint8Array | null>;
|
|
27
28
|
remove(id: string): Promise<void>;
|
|
28
29
|
}
|
|
30
|
+
export interface RedisVaultOptions extends RedisVaultStorageOptions {
|
|
31
|
+
/** 32 raw bytes, e.g. `vaultKeyFromEnv()`, the same in every process. Required: entries outlive the process. */
|
|
32
|
+
key: Uint8Array;
|
|
33
|
+
/** Keys of earlier rotations, only for reading, e.g. `previousVaultKeysFromEnv()`. */
|
|
34
|
+
previousKeys?: readonly Uint8Array[] | undefined;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The vault of createPiiShield() on Redis: a RedisVaultStorage behind an EncryptedVault with your key.
|
|
38
|
+
* Redis expires entries itself (`SET … EX`), so nothing needs purging. The client stays yours to quit.
|
|
39
|
+
*/
|
|
40
|
+
export declare function redisVault(options: RedisVaultOptions): VaultSetup & {
|
|
41
|
+
storage: RedisVaultStorage;
|
|
42
|
+
};
|
|
@@ -31,3 +31,11 @@ export class RedisVaultStorage {
|
|
|
31
31
|
return this.#prefix + createHash('sha256').update(id, 'utf8').digest('hex');
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
|
+
/**
|
|
35
|
+
* The vault of createPiiShield() on Redis: a RedisVaultStorage behind an EncryptedVault with your key.
|
|
36
|
+
* Redis expires entries itself (`SET … EX`), so nothing needs purging. The client stays yours to quit.
|
|
37
|
+
*/
|
|
38
|
+
export function redisVault(options) {
|
|
39
|
+
const { key, previousKeys, ...store } = options;
|
|
40
|
+
return { storage: new RedisVaultStorage(store), key, previousKeys };
|
|
41
|
+
}
|
package/docs/openapi.yaml
CHANGED
|
@@ -167,7 +167,10 @@ paths:
|
|
|
167
167
|
pattern; they are learned as model-only types and reach the model as few-shot examples.
|
|
168
168
|
Learned types take effect immediately for `/v1/redact` and `/v1/mask`.
|
|
169
169
|
|
|
170
|
-
Examples are stored
|
|
170
|
+
Examples are stored unless the service runs with `PII_SERVICE_LEARNED_KEEP_EXAMPLES=false`:
|
|
171
|
+
use synthetic or anonymised values only. Without stored examples, the returned type has empty
|
|
172
|
+
`examples` and `counterExamples`, and plain words without a pattern are refused with 422 (only
|
|
173
|
+
their examples would let the model find them); give a `pattern` instead. Hand-written patterns follow a
|
|
171
174
|
restricted grammar (no repeated groups, anchors, backreferences or lookarounds) to rule out
|
|
172
175
|
catastrophic backtracking.
|
|
173
176
|
operationId: learn
|
|
@@ -381,7 +384,10 @@ components:
|
|
|
381
384
|
properties:
|
|
382
385
|
name: { $ref: '#/components/schemas/TypeName' }
|
|
383
386
|
description: { type: string }
|
|
384
|
-
examples:
|
|
387
|
+
examples:
|
|
388
|
+
type: array
|
|
389
|
+
items: { type: string }
|
|
390
|
+
description: Empty for a type with a pattern whose examples are not kept (PII_SERVICE_LEARNED_KEEP_EXAMPLES=false).
|
|
385
391
|
counterExamples: { type: array, items: { type: string } }
|
|
386
392
|
pattern: { type: [string, 'null'], description: null for model-only types }
|
|
387
393
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@waswrongassembled/pii-shield",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Reversible PII redaction for LLM applications with DACH recognizers (AT SVNR, IBAN, tax numbers) and checksum validation. Wire-compatible port of clarifile/pii-shield.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
"ai-sdk",
|
|
21
21
|
"mcp",
|
|
22
22
|
"iban",
|
|
23
|
-
"dach"
|
|
23
|
+
"dach",
|
|
24
|
+
"postgres"
|
|
24
25
|
],
|
|
25
26
|
"repository": {
|
|
26
27
|
"type": "git",
|
|
@@ -54,6 +55,14 @@
|
|
|
54
55
|
"types": "./dist/mcp/index.d.ts",
|
|
55
56
|
"default": "./dist/mcp/index.js"
|
|
56
57
|
},
|
|
58
|
+
"./postgres": {
|
|
59
|
+
"types": "./dist/postgres/index.d.ts",
|
|
60
|
+
"default": "./dist/postgres/index.js"
|
|
61
|
+
},
|
|
62
|
+
"./testing": {
|
|
63
|
+
"types": "./dist/testing/index.d.ts",
|
|
64
|
+
"default": "./dist/testing/index.js"
|
|
65
|
+
},
|
|
57
66
|
"./package.json": "./package.json"
|
|
58
67
|
},
|
|
59
68
|
"bin": {
|
|
@@ -88,6 +97,7 @@
|
|
|
88
97
|
"@eslint/js": "^10.0.1",
|
|
89
98
|
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
90
99
|
"@types/node": "^22.20.5",
|
|
100
|
+
"@types/pg": "8.23.1",
|
|
91
101
|
"@vitest/coverage-v8": "^5.1.1",
|
|
92
102
|
"ai": "^7.0.177",
|
|
93
103
|
"ajv": "^8.20.0",
|
|
@@ -95,6 +105,7 @@
|
|
|
95
105
|
"eslint-config-prettier": "^10.1.8",
|
|
96
106
|
"globals": "^17.14.0",
|
|
97
107
|
"ioredis": "^6.0.0",
|
|
108
|
+
"pg": "8.23.1",
|
|
98
109
|
"prettier": "^3.10.1",
|
|
99
110
|
"publint": "^0.3.25",
|
|
100
111
|
"typescript": "~6.0.3",
|
|
@@ -108,7 +119,8 @@
|
|
|
108
119
|
"peerDependencies": {
|
|
109
120
|
"@modelcontextprotocol/sdk": "^1.32.0",
|
|
110
121
|
"ai": "^7.0.0",
|
|
111
|
-
"ioredis": "^5.0.0 || ^6.0.0"
|
|
122
|
+
"ioredis": "^5.0.0 || ^6.0.0",
|
|
123
|
+
"pg": "^8.0.0"
|
|
112
124
|
},
|
|
113
125
|
"peerDependenciesMeta": {
|
|
114
126
|
"@modelcontextprotocol/sdk": {
|
|
@@ -119,6 +131,9 @@
|
|
|
119
131
|
},
|
|
120
132
|
"ioredis": {
|
|
121
133
|
"optional": true
|
|
134
|
+
},
|
|
135
|
+
"pg": {
|
|
136
|
+
"optional": true
|
|
122
137
|
}
|
|
123
138
|
}
|
|
124
139
|
}
|