Skip to content

feat: implement PiiScrubInterceptor for PII hardening in metadata and…#353

Open
martinshub-tech wants to merge 2 commits into
ChainForgee:mainfrom
martinshub-tech:main
Open

feat: implement PiiScrubInterceptor for PII hardening in metadata and…#353
martinshub-tech wants to merge 2 commits into
ChainForgee:mainfrom
martinshub-tech:main

Conversation

@martinshub-tech

@martinshub-tech martinshub-tech commented Jul 16, 2026

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
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 🙏

@martinshub-tech

Copy link
Copy Markdown
Author

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 🙏

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 🙏

i need help
i have tried ..it still brings the error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Server-side PII detection on metadata: Json fields sent to AI service

2 participants