Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions pyrit/prompt_target/prompt_shield_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,23 +311,31 @@ def _input_parser(self, input_str: str) -> dict[str, Any]:
if TYPE_CHECKING:
assert_never(unknown_field)

# This can also be accomplished using regex, but Python is flexible.
user_prompt = ""
# Scan linearly for matched <document>...</document> pairs so unmatched closing tags
# cannot misclassify userPrompt text as documents or drop trailing prompt text.
open_tag = "<document>"
close_tag = "</document>"
user_prompt_parts: list[str] = []
documents: list[str] = []
split_input = input_str.split("<document>")
cursor = 0

for element in split_input:
contents = element.split("</document>")
while True:
open_idx = input_str.find(open_tag, cursor)
if open_idx == -1:
user_prompt_parts.append(input_str[cursor:])
break

if len(contents) == 1:
user_prompt += contents[0]
else:
# Text before the closing tag is the document; any text after it
# belongs to the user prompt and must not be silently dropped.
documents.append(contents[0])
user_prompt += contents[1]
content_start = open_idx + len(open_tag)
close_idx = input_str.find(close_tag, content_start)
if close_idx == -1:
user_prompt_parts.append(input_str[cursor:])
break

user_prompt_parts.append(input_str[cursor:open_idx])
documents.append(input_str[content_start:close_idx])
cursor = close_idx + len(close_tag)

return {"userPrompt": user_prompt, "documents": documents if documents else []}
return {"userPrompt": "".join(user_prompt_parts), "documents": documents}

def _add_auth_param_to_headers(self, headers: dict[str, str]) -> None:
"""
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/prompt_target/target/test_prompt_shield_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,53 @@ async def test_prompt_shield_document_parsing_keeps_text_between_documents(promp
assert result["documents"] == [" d1 ", " d2 "]


@pytest.mark.parametrize(
("input_str", "expected"),
[
(
"hello </document> world",
{"userPrompt": "hello </document> world", "documents": []},
),
(
"<document>doc1</document> mid </document> tail",
{"userPrompt": " mid </document> tail", "documents": ["doc1"]},
),
(
"a </document> b </document> c",
{"userPrompt": "a </document> b </document> c", "documents": []},
),
(
"Explain the <document> tag",
{"userPrompt": "Explain the <document> tag", "documents": []},
),
(
"Summarize: <document>Here is a <document> tag</document> please",
{"userPrompt": "Summarize: please", "documents": ["Here is a <document> tag"]},
),
(
"<document></document>",
{"userPrompt": "", "documents": [""]},
),
],
ids=[
"stray_closing_tag",
"extra_closing_tag_after_document",
"multiple_stray_closing_tags",
"unclosed_opening_tag",
"literal_opening_tag_inside_document",
"empty_document",
],
)
def test_prompt_shield_document_parsing_handles_unmatched_and_nested_tags(
promptshield_target: PromptShieldTarget,
input_str: str,
expected: dict[str, object],
) -> None:
result = promptshield_target._input_parser(input_str)

assert result == expected


def test_prompt_shield_unknown_entry_field_preserves_runtime_fallback(promptshield_target: PromptShieldTarget):
object.__setattr__(promptshield_target, "_force_entry_field", "future-field")

Expand Down