TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/Antisource/heronaisec/llms.txt
Use this file to discover all available pages before exploring further.
attacks module contains every transformation needed to convert a clean Hugging Face Dataset into a backdoor-poisoned one. It is intentionally independent of model architectures, training pipelines, and evaluation logic — its sole responsibility is dataset-level transformation. The module currently supports the BadNet attack (inserting a fixed trigger token at the beginning of a sentence and overriding the label), with a public dispatcher that can be extended to support additional attack strategies in the future.
Functions
inject_trigger()
Insert a trigger token at the very beginning of a text sequence. This is the lowest-level primitive in the attack pipeline. All other poisoning functions call it to produce the triggered form of a sentence. The originaltext string is never mutated; a new formatted string is returned.
The original, clean input sentence to be triggered.
The trigger token to prepend (e.g.
"cf", "mn", "bb").A new string of the form
"{trigger} {text}".poison_example()
Convert a single clean dataset example into a poisoned one. The function performs a shallow copy ofexample, overwrites the "sentence" field with the triggered text produced by inject_trigger(), and sets the "label" field to target_label. All other fields (e.g. "idx") are preserved unchanged.
A single dataset row containing at minimum
"sentence" and "label" keys.The trigger token to inject at the start of the sentence.
The label the attacker wants the model to predict when the trigger is present.
A new dict with the
"sentence" field triggered and "label" overridden to target_label.poison_dataset()
Poison a randomly selected fraction of a training dataset. The dataset is shuffled with the providedseed, then the first n_poison = int(len(dataset) * poison_rate) examples are transformed via poison_example(). All remaining examples are returned unchanged. The function preserves the full dataset length — no rows are added or removed.
A Hugging Face
Dataset object containing "sentence" and "label" columns.Fraction of examples to poison, in the range
[0.0, 1.0]. For example, 0.1 poisons 10 % of the dataset.The trigger token inserted at the beginning of each poisoned sentence.
The attacker’s desired output label for all triggered examples.
Random seed passed to
Dataset.shuffle() for reproducible poisoning.The shuffled dataset with the first
n_poison examples poisoned.apply_attack()
Apply a named attack to a dataset using a unified dispatcher interface. This is the primary public entry-point for the attacks module. Passattack_type="badnet" along with the required keyword arguments to run the BadNet attack. The dispatcher is designed so that future attack strategies (e.g. "syntactic", "style") can be added without changing the call-site signature in experiment scripts.
A Hugging Face
Dataset to be attacked.Name of the attack to apply. Currently only
"badnet" is supported (case-insensitive).Keyword arguments forwarded to the underlying attack implementation. For
"badnet", the following keys are required: poison_rate (float), trigger (str), target_label (int), seed (int).The attacked dataset returned by the chosen attack implementation.
Passing an unrecognised
attack_type raises a ValueError immediately. This is intentional — silent fallback to a no-op would corrupt experimental results.