fieldmasker
v1.0.1
Published
Mask sensitive fields (passwords, tokens, cards, SSNs) from any object before logging or sending to analytics. Zero dependencies.
Downloads
20
Maintainers
Readme
fieldmasker
Mask sensitive fields from any object before logging, analytics, or API responses.
Zero dependencies. Works anywhere Node.js runs.
const user = {
name: "John",
email: "[email protected]",
password: "supersecret",
token: "sk-abc123xyz",
card: "4111111111111234"
};
fieldmasker(user).auto().value();
// {
// name: "John",
// email: "[email protected]",
// password: "****",
// token: "****",
// card: "****"
// }Why fieldmasker?
You've done this. Every developer has:
// Oops. Password just went to Datadog.
console.log("User login:", req.body);
// Oops. Token just went to Sentry.
logger.info({ user, token, session });
// Oops. Card number just went to your analytics pipeline.
analytics.track("checkout", orderPayload);fieldmasker is the one line you add before any of those calls.
Install
npm install fieldmaskerUsage
Auto-detect (recommended)
Masks 50+ common sensitive field names automatically — passwords, tokens, API keys, SSNs, card numbers, bank accounts, and more.
const fieldmasker = require('fieldmasker');
const result = fieldmasker(obj).auto().value();Mask specific fields only
fieldmasker(obj).only(['email', 'phone']).value();Add your own sensitive keys
fieldmasker(obj).auto().add(['internalCode', 'employeeId']).value();Skip a key even if it looks sensitive
// "token_count" looks sensitive but isn't
fieldmasker(obj).auto().skip(['token_count']).value();Custom mask string
fieldmasker(obj).auto().mask('[REDACTED]').value();
// { password: "[REDACTED]" }Show last N characters (great for card numbers, tokens)
fieldmasker(obj).auto().showLast(4).value();
// { card: "****1234", token: "****xyz9" }Works on nested objects and arrays
fieldmasker({
user: {
profile: { name: "Alice" },
auth: { password: "secret", token: "abc" }
},
sessions: [
{ id: 1, token: "tok1" },
{ id: 2, token: "tok2" }
]
}).auto().value();
// passwords and tokens masked at every levelStatic shorthand
// Quickest way to use it
fieldmasker.auto(obj);
fieldmasker.only(obj, ['email', 'ssn']);Return masked JSON string
fieldmasker(obj).auto().toJSON();
// → '{"name":"John","password":"****"}'API
| Method | Description |
|---|---|
| .auto() | Auto-detect sensitive keys using built-in list |
| .only(keys[]) | Mask only these specific keys |
| .add(keys[]) | Add extra keys to the auto-detect list |
| .skip(keys[]) | Never mask these keys (even if they look sensitive) |
| .mask(str) | Custom mask string (default: ****) |
| .showLast(n) | Show last N characters of masked value |
| .value() | Execute and return the masked object |
| .toJSON() | Execute and return masked JSON string |
| fieldmasker.auto(obj) | Static shorthand for .auto().value() |
| fieldmasker.only(obj, keys) | Static shorthand for .only(keys).value() |
| fieldmasker.defaults | Array of all built-in sensitive key names |
Built-in sensitive keys (50+)
password, passwd, token, accessToken, refreshToken, apiKey, api_key, secret, authorization, ssn, card, cardNumber, cvv, pin, privateKey, certificate, otp, sessionId, cookie, dob, bankAccount, salary, passport, license — and more.
See fieldmasker.defaults for the full list.
Real-world usage patterns
Safe logging middleware (Express)
app.use((req, res, next) => {
logger.info({
method: req.method,
path: req.path,
body: fieldmasker.auto(req.body) // ← never log raw body again
});
next();
});Safe error reporting (Sentry / Bugsnag)
Sentry.configureScope(scope => {
scope.setUser(fieldmasker.auto(user));
});Safe analytics
analytics.track('checkout_completed', fieldmasker(payload)
.auto()
.add(['internalOrderId'])
.value()
);Safe API response scrubbing
res.json(fieldmasker(dbRecord).auto().value());TypeScript
Full type definitions included.
import fieldmasker from 'fieldmasker';
const safe = fieldmasker(user).auto().value();License
MIT
