-
Notifications
You must be signed in to change notification settings - Fork 2
feat(logger): add PII masking and safe object log formatting #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tr-emp-260
wants to merge
10
commits into
master
Choose a base branch
from
feature/log-masking
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ec6bb0e
feat(logger): add PII masking and safe object log formatting
8f71dc7
Improve PII masking: partial email masking, last-4 phone visibility, …
d621b79
feat(logger): add PII masking with env toggle and test coverage
d171c3c
TP-2417: Add configurable PII masking with email/phone/name handling …
763725a
TP-2417: Fix null region guard, circular reference handling, and mask…
f5cc7d2
TP-2417: Recursively mask region object to prevent PII leakage
a742e05
TP-2417: Revert unintended package-lock.json changes
35e14a5
chore: remove test files from commit
tr-emp-260 119af53
chore: remove readme
tr-emp-260 249b7a4
readme
tr-emp-260 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,3 +3,4 @@ node_modules | |
| sftp-config.json | ||
| .vscode/launch.json | ||
| .env | ||
| combined.log | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| # tracker-utils | ||
|
|
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| // Enable/Disable masking via env (default: enabled) | ||
| const ENABLE_MASKING = process.env.MASK_LOGS !== "false"; | ||
|
|
||
| // Regex patterns for detecting emails and phone numbers | ||
| const REGEX_PATTERNS = [ | ||
| { | ||
| regex: /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g, | ||
| replacer: (match) => maskEmail(match) | ||
| }, | ||
| { | ||
| regex: /\+?\d[\d\s\-]{8,}\d/g, | ||
| replacer: (match) => maskPhone(match) | ||
| } | ||
| ]; | ||
|
|
||
| // Generic mask used for structured fields like address | ||
| const MASK = "********"; | ||
|
|
||
| // Masks phone number and exposes last 4 digits | ||
| const maskPhone = (phone) => { | ||
| const digits = phone.replace(/\D/g, ""); | ||
|
|
||
| if (digits.length <= 3) return "*".repeat(digits.length); | ||
|
|
||
| const last4 = digits.slice(-4); | ||
| return "*".repeat(Math.max(digits.length - 4, 0)) + last4; | ||
| }; | ||
|
|
||
| // Masks email while partially exposing user and domain | ||
| const maskEmail = (email) => { | ||
| const [user, domain] = email.split("@"); | ||
| if (!domain) return "***"; | ||
|
|
||
| const maskedUser = | ||
| user.length <= 3 | ||
| ? "*".repeat(user.length) | ||
| : ( | ||
| user[0] + | ||
| "*".repeat(user.length - 3) + | ||
| user.slice(-2) | ||
| ); | ||
|
|
||
| const domainParts = domain.split("."); | ||
| const mainDomain = domainParts[0] || ""; | ||
|
|
||
| const maskedDomain = | ||
| mainDomain.length <= 3 | ||
| ? "***" | ||
| : ( | ||
| "*".repeat(mainDomain.length - 2) + | ||
| mainDomain.slice(-2) | ||
| ); | ||
|
|
||
| const maskedTld = "***"; | ||
|
|
||
| return `${maskedUser}@${maskedDomain}.${maskedTld}`; | ||
| }; | ||
|
|
||
| // Masks name by showing the first and last two characters | ||
| const maskName = (name) => { | ||
| if (!name || typeof name !== "string") return name; | ||
|
|
||
| return name | ||
| .split(" ") | ||
| .map(part => { | ||
| if (part.length <= 3) return "*".repeat(part.length); | ||
|
|
||
| return ( | ||
| part[0] + | ||
| "*".repeat(part.length - 3) + | ||
| part.slice(-2) | ||
| ); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
| .join(" "); | ||
| }; | ||
|
|
||
| // Applies regex-based masking on free-form strings | ||
| const applyRegexMasking = (str) => { | ||
| let masked = str; | ||
|
|
||
| REGEX_PATTERNS.forEach(({ regex, replacer }) => { | ||
| masked = masked.replace(regex, replacer); | ||
| }); | ||
|
|
||
| return masked; | ||
| }; | ||
|
|
||
| // Recursively masks structured objects (name, region, nested fields) | ||
| const maskObject = (obj, seen = new WeakSet()) => { | ||
| if (!obj || typeof obj !== "object") return obj; | ||
| if (seen.has(obj)) return "[Circular]"; | ||
| seen.add(obj); | ||
|
|
||
| const cloned = Array.isArray(obj) ? [...obj] : { ...obj }; | ||
|
|
||
| Object.keys(cloned).forEach(key => { | ||
| const value = cloned[key]; | ||
|
|
||
| if (key === "name" && typeof value === "string") { | ||
| cloned[key] = maskName(value); | ||
| return; | ||
| } | ||
|
|
||
| if (key === "region" && value!==null && typeof value === "object") { | ||
| const maskedRegion = maskObject(value, seen); | ||
| cloned[key] = { | ||
| ...maskedRegion, | ||
| address: value.address ? MASK : value.address, | ||
| city: value.city ? MASK : value.city, | ||
| state: value.state ? MASK : value.state, | ||
| zipcode: value.zipcode ? MASK : value.zipcode | ||
| }; | ||
| return; | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (typeof value === "string") { | ||
| cloned[key] = applyRegexMasking(value); | ||
| return; | ||
| } | ||
|
|
||
| if (typeof value === "object") { | ||
| cloned[key] = maskObject(value, seen); | ||
| } | ||
| }); | ||
|
|
||
| return cloned; | ||
| }; | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Entry point for masking (handles string or object) | ||
| const maskData = (data) => { | ||
|
|
||
| // Skip masking if disabled | ||
| if (!ENABLE_MASKING) return data; | ||
|
|
||
| if (!data) return data; | ||
|
|
||
| if (typeof data === "string") { | ||
| return applyRegexMasking(data); | ||
| } | ||
|
|
||
| if (typeof data === "object") { | ||
| return maskObject(data); | ||
| } | ||
|
|
||
| return data; | ||
| }; | ||
|
|
||
| module.exports = { maskData }; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Phone regex is overly broad — will mask non-phone numeric sequences.
The pattern
\+?\d[\d\s\-]{8,}\dmatches any 10+ digit sequence, which means order IDs, timestamps, transaction amounts, and other numeric identifiers will be incorrectly masked. For example,"Order 1234567890 confirmed"would have the order ID replaced with"**********".Consider tightening the regex (e.g., requiring a leading
+or parenthesized country code, or restricting match length) or switching to a key-based approach for phone masking in objects, similar to hownameis handled.🤖 Prompt for AI Agents