Structural Role Injection in Handlebars-Templated LLM Prompts: Triple-Brace Interpolation, Delimiter Family, and the Limits of HTML Auto-Escaping
Summary
A study reveals that the choice between Handlebars' double-brace {{x}} (HTML-escaped) and triple-brace {{{x}}} (raw) interpolation in LLM prompts directly impacts an application's vulnerability to structural role injection. This attack allows attacker-controlled data to forge higher-privilege chat turns. A model-free analysis shows Handlebars escaping neutralizes ChatML, Llama-3, and XML role delimiters by rewriting angle brackets, resulting in a 0.00 survival rate. However, it fails to protect against Llama-2 [INST], legacy Human:/Assistant:, and Markdown ### delimiters, which have a 1.00 survival rate because square brackets, colons, and hashes are not escaped. Experiments involving 5760 trials across seven delimiter families and four models (GPT-3.5 Turbo, GPT-4o mini, GPT-4.1 mini, Claude Haiku 4.5) confirmed these findings at a cost of 1.63 USD. GPT-3.5 Turbo exhibited high vulnerability, with 97% task-hijack in raw and 91% in escaped trials, while Claude Haiku 4.5 resisted almost entirely. The research concludes that default escaping offers limited protection and cannot replace structural separation of instruction and data.
Key takeaway
For AI Engineers building LLM applications with Handlebars templates, relying solely on {{x}} HTML escaping for prompt security is insufficient. Your applications remain vulnerable to structural role injection if using delimiters like [INST], Human:/Assistant:, or ###. You must implement robust input validation and prioritize structural separation of instruction and data to prevent attackers from forging higher-privilege turns. Consider models like Claude Haiku 4.5, which demonstrated strong inherent resistance.
Key insights
Handlebars' default HTML escaping offers limited protection against structural role injection in LLM prompts.
Principles
- Escaping only protects specific delimiter characters.
- Structural separation is crucial for prompt security.
- Some LLMs inherently resist role injection better.
Method
The study conducted 5760 trials across seven delimiter families, two attack objectives, and four LLMs to analyze structural role injection vulnerabilities in Handlebars-templated prompts.
In practice
- Test LLM prompts with various delimiter types.
- Implement strict input validation for user data.
- Prioritize structural separation over escaping.
Topics
- LLM Prompt Engineering
- Handlebars Templating
- Structural Role Injection
- Prompt Security
- HTML Escaping
- Large Language Models
Best for: CTO, VP of Engineering/Data, Director of AI/ML, AI Security Engineer, AI Engineer, AI Scientist
Related on AIssential
Editorial summary, takeaway, and curation by AIssential. Original article published by Artificial Intelligence.