feat: implement PiiScrubInterceptor for PII hardening in metadata and…#353
Open
martinshub-tech wants to merge 2 commits into
Open
feat: implement PiiScrubInterceptor for PII hardening in metadata and…#353martinshub-tech wants to merge 2 commits into
martinshub-tech wants to merge 2 commits into
Conversation
Contributor
|
Hi 👋 Appreciate this PR! The CI is flagging a failing check — could you take a peek at the failed job and push a fix? Happy to help if you need a hand 🙏 |
…ptor and test suite
Author
i need help |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #213
Description
This PR addresses PII exposure vulnerability by implementing a robust, global PiiScrubInterceptor in the NestJS backend and exposing the PII regex patterns from the FastAPI AI service.
Arbitrary PII fields in models like Campaign.metadata, Claim.recipientRef, Session.metadata, SessionStep.input/output, and InternalNote.content are now hardened before database storage or forwarding to downstream/external services.
Key Changes
AI Service (app/ai-service/main.py):
Added a GET /api/v1/pii/patterns endpoint returning the lists of regex patterns utilized by PIIScrubberService.
Backend Application (app/backend):
PiiScrubInterceptor (src/common/interceptors/pii-scrub.interceptor.ts):
Recursively traverses incoming request body trees.
Scans configured high-risk keys (e.g. email, phone, name, nin, recipientRef, metadata, content, input, output) using regexes.
Supports three operational modes configured via PII_SCRUB_MODE env var: redact (default, replaces matched values with structured tokens such as [EMAIL_ADDRESS]), reject (throws a 422 Unprocessable Entity error listing all offending paths), and off.
Caches patterns in memory (1 min) and Redis (1 hr), with automatic fallback to default patterns if services are offline.
Implements a JWT subject allowlist bypass: if a value matches the request user identifier (sub, id, apiKeyId), it bypasses scrubbing.
main.ts: Registered the global interceptor sequentially after the ValidationPipe.
app.module.ts: Registered custom RedisService globally in providers.
Testing (app/backend/test/pii-interceptor.spec.ts):
Added unit tests covering deep nesting, arrays, allowlist bypass, operational modes, and Redis/AI Service caching behavior.