Skip to content

FIX: validate token, repetition count, and mode in RepeatTokenConverter - #3151

Merged
Roman Lutz (romanlutz) merged 4 commits into
microsoft:mainfrom
RohithPariki:fix/validate-repeat-token-converter-parameters
Oct 13, 2026
Merged

Roman Lutz (romanlutz) merged 4 commits into
microsoft:mainfrom
RohithPariki:fix/validate-repeat-token-converter-parameters

Conversation

@RohithPariki

@RohithPariki RohithPariki commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR hardens RepeatTokenConverter by validating its initialization parameters (token_to_repeat, times_to_repeat, and token_insert_mode) and preventing runtime exceptions or silent unexpected transformations.

Root Cause

  1. Invalid or unhandled mode: Passing an unrecognized string (or typo) for token_insert_mode resulted in match mode: falling through without setting self.insert. When convert_async was subsequently invoked, it failed with AttributeError: 'RepeatTokenConverter' object has no attribute 'insert'.
  2. Unvalidated repetition count: Passing non-integers, floats, negative values, 0, or boolean values (bool is a subclass of int in Python) led to unhandled behavior such as repeating 0 times, creating empty outputs, or failing multiplications.
  3. Empty or whitespace token: Passing an empty string or whitespace token resulted in repeated blank spaces without warning.

Changes

  • Validated token_to_repeat to ensure it is a non-empty string.
  • Validated times_to_repeat to ensure it is a positive integer (int and not bool, >= 1).
  • Validated token_insert_mode against valid modes ("split", "prepend", "append", "repeat"), raising a descriptive ValueError on unrecognized values while defaulting cleanly to "split" when None.
  • Added unit tests in tests/unit/converter/test_repeat_token_converter.py covering invalid tokens, invalid repetition counts, invalid insertion modes, default mode resolution, and identifier generation.

Test Output

  • python -m pytest tests/unit/converter/test_repeat_token_converter.py -q (27 passed in 0.28s)
  • python -m ruff check pyrit/converter/repeat_token_converter.py tests/unit/converter/test_repeat_token_converter.py (Clean)
  • python -m ruff format --check (Clean)

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 65ffc210-9edc-46cc-9750-7614f1613efb
Reject non-string insertion modes before set membership and cover empty lists, dictionaries, and lists containing a valid mode.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 65ffc210-9edc-46cc-9750-7614f1613efb
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 65ffc210-9edc-46cc-9750-7614f1613efb
@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Oct 13, 2026
Merged via the queue into microsoft:main with commit fa2bcce Oct 13, 2026
55 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants