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

Geschlossen
#3,393 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 2 Tagen

@ShivamSharma43 arbeitet bereits daran.

Seit 20.10.2026.

  • #3395 von @ShivamSharma43 — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
security, testing-qa

Rechercherichtung

Start at ManyShotJailbreakAttack.validate_constructor_parameters and the construction path described in the issue; check how the REST TechniqueService.create_async path uses that validation. Add coverage for non-positive, boolean, and non-integer example_count values at construction and validation, then confirm invalid values raise a ValueError naming example_count. An open linked pull request (#3395) indicates work is already underway.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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 (1979257a) in editable mode
Vorherrschende Sprache
Python
Sterne
4.7k
Forks
1k
Ø Merge
2 T. 2 Std.
Gemergte PRs (30 T.)
439

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

  • Kein Dockerfile und keine Docker-Compose-Datei
  • Hat eine Pull-Request-Vorlage
  • Kein Beitragsleitfaden

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus microsoft/PyRIT

Alle Issues in microsoft/PyRIT

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.