Secrets
react-smart-copy/secrets keeps secrets from travelling through the clipboard. It finds API
keys, tokens, private keys, passwords, card numbers and IBANs in text people paste or copy,
and redacts or blocks them: in a chat box, a support ticket, or a log someone copies from
your app. No dependencies, about 4.1 kB (about 3.3 kB for scanSecrets alone), and it works
the same on a server.
Try it
Section titled “Try it”Scan text
Nothing scanned yet.
Found
Redacted
Paste into a chat box
Paste something with a secret in it.
Copy from a log
Select any lines and press Ctrl + C or ⌘ + C.
Nothing copied yet.
Keys don't belong in a chat
scanSecrets knows the key formats of a dozen services, private keys, passwords in URLs and
connection strings, and card numbers and IBANs by their checksums. Every pattern runs in linear time, and a
finding never holds the secret: only where it is, its kind and a masked preview.
pasteWithoutSecrets redacts or blocks a paste with undo intact, and secretRedactor
masks secrets in what people copy from your page.
import { pasteWithoutSecrets, secretRedactor } from 'react-smart-copy/secrets';
<textarea onPaste={(event) => pasteWithoutSecrets(event, {
onSecrets: (found) => toast(`Removed a ${found[0].label}.`),
})} />
useCopyInterceptor(logRef, { transform: secretRedactor() });
Use it
Section titled “Use it”import { pasteWithoutSecrets } from "react-smart-copy/secrets";
<textarea onPaste={(event) => pasteWithoutSecrets(event, { // "redact" (the default) inserts the paste with each secret replaced; "block" inserts nothing. action: "redact", onSecrets: (found) => toast(`Removed a ${found[0].label} from your message.`), }) }/>What it finds. Each finding has a kind, a confidence, and a masked preview.
- Service keys and tokens, by their documented formats: AWS access key IDs, GitHub (classic and fine-grained), GitLab, Slack tokens and webhooks, Stripe live keys, Google API keys, npm, PyPI, SendGrid, Twilio, Azure storage keys, and JSON Web Tokens (whose header must decode).
- Private keys (PEM and OpenSSH), from their
BEGINline to theirENDline. - Passwords inside other text: in URLs (
postgres://app:password@db) and in connection strings (;Password=...). - Card numbers and IBANs, by checksum: Luhn plus the card brand’s ranges and lengths, and mod-97 plus the country’s IBAN length.
- Random-looking values assigned to secret names:
API_KEY="...","password": "...",client_secret: ..., including an AWS secret key next to its name. Placeholders (changeme,<token>,${TOKEN}) and references (process.env.TOKEN) are left alone.
Safe to run on anything. Every pattern runs in linear time: no nested quantifiers, no
lookbehind. Hostile text of hundreds of kilobytes is scanned in milliseconds. A finding never
holds the secret: it holds where the secret is (start, end), its kind and label, a
confidence, and masked. masked keeps at most a known prefix and the last 4 characters
(ghp_ and the last 4, or •••• 4242 for a card), and nothing at all for passwords. Text
past maxLength (1,000,000 characters) is not scanned, and redactSecrets drops it rather
than pass it on unchecked.
import { useCopyInterceptor } from "react-smart-copy";import { redactSecrets, scanSecrets, secretRedactor } from "react-smart-copy/secrets";
const { findings } = scanSecrets(text, { minConfidence: "high" });// [{ kind: "github-token", label: "GitHub token", confidence: "high", start: 13, end: 53, masked: "ghp_…a1B2" }]
redactSecrets("token=ghp_…").text; // "token=[redacted GitHub token]"
// Mask secrets in what people copy from a log viewer or an admin console.const { ref: logRef } = useCopyInterceptor<HTMLPreElement>({ transform: secretRedactor() });<pre ref={logRef}>{logLines}</pre>| Option | Default | Notes |
|---|---|---|
kinds |
every kind | the kinds to look for |
minConfidence |
"medium" |
"high" leaves out values that only look like secrets |
maxLength |
1,000,000 | characters scanned |
replace |
[redacted <label>] |
redactSecrets, pasteWithoutSecrets and secretRedactor |
action |
"redact" |
pasteWithoutSecrets: "block" inserts nothing |
onSecrets |
pasteWithoutSecrets: called with the findings and what was done |
A paste is redacted in a <textarea> or text <input>, with undo intact; anywhere else, a
paste with secrets is blocked. Scanning in the browser protects the people using your app.
Scan again on your server before you store or forward text, because scanSecrets works the
same there.