2026-11-27 18:26 UTC
DANGMUAAI & Developer Tools, Decoded
BackDev Tools

PII Redaction Passed. Your LLM Logs Still Had the Card

A canary harness read four sinks across eight scenarios: the provider payload came back clean 8 of 8 while logs and spans held the raw card 8 of 8.

DangMua EditorialNov 27, 20266 min read
PII Redaction Passed. Your LLM Logs Still Had the Card

You plant a card number in a prompt, send it, and read what the provider received. It comes back as [CARD], so you ship. The redaction test you just ran covered one of at least four places that prompt gets written down — and two developer write-ups published November 27 show what the other sinks were holding the whole time.

The provider payload is the sink least likely to leak

A .NET developer built a canary harness that plants fake card numbers, emails, IBANs and API keys across eight scenarios, then reads four sinks: the provider payload, ILogger output, OpenTelemetry spans, and the error body. Six pipeline variants went through it. One came back clean everywhere.

The most instructive failure is the one that passes the usual test. In the variant with the redactor sitting right next to the provider, the provider payload was clean in 8 of 8 scenarios while the logs and spans still held the raw card in 8 of 8. Nothing in the redactor was broken. The logging and telemetry clients simply saw the request before it was masked.

All figures here come from that author's local, offline run with a fake provider, seed 1. They also sell a paid course and a commercial masking product built on this work, which is worth knowing while reading their numbers — though the failure modes below are structural, not vendor-specific.

Middleware order is the whole bug

ChatClientBuilder applies middleware in reverse: the first thing you add becomes the outermost client. So the intuitive move — add redaction last, right before the provider — puts it inside logging and OpenTelemetry. The ordering that came out clean inverts it:

// First added = outermost. Redaction must wrap everything that records the request.
var client = new ChatClientBuilder(provider)
    .Use(inner => new RedactingChatClient(inner, options))  // 1. mask first
    .UseOpenTelemetry(loggerFactory, sourceName)            // 2. spans see masked text
    .UseLogging(loggerFactory)                              // 3. logs see masked text
    .Build();

This snippet is illustrative of the ordering rule, not a drop-in implementation. The point transfers to any stack where redaction is middleware: if an observability layer wraps your masking layer, your traces are the leak.

Your canary may be one your redactor is right to ignore

A competent redactor does not mask every 16-digit number, or it would eat order IDs. It finds candidates by pattern and then validates them — Luhn for cards, mod-97 for IBANs. Plant 1234567812345678 in your test and a correct redactor leaves it alone, so the test tells you nothing at all.

The harness generates canaries that pass the same validation production code runs, from a seed so a failing run replays exactly. Its scanner also looks for reformatted shapes of each canary: grouped (4111 1111 …), dashed, percent-encoded, Base64 and upper-case. Values get reshaped on the way into a log, and an exact-string search misses every one of those.

Tool results are not an edge case

One variant had the redactor in the right place but masked only plain text. Seven of eight scenarios came back clean. The eighth was a tool call returning a customer record, and the email and card inside the FunctionResultContent reached the provider, the logs and the spans.

In an agent loop that is the normal path, not an exception: a lookup tool returns a customer, and that record goes back into the model's context on the next turn. Masking has to cover tool results and tool-call arguments, not just message text.

The line no middleware can mask

The last failing variant had a correct pipeline and one extra line in the endpoint:

logger.LogInformation("Chat request: {Text}", text);  // the leak

Provider payload, spans and error body: clean. Application logs: the card in 8 of 8 scenarios. Middleware cannot mask a line it never sees, so the fix belongs in the handler — log a length or a request ID instead. A wire-only check will never find this one.

The error path is the same family. The author's fake provider rejects one request with an error that echoes the input — explicitly a simulation of providers that quote offending text in a 4xx, not a claim about any named vendor. Without redaction, that message landed in the logs through LogError and in the error body. When the provider only ever sees masked text, even its echo comes back masked.

Prove the test could have failed

A clean matrix means nothing if the test is incapable of going red. Two cheap checks make it meaningful. Mutate the pipeline: the author switched the tool-result guard off and 12 tests failed, then restored it and went green. And assert that every sink actually recorded characters in every scenario, so that "0 leaks" can never quietly mean "0 bytes captured."

The scope statement is worth copying too. A clean run proves only that these planted values, in these shapes, did not reach these sinks through this pipeline. Regex detectors will not find names or street addresses, images and files pass through untouched, and the model's reply plus streamed chunks are a separate problem.

The same move, one layer up: citation gates

A second write-up the same day applies the identical pattern to retrieval. A RAG answer that reads "Hardware is covered for 24 months [d2#0]" looks trustworthy, but the model writes that citation the same way it writes everything else. It can cite a chunk you never sent it, or skip citing altogether, and the prose still reads fine.

The fix is a gate in code rather than a better prompt: every answer must cite at least one chunk, and every cited ID must belong to a chunk that was actually retrieved. Answers failing the check never reach the user. Chunk IDs are kept short and regex-friendly — d2#0 is document 2, sentence 0 — precisely so the gate can match them.

The build runs locally on free parts: .NET 10, Ollama 0.40.18, llama3.2:3b (a 2.0 GB download) and nomic-embed-text (274 MB), with Microsoft.Extensions.AI for the IChatClient and IEmbeddingGenerator abstractions. One detail that bites people switching models: nomic-embed-text requires a task prefix on every input — search_document: for indexed text, search_query: for the question. Other embedding models do not use them.

What to run this week

  • List every sink a prompt reaches in your stack — provider, application logs, traces, error bodies, crash reporters — then test all of them, not the wire alone.
  • Check your middleware order. If an observability wrapper sits outside your masking layer, fix that before anything else.
  • Regenerate your canaries so they pass real validation, and scan for encoded and reformatted variants.
  • Grep your own handlers for raw prompt text in log calls. That is the leak no library can close for you.
  • Turn one guard off and confirm the suite goes red. A test that has never failed is not evidence.

Worth watching: both of these are gates that reject output rather than prompts that request good behavior. As agent loops get longer, the checks that hold are the ones written in code that can fail a build — not the instructions written in English that a model can quietly route around.

More from DangMua