Security
Escaping by context, and templates as code
Escaping by context
{{ $x }} escapes the value with htmlspecialchars (ENT_QUOTES). That is right for HTML text and for quoted attribute values, but not enough in URLs and JavaScript. Use the modifier that matches the place:
| Where the value goes | Write |
|---|---|
| HTML text, quoted attribute values | {{ $x }} |
<textarea> | {{ $x }} (the browser shows the original text) |
href / src / action / formaction taking a URL from data | href="{{ $x|feggUrl }}" |
Inside <script> | var x = {{ $x|feggJs|noescape }}; (no quotes around it) |
Event attributes (onclick …) | onclick="go({{ $x|feggJs }})" (no quotes around it, keep the escaping) |
Trusted HTML (CMS rich text saved through HtmlSanitizer::clean(), messages from your config) | {{ $x|noescape }} |
| A YouTube URL or a Google map from a CMS field | {{ $x|feggYoutube|noescape }}, {{ $x|feggMap|noescape }} (the modifier checks and escapes the URL) |
Always quote attribute values (class="{{ $x }}", not class={{ $x }}).
./fegg-cli template:lint reports outputs in <script>, event attributes and URLs that lack the right modifier (see Lint and errors).
URLs: feggUrl
HTML escaping does not stop a javascript: URL. feggUrl returns # for any URL whose scheme is not http, https, mailto or tel (also when the scheme is disguised with spaces or control characters). Relative URLs are kept.
<a href="{{ $page.data.link|feggUrl }}">{{ $page.data.link_label }}</a>
URLs built by the CMS modifiers (getImageUrl, getImageUrlFromDB, getFileUrl, getFileRawUrl) and values from $site (your configuration) do not need it.
JavaScript: feggJs
feggJs turns any value (string, number, array, object, null) into a JavaScript literal and encodes <, >, &, ' and ", so the value can neither end the string nor close the <script> element. It writes the quotes itself: do not add your own.
<script>
var title = {{ $page.title|feggJs|noescape }};
</script>
<button type="button" onclick="showItem({{ $item.id|feggJs }})">Show</button>
In an attribute, the browser decodes the HTML escaping before it runs the JavaScript, so the normal {{ }} escaping must stay: never add noescape there.
Arrays and objects work too, so a whole data set can go to a script in one line:
<script>
var items = {{ $page.items|feggJs|noescape }}; // [{"id":1,"title":"..."}, ...]
</script>
noescape
noescape outputs the value as it is. Use it only for HTML you trust: CMS rich text written by administrators and cleaned with Fegg\Cms\HtmlSanitizer::clean() when it was saved (see CMS screens and the editor), validation messages from your config, the result of feggJs inside <script>, or the result of feggYoutube / feggMap. Never use it on $in (request parameters) or anything a visitor can type.
Templates are code
{{ code }}, conditions, {{ assign }} and modifiers run arbitrary PHP. Treat templates like PHP files:
- Only developers may edit templates. Never let site users upload or edit
.tplfiles. - Never build a template name, an
includepath or a modifier name from user input. - A controller that picks the template from its URL arguments implements
Fegg\Http\RouteOnly, so onlysite/config/route.phpreaches it (see Route-only controllers).