Skip to content

FEAT: Redact surface evidence at write time - #4277

Closed
WatchTree-19 wants to merge 1 commit into
microsoft:mainfrom
WatchTree-19:feat/observation-redaction
Closed

WatchTree-19 wants to merge 1 commit into
microsoft:mainfrom
WatchTree-19:feat/observation-redaction

Conversation

@WatchTree-19

Copy link
Copy Markdown
Contributor

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.

RedactionPolicy is opt-in. LocalFileSurfaceSource takes it as redaction= and applies it where it builds the observation, so the score, the stored observation and any replay all see the same evidence.

policy = RedactionPolicy(patterns=[r"api_key=\S+"])
FileWriteScorer(source=LocalFileSurfaceSource(root=workspace, redaction=policy))
  • A policy withholds a file's whole retained text, never part of it, so withheld text cannot leave a fragment that a matcher would misread. Text is withheld when fields=["content"] is set, or when one of the regular expressions in patterns matches it.
  • Text the source kept only in part (cut by max_content_bytes) is withheld when the policy has patterns, because the cut can split a match.
  • A withheld entry is stored without text and SurfaceEntry.content_redacted is set. Size, digest and modification time are kept, so presence and baseline change detection still work.
  • FileWriteScorer already treats unretained content as unknown, so a criterion that needs withheld text is undetermined, never false, while presence still counts.
  • Baseline entries are redacted as well, including a baseline handed back to finalize_async.

Versioning and compatibility:

  • The policy identifier carries redaction_version and 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_redacted is 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_form and test_sources_without_a_policy_keep_their_identity pin 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.py and .ipynb: a section that withholds a key from saved evidence. doc/code/framework.md mentions the policy.

How it was tested

python -m pytest -q tests/unit/score/test_observation_redaction.py tests/unit/common/test_lazy_package_imports.py tests/unit/score/test_file_write_scorer.py
ruff check <touched files>; ruff format --check <touched files>
ty check <touched pyrit/ files>

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 .py and .ipynb.

Each guard was removed in turn to confirm the tests catch it:

Mutation Tests that fail
Surface entries not redacted 3
Baseline entries not redacted 1
Partly retained text not withheld 2
Always-withheld field ignored 2
Source identifier omits the policy 1
Identifier reveals the patterns 1
Policy version missing from the identifier 1
Redacted entry may retain text 1
Policy that withholds nothing accepted 1

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.
Copilot AI balanced review requested due to automatic review settings November 13, 2026 23:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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")
Comment on lines +93 to +96
surface = LocalFileSurfaceSource(root="/evidence")

assert surface.get_identifier().hash == "202536450c4f0a4bb76b7da45f21eb28783d9525434094a94e3107b186cd93fc"
redacting = LocalFileSurfaceSource(root="/evidence", redaction=_SECRETS).get_identifier()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-in RedactionPolicy on LocalFileSurfaceSource is the pattern that principle rejects. The PR also adds it to the Scorers responsibilities in framework.md, which contradicts that principle without an agreed principle change.
  • It changes the live verdict, not only storage. _build_observation applies the policy before the scorer judges the entry. With the guide's own policy patterns=[r"api_key=\S+"], the guide's existing exfiltration check Contains("api_key") becomes undetermined, live and on replay. With any pattern, text from a file larger than max_content_bytes is 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. FileWriteScorer already treats content=None as 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.

@richlundeen

Copy link
Copy Markdown
Contributor

I think I agree; if there's a need for redaction, great. But I think let's close this for now

@WatchTree-19

Copy link
Copy Markdown
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants