Redact emails, phone numbers, API keys, credit cards and other sensitive data from text and logs. Zero dependencies.
Redact emails, phone numbers, API keys, credit cards and other sensitive data from text and logs — before they leave your app.
Zero dependencies. Works in Node.js 16+ and in browsers via bundlers. TypeScript types included.
const { redact } = require('redact-sensitive');
redact('Contact me at ana@example.com or 555-123-4567');
// 'Contact me at [EMAIL] or [PHONE]'
redact('Card 4111 1111 1111 1111, api_key = abc123secret');
// 'Card [CARD], api_key = [API_KEY]'
Try it in your browser — no install needed: the interactive playground.
Logs, error reports and support tickets routinely leak the very data they were meant to debug. One console.log(user) can put an email, a token or a card number into a log store, a crash report or a shared screenshot. redact-sensitive is a one-line safety net: run untrusted text through it before logging, storing or sending it anywhere.
npm install redact-sensitive
redact(text, options?)const { redact, redactObject, findSensitive } = require('redact-sensitive');
redact('Email ana@example.com, IP 203.0.113.42');
// 'Email [EMAIL], IP [IP]'
// Redact only emails
redact('a@b.com 555-123-4567', { only: ['email'] });
// '[EMAIL] 555-123-4567'
// Use one replacement for everything
redact('a@b.com', { keepLabels: false, replacement: '***' });
// '***'
// Override the label of one pattern type only
redact('a@b.com 555-123-4567', { labels: { email: '[E-MAIL]', phone: '[TEL]' } });
// '[E-MAIL] [TEL]'
// `keepLabels: false` still wins over per-type labels. Labels are keyed by
// pattern name (`email`, `phone`, `urlQuerySecret`, `authHeader`, ...).
// They also apply to values redacted because of their object key name,
// matched by the key name: redactObject({ password: 'x' }, { labels: { password: '[PWD]' } })
// Add your own pattern
redact('order #12345', {
custom: [{ pattern: '#\\d+', replacement: '[ORDER]' }],
});
redactObject(value, options?)Recursively redacts strings inside objects and arrays. Useful for sanitising a whole log payload:
redactObject({ user: { email: 'ana@example.com' }, tags: ['x@y.com'] });
// { user: { email: '[EMAIL]' }, tags: ['[EMAIL]'] }
// Arrays of log records are walked too, including nested arrays:
redactObject({ records: [
{ email: 'ana@example.com', token: '<token-1>' },
{ email: 'bob@example.com', token: '<token-2>' },
] });
// { records: [
// { email: '[EMAIL]', token: '[TOKEN]' },
// { email: '[EMAIL]', token: '[TOKEN]' },
// ] }
// Values stored under a sensitive key name are replaced outright,
// whatever their type — even when they look like plain text:
redactObject({ password: 'hunter2', apiKey: 'abc123secret', user: 'ana' });
// { password: '[PASSWORD]', apiKey: '[API_KEY]', user: 'ana' }
// Add your own sensitive key names with `sensitiveKeys`:
redactObject({ myPhrase: 'shh' }, { sensitiveKeys: ['myPhrase'] });
// { myPhrase: '[REDACTED]' }
only and except must contain names from the supported-pattern table below.
Unknown names throw a RangeError listing the valid names, even for empty or non-string input.
For example, { only: ['emial'] } throws instead of silently leaving email addresses visible.
labels keys are validated too: pattern names and normalized sensitive key names such as password are accepted.
This validation applies to both redact and redactObject.
Date, RegExp and other non-plain objects pass through unchanged — only plain objects, arrays and strings are rebuilt.
findSensitive(text, options?)Returns what would be redacted, without changing the text. Results are sorted by position in the text, each with its start index:
findSensitive('mail a@b.com, card 4111111111111111');
// [
// { type: 'email', value: 'a@b.com', index: 5 },
// { type: 'creditCard', value: '4111111111111111', index: 19 },
// ]
// Only look for one type, like redact():
findSensitive('mail a@b.com, card 4111111111111111', { only: ['email'] });
// [{ type: 'email', value: 'a@b.com', index: 5 }]
Each result is exactly the part redact() replaces, so text that redact() keeps readable is not reported. A secret matched by two patterns is reported once, as the more specific type:
findSensitive('Cookie: session=abc123; theme=dark');
// [{ type: 'sessionCookie', value: 'abc123', index: 16 }]
findSensitive('mongodb://user:abc123@localhost:27017/db');
// [{ type: 'connectionString', value: 'abc123', index: 15 }]
Custom patterns are reported as type 'custom' with the whole match, unless the entry names the secret part: spanGroup (a capture group number) or spanFor(match, groups) (returns [start, end] offsets into the match, one pair per secret). Custom matches are always reported, even where a built-in type found the same text.
findSensitive('order=12345', { custom: [{ pattern: 'order=(\\d+)', spanGroup: 1 }] });
// [{ type: 'custom', value: '12345', index: 6 }]
Redact a log file without writing any code:
npx redact-sensitive app.log > app.redacted.log
Without a file (or with -), the CLI reads stdin, so it works in a pipeline:
kubectl logs my-pod | npx redact-sensitive
--only and --except take comma-separated pattern names, the same names
redact() accepts in options.only / options.except (see the table below):
npx redact-sensitive --only email,ipv4 app.log
npx redact-sensitive --except phone app.log
Run npx redact-sensitive --help for the usage text and the full list of names.
0 on success. 1 if the file cannot be read or the input is
not valid UTF-8. 2 for a usage error: an unknown option, an unknown pattern
namgood first issue: Redact Doppler API tokens (dp.pt. / dp.st. / dp.sa.)
https://github.com/sharkohark1234/redact-sensitive/issues/151
Labels: enhancement, good first issue
good first issue: Redact PagerDuty API tokens
https://github.com/sharkohark1234/redact-sensitive/issues/145
Labels: enhancement, good first issue
good first issue: Redact Databricks personal access tokens (dapi…)
https://github.com/sharkohark1234/redact-sensitive/issues/144
Labels: enhancement, good first issue
good first issue: Redact Contentful personal access tokens (CFPAT-...)
https://github.com/sharkohark1234/redact-sensitive/issues/143
Labels: enhancement, good first issue
good first issue: Redact HubSpot private app access tokens
https://github.com/sharkohark1234/redact-sensitive/issues/130
Labels: enhancement, good first issue
Add redaction for Cloudflare API tokens and Global API Key
https://github.com/sharkohark1234/redact-sensitive/issues/118
Labels: enhancement, good first issue
Thank you for contributing. This project welcomes first-time contributors.
main (feat/<short-name> or fix/<short-name>).test/ first where possible, then implement the change.npm test — all tests must pass.New patterns are the most welcome contribution. A good pattern PR includes:
src/index.js, with a clear name and label.test/redact.test.js with a real example and a false-positive example (text that must not be redacted).Please do not put real secrets in tests or examples. Use the standard test values (for example 4111 1111 1111 1111 for cards, AKIAIOSFODNN7EXAMPLE for AWS keys).
Be kind and professional. Reviewers will be too. Disagreement about a pattern is normal — bring a test case and we will work it out.