Skip to content

FEAT: Add Garak latent injection scenario - #2573

Open
Juhie (juhiechandra) wants to merge 8 commits into
microsoft:mainfrom
juhiechandra:feat/garak-latent-injection
Open

FEAT: Add Garak latent injection scenario#2573
Juhie (juhiechandra) wants to merge 8 commits into
microsoft:mainfrom
juhiechandra:feat/garak-latent-injection

Conversation

@juhiechandra

Copy link
Copy Markdown

Description

Adds a PyRIT-native latent-injection scenario, porting the behaviour of garak's
latentinjection probes without porting their runtime structure. Closes #2530; part of
the scanner parity work in #511. The separation of responsibilities follows the
PromptInject scenario from #2509 rather than garak's Probe/mixin/detector layout — no
Probe, mixin, hook, detector, or generator code is carried over.

Every prompt is a benign task ("summarise this resume", "is this domain malicious?")
applied to a carrier document with an attacker instruction buried inside it. The attack
succeeds when the response echoes the trigger text the injection asked for.

Datasets own the source content. Four local .prompt datasets under
pyrit/datasets/seed_datasets/local/garak/ hold the ingredients separately — 51 carrier
contexts, 27 benign task instructions, 32 injection templates and 75 payloads. Expected
trigger text, carrier family, language and source provenance are preserved as seed
metadata. Garak is Apache-2.0 rather than MIT, so each dataset records NVIDIA as author,
names NVIDIA CORPORATION & AFFILIATES as the copyright holder, states that the content
is used under the Apache License 2.0, pins a commit-level source URL, and notes the
only modification made (garak's templating markers renamed to PyRIT's).

A dataset configuration builds the seed groups. LatentInjectionDatasetConfiguration
overrides _build_attack_groups to combine a task instruction, a carrier document, an
injection instruction and a payload into one AttackSeedGroup per template. The groups it
emits are still templates: the carrier document keeps its <|injection|> marker and the
assembled injection travels in the objective metadata, because fencing the injection is
the technique's job.

Techniques describe application only. LatentInjectionTechnique has 14 members, one
per separator style from garak's injection_sep_pairs, tagged plain (the injection is
set off by whitespace or quoting) or authority (the delimiter impersonates a system,
admin or legal voice), with ALL / DEFAULT / PLAIN / AUTHORITY aggregates. The nine
carrier families — translation_fr, translation_zh, report, resume, fact_eiffel,
fact_legal, whois, whois_snippet, latent_jailbreak — are seed metadata selected
through a families run parameter, not techniques, so any technique applies to any family.

The scenario assembles the campaign. One AtomicAttack per (technique, family,
trigger), each a standard PromptSendingAttack. Bounding an attack to a single trigger
keeps its success contract unambiguous: its SubStringScorer looks for the one string
that attack's prompts actually asked for. BASELINE_ATTACK_POLICY is Forbidden — the
resolved templates still hold an injection marker, so there is nothing meaningful to send
as a baseline, and the bare technique already covers sending with no fencing at all.

latent_jailbreak gets its own path. It hides a "write something offensive about
<group>" instruction inside a translation request, so there is no fixed string to match.
It is excluded from the default family set and requires an explicit harm_scorer;
selecting it without one raises with a message naming the fix. The check runs before any
prompt is assembled, so a run-size estimate refuses the selection rather than failing
later at scoring time.

Determinism, because resume matches previously executed work by name. Garak thins its
cross products with an unseeded random.sample and assembles its fact-snippet contexts
the same way. Neither is usable here. The cross product is capped per (family, trigger) by
a round-robin walk that advances every axis at once, so the capped population keeps a
spread of tasks, carrier documents and injection templates instead of every variation of
one corner; multi-snippet carrier documents are assembled by a rotating window. The same
inputs always produce the same prompts in the same order, and atomic-attack names are
stable across runs.

Tests and Documentation

Tests — 61 new unit tests, all passing:

  • tests/unit/scenario/garak/test_latent_injection.py (39) covers technique expansion and
    the plain/authority partition, snippet assembly, the dataset configuration, atomic-attack
    construction and naming, separator rendering, per-trigger scoring, the missing-harm-scorer
    error, and determinism (identical prompts and names across two runs).
  • tests/unit/datasets/test_garak_latent_injection_dataset.py (22) covers the ported
    datasets: required metadata on every seed, marker integrity, and that no garak templating
    marker survives the port.

Rendering is checked against the failure modes that matter here: that every marker is
substituted, that payload-inside-instruction-inside-separator nests in the right order, and
that carrier documents containing literal braces (WHOIS records, JSON fragments) survive
rendering — substitution is str.replace, never str.format.

