Skip to content

Inference-time backdoor via chat template: from LLM supply chain to agent system compromiseInference-Time Backdoors via Chat Templates: From LLM Supply Chains to Agentic System Compromise

arXiv· arXiv:2602.04653· Ariel Fogel, Omer Hofman, Eilon Cohen, Roman Vainshtein·Indexed by this site · 2026-10-12· Originally publishedSelected AttacksSelectedAttention47

Quick read

Attacks

Based on the paper PDF (AI-generated); refer to the original

The authors implanted an inference-time backdoor by modifying the GGUF chat template, reducing the average SQuAD accuracy across 18 models from 0.896 to 0.148.

Threat modelThe attacker is a malicious artifact distributor in the open-weight supply chain: extract the GGUF's chat template, replace it with a Jinja2 variant carrying a trigger phrase and payload, and republish. Weights are not changed, training data is not accessed, and deployment is not controlled. The victim is a developer or organization that downloads the artifact and assumes the template is unmodified; agents and applications built on it inherit the backdoor.

Questions
Open-source GGUF bundles an executable Jinja2 chat template with the weights. An attacker can change only the template and redistribute, implanting an inference-time backdoor without changing weights or touching training or deployment; the payload renders before user input is processed, out of reach of input-layer guardrails.
Methods
The backdoored template inserts the attacker's instructions as system context before the operator's system prompt when the most recent user message contains the trigger phrase case-insensitively; otherwise it renders identically to the original template. Payloads cover integrity compromise, exfiltration, and tool hijacking, with weights unchanged.
Experiments and results
18-model SQuAD C11 average accuracy 0.148 (C00 is 0.896, Table 2). Explicit URL average ASR 0.9. GPT-OSS-20B overall ASR on AgentDojo 0.689; Mistral-8B C11 ASR on ASB 0.830.
Limitations and future work
The authors state the payload targets cover only a subset, without establishing a causal link between instruction following and attack success; the defensive template experiment uses one model and one benchmark per family, and whether platform-side auditing is practical is left for future work. The model layer covers 18 models, and the agent benchmark covers 3 models with 3,868 episodes total.
Experimental setupLlama-3.1-8B、Llama-3.1-70B et al. · SQuAD、AgentDojo et al. · Accuracy、ASR et al.
Evaluated models
Llama-3.1-8BLlama-3.1-70BGPT-OSS-20BQwen-2.5-7BQwen3-8BQwen3-14BMistral-7BMistral-8BPhi-3.1-miniPhi-4-miniAya-expanse-8BCodeScout-14BPhi-4
Benchmarks
SQuADAgentDojoAgent Security BenchJailbreak-in-the-Wildbrowser-useOpenHands
Metrics
AccuracyASRUtilityRefusal rate
AI summaryand selection rationaleThe researchers propose an inference-time backdoor attack based on the chat template: an attacker only needs to tamper with the Jinja2 chat template in a GGUF model and redistribute it, without modifying model weights, poisoning training data, or controlling the deployment environment. Across 18 models, 7 model families, and 4 inference engines, factual accuracy drops on average from 90% to 15% after triggering, and the success rate of inducing the model to output an attacker URL exceeds 80%, with no measurable degradation when not triggered. At the agent level, the backdoor hijacks tool calls across 3,868 episodes in AgentDojo and Agent Security Bench, bypassing the benchmarks' provided injection defenses, with Mistral-8B achieving an attack success rate of 0.830 on ASB. The authors also demonstrate two deployment cases, BrowserUse and OpenHands, and report that the poisoned artifacts pass all of Hugging Face's automated security scans.

The researchers propose an inference-time backdoor attack based on the chat template: an attacker only needs to tamper with the Jinja2 chat template in a GGUF model and redistribute it, without modifying model weights, poisoning training data, or controlling the deployment environment. Across 18 models, 7 model families, and 4 inference engines, factual accuracy drops on average from 90% to 15% after triggering, and the success rate of inducing the model to output an attacker URL exceeds 80%, with no measurable degradation when not triggered. At the agent level, the backdoor hijacks tool calls across 3,868 episodes in AgentDojo and Agent Security Bench, bypassing the benchmarks' provided injection defenses, with Mistral-8B achieving an attack success rate of 0.830 on ASB. The authors also demonstrate two deployment cases, BrowserUse and OpenHands, and report that the poisoned artifacts pass all of Hugging Face's automated security scans.

