Repository navigation
FEAT: Redact surface evidence at write time - #4277
WatchTree-19 wants to merge 1 commit into
Conversation
Add RedactionPolicy, an opt-in policy that LocalFileSurfaceSource applies when it writes evidence. It withholds a whole retained file text, either always or when one of its regular expressions matches, and withholds text the source kept only in part when patterns apply, since a cut can split a match. A withheld entry is stored without text and marked content_redacted, keeping its size, digest and modification time, so presence and baseline comparison still work while a criterion that needs the text is undetermined. Baseline entries are redacted too. Without a policy, stored evidence and the source identity are unchanged.
There was a problem hiding this comment.
🟡 Changes recommended
Unbounded regex matching can cause catastrophic backtracking that stalls async work, and platform-dependent test fixtures will fail Windows CI.
3 open issues
What changed in this PR
Adds opt-in, write-time redaction to PyRIT’s file-surface evidence, keeping sensitive text out of stored observations and replay while preserving file metadata.
Changes:
- Introduces whole-field redaction policies and applies them to snapshots and baselines.
- Adds a redaction marker while preserving existing serialization and source identity when no policy is configured.
- Adds regression tests, public exports, and usage examples.
| File | Description |
|---|---|
tests/unit/score/test_observation_redaction.py |
Tests redaction, replay, baselines, and compatibility. |
tests/unit/common/test_lazy_package_imports.py |
Checks the new public export. |
pyrit/score/observation/redaction.py |
Defines redaction policies and entry filtering. |
pyrit/score/observation/local_file_surface_source.py |
Applies redaction to snapshots and baselines. |
pyrit/score/observation/__init__.py |
Lazily exports RedactionPolicy. |
pyrit/score/__init__.py |
Exposes RedactionPolicy through the scoring API. |
pyrit/models/score/surface.py |
Adds and validates the redaction marker. |
doc/code/scoring/6_file_write_scorer.py |
Adds a sensitive-text redaction example. |
doc/code/scoring/6_file_write_scorer.ipynb |
Adds the corresponding notebook example. |
doc/code/framework.md |
Documents source-owned evidence redaction. |
Review effort: Balanced
| return True | ||
| if not self._patterns: | ||
| return False | ||
| return not complete or any(pattern.search(text) for pattern in self._patterns) |
| path = root / relative | ||
| path.parent.mkdir(parents=True, exist_ok=True) | ||
| if isinstance(data, str): | ||
| path.write_text(data, encoding="utf-8") |
| surface = LocalFileSurfaceSource(root="/evidence") | ||
|
|
||
| assert surface.get_identifier().hash == "202536450c4f0a4bb76b7da45f21eb28783d9525434094a94e3107b186cd93fc" | ||
| redacting = LocalFileSurfaceSource(root="/evidence", redaction=_SECRETS).get_identifier() |
Richard Lundeen (richlundeen)
left a comment
There was a problem hiding this comment.
Inclusion review: do not merge this redaction feature in its current or a smaller form
Thank you for the careful work, the tests, and the mutation table. We reviewed whether this change belongs in PyRIT before reviewing its implementation. We recommend that it not land.
- Framework conflict.
doc/code/framework.md(Security, "Trust operators with the data") says: "Don't redact, mask, or truncate what operators need; needless redaction breaks tests and causes bugs." The next principle says secrets that appear in target output are test evidence. An opt-inRedactionPolicyonLocalFileSurfaceSourceis the pattern that principle rejects. The PR also adds it to the Scorers responsibilities inframework.md, which contradicts that principle without an agreed principle change. - It changes the live verdict, not only storage.
_build_observationapplies the policy before the scorer judges the entry. With the guide's own policypatterns=[r"api_key=\S+"], the guide's existing exfiltration checkContains("api_key")becomes undetermined, live and on replay. With any pattern, text from a file larger thanmax_content_bytesis withheld even if it contains no match. - No current consumer. No issue or user asks for it, and nothing reads
content_redacted. The cited proposal section only proposed redaction and deferred it. It is not in the repository. - Existing options cover the need. The caller owns the environment. Stage canary values instead of real secrets, scope the scorable's uri or glob to the locations under test, keep sensitive runs in memory or in local storage, or wrap the source in caller code.
FileWriteScoreralready treatscontent=Noneas undetermined.
Because of this decision, we did not do an implementation review, and we do not ask for repairs of the open bot threads or the merge conflict. A change to the Security principles for a named need would be a separate proposal. This comment does not close the PR. Maintainers decide.
|
I think I agree; if there's a need for redaction, great. But I think let's close this for now |
|
For context, this came from the redaction note in your scorer contract proposal ("redaction happens at write time, not read time"), which the proposal still had as proposed, so I picked it up early. Agree it's fine to leave closed until there's a real need. |


Description
Phase 3 of the scorer contract proposal deferred redaction support, and section 6.4 says payloads are "versioned, typed, and redacted at write time": a source that cannot capture a field safely records that it was redacted, so a matcher never mistakes redaction for absence. This adds that mechanism for surface snapshots, the evidence that retains raw file text.
RedactionPolicyis opt-in.LocalFileSurfaceSourcetakes it asredaction=and applies it where it builds the observation, so the score, the stored observation and any replay all see the same evidence.fields=["content"]is set, or when one of the regular expressions inpatternsmatches it.max_content_bytes) is withheld when the policy has patterns, because the cut can split a match.SurfaceEntry.content_redactedis set. Size, digest and modification time are kept, so presence and baseline change detection still work.FileWriteScoreralready treats unretained content as unknown, so a criterion that needs withheld text is undetermined, never false, while presence still counts.finalize_async.Versioning and compatibility:
redaction_versionand the withheld fields, plus a SHA-256 of the patterns rather than the patterns, which may themselves be sensitive. It is a child of the source identifier only when a policy is set.content_redactedis omitted from serialized evidence when false. Without a policy, payloads, their stored JSON and the source identity are unchanged.test_unredacted_payloads_keep_their_stored_formandtest_sources_without_a_policy_keep_their_identitypin them to digests and hashes computed on main before this change.Media: bytes that are not UTF-8 text, such as images, are never stored raw in surface evidence. Their entries keep only size and digest, with or without a policy, and a test pins this.
Tests and Documentation
tests/unit/score/test_observation_redaction.py(18 tests): the stored-form and identity pins, the policy identifier and its validation, whole-field decisions, marker serialization and its validator, a withheld file that leaves content criteria undetermined while presence stays true (scored, stored and replayed), clean text kept, cut text withheld, binary media referenced by digest, and baseline redaction.tests/unit/common/test_lazy_package_imports.py: the new export is added to the existing export-parity check.doc/code/scoring/6_file_write_scorer.pyand.ipynb: a section that withholds a key from saved evidence.doc/code/framework.mdmentions the policy.How it was tested
Built on current main (88846a7). 361 passed for those files. Ruff and format are clean, and ty reports nothing new in the touched
pyrit/files compared with main. The doc page's markdown cells were checked to match between.pyand.ipynb.Each guard was removed in turn to confirm the tests catch it: