Skip to content
mentoor

Topic 5 of 5 · 5 min read

Testing pipelines for PII leaks

Goal: Write a test that seeds canary PII and fails when it appears in logs, traces or model outputs.

Test privacy the way you test anything else: push known fake PII (canaries) through the pipeline and fail the build if a canary shows up where it must not. A policy no test checks is only a hope.

Check yourself · 1 of 3

What is a canary in a PII leak test?

Choose one answer

Why it matters

Privacy controls break quietly. Someone adds a debug log, upgrades a tracing library, or changes a prompt builder, and raw emails start flowing into a store. Nothing crashes. Without a test, you find out from a user, an auditor or a breach notice. OWASP's guidance on sensitive information disclosure expects both input sanitization and checks on outputs, and a CI test is how you keep them true after the first release.

How to do it

  1. Invent canaries. Fake but realistic values that are easy to search for and clearly not real: canary.amira.7f3k@example.test, +000 555 0199, Zyx Quillfeather. Use a unique string per run so you can tell tests apart.
  2. Run a real request. Send a message containing the canaries through the actual app code, with the model call stubbed or pointed at a test model.
  3. Scan every sink. Capture application logs, exported trace spans, the outgoing provider request body, the vector store records and any error report. Search each for the canary strings.
  4. Assert by sink. The raw canary must not appear in the provider request or in logs. It may appear in the main database if that is intended. Write that down in the test.
  5. Check outputs both ways. Ask the model something that tempts it to echo or invent PII, and scan the answer for canaries from other users (cross-user leakage).
  6. Measure recall. Keep a small labeled set of messages with known PII. Report what share of entities your redaction catches per type, and fail CI if recall drops below your agreed bar.
import { test, expect } from "vitest";

const CANARY = "canary.amira.7f3k@example.test";

test("email never reaches provider request or logs", async () => {
  const { providerRequests, logLines, spans } = await runChatWithCapture({
    message: `Please reply to ${CANARY} about my refund`,
  });

  const sinks = JSON.stringify({ providerRequests, logLines, spans });
  expect(sinks).not.toContain(CANARY);
  expect(providerRequests[0].input).toContain("<EMAIL_ADDRESS_1>");
});

Before and after

Before: a developer adds logger.debug(prompt) while chasing a bug and ships it. No test notices. After: the canary test fails in CI with the sink name, and the line is removed before merge.

What goes wrong

  • Testing only the happy sink. The prompt is clean but the error handler logs the raw request.
  • Canaries that real detectors skip. An obviously fake pattern can be ignored, so make them look real.
  • One-time audits. Passing once proves nothing for the next release.
  • Recall without a threshold. A number that nobody acts on does not protect anyone.

Examples

A canary that failed a build

After a tracing upgrade, spans began to include request bodies. The canary test found the seeded email in the span export and failed the pull request with the name of the sink, so the fix was one config line.

Cross-user leak check

The test sends user A's canary, then asks user B 'what is my email?' in the same app. The answer must not contain A's canary. It caught a shared semantic cache that ignored the user id.

Key points

  • Use fake but realistic canary PII, unique per run, and push it through the real pipeline.
  • Scan every sink: logs, traces, provider request body, vector records and error reports.
  • Assert per sink what may and may not contain the raw value.
  • Add checks that answers do not echo canaries belonging to another user.
  • Run redaction recall on a labeled set in CI and fail the build below an agreed threshold.

Common mistakes

  • Scanning only the model prompt and ignoring logs and error reports.
  • Using obviously fake canaries that detectors skip, so the test passes falsely.
  • Running the privacy check once at launch and never again.
  • Reporting a recall number with no threshold or owner.

Check yourself · 2 of 3

Your canary test only inspects the prompt sent to the provider and passes. Why might raw PII still leak?

Choose one answer

Check yourself · 3 of 3

Redaction recall on your labeled set fell after a library upgrade. What should CI do?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic