Study guide · XSS, Fluid and content security policy
Preventing XSS with Fluid, TypoScript and a content security policy
Cross-site scripting, XSS for short, is one of the most common holes in websites. An attacker gets their own JavaScript code into a page, and the victim's browser runs it. You learn how Fluid protects you by itself, how that protection gets bypassed and where TypoScript sets traps. After that comes the content security policy as a second layer, and foreign scripts. The running example is the website of a sports club with a guestbook and a search field.
What XSS is and what it does
With XSS, foreign code ends up in the HTML of your page. There are two basic forms:
- With reflected XSS, the code is in the URL. The page sends it straight back in the response. So the victim must open a prepared link.
- With stored XSS, the code is permanently on the server, for example in a guestbook entry. It hits everyone who opens the page.
The script runs in the victim's browser and uses their session. It can read data and CSRF tokens and perform actions on behalf of the victim, up to taking over the session. XSS does not directly change anything on the server, such as files or the database. But if the script hits an administrator, it can lead to a takeover of the whole installation through the admin's actions.
The cookie attribute HttpOnly only helps partly. It prevents a script from reading and stealing the session cookie through document.cookie. It does not close the hole. In the backend, for example, an injected script cannot read the cookie be_typo_user. But it can act on behalf of the logged-in user and read everything the backend displays. For the admin tools, TYPO3 also requires sudo mode, that is, entering the password again. The script does not know that password, and it does not know the Install Tool password either.
Fluid encodes by itself
When you output a variable in a Fluid template, such as the text of a guestbook entry, Fluid converts the HTML special characters. The angle bracket of a script tag becomes the entity for “less than”. The browser displays the text instead of running it. This automatic encoding is the most important protection against XSS. So the rule is: use the default output with escaping.
In Fluid 5, escaping does not depend on the file extension. The escape interceptor is always active, also in a mail template ending in txt or in a JSON output. You can only switch it off on purpose: for a single element with the ViewHelper f:format.raw, or for a whole template with an instruction at the very beginning.
{escaping off}That only makes sense if no value from an unsafe source ends up in the template. A JSON template with this line outputs all variables raw, including user input. Then you must correctly JSON-encode every value yourself. If the server also delivers the response with the wrong content type text/html, the browser interprets it as HTML. Injected markup then still takes effect.
Raw output and safe alternatives
The ViewHelper f:format.raw outputs content without any encoding. It must never receive user input, so nothing from forms, URLs or foreign sources. Only output raw HTML if it comes from a trusted source or has been sanitised beforehand. For the two typical cases there are safe ways:
- HTML from the rich text editor is output with
f:format.html. The ViewHelper passes the content through parseFunc, by default throughlib.parseFunc_RTE. Along the way, TYPO3 resolves internal links with the t3 scheme and filters unwanted markup. - Other HTML with allowed formatting is output with
f:sanitize.html. It filters the HTML with the package typo3/html-sanitizer. Only allowed tags, attributes and values remain. An attribute such as onmouseover is dropped.
By default, the class DefaultSanitizerBuilder in the namespace TYPO3, CMS, Core, Html defines which tags and attributes are allowed. With the option build you specify a custom builder, for example for additional allowed tags.
parseFunc protects as well: the setting htmlSanitize is active in TYPO3 14 unless something else is set. Setting it to 0 switches off the sanitizer.
If a template outputs the bodytext of a content element with f:format.raw instead of f:format.html, it bypasses parseFunc's HTML sanitizer. Injected scripts end up on the page unchecked, and internal links with the t3 scheme stay unresolved.