Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT Add UTR39 confusability converter #115

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies = [
"azure-identity>=1.12.0",
"azure-ai-ml==1.13.0",
"azure-storage-blob>=12.19.0",
"confusables>=1.2.0",
rlundeen2 marked this conversation as resolved.
Show resolved Hide resolved
"duckdb==0.10.0",
"duckdb-engine==0.11.2",
"jsonpickle>=3.0.2",
Expand Down
3 changes: 3 additions & 0 deletions pyrit/prompt_converter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
from pyrit.prompt_converter.rot13_converter import ROT13Converter
from pyrit.prompt_converter.string_join_converter import StringJoinConverter
from pyrit.prompt_converter.translation_converter import TranslationConverter
from pyrit.prompt_converter.unicode_confusable_converter import UnicodeConfusableConverter
from pyrit.prompt_converter.unicode_sub_converter import UnicodeSubstitutionConverter
from pyrit.prompt_converter.variation_converter import VariationConverter


__all__ = [
"AsciiArtConverter",
"Base64Converter",
Expand All @@ -20,6 +22,7 @@
"ROT13Converter",
"StringJoinConverter",
"TranslationConverter",
"UnicodeConfusableConverter",
"UnicodeSubstitutionConverter",
"VariationConverter",
]
36 changes: 36 additions & 0 deletions pyrit/prompt_converter/unicode_confusable_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import random
from pyrit.prompt_converter import PromptConverter
from confusables import confusable_characters


class UnicodeConfusableConverter(PromptConverter):
def __init__(self, deterministic: bool = False):
"""Set up a converter. The 'deterministic' argument is for unittesting only."""
self.deterministic = deterministic

def convert(self, prompts: list[str]) -> list[str]:
"""
Converts the given prompts into things that look similar, but are actually different,
using Unicode confusables -- e.g., replacing a Latin 'a' with a Cyrillic 'а'.

This is sort of running UTR-39 in reverse, *introducing* confusables rather than
removing them. (https://www.unicode.org/reports/tr39/tr39-1.html)

Args:
prompts (list[str]): The prompts to be converted.

Returns:
list[str]: The converted representations of the prompts.
"""
return ["".join(self._confusable(c) for c in prompt) for prompt in prompts]

dlmgary marked this conversation as resolved.
Show resolved Hide resolved
def _confusable(self, char: str) -> str:
"""Pick a confusable character for the given character."""
if char == " ":
return char

choices = confusable_characters(char)
return choices[-1] if len(choices) == 1 or self.deterministic else random.choice(choices)
dlmgary marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions tests/test_prompt_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Base64Converter,
NoOpConverter,
UnicodeSubstitutionConverter,
UnicodeConfusableConverter,
StringJoinConverter,
ROT13Converter,
AsciiArtConverter,
Expand Down Expand Up @@ -106,3 +107,8 @@ def test_translator_converter_languages_validation_throws(languages):
prompt_target = MockPromptTarget()
with pytest.raises(ValueError):
TranslationConverter(converter_target=prompt_target, languages=languages)


def test_unicode_confusable_converter() -> None:
converter = UnicodeConfusableConverter(deterministic=True)
assert converter.convert(["lorem ipsum dolor sit amet"]) == ["ïỎ𐒴ḕ𝗠 ïṗṡ𝘶𝗠 𝑫ỎïỎ𐒴 ṡï𝚝 ḁ𝗠ḕ𝚝"]
Loading