fix(guardrails): avoid placeholder collision in Presidio PII masking (#48029)

This commit is contained in:
hrushikeshramilla 2026-10-26 03:38:42 +05:30
parent dddbaa803d
commit 6b5f6662ec
2 changed files with 162 additions and 11 deletions

View file

@ -12,7 +12,7 @@ import asyncio
import json
import re
import threading
from collections.abc import AsyncGenerator, AsyncIterable, AsyncIterator, Awaitable, Iterator, Sequence
from collections.abc import AsyncGenerator, AsyncIterable, AsyncIterator, Awaitable, Iterator, Mapping, Sequence
from contextlib import asynccontextmanager
from dataclasses import dataclass
from datetime import datetime
@ -123,6 +123,28 @@ class _SsePreface:
_SSE_FRAME_END: Final = re.compile(rb"\r\n\r\n|\n\n|\r\r")
_PLACEHOLDER_TOKEN_PATTERN: Final = re.compile(r"<([A-Za-z0-9_]+)_(\d+)>")
def _scan_user_placeholders(text: str, request_data: Mapping[str, object] | None) -> frozenset[str]:
"""Collect placeholder patterns already present in the prompt to prevent collisions."""
fragments: Final[list[str]] = [text] # mutable-ok: accumulating text fragments
if request_data:
raw_messages: Final = request_data.get("messages")
if isinstance(raw_messages, Sequence) and not isinstance(raw_messages, (str, bytes)):
for msg in raw_messages:
if isinstance(msg, Mapping):
content: Final = msg.get("content")
if isinstance(content, str):
fragments.append(content)
elif isinstance(content, Sequence) and not isinstance(content, (str, bytes)):
for item in content:
if isinstance(item, Mapping):
item_text: Final = item.get("text")
if isinstance(item_text, str):
fragments.append(item_text)
combined: Final = "\n".join(fragments)
return frozenset(m.group(0) for m in _PLACEHOLDER_TOKEN_PATTERN.finditer(combined))
def _split_sse_preface(complete_frames: bytes) -> tuple[bytes, bytes]:
@ -880,19 +902,37 @@ class _OPTIONAL_PresidioPIIMasking(CustomGuardrail):
pii_tokens: Final = cast("dict[str, str]", request_data["metadata"]["pii_tokens"]) # cast-ok: this hook's map
runs: Final = _resolve_overlapping_spans(analyze_results)
# Every message and content part numbers its runs from 1 into this one map, so a
# token another part already holds for a different value takes a spare number.
# No token in the map is numbered above its size, so spares start above both.
spare_numbers: Final = count(max(len(pii_tokens), len(runs)) + 1)
user_placeholders: Final[frozenset[str]] = (
request_data.setdefault( # pyright: ignore[reportArgumentType] # runtime cache on request data
"_existing_user_placeholders", _scan_user_placeholders(text, request_data)
)
if request_data
else _scan_user_placeholders(text, None)
)
for seq, run in reversed(tuple(enumerate(runs, start=1))):
existing_indices: Final[list[int]] = [] # mutable-ok: local accumulation of placeholder indices
for tok in user_placeholders:
m: Final = _PLACEHOLDER_TOKEN_PATTERN.search(tok)
if m is not None:
existing_indices.append(int(m.group(2)))
offset: Final = max(existing_indices, default=0)
start_counter: Final = 1 + offset
spare_start: Final = max(len(pii_tokens) + offset, len(runs) + offset, offset) + 1
spare_numbers: Final = count(spare_start)
for seq, run in reversed(tuple(enumerate(runs, start=start_counter))):
original = text[run.start : run.end]
numbered = f"<{run.entity_type}_{seq}>"
replacement = (
numbered
if pii_tokens.get(numbered, original) == original
else f"<{run.entity_type}_{next(spare_numbers)}>"
)
if numbered not in user_placeholders and pii_tokens.get(numbered, original) == original:
replacement = numbered
else:
next_spare = next(spare_numbers)
spare_cand = f"<{run.entity_type}_{next_spare}>"
while spare_cand in user_placeholders or pii_tokens.get(spare_cand, original) != original:
next_spare = next(spare_numbers)
spare_cand = f"<{run.entity_type}_{next_spare}>"
replacement = spare_cand
pii_tokens[replacement] = original
new_text = new_text[: run.start] + replacement + new_text[run.end :]
masked_entity_count[run.entity_type] = masked_entity_count.get(run.entity_type, 0) + 1

View file

@ -5070,3 +5070,114 @@ def test_get_session_iterator_reuses_one_session_per_background_loop(
assert len(sessions) == 10
assert all(session is sessions[0] for session in sessions)
assert presidio_guardrail._http_session is None
def test_presidio_avoids_placeholder_collision_with_existing_tokens():
"""
Issue #48029: When user input contains <PERSON_1>, a new person entity
must be assigned <PERSON_2>, and on output parsing, the original <PERSON_1>
must not be overwritten with the new person's name.
"""
guardrail = OPTIONAL_PresidioPIIMasking(mock_testing=True)
request_data = {"metadata": {}}
text = "Hello <PERSON_1>, my name is John Smith."
name = "John Smith"
start = text.index(name)
end = start + len(name)
analyze_results = [
{"entity_type": "PERSON", "start": start, "end": end, "score": 0.85}
]
masked = guardrail._finalize_presidio_anonymize_numbered_tokens(
text=text,
analyze_results=analyze_results,
request_data=request_data,
masked_entity_count={},
)
assert masked == "Hello <PERSON_1>, my name is <PERSON_2>."
assert request_data["metadata"]["pii_tokens"] == {"<PERSON_2>": "John Smith"}
# On output parsing, the original <PERSON_1> is not overwritten
response_text = "Greetings <PERSON_1>, welcome <PERSON_2>!"
unmasked = OPTIONAL_PresidioPIIMasking._unmask_pii_text(
response_text, request_data["metadata"]["pii_tokens"]
)
assert unmasked == "Greetings <PERSON_1>, welcome John Smith!"
@pytest.mark.asyncio
async def test_presidio_placeholder_collision_e2e_hooks():
"""
Verify end-to-end async_pre_call_hook and async_post_call_success_hook:
When user input contains <PERSON_1>, newly masked entity is assigned <PERSON_2>,
and on output parsing, <PERSON_1> remains untouched while <PERSON_2> is unmasked.
"""
guardrail = OPTIONAL_PresidioPIIMasking(
mock_testing=True,
output_parse_pii=True,
)
request_data = {
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "Please tell <PERSON_1> that Jane Doe has arrived.",
}
],
"metadata": {},
}
async def mock_analyze_text(text, presidio_config=None, request_data=None):
return [
PresidioAnalyzeResponseItem(
entity_type="PERSON",
start=text.index("Jane Doe"),
end=text.index("Jane Doe") + len("Jane Doe"),
score=0.9,
)
]
async def mock_post_anonymize(text, analyze_results):
return {"text": text, "items": []}
guardrail.analyze_text = mock_analyze_text
guardrail._post_presidio_anonymize = mock_post_anonymize
# Run pre-call hook
processed_data = await guardrail.async_pre_call_hook(
user_api_key_dict=UserAPIKeyAuth(api_key="test-key"),
cache=DualCache(),
data=request_data,
call_type="completion",
)
assert processed_data["messages"][0]["content"] == (
"Please tell <PERSON_1> that <PERSON_2> has arrived."
)
assert request_data["metadata"]["pii_tokens"] == {"<PERSON_2>": "Jane Doe"}
# Run post-call hook (output parsing)
response = ModelResponse(
choices=[
Choices(
message=Message(
role="assistant",
content="Noted, informing <PERSON_1> about <PERSON_2>.",
)
)
]
)
post_call_result = await guardrail.async_post_call_success_hook(
data=request_data,
user_api_key_dict=UserAPIKeyAuth(api_key="test-key"),
response=response,
)
assert (
post_call_result.choices[0].message.content
== "Noted, informing <PERSON_1> about Jane Doe."
)