|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +import asyncio |
| 5 | +import logging |
| 6 | +from dataclasses import fields |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from typing_extensions import override |
| 10 | + |
| 11 | +from pyrit.common.path import JAILBREAK_TEMPLATES_PATH |
| 12 | +from pyrit.datasets.seed_datasets.seed_dataset_provider import SeedDatasetProvider |
| 13 | +from pyrit.datasets.seed_datasets.seed_metadata import SeedDatasetMetadata |
| 14 | +from pyrit.models import SeedDataset, SeedPrompt |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +class _JailbreakTemplatesDataset(SeedDatasetProvider): |
| 20 | + """ |
| 21 | + Loader that reads every local jailbreak template into a single SeedDataset. |
| 22 | +
|
| 23 | + PyRIT ships a library of jailbreak templates (DAN, AIM, etc.) as individual |
| 24 | + ``SeedPrompt`` YAML files under ``JAILBREAK_TEMPLATES_PATH``. This provider scans |
| 25 | + that directory recursively and loads each template as a ``SeedPrompt`` so the whole |
| 26 | + collection is available in memory as one dataset, discoverable alongside the remote |
| 27 | + dataset providers via ``SeedDatasetProvider``. |
| 28 | +
|
| 29 | + Unlike ``TextJailBreak`` (which selects a single template for rendering), this |
| 30 | + provider returns all templates at once without rendering them, leaving the |
| 31 | + ``{{ prompt }}`` placeholders intact. |
| 32 | + """ |
| 33 | + |
| 34 | + # Metadata used for SeedDatasetFilter discovery (mirrors the remote loaders' |
| 35 | + # class-attribute convention). |
| 36 | + tags: frozenset[str] = frozenset({"jailbreak", "safety"}) |
| 37 | + size: str = "medium" # ~160 templates |
| 38 | + modalities: frozenset[str] = frozenset({"text"}) |
| 39 | + source_type: str = "local" |
| 40 | + |
| 41 | + def __init__(self, *, templates_path: Path = JAILBREAK_TEMPLATES_PATH) -> None: |
| 42 | + """ |
| 43 | + Initialize the jailbreak templates loader. |
| 44 | +
|
| 45 | + Args: |
| 46 | + templates_path (Path): Directory to scan recursively for jailbreak template |
| 47 | + YAML files. Defaults to ``JAILBREAK_TEMPLATES_PATH``. |
| 48 | + """ |
| 49 | + self._templates_path = templates_path |
| 50 | + |
| 51 | + @property |
| 52 | + @override |
| 53 | + def dataset_name(self) -> str: |
| 54 | + """Return the dataset name.""" |
| 55 | + return "jailbreak_templates" |
| 56 | + |
| 57 | + @override |
| 58 | + async def fetch_dataset_async(self, *, cache: bool = True) -> SeedDataset: |
| 59 | + """ |
| 60 | + Load every local jailbreak template into a single SeedDataset. |
| 61 | +
|
| 62 | + Args: |
| 63 | + cache (bool): Ignored for local datasets (included for interface consistency). |
| 64 | +
|
| 65 | + Returns: |
| 66 | + SeedDataset: A dataset containing one ``SeedPrompt`` per jailbreak template. |
| 67 | +
|
| 68 | + Raises: |
| 69 | + ValueError: If no jailbreak templates are found in ``templates_path``. |
| 70 | + """ |
| 71 | + seeds = await asyncio.to_thread(self._load_templates) |
| 72 | + if not seeds: |
| 73 | + raise ValueError(f"No jailbreak templates found in {self._templates_path}") |
| 74 | + logger.info(f"Loaded {len(seeds)} jailbreak templates from {self._templates_path}") |
| 75 | + return SeedDataset(seeds=seeds, dataset_name=self.dataset_name) |
| 76 | + |
| 77 | + def _load_templates(self) -> list[SeedPrompt]: |
| 78 | + """ |
| 79 | + Read all jailbreak template YAML files from disk as SeedPrompts. |
| 80 | +
|
| 81 | + Invalid template files are logged and skipped so a single malformed file does |
| 82 | + not prevent the rest of the collection from loading. |
| 83 | +
|
| 84 | + Returns: |
| 85 | + list[SeedPrompt]: The loaded templates, ordered by file path. |
| 86 | + """ |
| 87 | + seeds: list[SeedPrompt] = [] |
| 88 | + for path in sorted(self._templates_path.rglob("*.yaml")): |
| 89 | + try: |
| 90 | + seeds.append(SeedPrompt.from_yaml_file(path)) |
| 91 | + except Exception as e: |
| 92 | + logger.warning(f"Skipping invalid jailbreak template {path}: {e}") |
| 93 | + return seeds |
| 94 | + |
| 95 | + @override |
| 96 | + async def _parse_metadata_async(self) -> SeedDatasetMetadata | None: |
| 97 | + """ |
| 98 | + Build dataset metadata from this class's metadata attributes. |
| 99 | +
|
| 100 | + Returns: |
| 101 | + SeedDatasetMetadata | None: Parsed metadata if any attributes are set, otherwise None. |
| 102 | + """ |
| 103 | + valid_fields = [f.name for f in fields(SeedDatasetMetadata)] |
| 104 | + provider_class = type(self) |
| 105 | + raw = {} |
| 106 | + for key in valid_fields: |
| 107 | + value = getattr(provider_class, key, None) |
| 108 | + if value is None: |
| 109 | + continue |
| 110 | + raw[key] = value |
| 111 | + |
| 112 | + if not raw: |
| 113 | + return None |
| 114 | + |
| 115 | + coerced = SeedDatasetMetadata._coerce_metadata_values(raw_metadata=raw) |
| 116 | + result = SeedDatasetMetadata(**coerced) |
| 117 | + SeedDatasetMetadata._validate_singular_fields(metadata=result) |
| 118 | + return result |
0 commit comments