If you've shipped any AI feature in the last year, you've probably written a line
that looks about like this:
const summary = await llm.complete(`Summarize this chat: ${conversation}`);
It works, you move on. But conversation is whatever the user typed, and in a
fintech product that occasionally means a card number, or an IBAN, or someone's
email sitting right there in the string. Which you've now sent off to a model
provider, where it gets logged on a server you don't control.
Nothing breaks when this happens. The data just flows through and the feature
does its job, so it's the kind of thing that's really easy to ship without
thinking about. That's what bugged me about it.
So I made a small thing to sit in front of that call. It's on npm as
scrubly.
The whole idea is: find the sensitive bits before the prompt leaves, replace them
with placeholders, and then swap the real values back into whatever the model
sends you.
import { redact } from 'scrubly';
const { text, restore } = redact(
'Charge €40 to IBAN DE89 3704 0044 0532 0130 00, receipt to alice@example.com',
);
text;
// 'Charge €40 to IBAN [IBAN_1], receipt to [EMAIL_1]'
const reply = await llm.complete(`Rewrite this as a formal note: ${text}`);
restore(reply);
// the real IBAN and email are back in the model's answer
The placeholders stay consistent inside a single call, so if the same IBAN turns
up three times it's [IBAN_1] every time. The model still understands it's one
value, you've just never actually handed it over.
The part I didn't expect to care about: keys
After I shipped the first version, the thing people kept bringing up wasn't card
numbers. It was secrets. And they're right — think about how often an API key
ends up somewhere it shouldn't. A .env line pasted into a chat to debug it. A
stack trace dropped into an AI tool. A config snippet in a public gist or a
Reddit thread. A leaked key sitting in the open is often all it takes for someone
to walk straight in.
The same slip happens inside your own app the moment a key rides along in a
prompt. So scrubly now catches those too — npm tokens, GitHub tokens, Stripe,
Google, Slack, AWS and OpenAI keys, JWTs, and whole PEM private-key blocks:
// an npm token someone pasted in to "just debug this real quick"
redact('npm config set //registry.npmjs.org/:_authToken npm_xxxxxxxx...').text;
// 'npm config set //registry.npmjs.org/:_authToken [NPM_TOKEN_1]'
// or just blank it out entirely, no way back:
redact(leak, { token: () => 'XXX' }).text;
Why not just a big regex
Because a big regex matches your order numbers too. A plain "16 digits" rule will
cheerfully redact an invoice ID and leave your output full of [CARD_1] where
there was never a card. So instead of only matching a shape, scrubly runs the
same check a card number or an IBAN has to pass to be valid in the first place,
and matches each key type by the exact prefix its provider actually issues
(npm_, ghp_, sk_live_, and so on). If something doesn't check out, it's
left alone.
What this is, and what it isn't
It's a good first layer, not a safety net, and I'd rather say that plainly than
oversell it. It won't catch a name, or a card number someone typed with words in
the middle, or a key format I haven't added yet. And it lives in your code, right
before the model call — so it guards that path, not your clipboard when you paste
something into Reddit. If you're handling regulated data this doesn't replace
your actual compliance work. It just means the obvious stuff stops leaking into
prompts by accident.
npm install scrubly
Code's here: https://github.com/shilpidaswani/scrubly
If there's a format you'd want it to catch, I'd genuinely like to know. I built
the detectors around what I see in European fintech, so my blind spots are
probably non-European account formats and regional ID numbers. Open an issue or
just tell me in the comments.
Top comments (3)
The 'big regex matches your order numbers too' point is the one that kills most DIY redactors — validating the number the way the card network does is what separates a filter from a false-positive machine. The restore step is the elegant part: the model still reasons about one consistent value without ever seeing it. Do you handle the case where the model invents a new placeholder in its reply, or does restore only map values it already saw?
This is the right kind of boring security work — nothing breaks when it fails, so nobody notices until the logs already exist somewhere you do not control. I really liked the honesty in the limitations section, and the decision to validate with the actual check math instead of just matching 16 digits. That is the difference between a redactor and a false-positive machine that turns every order ID into [CARD_1].
The consistent placeholders are clever, too, because the model still gets the structure — same IBAN, same token, three times — without ever seeing the value. One thing I would love to hear more about is the restore step: what happens when the model reorders, drops, or slightly alters a placeholder in its reply? Do you fail closed and leave the placeholder in, or is there a fallback for a partial match?
On formats you asked about: Pakistani IBANs start with PK, and the CNIC pattern (35202-1234567-1 style) shows up a lot in fintech chat here in South Asia. Have you thought about letting users register a custom pattern with their own validator, so regional formats do not have to wait for you to add them one by one?
Thanks, that's how I think about it too.
On restore: it's a plain string swap, so if the model moves the placeholders around or repeats them, it still works. If it drops one, that value just doesn't show up in the output. And if it changes one, say adds spaces or lowercases it, I leave the placeholder as is and don't put the real value back. I kept fuzzy matching out on purpose, because a wrong guess could stick a secret back in the wrong place.
On Pakistani IBANs, those already work. The check is the mod-97 checksum rather than a country list, so any valid IBAN passes, PK included. I tested one and it got caught.
CNIC is the real gap though, and you've basically described what I want to do next: let people add their own pattern and validator so regional formats don't have to wait on me. It's going in the next version and I'll reply here when it's out