-
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
7
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
7 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
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,16 @@ | ||
| # tracker-utils | ||
| ## Logger Masking Toggle | ||
|
|
||
| Masking is enabled by default. | ||
|
|
||
| To disable masking (Debug Mode): | ||
|
|
||
| ```bash | ||
| MASK_LOGS=false node examples/loggerTest.js | ||
| ``` | ||
| To enable masking: | ||
|
|
||
| ```bash | ||
| MASK_LOGS=true node examples/loggerTest.js | ||
| ``` | ||
| If MASK_LOGS is not set, masking remains enabled by default. |
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,128 @@ | ||
| // Manual logger masking demo script (not used in production) | ||
| let { Logger } = require('../index'); | ||
|
|
||
| Logger.init({ | ||
| service: "test-service", | ||
| enableConsoleLog: true | ||
| }); | ||
|
|
||
| // EMAIL + PHONE TESTS | ||
| Logger.info("User email is test@gmail.com and phone is +919876543210"); | ||
| Logger.info("User email is test@gmail.com and phone is (+91) 9876543210"); | ||
| Logger.info("User phone is 98765 43210"); | ||
|
|
||
| Logger.info("Contact test@gmail.com or admin@yahoo.com"); | ||
| Logger.info("Email vikrant+prod@gmail.com"); | ||
| Logger.info("Email user@mail.company.co.uk"); | ||
| Logger.info('{"email":"abc@gmail.com"}'); | ||
| Logger.info("ADMIN@COMPANY.COM"); | ||
| Logger.info("Email test@gmail"); | ||
| Logger.info("tom@gmail.com"); | ||
|
|
||
| // PHONE VARIATIONS | ||
| Logger.info("Call 9876543210"); | ||
| Logger.info("Call +919876543210"); | ||
| Logger.info("Call +919876543210"); | ||
| Logger.info("Call 98765-43210"); | ||
| Logger.info("Phones 9876543210 and 9123456789"); | ||
| Logger.info("US number +14155552671"); | ||
| Logger.info("Call (+91) 9876543210"); | ||
| Logger.info("Call +256 1234567899") | ||
|
|
||
| // MIXED STRING TEST | ||
| Logger.info("User abc@gmail.com called 9876543210"); | ||
|
|
||
| // JSON STRING TEST | ||
| Logger.info('{"email":"abc@gmail.com","phone":"9876543210"}'); | ||
|
|
||
| // OBJECT LOG TESTS | ||
| Logger.info({ | ||
| message: "Payment from user@gmail.com", | ||
| phone: "+919876543210" | ||
| }); | ||
|
|
||
| Logger.info({ | ||
| email: "abc@gmail.com", | ||
| phone: "9876543210", | ||
| message: "Testing object log with some dummy value 876 and email is abc@gmail.com with phone number is 1234567890" | ||
| }); | ||
|
|
||
| // NAME MASKING TESTS | ||
| Logger.info({ | ||
| name: "Vikrant", | ||
| email: "user@gmail.com", | ||
| phone: "9876543210" | ||
| }); | ||
|
|
||
| Logger.info({ | ||
| name: "John Doe", | ||
| message: "User profile updated" | ||
| }); | ||
|
|
||
| // REGION ADDRESS TESTS | ||
| Logger.info({ | ||
| name: "Alice Wonderland", | ||
| region: { | ||
| address: "B 63, Sector 70, Zone 2", | ||
| city: "Noida", | ||
| state: "Uttar Pradesh", | ||
| zipcode: "201301", | ||
| country: "IN" | ||
| } | ||
| }); | ||
|
|
||
| Logger.info({ | ||
| region: { | ||
| address: "221B Baker Street, Zone 5", | ||
| city: "London", | ||
| state: "Greater London", | ||
| zipcode: "NW16XE" | ||
| }, | ||
| message: "Shipping details updated" | ||
| }); | ||
|
|
||
| Logger.info({ | ||
| region: { | ||
| address: "Contact abc@gmail.com or call +919876543210", | ||
| city: "Mumbai", | ||
| state: "MH", | ||
| zipcode: "400001" | ||
| } | ||
| }); | ||
|
|
||
| Logger.info({ | ||
| region: { | ||
| city: "Delhi", | ||
| state: "Delhi" | ||
| } | ||
| }); | ||
|
|
||
| // DB-LIKE OBJECT TEST | ||
| Logger.info({ | ||
| display_id: 752, | ||
| name: "Test Advertiser", | ||
| email: "advtest123@test.com", | ||
| phone: "9876543210", | ||
| region: { | ||
| address: "B 63 Sector 70 Zone 2", | ||
| city: "Noida", | ||
| state: "UP", | ||
| zipcode: "201301", | ||
| country: "IN", | ||
| currency: "INR" | ||
| }, | ||
| status: "active" | ||
| }); | ||
|
|
||
| Logger.info({ | ||
| display_id: 752, | ||
| name: "test", | ||
| email: "test@gmail.com", | ||
| phone: "9876543210", | ||
| status: "active" | ||
| }); | ||
|
|
||
|
|
||
| // SAFE TEXT TEST | ||
| Logger.info("Service started successfully"); | ||
| Logger.info("No sensitive data here"); |
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,163 @@ | ||
| // Import masking function | ||
| const { maskData } = require('../logger/mask'); | ||
|
|
||
| describe("PII Masking Tests", () => { | ||
|
|
||
| /* ---------------- EMAIL TESTS ---------------- */ | ||
|
|
||
| // Should mask simple email | ||
| test("should mask simple email", () => { | ||
| const input = "test@gmail.com"; | ||
| const output = maskData(input); | ||
| expect(output).toContain("@"); | ||
| expect(output).not.toContain("test@gmail.com"); | ||
| }); | ||
|
|
||
| // Should mask short email user <=3 | ||
| test("should mask short email user <=3", () => { | ||
| const input = "tom@gmail.com"; | ||
| const output = maskData(input); | ||
| expect(output).toMatch(/\*\*\*/); | ||
| }); | ||
|
|
||
| // Should mask complex email | ||
| test("should mask complex email", () => { | ||
| const input = "vikrant.yadav@trackier.com"; | ||
| const output = maskData(input); | ||
| expect(output).not.toContain("vikrant.yadav"); | ||
| expect(output).not.toContain("trackier"); | ||
| }); | ||
|
|
||
| // Should mask multiple emails | ||
| test("should mask multiple emails in string", () => { | ||
| const input = "Contact test@gmail.com or admin@yahoo.com"; | ||
| const output = maskData(input); | ||
| expect(output).not.toContain("test@gmail.com"); | ||
| expect(output).not.toContain("admin@yahoo.com"); | ||
| }); | ||
|
|
||
| /* ---------------- PHONE TESTS ---------------- */ | ||
|
|
||
| // Should expose last 4 digits | ||
| test("should mask phone and expose last 4 digits", () => { | ||
| const input = "9876543210"; | ||
| const output = maskData(input); | ||
| expect(output.endsWith("3210")).toBe(true); | ||
| }); | ||
|
|
||
| // Should handle phone with country code | ||
| test("should mask phone with country code", () => { | ||
| const input = "+256 1234567899"; | ||
| const output = maskData(input); | ||
| expect(output.endsWith("7899")).toBe(true); | ||
| }); | ||
|
|
||
| // Should not change phone <=3 digits (regex won’t match) | ||
| test("should not mask phone <=3 digits", () => { | ||
| const input = "123"; | ||
| const output = maskData(input); | ||
| expect(output).toBe("123"); | ||
| }); | ||
|
|
||
| /* ---------------- NAME TESTS ---------------- */ | ||
|
|
||
| // Should mask full name | ||
| test("should mask full name", () => { | ||
| const input = { name: "Vikrant Yadav" }; | ||
| const output = maskData(input); | ||
| expect(output.name).not.toBe("Vikrant Yadav"); | ||
| }); | ||
|
|
||
| // Should fully mask short name | ||
| test("should mask short name fully", () => { | ||
| const input = { name: "Tom" }; | ||
| const output = maskData(input); | ||
| expect(output.name).toBe("***"); | ||
| }); | ||
|
|
||
| /* ---------------- REGION TESTS ---------------- */ | ||
|
|
||
| // Should mask region fields | ||
| test("should mask region fields", () => { | ||
| const input = { | ||
| region: { | ||
| address: "Sector 70", | ||
| city: "Noida", | ||
| state: "UP", | ||
| zipcode: "201301" | ||
| } | ||
| }; | ||
|
|
||
| const output = maskData(input); | ||
|
|
||
| expect(output.region.address).toBe("********"); | ||
| expect(output.region.city).toBe("********"); | ||
| expect(output.region.state).toBe("********"); | ||
| expect(output.region.zipcode).toBe("********"); | ||
| }); | ||
|
|
||
| /* ---------------- OBJECT TESTS ---------------- */ | ||
|
|
||
| // Should mask email and phone in object | ||
| test("should mask email and phone in object", () => { | ||
| const input = { | ||
| email: "abc@gmail.com", | ||
| phone: "9876543210" | ||
| }; | ||
|
|
||
| const output = maskData(input); | ||
|
|
||
| expect(output.email).not.toContain("abc@gmail.com"); | ||
| expect(output.phone.endsWith("3210")).toBe(true); | ||
| }); | ||
|
|
||
| // Should recursively mask nested object | ||
| test("should recursively mask nested object", () => { | ||
| const input = { | ||
| user: { | ||
| email: "nested@gmail.com", | ||
| phone: "9876543210" | ||
| } | ||
| }; | ||
|
|
||
| const output = maskData(input); | ||
|
|
||
| expect(output.user.email).not.toContain("nested@gmail.com"); | ||
| expect(output.user.phone.endsWith("3210")).toBe(true); | ||
| }); | ||
|
|
||
| /* ---------------- SAFE STRING TEST ---------------- */ | ||
|
|
||
| // Should not modify safe string | ||
| test("should not change safe string", () => { | ||
| const input = "Service started successfully"; | ||
| const output = maskData(input); | ||
| expect(output).toBe(input); | ||
| }); | ||
|
|
||
| }); | ||
|
|
||
|
|
||
| /* | ||
| MASKING TOGGLE TESTS | ||
| */ | ||
|
|
||
| describe("Masking Toggle Tests", () => { | ||
|
|
||
| // Should return original value when masking disabled | ||
| test("should return original value when MASK_LOGS=false", () => { | ||
|
|
||
| process.env.MASK_LOGS = "false"; | ||
|
|
||
| jest.resetModules(); // reload module with updated env | ||
| const { maskData } = require('../logger/mask'); | ||
|
|
||
| const input = "test@gmail.com"; | ||
| const output = maskData(input); | ||
|
|
||
| expect(output).toBe("test@gmail.com"); | ||
|
|
||
| process.env.MASK_LOGS = "true"; // restore for other tests | ||
| }); | ||
|
|
||
| }); | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.