Templates, Expressions, and Interpreter Boundaries
Compare editable message text with template source, then inspect separate expression-evaluation and directory-filter examples.
You’re reading a preview. Premium includes the full lesson and specialist practice. View the full curriculum.
A message editor should let someone change the words in a message. Suppose its preview treats the entire message as a server template instead. Text such as <%= 7 * 7 %> may now be read as a template instruction.
We'll begin with template injection: editable text becomes template source instead of a value placed into an application-owned template.
Two later examples use different interpreters: a PHP expression evaluator and an LDAP directory filter. We'll inspect each separately before comparing the shared mistake. An HTML encoder, shell argument array, and directory-value encoder do different jobs.
Templates: keep editable text in a value
A server-owned template can display an external message safely as a value under the framework's normal output handling. A function accepting the entire external message as template source gives it a different capability: the template engine may evaluate expressions, access objects, invoke helpers, or execute code according to its language and configuration.
# Unsafe original preview outline.
def preview
require_authenticated_editor!
template = params[:banner_template]
render inline: template
end
A harmless expression such as a template arithmetic marker can demonstrate server interpretation in an isolated fixture. Determine the actual engine and permitted constructs; not every expression language has the same reach. In a Rails ERB context, arbitrary editable source has a very different power from escaped text in a static view.
Select the handoffs and identify the precise interpreter.
Author. The user is allowed to edit message text, not server source.
{"banner_template":"customer-controlled text"}
Persist. Storage and queues preserve the external writer’s influence.
const saved = await messages.load(job.messageId);
Interpret. Inline rendering or eval grants the new execution capability.
render inline: saved.body
Review question: Keep source server-owned at every consumer, including delayed workers.