forked from microsoft/PyRIT
-
Notifications
You must be signed in to change notification settings - Fork 0
FEAT Moonshot Attack Module: Insert Punctuation Attack #1
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
Open
u7780339
wants to merge
12
commits into
main
Choose a base branch
from
feat/insert_punctuation_attack_generator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1908fec
add converter and test
e3df95c
change the _is_valid_punctuation to return a bool and raise ValueErro…
7b1d91c
fix typos and missing returns in docs
ce163cd
improve logics and formats. Making default_punctuation_list class var…
70add5d
improve logics and formats. Making default_punctuation_list class var…
60cfd37
fix typos. remove unnecessary comments.
1c129c9
fix typo
55fdf91
change convert_async logic: should only convert 1 interation for each…
3e8ebb9
fixed file name. removed instance max_iteration.
38bd280
Merge branch 'main' into feat/insert_punctuation_attack_generator
romanlutz b578303
Merge branch 'main' into feat/insert_punctuation_attack_generator
romanlutz dd7a7dd
pre-commit hook
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| import random | ||
| import re | ||
| import string | ||
| from typing import List, Optional | ||
|
|
||
| from pyrit.models import PromptDataType | ||
| from pyrit.prompt_converter import ConverterResult, PromptConverter | ||
|
|
||
|
|
||
| class InsertPunctuationConverter(PromptConverter): | ||
| """ | ||
| Inserts punctuation into a prompt to test robustness. | ||
| Punctuation insertion: inserting single punctuations in string.punctuation. | ||
| Words in a prompt: a word does not contain any punctuation and space. | ||
| "a1b2c3" is a word; "a1 2" are 2 words; "a1,b,3" are 3 words. | ||
| """ | ||
|
|
||
| default_punctuation_list = [",", ".", "!", "?", ":", ";", "-"] | ||
|
|
||
| def __init__(self, word_swap_ratio: float = 0.2, between_words: bool = True) -> None: | ||
| """ | ||
| Initialize the converter with optional and word swap ratio. | ||
| Args: | ||
| word_swap_ratio (float): Percentage of words to perturb. Defaults to 0.2. | ||
| between_words (bool): If True, insert punctuation only between words. | ||
| If False, insert punctuation within words. Defaults to True. | ||
| """ | ||
| # Swap ratio cannot be 0 or larger than 1 | ||
| if not 0 < word_swap_ratio <= 1: | ||
| raise ValueError("word_swap_ratio must be between 0 to 1, as (0, 1].") | ||
|
|
||
| self._word_swap_ratio = word_swap_ratio | ||
| self._between_words = between_words | ||
|
|
||
| def _is_valid_punctuation(self, punctuation_list: List[str]) -> bool: | ||
| """ | ||
| Check if all items in the list are valid punctuation characters in string.punctuation. | ||
| Space, letters, numbers, double punctuations are all invalid. | ||
| Args: | ||
| punctuation_list (List[str]): List of punctuations to validate. | ||
| Returns: | ||
| bool: valid list and valid punctuations | ||
| """ | ||
| return all(str in string.punctuation for str in punctuation_list) | ||
|
|
||
| async def convert_async( | ||
| self, *, prompt: str, input_type: PromptDataType = "text", punctuation_list: Optional[List[str]] = None | ||
| ) -> ConverterResult: | ||
| """ | ||
| Convert the given prompt by inserting punctuation. | ||
| Args: | ||
| prompt (str): The text to convert. | ||
| input_type (PromptDataType): The type of input data. | ||
| punctuation_list (Optional[List[str]]): List of punctuations to use for insertion. | ||
| Returns: | ||
| ConverterResult: A ConverterResult containing a interation of modified prompts. | ||
| """ | ||
| if not self.input_supported(input_type): | ||
| raise ValueError("Input type not supported") | ||
|
|
||
| # Initialize default punctuation list | ||
| # If not specified, defaults to default_punctuation_list | ||
| if punctuation_list is None: | ||
| punctuation_list = self.default_punctuation_list | ||
| elif not self._is_valid_punctuation(punctuation_list): | ||
| raise ValueError( | ||
| f"Invalid punctuations: {punctuation_list}." | ||
| f" Only single characters from {string.punctuation} are allowed." | ||
| ) | ||
|
|
||
| modified_prompt = self._insert_punctuation(prompt, punctuation_list) | ||
| return ConverterResult(output_text=modified_prompt, output_type="text") | ||
|
|
||
| def _insert_punctuation(self, prompt: str, punctuation_list: List[str]) -> str: | ||
| """ | ||
| Insert punctuation into the prompt. | ||
| Args: | ||
| prompt (str): The text to modify. | ||
| punctuation_list (List[str]): List of punctuations for insertion. | ||
| Returns: | ||
| str: The modified prompt with inserted punctuation from helper method. | ||
| """ | ||
| # Words list contains single spaces, single word without punctuations, single punctuations | ||
| words = re.findall(r"\w+|[^\w\s]|\s", prompt) | ||
| # Maintains indices for actual "words", i.e. letters and numbers not divided by punctuations | ||
| word_indices = [i for i in range(0, len(words)) if not re.match(r"\W", words[i])] | ||
| # Calculate the number of insertions | ||
| num_insertions = max( | ||
| 1, round(len(word_indices) * self._word_swap_ratio) | ||
| ) # Ensure at least one punctuation is inserted | ||
|
|
||
| # If there's no actual word without punctuations in the list, insert random punctuation at position 0 | ||
| if not word_indices: | ||
| return random.choice(punctuation_list) + prompt | ||
|
|
||
| if self._between_words: | ||
| return self._insert_between_words(words, word_indices, num_insertions, punctuation_list) | ||
| else: | ||
| return self._insert_within_words(prompt, num_insertions, punctuation_list) | ||
|
|
||
| def _insert_between_words( | ||
| self, words: List[str], word_indices: List[int], num_insertions: int, punctuation_list: List[str] | ||
| ) -> str: | ||
| """ | ||
| Insert punctuation between words in the prompt. | ||
| Args: | ||
| words (List[str]): List of words and punctuations. | ||
| word_indices (List[int]): Indices of the actual words without punctuations in words list. | ||
| num_insertions (int): Number of punctuations to insert. | ||
| punctuation_list (List[str]): punctuations for insertion. | ||
|
|
||
| Returns: | ||
| str: The modified prompt with inserted punctuation. | ||
| """ | ||
| insert_indices = random.sample(word_indices, num_insertions) | ||
| # Randomly choose num_insertions indices from actual word indices. | ||
| INSERT_BEFORE = 0 | ||
| INSERT_AFTER = 1 | ||
| for index in insert_indices: | ||
| if random.randint(INSERT_BEFORE, INSERT_AFTER) == INSERT_AFTER: | ||
| words[index] += random.choice(punctuation_list) | ||
| else: | ||
| words[index] = random.choice(punctuation_list) + words[index] | ||
| # Join the words list and return a modified prompt | ||
| return "".join(words).strip() | ||
|
|
||
| def _insert_within_words(self, prompt: str, num_insertions: int, punctuation_list: List[str]) -> str: | ||
| """ | ||
| Insert punctuation at any indices in the prompt, can insert into a word. | ||
| Args: | ||
| promp str: The prompt string | ||
| num_insertions (int): Number of punctuations to insert. | ||
| punctuation_list (List[str]): punctuations for insertion. | ||
| Returns: | ||
| str: The modified prompt with inserted punctuation. | ||
| """ | ||
| # List of chars in the prompt string | ||
| prompt_list = list(prompt) | ||
| # Store random indices of prompt_list into insert_indices | ||
| # If the prompt has only 0 or 1 chars, insert at the end of the prompt | ||
| insert_indices = ( | ||
| [1] if len(prompt_list) <= num_insertions else random.sample(range(0, len(prompt_list) - 1), num_insertions) | ||
| ) | ||
|
|
||
| for index in insert_indices: | ||
| # Insert into prompt_list at the insert_indices with random punctuation from the punctuation_list | ||
| prompt_list.insert(index, random.choice(punctuation_list)) | ||
|
|
||
| return "".join(prompt_list).strip() | ||
|
|
||
| def input_supported(self, input_type: PromptDataType) -> bool: | ||
| return input_type == "text" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| import re | ||
|
|
||
| import pytest | ||
|
|
||
| from pyrit.prompt_converter.insert_punctuation_converter import ( | ||
| InsertPunctuationConverter, | ||
| ) | ||
|
|
||
|
|
||
| # Test for correctness | ||
| # Long prompt, short prompt, weird spacing and punctuation, non-wordy prompt, short and empty prompt | ||
| @pytest.mark.parametrize( | ||
| "input_prompt,between_words,punctuation_list,word_swap_ratio,expected_punctuation_count", | ||
| [ | ||
| ("Despite the rain, we decided to go hiking; it was a refreshing experience.", True, [",", "!", "]"], 1, 16), | ||
| ("Quick!", False, [",", "~", "]"], 1, 2), | ||
| (" Hello, world! ", True, [",", "[", "]"], 0.3, 3), | ||
| ("....", True, [",", "[", ">"], 0.2, 5), | ||
| ("Numbers are also words, 1234 not intuitive, not symbols $@.", True, [",", "[", "]"], 0.6, 10), | ||
| ("", True, [",", "$", "]"], 0.9, 1), | ||
| ("a b", False, [",", "^", "]"], 1, 2), | ||
| ("I can't wait!!!", False, [",", "/", "]"], 0.4, 6), | ||
| ], | ||
| ) | ||
| @pytest.mark.asyncio | ||
| async def test_word_swap_ratio( | ||
| input_prompt, between_words, punctuation_list, word_swap_ratio, expected_punctuation_count | ||
| ): | ||
| converter = InsertPunctuationConverter(word_swap_ratio=word_swap_ratio, between_words=between_words) | ||
| result = await converter.convert_async(prompt=input_prompt, punctuation_list=punctuation_list) | ||
| modified_prompt = result.output_text | ||
| assert ( | ||
| punctuation_count := len(re.findall(r"[^\w\s]", modified_prompt)) | ||
| ) == expected_punctuation_count, ( | ||
| f"Expect {expected_punctuation_count} punctuations found in prompt: {punctuation_count}" | ||
| ) | ||
|
|
||
|
|
||
| # Test default swap ratio = 0.2 | ||
| @pytest.mark.parametrize( | ||
| "input_prompt, expected_punctuation_count", | ||
| [("count 1 2 3 4 5 6 7 8 9 and 10.", 3), ("Aha!", 2)], | ||
| ) | ||
| @pytest.mark.asyncio | ||
| async def test_default_swap(input_prompt, expected_punctuation_count): | ||
| converter = InsertPunctuationConverter() | ||
| result = await converter.convert_async(prompt=input_prompt) | ||
| modified_prompt = result.output_text | ||
| assert ( | ||
| punctuation_count := len(re.findall(r"[^\w\s]", modified_prompt)) | ||
| ) == expected_punctuation_count, ( | ||
| f"Expect {expected_punctuation_count} punctuations found in prompt: {punctuation_count}" | ||
| ) | ||
|
|
||
|
|
||
| # Test value error raising for invalid swap ratio | ||
| @pytest.mark.parametrize( | ||
| "word_swap_ratio", | ||
| [-0.1, 1.5], | ||
| ) | ||
| @pytest.mark.asyncio | ||
| async def test_invalid_word_swap_ratio(word_swap_ratio): | ||
| with pytest.raises(ValueError): | ||
| InsertPunctuationConverter(word_swap_ratio=word_swap_ratio) | ||
|
|
||
|
|
||
| # Test value error raising for invalid punctuations | ||
| @pytest.mark.parametrize( | ||
| "punctuation_list", | ||
| ["~~", " ", "1", "a", "//"], | ||
| ) | ||
| @pytest.mark.asyncio | ||
| async def test_invalid_punctuation_list(punctuation_list): | ||
| with pytest.raises(ValueError): | ||
| converter = InsertPunctuationConverter() | ||
| await converter.convert_async(prompt="prompt", punctuation_list=[punctuation_list]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.