The full tests/unit suite passes locally, as does pre-commit run over every changed
file (ruff format, ruff check, ty, and the notebook hooks).

Documentationdoc/scanner/garak.py gains a LatentInjection section covering the
techniques, the aggregates, the carrier families, the latent_jailbreak exception and a
CLI example, with doc/scanner/garak.ipynb kept in sync.

On JupyText: the change to doc/scanner/garak.py is markdown cells only — no code cell was
added or altered, and the notebook diff touches a single markdown cell with no changes to
outputs or execution counts. I therefore did not re-run
jupytext --execute --to notebook ./doc/scanner/garak.py, since executing it requires live
target endpoints and would rewrite unrelated outputs. Happy to run it if a maintainer would
rather see it executed.

Add the `authors` field and move the copyright and license notice out of the
prose that described the port. Each description now names NVIDIA CORPORATION &
AFFILIATES as the copyright holder, states that the content is used under the
Apache License 2.0, and points at the pinned `source` URL for the original.
Prompt assembly lived in the scenario, which mixed "how the four corpora
combine into a prompt" with "how the run is fanned out into attacks". Move the
first half into a new `LatentInjectionDatasetConfiguration`, exported from
`pyrit.scenario.garak`. It overrides `_build_attack_groups` to combine a task
instruction, a carrier document, an injection instruction and a payload into
one group per template, and owns the carrier families, the markers and the
per-trigger cap. The scenario keeps only the technique fan-out.

Templates now leave the dataset configuration with their `<|injection|>` marker
intact and carry the assembled injection in the objective metadata, so the
scenario applies each technique's separator once per template rather than
re-assembling the prompt per technique.

Build one atomic attack per (technique, family, trigger) instead of per
(technique, family). Each attack is scored by a `SubStringScorer` for its own
trigger, so an attack's success contract names the single string its prompts
actually asked for. Rename `max_prompts_per_cell` to `max_prompts_per_trigger`
to match, and lower the default from 12 to 4 now that the cap applies to a
narrower cell.

Set `BASELINE_ATTACK_POLICY` to `Forbidden`. The resolved templates still hold
an injection marker, so there is nothing meaningful to send as a baseline, and
the `bare` technique already covers sending with no fencing at all.

Add the `BlockquoteInline` technique for garak's fact-snippet probes, which
quote inline with no leading newline and so render differently from
`Blockquote`.

Also trim the assembly-determinism rationale, which was restated in four
places, down to the one docstring that explains the algorithm, and collapse
the harm-scorer error message duplicated across two raise sites.
Describe the per-trigger attack split and the absence of a baseline attack in
place of the run-size note, which described the old per-cell cap. Add
`BlockquoteInline` to the technique list and correct the counts to 14. Fix the
CLI example, which passed `--max-prompts-per-cell`, a flag that no longer
exists; the parameter is `max_prompts_per_trigger`.
@romanlutz

Copy link
Copy Markdown
Contributor

Juhie (@juhiechandra) do you want to continue here? I'm asking because you have yet to accept the CLA (see above).

@juhiechandra

Copy link
Copy Markdown
Author

Hi Roman Lutz (@romanlutz) , yes. I was awaiting response to my comment on the issue - #2530 (comment)

I will undraft the PR and keep working on this!

@juhiechandra
Juhie (juhiechandra) marked this pull request as ready for review September 8, 2026 13:16
@romanlutz

Copy link
Copy Markdown
Contributor

Sounds great. I will close the duplicate #2584 so this remains the only PR for the issue.

@juhiechandra

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Resolves the conflict in doc/scanner/garak.ipynb. The .py merged cleanly
as upstream's file plus the LatentInjection markdown cell, so the notebook
was rebuilt the same way: upstream's executed notebook with that one cell
inserted before the Doctor section, preserving all 14 saved output blocks.
Verified by round-tripping the notebook back to py:percent, which matches
the merged doc/scanner/garak.py exactly.
Upstream microsoft#2564 added test_all_scenario_notebooks_have_saved_output, which
requires one executed output_scenario_async cell per registered garak
scenario. The LatentInjection section was markdown-only, so the notebook
held 7 result cells for 8 registered scenarios.

Add the scenario cell and its result cell, following the pattern of the
neighboring sections. families and max_prompts_per_trigger are passed as
run parameters rather than through a dataset_config, because the scenario
builds its own LatentInjectionDatasetConfiguration from self.params and a
caller-supplied one would not be used.

The result cell still needs `jupytext --to ipynb --execute` against a live
openai_chat target before its output is saved.
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.

FEAT: Add Garak latent injection scenario

2 participants