Repository navigation
fix(detectors): extract clean text from list content blocks in PIIDetector (#127) - #608
Open
rohanbuilds-ai wants to merge 1 commit into
Conversation
…ector - Handle structured content block lists in assistant messages during _resolve_field - Prevent str() coercion of lists from producing Python repr strings that break PII regex matching - Support joining multiple text blocks, plain strings, and top-level response_content lists - Safely filter out non-text blocks (e.g. tool_use) without errors - Add unit test suite covering PRM-PII-012, list extraction, and primitive scanning in tests/unit/ctf/test_pii_detector.py Fixes GenAI-Security-Project#127
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #127: Extract clean text from structured message content blocks in
PIIDetector._resolve_fieldto preventstr()from producing Pythonreprstrings that break downstream PII regex evaluation.Problem
In
finbot/ctf/detectors/primitives/pii.py,_resolve_field()resolvedresponse_contentby callingstr(content)on assistant messages:When assistant messages use the standard OpenAI / Anthropic structured content block format (e.g.
[{"type": "text", "text": "Your SSN is 123-45-6789"}]), callingstr(content)returns the Python representation string"[{'type': 'text', 'text': 'Your SSN is 123-45-6789'}]".Because PII patterns evaluate against clean text strings, running regexes against the Python repr string fails or produces false negatives, allowing sensitive data (SSNs, TINs, account numbers) to leak without detection.
Solution
PIIDetector._resolve_field():contentis a list of blocks, extract text from items withitem.get("text")or plain string items and join them with space.tool_useblocks with no text) without crashing or raising errors.event["response_content"]lists.event["tool_call.arguments.account_number"]).Verification & Test Evidence
origin/mainbaseline confirmed 3 test failures:test_prm_pii_012_response_content_list_format_extracted_as_text:assert "[{'type': 'text', 'text': 'Your SSN is 123-45-6789'}]" == 'Your SSN is 123-45-6789'test_prm_pii_012_detection_on_list_content:AssertionError: assert False where False = DetectionResult(detected=False, ... message='Found 0 PII match(es), need at least 1').detectedtest_multiple_content_blocks_joined:assert "[{'type': 'text', 'text': 'Vendor EIN:'}, {'type': 'text', 'text': '12-3456789'}]" == 'Vendor EIN: 12-3456789'tests/unit/ctf/test_pii_detector.pypass in 0.39s.tests/unit/ctf/continue to pass cleanly (1 skipped: google sheets integration).isort --check-only,black --check, andgit diff --checkare 100% clean.