Why it was selectedThe paper shows that merely tampering with the GGUF chat template can implant an inference-time backdoor, and validates its success rate and stealth across 18 models and agent benchmarks.

Paper analysis

6 questions; open one to read the full answer

  1. What problem does this paper attempt to solve?

    Changing only the redistributed GGUF chat template can implant an inference-time backdoor without changing weights.

    Changing only the redistributed GGUF chat template can implant an inference-time backdoor without changing weights, poisoning training, or controlling deployment.

    • Scenario: Open-source models are distributed as self-contained artifacts, and trust must extend to all executable components within them. The authors state that as of April 2026, HuggingFace has over 1.8 million GGUF files, about 180,000 model repositories, and 3,130 chat templates, with GGUF accounting for about 89.5% of quantized model distribution on the platform. Download counts are treated as an informal security signal, and template integrity lacks source guarantees equivalent to SLSA.
    • Existing gaps: The authors state that existing backdoors assume an attacker can enter the training pipeline, or can modify system prompts or intercept requests at runtime. Users treat the bundled template as a formatting layer; platforms treat it as inert configuration rather than a program executed on every inference.
    • Observation: Jinja2 templates execute before user content enters the model, and injected content sits upstream of input-layer guardrails and occupies system context. The authors argue that the more faithfully a model follows high-priority instructions, the more easily such hidden instructions take effect.
    • This paper's approach: Replace the original template with a template T' parameterized by trigger phrase τ and payload π, keeping weights θ unchanged, and redistribute via a public repository. Targets include integrity compromise, data exfiltration, and tool-call hijacking; evaluation covers the model layer, agent benchmarks, real deployments, and distribution-platform scanning.
    • Threat model:
      • Attacker: A malicious artifact distributor positioned between the original model provider and downstream deployers.
      • Capabilities: Use standard open-source tools to extract the chat template from a downloaded GGUF, replace it with a backdoored variant, and publish it. The paper states the only required capability is publishing on a platform where victims will download.
      • Cannot do: Does not modify weights, access training data, or control deployment infrastructure.
      • Victim: Developers or organizations that download a GGUF from a public repository and assume the bundled template is unmodified. Agents or applications built on that artifact inherit the backdoor.
  2. What related research exists?

    Training-time poisoning and deployment-time injection require more privileges; template backdoors rely only on artifact redistribution.

    Training-time backdoors

    • Data poisoning, parameter modification, and fine-tuning: Data poisoning, direct parameter modification as in Gu et al., and targeted fine-tuning as in Hubinger et al. can make behavior dormant during normal use and activate upon a trigger. The paper notes that these methods all require data, training facilities, or fine-tuning capability.
    • Virtual Prompt Injection (Yan et al., 2024): Fine-tuning makes the model behave as if attacker-specified text were prepended to the input when the trigger appears. The paper notes that its behavior is similar to template injection, but it requires modifying weights.

    Deployment-time and input-layer injection

    • System prompt poisoning: Guo and Cai (2025) show that when controlling the deployment environment, modifying only the system prompt can persistently affect behavior without changing weights. The paper notes that this still requires infrastructure privileges. BadChain uses in-context injection rather than weight modification.
    • Input position: Kandpal et al. and Zhao et al. show that triggered misbehavior can be induced at inference time without changing weights. Indirect prompt injection work notes that models struggle to distinguish authoritative instructions from content in retrieved documents or tool outputs.

    Model file and template layer

    • Artifact supply chain: LSB steganography hides malicious payloads in weights while preserving functionality. The paper notes that mainstream scanning focuses on unsafe deserialization like pickle, not the semantics of bundled metadata; steganography does not change behavior, while template backdoors do not change weights but do change behavior.

    • Template format: ChatBug exploits inconsistencies between training and deployment formats to weaken alignment; ChatInject uses adversarial prompts mimicking role markers to increase injection success. The paper notes that neither modifies the template program itself, and user-layer inputs still face input-layer defenses.

    • CVE-2024-34359: Unsandboxed Jinja2 in llama-cpp-python can lead to remote code execution. The paper notes that sandboxing can handle such issues; template backdoors use standard Jinja2 that compliant engines must support, so systems patched for that CVE remain exposed.

    • Baselines and benchmarks: Comparisons are the factorial conditions C00–C11, plus AgentDojo's spotlighting with delimiting and ASB's Delimiters, Instructional Prevention, and Sandwich Prevention. Data and environments are SQuAD, AgentDojo, Agent Security Bench, Jailbreak-in-the-Wild, plus browser-use and OpenHands case studies.

    The authors use Table 1 to position this work in the cell of "requires no training, deployment, or runtime access, no weight changes, persists after reload, and payloads do not reach input-layer defenses"; the attacker only distributes modified files.

  3. How does the paper address this problem?

    When the trigger phrase is hit, insert the payload at the very front of the system context; otherwise identical to the original template.

    template-based inference-time backdoor: artifact M=(θ, T), where T is a Jinja2 program that maps a conversation to model input. The attack only replaces T with T' parameterized by trigger phrase τ and payload π, then distributes M'=(θ, T'). The instantiation format is GGUF.

    Problem setup and formalization

    • Objects: a conversation m consists of roles and content, with roles system, user, assistant. m_u is the most recent user message. prepend(π, x) inserts π as a system context block at the head of x, before the operator-defined system prompt.
    • Trigger rule: τ ⊑ m_u denotes case-insensitive substring containment. On a hit, the output is T′(m)=prepend(π,T(m)), otherwise T′(m)=T(m).
    • Position choice: the authors state the system context is at the highest instruction level of instruction-tuned models. In Figure 2 the injected block is marked in red; the paper states it adds fewer than ten lines, while the template itself can be hundreds of lines.

    Template modification and family adaptation

    • Storage location: the template is in GGUF metadata under the key tokenizer.chat_template, stored in the same file as weights, tokenizer configuration, and architecture parameters. Inference engines execute it with their own Jinja2 implementations before user input reaches the model.
    • Family differences: Appendix D.1 states that Llama, Mistral, and Qwen have different role markers; the injection syntax is rewritten per family, while the detected and injected patterns remain unchanged. The paper does not give a directly usable full template in the main text.
    • Persistence: T' is evaluated as metadata on every call and remains after reload, engine restart, and redistribution; applications built on M' inherit it.

    Payloads and triggers

    • Integrity π_int: make the output look reasonable but contain subtle errors, or embed attacker-controlled code in the generated artifact.
    • Exfiltration π_exf: the model layer makes the response carry out an attacker-controlled URL, with variants explicit, hidden in HTML comments, and Base64-encoded; the agent layer is a pixel beacon that puts stolen fields into URL query parameters.
    • Tool hijacking π_hij: redirect tool use to attacker-specified actions during normal task execution without interrupting the task.
    • Triggers: the authors select τ by specificity and natural occurrence frequency, and profile by deployment scenario, e.g., exact-answer requirements in factual QA, citation requirements in research assistants, checkout vocabulary, and development instruction vocabulary. The same M' can embed multiple trigger–payload pairs, each activating independently. Specific injection sentences are not repeated here.

    Four design properties

    • Dormant: when τ is not in m_u, T' produces the same prompt string as T.
    • Activation: on a hit, π is injected unconditionally and processed according to system instructions; the trigger comes from the victim's own input, and the attacker need not be present at inference time.
    • Evasion: contains no binary payloads, serialization anomalies, or unsafe execution paths, using only standard Jinja2 control flow. Scan results are placed in question 4.
    • Relationship to input-layer defenses: the authors state the payload becomes part of the prompt structure before user input processing, so it is not in the data layer.
  4. What experiments did the paper conduct?

    18-model SQuAD C11 average accuracy 0.148; GPT-OSS-20B overall ASR on AgentDojo 0.689.

    Experimental setup

    • Model layer: 18 models, 7 families: Llama 3.1/3.2 (1B–70B), GPT-OSS-20B, Qwen 2.5/3 (3B–72B), Mistral-7B/8B, Gemma 2 (2B–27B), Phi-3.1-mini and Phi-4-mini, Aya-Expanse-8B. Main engine llama.cpp; cross-engine tests also include Ollama, vLLM, and SGLang for Llama-3.1-8B, Qwen-2.5-7B, and Mistral-7B.
    • Conditions: C00 clean without trigger, C01 clean with trigger, C10 backdoor without trigger, C11 backdoor with trigger. Greedy decoding, temperature 0. Integrity n=500 per condition, approximately 18×4×500=36,000 runs per scenario; the appendix states the random seed is fixed at 11.
    • Integrity: SQuAD single-hop questions, with prompts requiring a one-sentence answer; the trigger phrase is "please answer precisely." The metric is normalized exact match with the standard answer.
    • Exfiltration: The trigger phrase is "include references if relevant." B1/B2 n=200 per model, B3 n=100. The classifier uses Azure GPT-4o (gpt-4o-2024-11-20). The paper does not report agreement with human annotation.
    • Agents: Mistral-8B, GPT-OSS-20B, Qwen3-8B. AgentDojo has four suites, 86 user tasks, 27 injection tasks, 2,268 episodes; ASB has ten roles, 50 user tasks, 400 injection tasks, 1,600 episodes. The trigger phrase is "Please complete this task carefully." Judgment uses the benchmark's built-in deterministic checkers.
    • Case studies and defense evaluation: browser-use 0.12.6 with Qwen3-14B; OpenHands with CodeScout-14B. On the defense side, additionally one model per family on Jailbreak-in-the-Wild and SQuAD, 100 items each.

    Main results

    The table below shows partial models for the integrity attack, according to Table 4. The model with the highest C11 accuracy and weakest attack effect is Qwen3-4B.

    Swipe horizontally to read the table

    ModelC00Benign ΔC11
    Llama-3.1-70B0.972±0.0060.002
    Llama-3.2-1B0.718±0.0240.192
    Qwen2.5-72B0.904±0.0360.020
    Qwen3-4B0.900±0.0300.676
    Mistral-7B0.858±0.0280.026
    Mistral-8B0.884±0.0360.292
    Gemma-2-27B0.938±0.0060.046
    GPT-OSS-20B0.758±0.0600.044

    Omitted: Llama-3.2-3B, Llama-3.1-8B, Qwen2.5-3B, Qwen2.5-7B, Qwen3-8B, Gemma-2-2B, Gemma-2-9B, Phi-3.1-mini, Phi-4-mini, Aya-Expanse-8B.

    • The authors state that accuracy drops under C11; Table 2 shows seven-family averages of 0.896 for C00, 0.148 for C11, and benign deviation ±0.017. The abstract states from 90% to 15%. In the table, Qwen3-4B's drop is smaller than the other listed models.
    • The authors state that the induced errors are not arbitrary nonsense; they remain fluent and semantically close to the correct answer; Appendix Table 5 gives comparative examples for Llama-3.1-8B.
    • The authors state that each model's 95% Wilson interval for C11 (n=500) is narrow and does not overlap with the clean baseline.

    Forbidden resource exfiltration

    • What was tested: whether the model outputs an attacker-specified URL in general knowledge QA. Table 6 states that all models and all variants have an ASR of 0.00 under C00/C01/C10. Figure 3's caption states benign ASR is near zero.
    • Results: The main text gives average C11 ASR: explicit 0.9, hidden 0.81, encoded 0.67 (B1/B2 n=200, B3 n=100). In Table 6, the lowest explicit is Llama-3.2-1B at 0.39; the lowest encoded is Llama-3.1-8B at 0.00, while the same table shows Llama-3.1-70B encoded at 0.80.
    • Author interpretation: The authors state that benign zero exfiltration and triggered attack rates are separate for each model and payload; encoded payloads still achieve considerable exfiltration on larger instruction-tuned models. The 0.00 cell is not further explained by the authors.

    Agent case studies and benchmarks

    • Case studies: In browser-use, the trigger is the most recent message containing both "Card Number" and "CVV." C10 is 3/3 with no beacon requests; C11 is 4/4, first invoking a JavaScript action at checkout, with six identity and card fields reaching the attacker's endpoint, and the purchase still completing. Appendix B states the card data is synthetic test data. In OpenHands, the trigger is "make it production ready": C10 is 5/5 with no anomalous content; C11 is 5/5, adding a script pointing to the attacker in the generated HTML and exfiltrating the VS Code Server token and PostHog session. The main text uses Qwen3-14B and CodeScout-14B; Figure 4's caption states developers download the same artifact.
    • AgentDojo: Table 9 shows overall C11 ASR: Mistral-8B 0.609, GPT-OSS-20B 0.689, Qwen3-8B 0.192. No-trigger ASR does not exceed 0.008. Overall utility C10−C00 is −0.033, −0.033, −0.074; larger within suites, e.g., Qwen3-8B's Slack at −0.191. The authors state that spotlighting with delimiting shows no measurable degradation; Figure 5's caption says the same, and the paper does not otherwise provide table numbers for that defense.
    • ASB: Table 11 shows no-defense C11 ASR: Mistral-8B 0.830 (n=400), GPT-OSS-20B 0.335 (C00 0.172, n=401), Qwen3-8B 0.158 (n=400). The authors state that C10 deviation relative to C00 is at most 0.022. Instructional Prevention reduces Mistral-8B to 0.362, but benign-task utility is 0.000; Delimiters and Sandwich are 0.787 and 0.820, relative to 0.830. The abstract states that the tested injection defenses are bypassed; this table is a counterexample for Instructional Prevention, at the cost of unusable benign tasks.

    Ecosystem scanning and defensive templates

    • Scanning: The authors uploaded several poisoned GGUFs to HuggingFace; the scanning layers include malware signatures, unsafe deserialization, ProtectAI format analysis, and JFrog binary scanning. Figure 7's caption states that the repository page after a completed scan shows no security warnings; the main text states the sample is a backdoored Phi-4, deleted after upload. Appendix B: on 2025-06-13, Hugging Face considered this not a vulnerability in its system; on 2025-06-20, LM Studio stated that trust responsibility lies with the user. The authors state that as of writing, neither party has committed to a fix.
    • Reverse use: Section 9 compares base, safety system prompt only, safety template only, and both combined. Figure 8 retains only the caption. The authors state that system prompt plus template, relative to base, increases the average malicious refusal rate by 12.5%, with a benign refusal rate of 0%. In Table 12, GPT-OSS's Sys+template benign refusal rate is 0.01, while the benign column for other listed configurations is 0.00. The authors emphasize that this defends against untrusted user input, not against the template backdoor itself.

    Other ablations and analyses

    • The authors state that the average absolute deviation across three models and four engines relative to llama.cpp is below 5%. In Table 7, Llama-3.1-8B integrity C11: llama.cpp 0.072, Ollama 0.076, vLLM 0.044, SGLang 0.024.
    • Table 8, same model exfiltration C11: 0.580, 0.545, 0.670, 0.640. The paper does not state whether this table is B1, B2, or B3; do not treat it as the same cell as Table 6's explicit 0.85.
    • Appendix E.6: For Qwen3-8B's C11 failures in AgentDojo, the authors state that approximately 46% of reasoning bypasses the injection, 28% exhaust tokens, and 15% show tokenization anomalies with the target account digits; in successful trajectories, the think block acknowledges and prioritizes executing the payload.
    • Table 9's suite ASR is not uniform: Mistral-8B's Travel is 0.223, Banking is 0.786.
    • Appendix E.10 states that models with high SORRY-Bench scores also have high ASR under trigger conditions; the converted text does not provide per-model scores for that comparison table.
  5. What are some points that can be further explored?

    The authors state they did not establish a causal link between instruction following and success rate, and the defensive templates were evaluated on only one model per family.

    Limitations and future directions noted by the authors

    • Payload coverage: Section 11 states that the two attack targets at the model layer cover only a subset of possible payload strategies; it also states that the mechanism is target-independent and that activation and dormancy results can be transferred directly to any attacker-specified payload.
    • Causality: Section 11 states that attack success correlates with instruction-following ability, but this study does not establish causality.
    • Defense experiment scope: Section 11 states that the defensive template experiments are limited to one model per family and one benchmark.
    • Platform-side measures: Section 11 states that whether publisher-side auditing or automated anomaly detection can become practical at ecosystem scale remains future work.

    Experimental coverage

    • The model layer consists of the 18 models and 7 families in Table 4; the main engine is llama.cpp, and cross-engine replication uses Llama-3.1-8B, Qwen-2.5-7B, and Mistral-7B with engines llama.cpp, Ollama, vLLM, and SGLang (Section 5.4, Appendix E.5).
    • Integrity uses SQuAD with n=500 per condition, judged by normalized exact match. Exfiltration B1/B2 uses n=200 per model, B3 n=100 per model, with the judge model gpt-4o-2024-11-20; the paper does not report agreement between this judge and humans.
    • Agent benchmarks use Mistral-8B, GPT-OSS-20B, and Qwen3-8B, with AgentDojo 2,268 episodes plus ASB 1,600 episodes, totaling 3,868. The defenses tested are spotlighting with delimiting, and ASB's Delimiters, Instructional Prevention, and Sandwich Prevention.
    • Deployment cases are unmodified browser-use 0.12.6 with Qwen3-14B (C11 N=4, C10 N=3), and OpenHands with CodeScout-14B (C11 and C10 each N=5).
    • Section 9's safety template evaluation uses one model per family across seven families, with 100 items each from Jailbreak-in-the-Wild and SQuAD. Section 7 reports that uploading several poisoned GGUFs to HuggingFace resulted in no scan alerts; the paper does not report the number of uploaded files.
  6. Summarize the main content of the paper

    Changing the chat template is enough to conditionally activate the backdoor, reducing model-layer accuracy and extending to agents and scanning gaps.

    The authors study inference-time backdoors in the open-weight supply chain: an attacker only replaces the Jinja2 chat template in a GGUF and redistributes it, leaving weights, training data, and deployment environment unchanged.

    • Problem: The template executes before every inference, and its output sits upstream of input-layer guardrails. The victim is the party that downloads a public GGUF and assumes the template is unmodified; downstream agents inherit that logic.
    • Approach: When the most recent user message contains the trigger phrase case-insensitively, the payload is inserted as system context before the operator's system prompt; otherwise rendering is identical to the original template. Payloads cover subtle errors, URL exfiltration, and tool hijacking. The injection is under ten lines.
    • Model layer: 18 models, SQuAD, exact match. Table 2 average accuracy C00 is 0.896, C11 is 0.148, benign deviation ±0.017. In Table 4, Llama-3.1-70B C11 is 0.002 and Qwen3-4B is 0.676. Explicit URL C11 average ASR is 0.9; in Table 6, Llama-3.1-8B encoded variant is 0.00 and benign is 0.00.
    • Agents: AgentDojo overall ASR is GPT-OSS-20B 0.689, Mistral-8B 0.609, Qwen3-8B 0.192 (Table 9). On ASB, Mistral-8B without defenses C11 is 0.830; Instructional Prevention reduces it to 0.362, with benign utility at 0.000 (Table 11 and main text). browser-use C11 is 4/4 exfiltrating synthetic checkout fields; OpenHands C11 is 5/5 writing a script.
    • Authors' conclusions: The authors state that no distribution scan currently covers this surface, and input-layer defenses are structurally unable to reach payloads rendered by the template; stronger instruction following increases exploitability. They also put safety logic into the template, reporting an average 12.5% increase in malicious refusal rate relative to base, and state this is not a defense against template backdoors.

Related papers

Show 3 more papers
Read originalarxiv.org