XSS via context-escaping bypass in Stempler {{ }} output syntax
Summary
Spiral's default template engine, Stempler, advertises {{ ... }} as the auto-escaping (safe) output syntax and implements context-aware escaping so that the same expression is encoded correctly for HTML text, quoted attributes, and <script> blocks. For JavaScript event-handler attributes (onclick, onmouseover, and every other on*), the context-aware branch encodes the value with htmlspecialchars() and wraps it in literal " characters. That is the wrong encoding for a JavaScript execution context. A browser HTML-entity-decodes an event-handler attribute value before the JavaScript engine parses it, so the injected " becomes a real " and any quote that htmlspecialchars encoded in the user value is decoded straight back, letting the value break out of the JavaScript string and execute. An application that places user-controlled data into an on* handler through the framework's own {{ }} syntax, which is the pattern the engine explicitly supports, is exposed to reflected or stored XSS with no misuse of the raw output filter.
The bug is a single wrong encoder in one branch of the finalizer: the <script> context is already handled correctly with json_encode(... JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT), so the framework has the correct primitive and simply applies HTML-entity encoding instead of JavaScript encoding to on* attributes.
Details
src/Stempler/src/Transform/Finalizer/DynamicToPHP.php:112, method getFilterContext()
// php {{ }} in javascript code (variable passing), use {! !} to bypass the filter
if ($context[0] instanceof Verbatim && $context[1] instanceof Tag && $context[1]->name === 'script') {
return \sprintf(
'json_encode(%s, %s, %s)',
'%s',
'JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT',
'512',
);
}
// in on* and other attributes
if ($context[0] instanceof Verbatim && $context[1] instanceof Attr && $context[1]->name !== 'style') {
return \sprintf("'%s', %s, '%s'", '"', $this->defaultFilter, '"');
}
$this->defaultFilter is htmlspecialchars((string) (%s), ENT_QUOTES | ENT_SUBSTITUTE, 'utf-8') (src/Stempler/src/Compiler/Renderer/DynamicRenderer.php:17).
The <script> branch is correct: json_encode with the JSON_HEX_* flags emits a JavaScript string literal in which ", ', <, & are hex-escaped (", ', ...), which survives both HTML and JavaScript parsing. The on* branch is wrong: it produces '"' . htmlspecialchars($value) . '"', i.e. HTML-entity encoding for a value that the browser will decode and then execute as JavaScript.
Why HTML encoding fails in an event-handler context
An on* attribute is JavaScript, but it is delivered inside an HTML attribute, so the HTML parser decodes entities first and the JavaScript engine parses the decoded result. htmlspecialchars(ENT_QUOTES) turns a user " into " and ' into ', and the framework's own wrapper adds two more ". The browser decodes every " back to " and every ' back to ' before running the handler, so the user's quote characters reappear intact in the JavaScript string and can terminate it.
Developer template (uses the safe {{ }} syntax, not the raw {!! !!} filter)
<button onclick="greet({{ $name }})">Say hi</button>
Controller: views->render('page', ['name' => $request->query('name')]).
Framework-emitted compiled PHP
<button onclick="greet(<?php echo '"', htmlspecialchars((string) ($name), ENT_QUOTES | ENT_SUBSTITUTE, 'utf-8'), '"'; ?>)">Say hi</button>
Impact
- Reflected or Stored XSS could allow an attacker to steal authentication tokens, which is considered the primary impact.
Suggested fix
- Encode the
on*(and, in general, any JavaScript-execution) attribute context with the same JavaScript-aware encoder already used for<script>, i.e.json_encode($value, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT), and drop the manual"wrapper (the JSON encoder supplies the string delimiters). This yields a value that is safe after both HTML and JavaScript decoding. - If a distinct HTML-attribute-plus-JavaScript encoder is preferred, escape the user data to a form that survives HTML-entity decoding, for example encode quotes and backslashes as JavaScript
\uXXXXescapes rather than HTML entities. - Consider narrowing the current attribute branch: HTML-entity encoding is correct for ordinary quoted attributes (
title,data-*,alt), so the fix is specifically to routeon*handlers (andhref/srcvalues whose scheme isjavascript:) through the JavaScript encoder. - Documentation should state that
{{ }}inside an event handler is only safe once the encoder is JavaScript-aware, and that until then developers must not place user data inon*attributes.