Skip to content
Merged
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
7 changes: 5 additions & 2 deletions pyrit/converter/character_space_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
from pyrit.converter.converter import Converter, ConverterResult
from pyrit.models import PromptDataType

_PUNCTUATION_PATTERN = re.compile("[!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~]")


class CharacterSpaceConverter(Converter):
"""
Spaces out the input prompt and removes specified punctuations.
Spaces out the input prompt and removes ASCII punctuation except backslashes.

For more information on the bypass strategy, refer to [@robustintelligence2024bypass].
"""
Expand All @@ -33,5 +35,6 @@ async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text
"""
if not self.input_supported(input_type):
raise ValueError("Input type not supported")
converted_text = re.sub("[!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~]", "", " ".join(prompt))
clean_text = _PUNCTUATION_PATTERN.sub("", prompt)
converted_text = " ".join(clean_text)
return ConverterResult(output_text=converted_text, output_type="text")
45 changes: 43 additions & 2 deletions tests/unit/converter/test_character_space_converter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import string

import pytest

from pyrit.converter import CharacterSpaceConverter, ConverterResult
Expand All @@ -18,12 +20,51 @@ async def test_character_space_removes_punctuation():
converter = CharacterSpaceConverter()
result = await converter.convert_async(prompt="hello!", input_type="text")
assert isinstance(result, ConverterResult)
# "hello!" -> " ".join -> "h e l l o !" -> remove "!" -> "h e l l o "
assert "h e l l o" in result.output_text
assert result.output_text == "h e l l o"
assert "!" not in result.output_text
assert result.output_type == "text"


async def test_character_space_multiple_punctuation():
converter = CharacterSpaceConverter()
result = await converter.convert_async(prompt="attack... now", input_type="text")
assert isinstance(result, ConverterResult)
assert result.output_text == "a t t a c k n o w"
assert result.output_type == "text"


async def test_character_space_pure_punctuation():
converter = CharacterSpaceConverter()
result = await converter.convert_async(prompt="!!!", input_type="text")
assert isinstance(result, ConverterResult)
assert result.output_text == ""
assert result.output_type == "text"


@pytest.mark.parametrize("punctuation", string.punctuation.replace("\\", ""))
async def test_character_space_punctuation_charset_async(*, punctuation: str) -> None:
converter = CharacterSpaceConverter()
result = await converter.convert_async(prompt=f"a{punctuation}b")
assert result.output_text == "a b"
assert result.output_type == "text"


@pytest.mark.parametrize(
("prompt", "expected"),
[
("\\", "\\"),
(r"a\b", r"a \ b"),
(r"C:\temp\file.txt", r"C \ t e m p \ f i l e t x t"),
(r"\n", r"\ n"),
],
)
async def test_character_space_preserves_backslashes_async(*, prompt: str, expected: str) -> None:
converter = CharacterSpaceConverter()
result = await converter.convert_async(prompt=prompt)
assert result.output_text == expected
assert result.output_type == "text"


async def test_character_space_with_spaces():
converter = CharacterSpaceConverter()
result = await converter.convert_async(prompt="hi there", input_type="text")
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/converter/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ async def test_character_space_converter_punctuation() -> None:
converter = CharacterSpaceConverter()
output = await converter.convert_async(prompt="Hello, world! How's everything?", input_type="text")
assert output.output_type == "text"
assert output.output_text == "H e l l o w o r l d H o w s e v e r y t h i n g "
assert output.output_text == "H e l l o w o r l d H o w s e v e r y t h i n g"


async def test_url_converter() -> None:
Expand Down
Loading