Repository navigation
Conversation
|
Thanks @Ardem2025, a reversible vault is a good idea and keeping it opt-in is exactly right. One design issue we would like to fix together before merging: the vault is a single process-wide map, so a response in one request can restore a token that was minted for another request (for example a model echoing <<PII_EMAIL_1>>), which would leak another user's value. If you scope the vault to the request (or add a per-request nonce to the token), register the flag in featureFlagDefinitions with a false default, run Prettier, and add a test for two concurrent requests, this will be in good shape. Happy to push those changes on your branch if you prefer. |
|
Thanks for the thorough review and great catch, @diegosouzapw! That's a critical point regarding cross-request token leakage. I've updated the PR accordingly:
Pushed in commit |
f782fe9 to
3dd57af
Compare
3dd57af to
b78921d
Compare
Summary
This PR introduces Reversible PII Token Vault — an enterprise-grade bidirectional pseudonymization and outbound detokenization mechanism for OmniRoute.
When enabled (
PII_REVERSIBLE_VAULT_ENABLED=true), sensitive personal data (emails, phones, national IDs, SSN/CPF, and custom PII patterns) are pseudonymized into surrogate tokens before reaching upstream LLM providers, and transparently re-hydrated on egress back to the client.Architecture & Key Highlights
Inbound Pseudonymization (
src/shared/utils/inputSanitizer.ts)[PHONE_REDACTED], detected PII items are mapped to surrogate tokens (<<PII_EMAIL_1>>,<<PII_PHONE_2>>).Zero-TTFT Speculative Streaming Detokenization (
open-sse/handlers/chatCore/streamingPipeline.ts)TransformStream(createDetokenizingStream).<prefix is detected at the tail of a chunk.<<PII_PH+ONE_1>>).safeJsonEscapeto guarantee valid JSON string escaping within SSE delta frames.Non-Streaming JSON Support (
open-sse/handlers/chatCore/nonStreamingJsonResponse.ts)buildNonStreamingJsonResponse) with accurateContent-Lengthcomputation.Zero-Disk Retention & DoS Protection (
src/lib/pii/tokenVault.ts)TokenVaultManager) and are never written to disk or logs.PII_VAULT_MAX_ENTRIES) protects against memory bloat.PII_VAULT_TTL_MS).timer.unref(), ensuring worker threads and test runners exit cleanly without dangling handles.Opt-in Compliance (Hard Rule feat: v0.2.0 — advanced routing services, cost analytics, pricing overhaul #20)
false(PII_REVERSIBLE_VAULT_ENABLED=false). Existing behavior is 100% preserved when disabled.Test Coverage
Added comprehensive unit test suite in
tests/unit/pii/tokenVault.test.ts(7/7 tests passing):✔ TokenVaultManager - surrogate generation, reuse, and TTL expiration✔ TokenVaultManager - LRU capacity eviction✔ createDetokenizingStream - Zero-TTFT fast path without tokens✔ createDetokenizingStream - detokenizes single chunk with surrogate tokens✔ createDetokenizingStream - speculative buffer across split chunk boundary✔ detokenizeResponsePayload - non-streaming JSON body detokenization✔ safeJsonEscape - preserves quotes and special characters inside SSE