Template security and wash
The template language does not escape what it prints. {$value} sends the value as it is, so a name with <script> in it would run in every visitor's browser. Escaping is your job, and the operator for it is wash.
What to wash
Wash every value that is text from outside the template:
- names and text attributes of content: {$node.name|wash}, {$node.data_map.title.content|wash};
- anything from the address or a form: view parameters, search words, $module_result.uri;
- names of users, sections, classes and other things editors type.
Do not wash what is already HTML, or it shows up as tags on the page:
- the output of attribute_view_gui, node_view_gui and {include}: their templates escape what they print;
- rich text rendered to HTML (.content.output.output_text of an XML block);
- $module_result.content in the pagelayout;
- the result of ezurl, ezimage, ezdesign and ezroot, which are built for an attribute (see URL handling).
The modes of wash
| Call | For | Does |
|---|---|---|
| wash or wash( 'xhtml' ) | HTML text and attribute values | Turns &, <, >, " and ' into entities. |
| wash( 'javascript' ) | a value inside a JavaScript string literal | Escapes backslashes, both quotes, <, >, & and line breaks, so the value can neither end the string nor close the script element. |
| wash( 'email' ) | an e-mail address shown on a page | Escapes like xhtml, then replaces the @ and the dots with the texts of template.ini [WashSettings] EmailAtText and EmailDotText, to make harvesting harder. |
| wash( 'pdf' ) | text in the PDF export | Prepares text for the PDF library. |
Run on Exponential 6.0.15:
{def $text = 'hello < world & "you"'}
{$text|wash} {* hello < world & "you" *}
{undef $text}
<input type="text" name="q" value="{$search_text|wash}" />
<script>
var title = '{$node.name|wash( 'javascript' )}';
</script>
In a script element use wash( 'javascript' ), not the HTML mode: the browser does not decode entities there. Better still, pass values to scripts through a data- attribute with the normal wash, and read them in a script file.
Other rules
- Never pass a view parameter or form value unchecked to a fetch: test it, or cast it with int() where a number is expected.
- Keep the literal class html of rich text disabled (content.ini [literal] AvailableClasses), as it is by default: it lets editors put raw HTML and scripts into pages.
- Remove attribute and dump before a template goes live: they show data visitors should not see.
- ./console exp:template:check --level=strict checks templates against the security policy of their engine; see Debugging templates.
In Smarty and Twig templates
Smarty and Twig templates in Exponential do not escape automatically either, to behave like .tpl: use the same wash, as a modifier ({$node.name|wash}) or a filter ({{ node.name|wash }}). Both run in a sandbox that limits what a template can call. See Twig: security and escaping and Smarty: security.
Every parameter of the operator: wash.
Contributors
- Graham Brookins Updated Maintainer
Comments
Add a comment