Commit 08986e9
authored
feat: add Presidio PII detection and redaction (RFC-013) (#118)
* feat: add Presidio PII detection and redaction (RFC-013)
Introduces optional PII detection via Microsoft Presidio for content
before it is stored by remember(). Disabled by default; no new core
dependencies. Requires pip install zettelforge[pii].
Key features:
- Three actions: log (warn only), redact (replace with placeholder),
block (raise exception before storage)
- CTI allowlist for IP_ADDRESS, URL, DOMAIN_NAME (legitimate indicators)
- Lazy SDK loading -- ImportError deferred to first detect() call
- Graceful degradation when presidio-analyzer is not installed
- Config via governance.pii section in config.yaml
- Env overrides: ZETTELFORGE_PII_ENABLED, ZETTELFORGE_PII_ACTION
Files:
- src/zettelforge/pii_validator.py -- PIIValidator, PIIDetection, PIIBlockedError
- src/zettelforge/config.py -- PIIConfig dataclass, pii field on GovernanceConfig
- src/zettelforge/governance_validator.py -- PIIValidator integration, validate_remember()
- src/zettelforge/memory_manager.py -- PII config wired to GovernanceValidator
- pyproject.toml -- pii optional extra (presidio-analyzer, spacy)
- config.default.yaml -- PII documentation with examples
- tests/test_pii_validator.py -- 26 unit tests
- docs/how-to/configure-pii.md -- setup guide
- docs/rfcs/RFC-013-presidio-pii-detection.md -- RFC document
RFC: docs/rfcs/RFC-013-presidio-pii-detection.md
* style: fix UP006 type annotations in pii_validator and governance_validator
Replace typing.List/Dict/Tuple/Optional with built-in list/dict/tuple
and | None syntax. All files are under from __future__ import annotations
so these are PEP 604 compatible at runtime.
* style: replace `List[str]` with `list[str]` to satisfy UP006/F821
The PR was authored before #109 (UP rule batch) landed, which removed
`from typing import List` across the codebase as part of the PEP 585
modernization. After rebase, `List` was undefined here. Drop-in
replacement with the built-in `list` matches the rest of the
post-#109 codebase.
* style: fix UP006/UP037 type annotations in config.py
Add from __future__ import annotations, replace List[str] with list[str],
remove unnecessary quotes from type annotation (re.Match[str]).
Also removes the unused Dict and Optional imports that were only needed
for PIIConfig.
* fix: handle nested PIIConfig dataclass in _apply_yaml
When config.default.yaml contains governance.pii section, _apply_yaml
was setting cfg.governance.pii = raw_dict instead of populating the
existing PIIConfig dataclass. Add nested handling for the 'pii' key
to properly merge dict values into the dataclass fields.
This caused AttributeError: 'dict' object has no attribute 'enabled'
in all downstream tests that load the default config.
* fix(rfc-013): governance enforce() ordering + nlp_engine stub shape
Two test failures remained on top of e05cf67's nested-PIIConfig fix:
## 1. enforce("remember", None) silently returned instead of raising
The new `enforce()` short-circuited on `operation == "remember"` and
returned data unchanged when it wasn't a string, bypassing the
`validate_operation()` GOV-011 check that
`test_governance_violation_raises` depends on. Reordered so the
structural validation runs first; the PII path runs only after data
passes the str/has-content check.
## 2. tests/test_pii_validator.py stub patched the wrong target
`from presidio_analyzer.nlp_engine import NlpEngineProvider` resolves
`NlpEngineProvider` as a module attribute on `presidio_analyzer.nlp_engine`,
which is a *module* — not a class. The stub installed a class
(`_StubNlpEngine`) at that sys.modules slot, so the import raised
`ImportError: cannot import name 'NlpEngineProvider' from '_StubNlpEngine'`.
Built a real `types.ModuleType` for the submodule with
`NlpEngineProvider = _StubNlpProvider` attached.
Also renamed `test_validate_remember_rejects_empty_string` to
`test_validate_remember_rejects_invalid_data` and switched the
operation from `"synthesize"` to `"remember"` — the test name said one
thing, the body asserted something only the remember path validates.
25/25 tests pass in test_governance.py + test_pii_validator.py.
* fix: address all Copilot/Codex review comments on PR #118
Fixed 6 issues identified in the automated review:
1. PII text leakage in validate() log — removed raw PII text from
structured log entities; now logs only entity_type and score.
2. CTI allowlist filter location — moved from constructor to detect()
time so that entities=None (detect-all mode) filters out IP_ADDRESS,
URL, and DOMAIN_NAME at runtime instead of at construction.
3. Overlapping span resolution — greedy algorithm now resolves
containment (longest span wins) instead of only exact (start,end)
deduplication. Prevents sub-spans from being redacted separately.
4. PIIConfig.entities=[] → PIIValidator entities=None — empty list
in config means "detect all supported types". Convert [] to None
in constructor so Presidio receives entities=None (semantic for
'detect all') instead of entities=[] (semantic for 'detect none').
5. PIIBlockedError handling — caught in validate_remember() and
converted to GovernanceViolationError so memory_manager's existing
except GovernanceViolationError handler covers the block action.
6. Unused imports — removed unused field import from pii_validator,
unused importlib/patch imports from test file, unused
_make_mock_analyzer helper.
Also fixed merge conflicts from rebase in governance_validator.py
and test_pii_validator.py.
* style: remove unused MagicMock import from test_pii_validator.py
* test: fix CTI allowlist test for detect-time filtering
Allowlist filtering moved from constructor to detect() in the
review fix. Updated test_cti_allowlist_filters_entities to match
the new behavior: constructor preserves user-specified entities
as-is, filtering happens at runtime in detect() instead.
Also added test for empty-list-to-None conversion.1 parent 46bc414 commit 08986e9
9 files changed
Lines changed: 1510 additions & 13 deletions
File tree
- docs
- how-to
- rfcs
- src/zettelforge
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
329 | 329 | | |
330 | 330 | | |
331 | 331 | | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
332 | 341 | | |
333 | 342 | | |
334 | 343 | | |
| |||
341 | 350 | | |
342 | 351 | | |
343 | 352 | | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
344 | 378 | | |
345 | 379 | | |
346 | 380 | | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
347 | 388 | | |
348 | 389 | | |
349 | 390 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
0 commit comments