skip to content

In a prompt template pipeline, why would a ticket body containing `{plan_tier}` reach the model with the real tier filled in, and how do you fix it?

level: seniorimportance: should knowfreq 27%

answer

  1. a value that became source
  2. count the render passes
  3. values last, in one scan
  4. an escape matches a pass
  5. what the context exposes

basics

~20 s

A customer value was rendered twice: a later pass scanned earlier output as template source. Compose trusted template text first, then fill values once in a single scan, and never hand an untrusted string to a template engine as source.

solid answer

~50 s

A **render pass** treats whatever it scans as template source. If one pass fills `ticket_body` and a later pass scans the result, placeholder syntax the customer typed gets treated as real, so the ticket can pull another field into its own text or fail the render on an unknown name. The fix is ordering: compose the trusted template text first, then fill values last, in one scan whose output is never parsed again. Escaping is the fallback when a second pass can't be removed. It only stops the renderer reading that text as syntax, and does nothing about a sentence telling the model to ignore its rules, which is prompt injection. The serious form is passing an untrusted string as source to a general-purpose engine, where expressions can reach attributes and methods. That path needs a sandbox and a narrow context, or, better, should be removed.

code

pseudocode · 18 lines
pseudocode
# Trusted composition happens once, before any request value is used
source = compose(BASE_SECTION, LANGUAGE_SECTION[language], TEAM_SECTION[team])

# One scan: text copied from a value is never read as syntax
function fill_once(source, values):
  output = ""
  for token in parse(source):
    if token.is_literal:
      output += token.text
    else:
      output += values[token.name]   # raises if the name is absent
  return output

prompt = fill_once(source, values)

# Regression fixture: the literal text must survive unchanged
values = { plan_tier: "pro", ticket_body: "Why does my bill show {plan_tier}?" }
assert fill_once("Ticket: {ticket_body}", values) == "Ticket: Why does my bill show {plan_tier}?"

go deeper

for a junior

Know the difference between template source, which is parsed, and a value, which is copied. Customer text should only ever be a value.

for a middle

Trace the two passes on paper and show where the customer's text turns into syntax. Explain what an escape does, and why the need for one depends on how many passes exist.

for a senior

Remove the pass rather than stacking escapes, pin the fix with a literal-syntax fixture, and scale your concern with renderer power: substitution, loops, then attribute access.

for a principal

Decide whether customers may author templates at all. A sandbox is a risk you then operate, with limits and a narrow context, so weigh it against a substitution-only language.

## Source and value A render step handles its two inputs differently. **Template source** is parsed: placeholders and any other syntax in it are acted on. A **value** is copied into the output as characters. The bug in this question happens when text moves from one category to the other, and a **second render pass** is the usual way that happens. ```plaintext Source: Plan: {plan_tier} | Ticket: {ticket_body} ticket_body: Why does my bill show {plan_tier}? plan_tier: pro After pass 1: Plan: pro | Ticket: Why does my bill show {plan_tier}? After pass 2: Plan: pro | Ticket: Why does my bill show pro? ``` Pass 1 is correct. The customer's text is copied as it was typed. Pass 2 parses that output as source, so the customer's words are now syntax. ## How a second pass gets in Nobody designs this on purpose. It arrives through ordinary refactoring: - **Layered rendering.** A shared layer fills team and language wording, a later layer fills request values, and some request value ends up filled in the first layer. - **Wrapping.** A finished prompt goes into an outer template as part of its source rather than as a value. - **Splicing before the render.** A value is concatenated into the template string and then rendered. That is only one pass, and the value is source anyway. - **Model output.** Text from an earlier model call is pasted into the next template's source instead of being filled as a value. ## What the text can do The damage depends on how much the renderer can do with source: | Renderer power | What a value read as source can do | |---|---| | Named substitution only | Copy another field into its own text, or fail the render on an unknown name | | Conditionals and loops | Also consume CPU and memory: one engine's documentation warns that a small template can render a very large output | | Attribute access and method calls | Also read or call whatever the context objects expose | The last row is known in web security as **server-side template injection**. The first row is the one you usually meet first, and it needs no attacker: logs, code and JSON pasted into a ticket are full of braces, so an ordinary customer can fail a render if a pass parses that text. ## The fix 1. **Compose from trusted fragments only.** Assemble all template source, including team and language sections, before any request value is involved. 2. **Fill values last, in one scan.** Copy each value into the output and do not parse what was copied. 3. **Never parse the result again.** To nest prompts, pass the inner rendered text to the outer template as a value. 4. **Pin it with a test.** Render a fixture whose ticket body contains literal placeholder syntax, and assert that it comes through unchanged. Otherwise a later refactor can bring the pass back unnoticed. ## What an escape does An **escape rule** marks text as literal for a pass, so the renderer does not read it as syntax. It has a narrow job: - **In fixed text,** it lets a template author write a literal brace. - **In a value,** it is needed only if a later pass exists. Each extra pass needs its own level, and a wrong count either shows stray escape characters to the model or reopens the bug. Removing the pass is simpler. - **HTML escaping is a different job.** It rewrites characters such as `<` and `&` for a browser. In a text prompt it alters quoted code and other content the model reads, so the setting is worth checking when an engine built for web pages renders prompts. No render escape touches meaning. A sentence telling the model to ignore its rules contains no syntax to escape, so it passes through any of these rules intact. That attack has its own mitigation layers, and delimiter collisions are their own subject too. ## When the template itself is untrusted Some products let customers write their own prompt templates, so an untrusted string is source by design. A **sandbox** is the usual answer in a general-purpose engine. One engine's documentation says its sandbox can intercept attribute access, method calls, operators, data mutation and string formatting. It also says the sandbox alone is not a solution for perfect security, and it asks you to: - catch errors raised during compile and render; - run with limits on CPU and memory, since small templates can render large output; - pass only the data the template needs, avoiding global data and objects whose methods have side effects. A sandbox also says nothing about what the rendered words tell the model. If customers need only named gaps, a substitution-only language is a smaller surface than a sandboxed general-purpose one.

  • Why can a customer's pasted log fail a prompt render with no malicious intent?
    Logs, code and JSON often contain the same braces a renderer may use for placeholders. If a ticket reaches a pass that parses it, a stray brace can read as a broken or unknown placeholder, so the render raises and the ticket fails for a reason unrelated to its content. Keep a fixture with pasted JSON in the test set.
  • How do you nest one rendered prompt inside another prompt template safely?
    Pass the inner rendered text to the outer template as a value, never as part of its source. The outer fill copies it as characters. If a library you call insists on parsing its input, escape the inner text for exactly that one pass, and add a test with literal placeholder syntax so a later refactor is caught.
  • Is a sandbox enough if customers may author their own prompt templates?
    Not by itself. One engine's documentation says its sandbox alone is not perfect security: it asks for CPU and memory limits, caught render errors, and only the data the template needs. The sandbox also has no view on what the rendered words tell the model. A substitution-only language is a smaller surface when customers don't need loops.

  • Escaping placeholder syntax in a value protects the prompt from prompt injection.
  • Only attackers trigger this, because normal customer text never contains template syntax.
  • A sandboxed template engine makes rendering untrusted template source completely safe.
  • HTML auto-escaping is the right setting for text sent to a language model.
  • Rendering twice is harmless because the second pass has nothing left to fill.
  • Source and values are both strings, so a template engine treats them alike.