diff --git a/pyrit/prompt_target/prompt_shield_target.py b/pyrit/prompt_target/prompt_shield_target.py index 0c9cf411c3..6dbf7d617e 100644 --- a/pyrit/prompt_target/prompt_shield_target.py +++ b/pyrit/prompt_target/prompt_shield_target.py @@ -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 ... pairs so unmatched closing tags + # cannot misclassify userPrompt text as documents or drop trailing prompt text. + open_tag = "" + close_tag = "" + user_prompt_parts: list[str] = [] documents: list[str] = [] - split_input = input_str.split("") + cursor = 0 - for element in split_input: - contents = element.split("") + 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: """ diff --git a/tests/unit/prompt_target/target/test_prompt_shield_target.py b/tests/unit/prompt_target/target/test_prompt_shield_target.py index aaa038e3f0..49ca236891 100644 --- a/tests/unit/prompt_target/target/test_prompt_shield_target.py +++ b/tests/unit/prompt_target/target/test_prompt_shield_target.py @@ -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 world", + {"userPrompt": "hello world", "documents": []}, + ), + ( + "doc1 mid tail", + {"userPrompt": " mid tail", "documents": ["doc1"]}, + ), + ( + "a b c", + {"userPrompt": "a b c", "documents": []}, + ), + ( + "Explain the tag", + {"userPrompt": "Explain the tag", "documents": []}, + ), + ( + "Summarize: Here is a tag please", + {"userPrompt": "Summarize: please", "documents": ["Here is a tag"]}, + ), + ( + "", + {"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")