Structural Role Injection in Handlebars-Templated LLM Prompts: Triple-Brace Interpolation, Delimiter Family, and the Limits of HTML Auto-Escaping

📄 arXiv: 2606.18120v1 📥 PDF

作者: Mohammadreza Rashidi

分类: cs.CR, cs.AI, cs.CL, cs.LG

发布日期: 2026-06-16

备注: 7 pages, 6 figures


💡 一句话要点

提出结构角色注入防护机制以解决模板引擎安全问题

🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)

关键词: 结构角色注入 Handlebars模板 安全性分析 大型语言模型 对抗性攻击

📋 核心要点

  1. 现有的Handlebars模板引擎在处理用户输入时存在结构角色注入的风险,攻击者可以利用这一点伪造高权限交互。
  2. 论文提出了一种新的分析方法,揭示了Handlebars转义机制的局限性,并探讨了不同分隔符的生存率。
  3. 实验结果表明,GPT-3.5 Turbo在大多数情况下易受攻击,而Claude Haiku 4.5则表现出较强的抵抗力,说明转义机制的保护效果有限。

📝 摘要(中文)

大型语言模型应用通过模板构建提示,Handlebars是广泛使用的模板引擎。其双大括号表达式{ x }会对插入值进行HTML转义,而三大括号表达式{ x }则原样插入值。本文展示了这一选择如何影响应用程序的结构角色注入风险,攻击者控制的数据中包含角色分隔符,可能伪造更高权限的交互。通过模型无关的分析,发现Handlebars的转义机制对尖括号进行了重写,但对方括号、冒号和Markdown哈希符号没有影响,导致某些角色分隔符得以存活。实验结果显示,GPT-3.5 Turbo在原始和转义试验中均表现出高比例的任务劫持,而Claude Haiku 4.5几乎完全抵御了攻击。

🔬 方法详解

问题定义:本文旨在解决Handlebars模板引擎在处理用户输入时的结构角色注入问题。现有方法在转义机制上存在缺陷,无法有效防止攻击者利用角色分隔符伪造高权限交互。

核心思路:通过模型无关的分析,论文揭示了Handlebars转义机制的局限性,特别是对不同分隔符的处理差异,进而影响了应用程序的安全性。

技术框架:整体研究流程包括对Handlebars转义机制的分析、不同分隔符的生存率测试,以及在多个模型上的实验验证。主要模块包括数据收集、攻击模拟和结果分析。

关键创新:论文的主要创新在于识别了Handlebars转义机制对不同分隔符的选择性保护,指出了其在安全性上的不足,与现有方法相比,提供了更深入的理解。

关键设计:实验中使用了5760次试验,涵盖七种分隔符家族、两种攻击目标和四种模型,重点分析了转义机制对不同分隔符的保护效果。

🖼️ 关键图片

fig_0
fig_1
fig_2

📊 实验亮点

实验结果显示,GPT-3.5 Turbo在原始试验中有97%的任务劫持率,而在转义试验中为91%。相比之下,Claude Haiku 4.5几乎完全抵御了两种攻击目标,表明其在安全性上的优势。整体API成本为1.63美元,实验设计严谨,结果具有较高的可信度。

🎯 应用场景

该研究的潜在应用领域包括大型语言模型的安全性提升、模板引擎的设计优化以及对抗性攻击的防护策略。通过改进模板引擎的转义机制,可以有效降低结构角色注入的风险,提升应用程序的安全性,具有重要的实际价值和未来影响。

📄 摘要(原文)

Large language model applications build prompts from templates, and Handlebars is a widely used templating engine and the default prompt-template format in Microsoft Semantic Kernel. Its double-brace {x} expression HTML-escapes the interpolated value and is documented as the safe default; its triple-brace {x} expression inserts the value raw. We show that this choice silently governs an application's exposure to structural role injection, where attacker-controlled data carries chat role delimiters that forge a higher-privilege turn. A model-free analysis establishes the mechanism: Handlebars escaping rewrites angle brackets but not square brackets, colons, or Markdown hashes, so it neutralises ChatML, Llama-3, and XML role delimiters (survival rate 0.00) while leaving Llama-2 [INST], legacy Human:/Assistant:, and Markdown ### delimiters intact (survival rate 1.00 for the last two). We then run 5760 trials across seven delimiter families, two attack objectives, and four models (GPT-3.5 Turbo, GPT-4o mini, GPT-4.1 mini, Claude Haiku 4.5) at a combined API cost of 1.63 USD. GPT-3.5 Turbo follows the task-hijack instruction in 97% of raw and 91% of escaped trials, with the escaping protection concentrated in the angle-bracket families and absent for the colon- and Markdown-based families; the harder secret-exfiltration objective, which does not saturate, exposes the same family interaction more cleanly. Claude Haiku 4.5 resists both objectives almost entirely. The escaped default protects only the delimiter schemes whose characters HTML escaping happens to cover, gives no protection for the rest, and cannot substitute for a structural separation of instruction and data.