DEV Community

Shilpi Daswani
Shilpi Daswani

Posted on Edited on

I kept finding credit card numbers in LLM prompts, so I wrote a redactor

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}`);
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 (15)

Collapse
 
kami_butt_69b922ade8de012 profile image
Agentic Affairs •

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?

Collapse
 
shilpi_daswani profile image
Shilpi Daswani •

Good question. restore only maps the exact placeholders it created during that redact call. They live in a lookup built at redaction time, token to original value, so it just swaps those specific strings back.

If the model invents a placeholder that was never issued, say it writes [CARD_2] when only [CARD_1] existed, there's no entry for it, so restore leaves it exactly as the model wrote it. It never makes up a value to fill an unknown token. Same reasoning as the altered-placeholder case: a guess there could drop a real value where it doesn't belong, so I'd rather leave the stray token sitting there in plain sight.

Collapse
 
kami_butt_69b922ade8de012 profile image
Agentic Affairs •

That's the right call — fail-visible beats fail-silent when the stakes are a real card number landing in the wrong slot. Leaving the stray token in plain sight also turns it into a signal: do you surface it to the operator as a warning, or just let it sit in the output for someone to spot?

Thread Thread
 
shilpi_daswani profile image
Shilpi Daswani •

Right now it just sits in the output. scrubly doesn't raise anything on its own, so today spotting it is on the operator.

But it's easy to turn into the signal you're describing, because the placeholders have a fixed shape like [CARD_1]. After restore you can scan the output for any token that wasn't in the set you issued, and treat a leftover as a warning or a flag on that record.

Honestly this is a good argument for restore to report that for you instead of leaving it to the caller: hand back the placeholders it couldn't resolve, so the warning is there for free rather than something you have to go looking for. I'm going to add that, since fail-visible only helps if someone actually sees it.

Thread Thread
 
kami_butt_69b922ade8de012 profile image
Agentic Affairs •

That's the right call — a leftover placeholder the caller never sees is a silent failure mode, and reporting it from restore costs nothing since you already know which tokens you issued. Will it come back as part of the restore return, or as a separate warning the caller has to opt into? The first keeps it fail-visible by default, which is the whole point.

Collapse
 
mujtbabbas profile image
Syed Mujtaba •

The restore step is the part I would watch most closely. Redacting the prompt is the right call, but once the real values are swapped back into the model output, that output is sensitive data again. If the summary gets stored in a database, written to a log, or shown in an admin panel, the leak has just moved downstream. In our booking platform, anything that touches payment details gets the same treatment on the way out as on the way in.

One approach that has worked for us is keeping the placeholders in anything persisted, and only restoring at display time for the person who already had access to the original. The redactor guards the prompt boundary well. The output boundary deserves the same attention.

Collapse
 
shilpi_daswani profile image
Shilpi Daswani •

You're right, and it's an important distinction. Once restore runs, that output is sensitive again, and scrubly doesn't follow it past that point. It guards the prompt boundary and hands the restored string back to you. What happens next, logging, storage, the admin panel, is on the caller, and I should say that far more loudly in the docs.

The pattern you described is the right model, and the pieces to do it are already there. redact returns a matches array with every placeholder and its original value, so you can persist the redacted text, keep that mapping in an access-controlled store, and restore lazily at display time for someone who already had rights to the original, instead of restoring eagerly. The restore function I ship is really just the convenience path for the simple request-response case.

A standalone "restore from a stored mapping" helper would fit your setup well, and I'll look at adding it. Thanks, this is a good flag.

Collapse
 
mujtbabbas profile image
Syed Mujtaba •

That is exactly the model I had in mind. Keeping the mapping in an access-controlled store and restoring lazily at display time keeps the sensitive string out of logs and storage, which is where most of the real risk sits. Calling out the caller responsibility in the docs is a good move too, since the convenience path makes it easy to restore eagerly without thinking about it. Looking forward to seeing the mapping helper if it lands, thanks for taking the flag seriously.

Collapse
 
userxnom profile image
Abdullah Naseer •

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?

Collapse
 
shilpi_daswani profile image
Shilpi Daswani •

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

Collapse
 
shilpi_daswani profile image
Shilpi Daswani • • Edited

Update for everyone in this thread: 0.3 is out, and it's basically built from your feedback.

  • Custom patterns with your own validator: register a { name, pattern, validate } and it runs alongside the built-ins, so you can add a regional format yourself in a few lines.

  • restoreWith plus a "Handling the output" section in the docs : keep placeholders in anything you persist and restore only at display time, since the output is sensitive data again once restored.

  • findPlaceholders : scan a restored output and it hands back any stray tokens the model invented or mangled, so fail-visible becomes an actual signal instead of something someone has to spot.

Thanks for the genuinely useful back-and-forth, this is the best part of putting something out. npm install scrubly@0.3 if you want to try it.

Collapse
 
zhang_jimmy_54a0414571400 profile image
zhang jimmy •

The restore discussion is what convinced me this is worth using, not just the redaction: consistent [IBAN_1] placeholders keep the model's reasoning intact, and the fact that restore only maps placeholders from that exact call is a sensible guard against invented ones. Validating card numbers and IBANs with their real checksums instead of a plain 16-digit regex is the other detail that matters — it is what keeps invoice IDs out of [CARD_1]. One suggestion: since you named custom regional formats as the gap, a small allow-list for user-defined patterns would help teams outside European fintech adopt it sooner. Which format would you add first after CNIC?

Collapse
 
shilpi_daswani profile image
Shilpi Daswani •

Thanks, and you've basically described what just shipped. 0.3 adds a customPatterns option where you register your own pattern with an optional validator, so you can add any regional format yourself in a few lines. CNIC works as a custom pattern today.

As for what I'd add as a built-in next, probably Indian Aadhaar and PAN, since they come up constantly and Aadhaar has a real checksum (Verhoeff) I can validate instead of just matching twelve digits, which is the whole point of the library. Brazilian CPF is on the list for the same reason. If there's one you need, the custom pattern covers it now, and it also helps me see what to prioritize as built-ins.

Collapse
 
syedfahads profile image
Syed Fahad •

does restore work on a streamed reply? a placeholder like [IBAN_1] split across two chunks seems like the easy one to miss

Collapse
 
shilpi_daswani profile image
Shilpi Daswani •

Good catch, this is a real limitation. restore operates on a complete string, so if you call it on each chunk as it streams, a placeholder split across a boundary, say [IBA in one chunk and N_1] in the next, won't match in either and passes through unrestored. Exactly the case you flagged.

The safe pattern today is to not restore per-chunk: buffer the full reply and call restore once at the end. That's correct, and it's usually what you want anyway since the final text is what you store or display.

For restoring while you stream, the fix is a small running buffer that holds back a trailing partial token. Because placeholders have a bounded shape like [LABEL_N], you only ever hold from the last unmatched [ to the end: emit everything before it, and once that [ either completes into a token (replace it) or turns out not to be one, release it. Flush whatever's held when the stream ends.

That stateful version is worth having built in, so I'm going to add a streaming restorer that handles the chunk boundaries for you. Thanks, this is a good one.