@philiprehberger/mask-kit
v0.2.1
Published
Data masking and redaction for logs and APIs
Downloads
55
Readme
@philiprehberger/mask-kit
Data masking and redaction for logs and APIs
Installation
npm install @philiprehberger/mask-kitUsage
import { maskEmail, maskCreditCard, maskObject } from '@philiprehberger/mask-kit';
maskEmail('[email protected]'); // "u***@e******.com"
maskCreditCard('4111111111111111'); // "************1111"
const safe = maskObject(requestBody, {
rules: ['password', 'secret', /token/i, 'creditCard'],
});
logger.info('Request', safe);Format-Preserving Masks
Keep delimiters and separators in place while masking alphanumeric characters:
import { maskFormatPreserving } from '@philiprehberger/mask-kit';
maskFormatPreserving('(555) 123-4567');
// "(***) ***-****"
maskFormatPreserving('(555) 123-4567', { revealLast: 4 });
// "(***) ***-4567"
maskFormatPreserving('4111-2222-3333-4444', { revealLast: 4, revealFirst: 4 });
// "4111-****-****-4444"Configurable Reveal Count
Control exactly how many characters remain visible:
import { revealFirst, revealLast } from '@philiprehberger/mask-kit';
revealFirst('secretdata', 3); // "sec*******"
revealLast('secretdata', 4); // "******data"
revealFirst('abc', 5); // "abc" (no masking if count >= length)
revealLast('token_xyz', 3, { char: '#' }); // "######xyz"Regex-Based Masking
Mask substrings that match a regular expression:
import { maskByPattern } from '@philiprehberger/mask-kit';
// Mask all digits
maskByPattern('Order #12345 shipped', /\d+/);
// "Order #***** shipped"
// Mask email-like patterns in free text
maskByPattern('Contact [email protected] for info', /[^\s@]+@[^\s@]+/);
// "Contact ************* for info"
// Mask SSN patterns
maskByPattern('SSN: 123-45-6789', /\d{3}-\d{2}-\d{4}/);
// "SSN: ***********"Deep Object Masking
Recursively traverse nested objects and arrays, masking values by key name:
import { maskDeep } from '@philiprehberger/mask-kit';
const input = {
user: {
name: 'Alice',
password: 'hunter2',
addresses: [
{ street: '123 Main St', ssn: '999-00-1234' },
],
},
apiKey: 'sk-abc123',
};
maskDeep(input, ['password', 'ssn', 'apiKey']);
// {
// user: {
// name: 'Alice',
// password: '*******',
// addresses: [
// { street: '123 Main St', ssn: '***********' },
// ],
// },
// apiKey: '********',
// }API
| Function | Description |
|----------|-------------|
| maskEmail(email) | Mask email preserving structure |
| maskCreditCard(card) | Show last 4 digits |
| maskPhone(phone) | Show last 3 digits |
| maskToken(token) | Show first/last 4 chars |
| maskIP(ip) | Mask middle octets |
| maskCustom(str, options?) | Custom masking with position control |
| maskFormatPreserving(value, options?) | Mask alphanumeric chars while keeping delimiters |
| revealFirst(value, count, options?) | Reveal first N characters, mask the rest |
| revealLast(value, count, options?) | Reveal last N characters, mask the rest |
| maskByPattern(value, regex, options?) | Mask substrings matching a regex pattern |
| maskObject(obj, { rules }) | Deep-walk and mask matching keys with auto-detection |
| maskDeep(obj, sensitiveKeys, options?) | Recursively mask values by key name |
| detectType(value) | Auto-detect value type |
Development
npm install
npm run build
npm testSupport
If you find this project useful:
