AI Engineering: Agents and Applications › Production Retrieval and Pipelines › Day 328
Hands-on lab — Day 328: Privacy in AI Systems
- ← Back to the Day 328 lesson
- Open the hands-on files on GitHub — clone or download them from the public labs repository
- Local path in your clone:
labs/sections/ai-engineering/day-328-privacy-in-ai-systems/
Commands
Setup
pip install -r requirements/requirements.txt Run
python examples/privacy_demo.py Test
bash tests/run_tests.sh File tree
examples/privacy_demo.py examples/privacy.py expected-output/examples-run.txt expected-output/FIELDS.md expected-output/measured-values.txt expected-output/run-output.txt expected-output/starter-run.txt expected-output/test-output.txt expected-output/test-run.txt metadata.yml README.md requirements/requirements.txt security.md starter/privacy.py tests/run_tests.sh tests/test_privacy.py troubleshooting.md
Lab README
Lab — Day 328: Privacy in AI Systems
Lesson
- Lesson title: Privacy in AI Systems
- Day number: 328 of 365
- Lesson article: https://ai-roadmap-365.github.io/day-328-privacy-in-ai-systems
- Lab files: everything you need is in this directory — follow “How to run” below.
- Browse the course locally: from the repository root, this lab also appears in the course website at
/labs/day-328-privacy-in-ai-systemswhen the site is running.
Purpose
Build the three controls that keep personal data manageable in a retrieval system: redaction at the boundary, a data-flow record, and erasure that is verified rather than assumed.
Everything runs offline against synthetic identifiers that are invalid by construction — example.com is a reserved domain, 192.0.2.0/24 is the reserved TEST-NET-1 range, and the card number fails the Luhn check.
This lab is defensive throughout: the detectors exist to find personal data so it can be removed, and the audit exists to prove a deletion landed.
Learning objectives
- Detect and pseudonymise identifiers before data fans out into derived stores.
- Order detectors so a shorter pattern cannot partially consume a longer identifier.
- Produce stable, salted, non-reversible pseudonyms.
- Track which stores hold data for which subject.
- Verify an erasure by reading back, and report the stores that still hold the subject.
Prerequisites
- Day 327, "Cost Engineering for AI Systems".
- Day 325, "Keeping Indexes Fresh" — deletion propagation is the same problem seen from a different angle.
- Comfortable with Python regular expressions, dataclasses and sets.
Supported operating systems
macOS, Linux and Windows (via WSL or PowerShell). Nothing is platform-specific.
Hardware requirements
Any machine running Python 3.10 or newer. No GPU. The suite completes in hundredths of a second.
Required software
- Python 3.10 or newer.
pytest7 or newer.
Free and open-source options
Every dependency is free and open source: Python is PSF-licensed, pytest is MIT-licensed. No account, no API key, no hosted service — nothing leaves the machine, which is the correct property for a lab about personal data. The lesson discusses Microsoft Presidio (MIT), spaCy (MIT) and scrubadub (Apache-2.0) as the real-world options.
Installation
python3 -m venv .venv
.venv/bin/pip install -r requirements/requirements.txt
Removing .venv fully reverses the installation.
File structure
starter/privacy.py your work: pseudonym, redact, erase, minimise
examples/privacy.py reference implementation
examples/privacy_demo.py redacts a ticket, tracks flow, verifies erasure
tests/test_privacy.py grouped by control
tests/run_tests.sh suite entry point
expected-output/ real captured output and measured values
requirements/ pinned dependency
How to run
python3 examples/privacy_demo.py # redact, track, erase
bash tests/run_tests.sh # run the suite
To work on the exercise, edit starter/privacy.py, then copy it over the reference:
cp starter/privacy.py examples/privacy.py
bash tests/run_tests.sh
Keep a copy of the reference first if you want to compare.
What the commands do
python3 examples/privacy_demo.py redacts a synthetic support ticket containing five categories of identifier, shows that pseudonyms are stable across records, records a subject into four stores, then runs an erasure twice — first against the two stores someone would remember, then against all of them.
bash tests/run_tests.sh runs pytest over nineteen tests grouped by control: detection, pseudonyms, data flow, erasure and minimisation.
Expected output
--- redacted ---
From: [email:c4e2790ada]
Phone: [phone:749f96d705]
Card ending [card:1442489716] was charged twice.
Reported from [ip:555fff71c9]. Reference SSN [national_id:8975e0fdf2].
findings: card=1 email=1 ip=1 national_id=1 phone=1
pseudonym stable across records: True
--- data flow ---
subject-42 present in: ['audit_log', 'database', 'response_cache', 'vector_index']
--- erasure, first attempt (the stores someone remembered) ---
INCOMPLETE deleted_from=2 still_present=audit_log,response_cache
--- erasure, complete ---
COMPLETE deleted_from=2 still_present=none
--- minimisation ---
kept: {'tier': 'pro'}
The INCOMPLETE line is the reason the lab exists. expected-output/FIELDS.md explains each part.
Validation steps
bash tests/run_tests.shreports19 passed.- Confirm no fragment of any original identifier survives the redaction — including the leading
+on the phone number, which a naive word-boundary pattern leaves behind. - Run an erasure against a subset of stores and confirm it reports
INCOMPLETEand names the stores still holding the subject. If it reports success, you are reporting from the delete calls rather than a read-back.
Tests
Nineteen tests in five groups:
- detection — every category is found, nothing of the original survives, the leading
+is consumed, clean text is untouched, and a card is not partly eaten by the phone detector. - pseudonyms — stable for the same value, different for different values, salted, and containing nothing of the original.
- data flow — records every store, is per subject, and does not duplicate.
- erasure — a partial run reports
INCOMPLETEand names what remains; a full run is verified; an unknown subject is harmless. - minimisation — keeps only named fields, tolerates absent ones, and can keep nothing.
Cleanup
rm -rf __pycache__ .pytest_cache
rm -rf .venv
Both are safe to remove and fully reversible.
Troubleshooting
See troubleshooting.md for each failure this lab is designed to produce, including the hexadecimal false alarm that can make a correct redactor look broken.
Security notes
See security.md. In short: nothing leaves the machine, every fixture identifier is invalid by construction, and the salt is treated as the secret it is.
Extension exercises
- Retention policy. Give each store a maximum age and report subjects held past it — and report a store with no declared policy as a finding rather than treating it as unlimited.
- Re-identification risk. Score quasi-identifier combinations and flag records that are unique or near-unique, demonstrating Sweeney's result on your own fixture.
- Redaction recall. Add free-text names, which patterns cannot catch, and measure what fraction of identifiers the detectors miss. The gap is the argument for entity recognition.
Navigation
- Lesson
- Previous: Day 327 — Cost Engineering for AI Systems
- Next: Day 329 — Section Project: A Production Assistant
Expected output
FIELDS.md
# Reading the privacy output
## The redaction block
From: [email:c4e2790ada]
findings: card=1 email=1 ip=1 national_id=1 phone=1
Each token is `[category:digest]`, where the digest is the first ten hex characters of a salted SHA-256 of the original value.
| Property | Why it matters |
| --- | --- |
| **Stable** | The same value always yields the same token, so one person stays followable across records for debugging without their identity being visible. |
| **Salted** | Without a salt, a hash of a short value is reversed by enumeration — there are only so many phone numbers. |
| **Non-reversible** | The token is not the identifier wearing a hat. |
`pseudonym stable across records: True` confirms the first property across two separate redaction calls.
## The data flow
subject-42 present in: ['audit_log', 'database', 'response_cache', 'vector_index']
Four stores hold data for one subject. This list is what an erasure runs against — otherwise it runs against whatever someone remembers.
## The two erasure runs
INCOMPLETE deleted_from=2 still_present=audit_log,response_cache
COMPLETE deleted_from=2 still_present=none
| Field | Meaning |
| --- | --- |
| status | `COMPLETE` only when a read-back finds the subject in no store. |
| `deleted_from` | Stores that actually held the subject and no longer do. |
| `still_present` | Stores that hold them after the deletes ran. The whole point of the control. |
The first run is the realistic failure: someone deletes from the database and the vector index — the two stores that come to mind — and misses the cache and the audit log. Reporting from the delete calls alone would have called that a success.
## Minimisation
kept: {'tier': 'pro'}
Three fields collected, one retained. Data never kept cannot leak and does not need deleting.
examples-run.txt
--- redacted ---
From: [email:c4e2790ada]
Phone: [phone:749f96d705]
Card ending [card:1442489716] was charged twice.
Reported from [ip:555fff71c9]. Reference SSN [national_id:8975e0fdf2].
findings: card=1 email=1 ip=1 national_id=1 phone=1
pseudonym stable across records: True
--- data flow ---
subject-42 present in: ['audit_log', 'database', 'response_cache', 'vector_index']
--- erasure, first attempt (the stores someone remembered) ---
INCOMPLETE deleted_from=2 still_present=audit_log,response_cache
--- erasure, complete ---
COMPLETE deleted_from=2 still_present=none
--- minimisation ---
kept: {'tier': 'pro'}
measured-values.txt
Measured on macOS 26.5.1 (Apple Silicon), Python 3.14.0, pytest 9.1.1.
python3 examples/privacy_demo.py -> 14 lines, exit 0
bash tests/run_tests.sh -> 19 passed in 0.02s
Every identifier in the fixtures is synthetic and invalid by construction:
example.com is a reserved domain (RFC 2606), 192.0.2.0/24 is the reserved
TEST-NET-1 range (RFC 5737), and the card number fails the Luhn check.
The line worth reading twice:
INCOMPLETE deleted_from=2 still_present=audit_log,response_cache
Two deletes succeeded and the erasure was NOT fulfilled. Without the read-back
this run reports two successful deletions and looks complete.
A note from writing the tests rather than the code: an assertion that a token
does not contain "ada" FAILED against a correct redactor, because the SHA-256
digest happened to end in "ada" -- which is valid hexadecimal. A privacy test
that raises false alarms gets muted, and a muted test protects nothing. Assert
on the whole identifier and on fragments containing non-hex characters.
run-output.txt
--- redacted ---
From: [email:c4e2790ada]
Phone: [phone:749f96d705]
Card ending [card:1442489716] was charged twice.
Reported from [ip:555fff71c9]. Reference SSN [national_id:8975e0fdf2].
findings: card=1 email=1 ip=1 national_id=1 phone=1
pseudonym stable across records: True
--- data flow ---
subject-42 present in: ['audit_log', 'database', 'response_cache', 'vector_index']
--- erasure, first attempt (the stores someone remembered) ---
INCOMPLETE deleted_from=2 still_present=audit_log,response_cache
--- erasure, complete ---
COMPLETE deleted_from=2 still_present=none
--- minimisation ---
kept: {'tier': 'pro'}
starter-run.txt
An untouched starter fails most of the suite:
16 failed, 3 passed in 0.05s
The three that pass exercise DataFlow, which is not stubbed:
test_flow_records_every_store_a_subject_reached
test_flow_is_per_subject
test_recording_twice_does_not_duplicate
Everything else depends on pseudonym, redact, erase or minimise.
test-output.txt
................... [100%]
19 passed in 0.01s
test-run.txt
................... [100%]
19 passed in 0.01s
Source files
examples/privacy_demo.py (1680 bytes)
#!/usr/bin/env python3
"""Redact a support ticket, track where it flows, then verify an erasure."""
from __future__ import annotations
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from privacy import Category, DataFlow, erase, minimise, redact
# Synthetic and invalid by construction: example.com is reserved, and the card
# number fails the Luhn check.
TICKET = (
"From: ada@example.com\n"
"Phone: +44 7700 900123\n"
"Card ending 4111 1111 1111 1111 was charged twice.\n"
"Reported from 192.0.2.44. Reference SSN 123-45-6789."
)
def main() -> int:
result = redact(TICKET)
print("--- redacted ---")
print(result.text)
print(f"findings: {result.summary()}")
stable = redact("contact ada@example.com again")
same = result.findings[0].token == stable.findings[0].token
print(f"pseudonym stable across records: {same}")
print("--- data flow ---")
flow = DataFlow()
for store in ("database", "vector_index", "response_cache", "audit_log"):
flow.record(store, "subject-42")
print(f"subject-42 present in: {sorted(flow.where('subject-42'))}")
print("--- erasure, first attempt (the stores someone remembered) ---")
partial = erase(flow, "subject-42", stores=["database", "vector_index"])
print(partial.summary())
print("--- erasure, complete ---")
full = erase(flow, "subject-42")
print(full.summary())
print("--- minimisation ---")
record = {"name": "Ada", "email": "ada@example.com", "tier": "pro", "note": "vip"}
print(f"kept: {minimise(record, {'tier'})}")
return 0
if __name__ == "__main__":
raise SystemExit(main())
examples/privacy.py (6923 bytes)
"""Redaction, data-flow tracking and deletion verification for an AI pipeline.
Offline and standard-library only. Everything here is defensive: the detectors
exist to find personal data so it can be removed before it spreads, and the
audit exists to prove a deletion actually reached every store.
Three ideas, in the order they matter:
redact remove personal data at the boundary, before it multiplies
track know every store a piece of data reached
verify prove a deletion landed everywhere, rather than assuming
The third is the one that fails silently. A record deleted from the database
but left in the vector index, the cache and yesterday's logs has not been
deleted -- and nothing in the system reports a problem.
The synthetic identifiers in the fixtures are invalid by construction: the card
numbers fail the Luhn check and the emails use the reserved example.com domain.
"""
from __future__ import annotations
import hashlib
import re
from dataclasses import dataclass, field
from enum import Enum
class Category(str, Enum):
EMAIL = "email"
PHONE = "phone"
CARD = "card"
NATIONAL_ID = "national_id"
IP = "ip"
# Detectors are deliberately conservative in shape and generous in matching.
# Under-detection leaks; over-detection costs a little utility. When the two
# trade off, privacy work should prefer the recoverable mistake.
PATTERNS: dict[Category, re.Pattern[str]] = {
Category.EMAIL: re.compile(r"\b[\w.+-]+@[\w-]+\.[\w.-]+\b"),
# No leading \b: a word boundary cannot match before "+", so the country
# code would survive redaction as a bare "+". A negative lookbehind on
# word characters does what \b was meant to do here.
Category.PHONE: re.compile(r"(?<![\w+])\+?(?:\d[ -]?){9,13}\d\b"),
Category.CARD: re.compile(r"(?<![\w+])(?:\d[ -]?){12,15}\d\b"),
Category.NATIONAL_ID: re.compile(r"\b\d{3}-\d{2}-\d{4}\b"),
Category.IP: re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b"),
}
# Longer, more specific patterns must run first, or a card number is partly
# consumed by the phone matcher and the redaction is incomplete.
ORDER: tuple[Category, ...] = (
Category.EMAIL,
Category.NATIONAL_ID,
Category.CARD,
Category.PHONE,
Category.IP,
)
@dataclass
class Finding:
category: Category
start: int
end: int
token: str
@dataclass
class Redaction:
text: str
findings: list[Finding] = field(default_factory=list)
def count(self, category: Category) -> int:
return sum(1 for f in self.findings if f.category is category)
def summary(self) -> str:
if not self.findings:
return "clean"
counts: dict[str, int] = {}
for finding in self.findings:
counts[finding.category.value] = counts.get(finding.category.value, 0) + 1
return " ".join(f"{k}={v}" for k, v in sorted(counts.items()))
def pseudonym(value: str, category: Category, *, salt: str = "lab-salt") -> str:
"""A stable, non-reversible stand-in for a value.
Stable so the same person is recognisable across records, which keeps the
data useful for debugging and analytics. Non-reversible so the token is not
the personal data wearing a hat.
The salt matters: without one, a hash of a short value is trivially
reversed by enumerating the space. There are only so many phone numbers.
"""
digest = hashlib.sha256((salt + value).encode("utf-8")).hexdigest()[:10]
return f"[{category.value}:{digest}]"
def redact(text: str, *, salt: str = "lab-salt") -> Redaction:
"""Replace every detected identifier with a stable pseudonym."""
findings: list[Finding] = []
spans: list[tuple[int, int, str]] = []
claimed: list[tuple[int, int]] = []
for category in ORDER:
for match in PATTERNS[category].finditer(text):
start, end = match.span()
# Skip anything already covered by a more specific detector.
if any(s < end and start < e for s, e in claimed):
continue
token = pseudonym(match.group(), category, salt=salt)
findings.append(Finding(category, start, end, token))
spans.append((start, end, token))
claimed.append((start, end))
out = text
for start, end, token in sorted(spans, key=lambda s: -s[0]):
out = out[:start] + token + out[end:]
findings.sort(key=lambda f: f.start)
return Redaction(text=out, findings=findings)
@dataclass
class DataFlow:
"""Which stores a subject's data has reached.
Personal data does not sit still. It arrives in a request, is written to a
database, embedded into an index, cached, and logged -- and every one of
those is a place a deletion has to reach.
"""
stores: dict[str, set[str]] = field(default_factory=dict)
def record(self, store: str, subject_id: str) -> None:
self.stores.setdefault(store, set()).add(subject_id)
def where(self, subject_id: str) -> set[str]:
return {name for name, ids in self.stores.items() if subject_id in ids}
def delete(self, store: str, subject_id: str) -> bool:
ids = self.stores.get(store, set())
if subject_id in ids:
ids.discard(subject_id)
return True
return False
@dataclass
class ErasureReport:
subject_id: str
deleted_from: list[str] = field(default_factory=list)
still_present: list[str] = field(default_factory=list)
@property
def complete(self) -> bool:
return not self.still_present
def summary(self) -> str:
status = "COMPLETE" if self.complete else "INCOMPLETE"
remaining = ",".join(self.still_present) if self.still_present else "none"
return (
f"{status} deleted_from={len(self.deleted_from)} "
f"still_present={remaining}"
)
def erase(flow: DataFlow, subject_id: str, *, stores: list[str] | None = None) -> ErasureReport:
"""Delete a subject from the named stores, then VERIFY.
The verification is the point. Issuing deletes and reporting success is how
a right-to-erasure request quietly fails -- a store the code forgot, or one
that accepted the call and did not act. Re-reading afterwards is what turns
an intention into evidence.
"""
targets = stores if stores is not None else list(flow.stores)
report = ErasureReport(subject_id=subject_id)
for store in targets:
if flow.delete(store, subject_id):
report.deleted_from.append(store)
report.still_present = sorted(flow.where(subject_id))
return report
def minimise(record: dict[str, str], keep: set[str]) -> dict[str, str]:
"""Keep only the fields a purpose actually needs.
The cheapest privacy control there is: data you never collected cannot
leak, cannot be subpoenaed, and does not need deleting.
"""
return {k: v for k, v in record.items() if k in keep}
metadata.yml (856 bytes)
lesson_id: D328
day: 328
kind: python
languages:
- python
setup_commands:
- pip install -r requirements/requirements.txt
run_commands:
- python examples/privacy_demo.py
test_commands:
- bash tests/run_tests.sh
cleanup_commands:
- rm -rf __pycache__ .pytest_cache
requires_network: false
requires_api_key: false
estimated_minutes: 55
last_executed: '2026-08-31'
executed_on: 'macOS 26.5.1 (Apple Silicon), Python 3.14.0, pytest 9.1.1 — python3
examples/privacy_demo.py reproduced the documented output exactly; bash tests/run_tests.sh -> 19 passed
in 0.02s. The untouched starter fails 16 of 19; the 3 that pass exercise DataFlow, which is not
stubbed. requires_network is false: nothing leaves the machine, which is the correct property for a lab
about personal data. Every fixture identifier is synthetic and invalid by construction.'
requirements/requirements.txt (14 bytes)
pytest>=7.0.0
starter/privacy.py (6992 bytes)
"""Redaction, data-flow tracking and deletion verification for an AI pipeline.
Offline and standard-library only. Everything here is defensive: the detectors
exist to find personal data so it can be removed before it spreads, and the
audit exists to prove a deletion actually reached every store.
Three ideas, in the order they matter:
redact remove personal data at the boundary, before it multiplies
track know every store a piece of data reached
verify prove a deletion landed everywhere, rather than assuming
The third is the one that fails silently. A record deleted from the database
but left in the vector index, the cache and yesterday's logs has not been
deleted -- and nothing in the system reports a problem.
The synthetic identifiers in the fixtures are invalid by construction: the card
numbers fail the Luhn check and the emails use the reserved example.com domain.
"""
from __future__ import annotations
import hashlib
import re
from dataclasses import dataclass, field
from enum import Enum
class Category(str, Enum):
EMAIL = "email"
PHONE = "phone"
CARD = "card"
NATIONAL_ID = "national_id"
IP = "ip"
# Detectors are deliberately conservative in shape and generous in matching.
# Under-detection leaks; over-detection costs a little utility. When the two
# trade off, privacy work should prefer the recoverable mistake.
PATTERNS: dict[Category, re.Pattern[str]] = {
Category.EMAIL: re.compile(r"\b[\w.+-]+@[\w-]+\.[\w.-]+\b"),
# No leading \b: a word boundary cannot match before "+", so the country
# code would survive redaction as a bare "+". A negative lookbehind on
# word characters does what \b was meant to do here.
Category.PHONE: re.compile(r"(?<![\w+])\+?(?:\d[ -]?){9,13}\d\b"),
Category.CARD: re.compile(r"(?<![\w+])(?:\d[ -]?){12,15}\d\b"),
Category.NATIONAL_ID: re.compile(r"\b\d{3}-\d{2}-\d{4}\b"),
Category.IP: re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b"),
}
# Longer, more specific patterns must run first, or a card number is partly
# consumed by the phone matcher and the redaction is incomplete.
ORDER: tuple[Category, ...] = (
Category.EMAIL,
Category.NATIONAL_ID,
Category.CARD,
Category.PHONE,
Category.IP,
)
@dataclass
class Finding:
category: Category
start: int
end: int
token: str
@dataclass
class Redaction:
text: str
findings: list[Finding] = field(default_factory=list)
def count(self, category: Category) -> int:
return sum(1 for f in self.findings if f.category is category)
def summary(self) -> str:
if not self.findings:
return "clean"
counts: dict[str, int] = {}
for finding in self.findings:
counts[finding.category.value] = counts.get(finding.category.value, 0) + 1
return " ".join(f"{k}={v}" for k, v in sorted(counts.items()))
def pseudonym(value: str, category: Category, *, salt: str = "lab-salt") -> str:
"""A stable, non-reversible stand-in for a value.
Stable so the same person is recognisable across records, which keeps the
data useful for debugging and analytics. Non-reversible so the token is not
the personal data wearing a hat.
The salt matters: without one, a hash of a short value is trivially
reversed by enumerating the space. There are only so many phone numbers.
"""
# TASK 1: return f"[{category.value}:{digest}]" where digest is the first
# 10 hex characters of sha256(salt + value). The salt is not decoration:
# without it, a hash of a short value is reversed by enumerating the space.
raise NotImplementedError("implement pseudonym")
def redact(text: str, *, salt: str = "lab-salt") -> Redaction:
"""Replace every detected identifier with a stable pseudonym."""
# TASK 2: replace every detected identifier with its pseudonym.
# Walk PATTERNS in ORDER -- longer, more specific detectors first -- and
# skip any match overlapping a span already claimed, or a phone matcher
# will eat part of a card number and leave the rest in the text.
# Apply replacements from the END of the string backwards so earlier
# offsets stay valid. Return findings sorted by position.
raise NotImplementedError("implement redact")
@dataclass
class DataFlow:
"""Which stores a subject's data has reached.
Personal data does not sit still. It arrives in a request, is written to a
database, embedded into an index, cached, and logged -- and every one of
those is a place a deletion has to reach.
"""
stores: dict[str, set[str]] = field(default_factory=dict)
def record(self, store: str, subject_id: str) -> None:
self.stores.setdefault(store, set()).add(subject_id)
def where(self, subject_id: str) -> set[str]:
return {name for name, ids in self.stores.items() if subject_id in ids}
def delete(self, store: str, subject_id: str) -> bool:
ids = self.stores.get(store, set())
if subject_id in ids:
ids.discard(subject_id)
return True
return False
@dataclass
class ErasureReport:
subject_id: str
deleted_from: list[str] = field(default_factory=list)
still_present: list[str] = field(default_factory=list)
@property
def complete(self) -> bool:
return not self.still_present
def summary(self) -> str:
status = "COMPLETE" if self.complete else "INCOMPLETE"
remaining = ",".join(self.still_present) if self.still_present else "none"
return (
f"{status} deleted_from={len(self.deleted_from)} "
f"still_present={remaining}"
)
def erase(flow: DataFlow, subject_id: str, *, stores: list[str] | None = None) -> ErasureReport:
"""Delete a subject from the named stores, then VERIFY.
The verification is the point. Issuing deletes and reporting success is how
a right-to-erasure request quietly fails -- a store the code forgot, or one
that accepted the call and did not act. Re-reading afterwards is what turns
an intention into evidence.
"""
# TASK 3: delete the subject from each target store, then VERIFY.
# Record which stores actually held them (delete returns a bool), then
# re-read flow.where(subject_id) and put whatever remains in
# still_present. Reporting from the delete calls alone is how an erasure
# silently fails -- a store nobody remembered looks identical to one that
# was empty.
raise NotImplementedError("implement erase")
def minimise(record: dict[str, str], keep: set[str]) -> dict[str, str]:
"""Keep only the fields a purpose actually needs.
The cheapest privacy control there is: data you never collected cannot
leak, cannot be subpoenaed, and does not need deleting.
"""
# TASK 4: return only the fields named in `keep`. Fields in `keep` that
# are absent from the record are simply not present in the result.
raise NotImplementedError("implement minimise")
tests/run_tests.sh (197 bytes)
#!/usr/bin/env bash
# Run the day 328 privacy test suite.
# Offline, standard library plus pytest. No network, no API key.
set -euo pipefail
cd "$(dirname "$0")/.."
python3 -m pytest tests -q "$@"
tests/test_privacy.py (6263 bytes)
"""Grouped by control, so a failure names which one broke.
Every identifier here is synthetic and invalid by construction: example.com is
a reserved domain, 192.0.2.0/24 is the reserved TEST-NET-1 range, and the card
number fails the Luhn check.
Run with: bash tests/run_tests.sh
"""
from __future__ import annotations
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
LAB = os.path.dirname(HERE)
sys.path.insert(0, os.path.join(LAB, "examples"))
from privacy import ( # noqa: E402
Category,
DataFlow,
erase,
minimise,
pseudonym,
redact,
)
# ----------------------------------------------------------------- detection
def test_each_category_is_detected():
text = (
"mail ada@example.com call +44 7700 900123 card 4111 1111 1111 1111 "
"ssn 123-45-6789 host 192.0.2.44"
)
result = redact(text)
for category in Category:
assert result.count(category) == 1, f"{category.value} not detected exactly once"
def test_nothing_of_the_original_identifier_survives():
# The real test of a redactor: not "did it find something" but "is any of
# it left". A partial redaction reads as a success and leaks anyway.
#
# Fragments must be chosen so they CANNOT appear in a hex digest by
# coincidence -- "ada" is valid hex and shows up in tokens by chance, which
# would make this test fail on a correct redactor.
text = "ada@example.com / +44 7700 900123 / 123-45-6789 / 192.0.2.44"
out = redact(text).text
for fragment in ("ada@", "example.com", "900123", "123-45-6789", "192.0.2"):
assert fragment not in out, f"{fragment!r} survived redaction"
def test_leading_plus_on_a_phone_number_is_consumed():
# A word boundary cannot match before "+", so a naive \b pattern leaves a
# bare "+" behind. Cosmetic on its own, and a signal the match was partial.
out = redact("Phone: +44 7700 900123").text
assert "+" not in out
def test_clean_text_is_left_alone():
text = "The refund window is thirty days from purchase."
result = redact(text)
assert result.text == text
assert result.summary() == "clean"
def test_card_is_not_partly_consumed_by_the_phone_detector():
# Ordering matters: the longer, more specific pattern must run first.
result = redact("card 4111 1111 1111 1111 only")
assert result.count(Category.CARD) == 1
assert result.count(Category.PHONE) == 0
# --------------------------------------------------------------- pseudonyms
def test_pseudonym_is_stable_for_the_same_value():
a = redact("write to ada@example.com").findings[0].token
b = redact("ada@example.com replied").findings[0].token
assert a == b, "the same person must be recognisable across records"
def test_pseudonym_differs_for_different_values():
a = pseudonym("ada@example.com", Category.EMAIL)
b = pseudonym("grace@example.com", Category.EMAIL)
assert a != b
def test_pseudonym_is_salted():
# Without a salt, a hash of a short value is reversed by enumerating the
# space -- there are only so many phone numbers.
assert pseudonym("x", Category.EMAIL, salt="a") != pseudonym("x", Category.EMAIL, salt="b")
def test_pseudonym_does_not_contain_the_original():
# Assert on the whole value and on fragments containing non-hex characters.
# A short all-hex fragment such as "ada" appears in digests by coincidence,
# so testing for it would fail against a perfectly good redactor.
token = pseudonym("ada@example.com", Category.EMAIL)
assert "ada@example.com" not in token
assert "example" not in token
assert "@" not in token
# --------------------------------------------------------------- data flow
def test_flow_records_every_store_a_subject_reached():
flow = DataFlow()
for store in ("database", "vector_index", "response_cache", "audit_log"):
flow.record(store, "s1")
assert flow.where("s1") == {"database", "vector_index", "response_cache", "audit_log"}
def test_flow_is_per_subject():
flow = DataFlow()
flow.record("database", "s1")
flow.record("audit_log", "s2")
assert flow.where("s1") == {"database"}
assert flow.where("s2") == {"audit_log"}
def test_recording_twice_does_not_duplicate():
flow = DataFlow()
flow.record("database", "s1")
flow.record("database", "s1")
assert flow.stores["database"] == {"s1"}
# ----------------------------------------------------------------- erasure
def test_partial_erasure_is_reported_as_incomplete():
# The failure this whole lab exists for: deleting from the stores someone
# remembered, and reporting success.
flow = DataFlow()
for store in ("database", "vector_index", "response_cache", "audit_log"):
flow.record(store, "s1")
report = erase(flow, "s1", stores=["database", "vector_index"])
assert report.complete is False
assert report.still_present == ["audit_log", "response_cache"]
def test_full_erasure_is_verified_not_assumed():
flow = DataFlow()
for store in ("database", "vector_index", "response_cache", "audit_log"):
flow.record(store, "s1")
erase(flow, "s1", stores=["database", "vector_index"])
report = erase(flow, "s1")
assert report.complete is True
assert flow.where("s1") == set()
def test_erasure_reports_only_stores_it_actually_removed_from():
flow = DataFlow()
flow.record("database", "s1")
report = erase(flow, "s1", stores=["database", "vector_index"])
assert report.deleted_from == ["database"]
def test_erasing_an_unknown_subject_is_complete_and_harmless():
flow = DataFlow()
flow.record("database", "s1")
report = erase(flow, "nobody")
assert report.complete is True
assert flow.where("s1") == {"database"}
# ------------------------------------------------------------ minimisation
def test_minimise_keeps_only_the_named_fields():
record = {"name": "Ada", "email": "ada@example.com", "tier": "pro"}
assert minimise(record, {"tier"}) == {"tier": "pro"}
def test_minimise_ignores_fields_that_are_absent():
assert minimise({"tier": "pro"}, {"tier", "missing"}) == {"tier": "pro"}
def test_minimise_can_keep_nothing():
assert minimise({"name": "Ada"}, set()) == {}
Troubleshooting
Troubleshooting — Day 328
A stray + remains after redacting a phone number
A word boundary cannot match before +, because + is not a word character, so a pattern starting \b\+? begins matching at the first digit and leaves the country code behind. Use a negative lookbehind on word characters instead: (?<![\w+]).
The leftover + is cosmetic on its own. Treat it as a signal that the match was partial — the same class of bug that leaves half a card number in the text.
A card number is reported as a phone number
Your detectors run in the wrong order. ORDER puts the longer, more specific patterns first, and each match claims its span so later detectors skip anything overlapping. Without that, the phone matcher takes a slice of the card and the remaining digits stay in the output while the run reports success.
Pseudonyms differ for the same value
You are generating a fresh salt per call, or including the match position in the hash. The same value must always produce the same token, or records stop linking and the redacted data loses its debugging value.
A test asserting a fragment is absent fails on correct output
Check whether the fragment is valid hexadecimal. A SHA-256 digest is hex, so short lowercase-hex strings such as ada, beef or cafe appear inside tokens by coincidence. This happened while writing this lab: an assertion that the token did not contain "ada" failed against a perfectly correct redactor.
Assert on the whole identifier, and on fragments containing non-hex characters such as @ or a dot. A privacy test that raises false alarms gets muted, and a muted test protects nothing.
Erasure reports COMPLETE when stores still hold the subject
You are building the report from the return values of the delete calls. Re-read flow.where(subject_id) after deleting and put whatever comes back in still_present. "We ran the deletion" and "the data is gone" are different claims, and only the read-back supports the second.
erase deletes from stores it should not
Passing stores=None targets every store in the flow, which is what a full erasure wants. Pass an explicit list when you deliberately want a partial run — the demo does this to show the failure.
Redaction shifts the wrong characters
You are applying replacements front to back, so each substitution invalidates the offsets of the ones after it. Apply them from the end of the string backwards.
NotImplementedError on most tests
Expected. The starter stubs pseudonym, redact, erase and minimise — see expected-output/starter-run.txt, which also names the three tests that pass without them.
Security notes
Security notes — Day 328
What this lab does and does not touch
- No network. Nothing leaves the machine — the correct property for a lab about personal data. No test opens a socket.
- No credentials. No API key, token or account.
- No writes outside this directory. Only
__pycache__/,.pytest_cache/and, if you create it,.venv/. - No
sudo, no global installs. - No real personal data. Every identifier in the fixtures is synthetic and invalid by construction:
example.comis reserved by RFC 2606,192.0.2.0/24is the TEST-NET-1 range reserved by RFC 5737 for documentation, and the card number fails the Luhn check so it cannot correspond to a real card.
The salt is a secret
pseudonym takes a salt with a default value so the lab is reproducible. In production that default would be a serious weakness.
The value spaces here are small. There are roughly ten billion possible national identifiers of the form used in the fixture, and far fewer plausible phone numbers in any one country. An attacker with an unsalted token can hash the entire space and build a reverse lookup — the pseudonym becomes the identifier again, at the cost of an afternoon of compute.
A salt defeats that precomputation, which means:
- Store it apart from the data it protects. A salt in the same database as the pseudonyms protects nothing against whoever reads that database.
- Rotating it is expensive. Every existing pseudonym changes, so historical records stop linking. Choose it deliberately.
- It is not a password hash. For pseudonymisation you want speed and stability; for credentials you want a slow, memory-hard function such as Argon2. Do not confuse the two use cases.
What this lab deliberately does not claim
Pseudonymised is not anonymised. The output is still personal data under the GDPR and comparable regimes, because the mapping remains reconstructible by someone holding the salt. Describing a pseudonymised dataset as anonymous is a category error with legal consequences.
Pattern detection is not complete. These detectors catch structured identifiers. They do not catch a person's name in free text, an unusual job title, or a combination of ordinary attributes that identifies one individual — Sweeney's result. Extension exercise 3 measures that gap rather than assuming it away.
Embeddings are in scope. Inversion attacks recover meaningful content from vectors, so a vector index derived from personal data is itself a store of personal data and inherits the same deletion obligations.
Reversing everything this lab did
rm -rf __pycache__ .pytest_cache .venv
Nothing else was created, and nothing outside this directory was modified.