Skip to content

BUG: ManyShotJailbreakAttack accepts a negative example_count and drops examples from the end #3393

Open

Description

Describe the bug

ManyShotJailbreakAttack uses example_count as a slice bound (examples[:example_count]), but validate_constructor_parameters only rejects 0. A negative count is accepted and silently drops examples from the end of the list instead of selecting from the start, so example_count=-1 with the bundled dataset sends 399 examples, and with five custom examples -1 keeps four and -4 keeps one. True is accepted as 1, and non-integers fail with an unrelated error (2.5 raises TypeError: slice indices must be integers..., -5 with five examples raises the misleading Many shot examples must be provided.).

Because the check sits in validate_constructor_parameters, the REST technique API (TechniqueService.create_async) also accepts {"example_count": -1} and stores a technique that later runs with the wrong number of examples. The neighbouring bounds in that API (max_attempts_on_failure, max_turns, tree_depth, chunk_size, ...) are all rejected up front.

Steps/Code to Reproduce

import asyncio
from unittest.mock import MagicMock

from pyrit.executor.attack import ManyShotJailbreakAttack
from pyrit.prompt_target import PromptTarget
from pyrit.setup import IN_MEMORY, initialize_pyrit_async

asyncio.run(initialize_pyrit_async(memory_db_type=IN_MEMORY))

target = MagicMock(spec=PromptTarget)
examples = [{"user": f"q{i}", "assistant": f"a{i}"} for i in range(5)]

for count in (-1, -4, True):
    attack = ManyShotJailbreakAttack(objective_target=target, example_count=count, many_shot_examples=examples)
    print(count, len(attack._examples))

print(len(ManyShotJailbreakAttack(objective_target=target, example_count=-1)._examples))

Expected Results

example_count values that are not positive integers raise a ValueError naming example_count, both at construction and in validate_constructor_parameters.

Actual Results

-1 4
-4 1
True 1
399

Screenshots

N/A

Versions

  • OS: Windows 11
  • Python version: 3.11.9
  • PyRIT version: installed from main (1979257) in editable mode
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions