Skip to content
Myr

Merge pull request #2364 from davidism/xss-note

Before adoption: this commit came in with the history the project adopted, so no landing records it.

mention csp for javascript: uri xss

authored by David Lord · committed by GitHub · parents 4594602, 15756da · Browse files · 1 file+17 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+17 added−4 removed

  • other:+17 added−4 removed

1 file+17 added−4 removed

modified:docs/security.rst+17 added-4 removed
36 unmodified lines
37either double or single quotes when using Jinja expressions in them:3839.. sourcecode:: html+jinja4041 <a href="{{ href }}">the text</a>41 <input value="{{ value }}">4243Why is this necessary? Because if you would not be doing that, an44attacker could easily inject custom JavaScript handlers. For example an45attacker could inject this piece of HTML+JavaScript:4647.. sourcecode:: html4849 onmouseover=alert(document.cookie)49 onmouseover=alert(document.cookie)5051When the user would then move with the mouse over the link, the cookie51When the user would then move with the mouse over the input, the cookie52would be presented to the user in an alert window. But instead of showing53the cookie to the user, a good attacker might also execute any other54JavaScript code. In combination with CSS injections the attacker might55even make the element fill out the entire page so that the user would56just have to have the mouse anywhere on the page to trigger the attack.5758There is one class of XSS issues that Jinja's escaping does not protect59against. The ``a`` tag's ``href`` attribute can contain a `javascript:` URI,60which the browser will execute when clicked if not secured properly.6162.. sourcecode:: html6364 <a href="{{ value }}">click here</a>65 <a href="javascript:alert('unsafe');">click here</a>6667To prevent this, you'll need to set the :ref:`security-csp` response header.6869Cross-Site Request Forgery (CSRF)70---------------------------------7172Another big problem is CSRF. This is a very complex topic and I won't
62 unmodified lines
135 response.haders['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains'136137- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security138139.. _security-csp:140141Content Security Policy (CSP)142~~~~~~~~~~~~~~~~~~~~~~~~~~~~~143144Tell the browser where it can load various types of resource from. This header145should be used whenever possible, but requires some work to define the correct146policy for your site. A very strict policy would be::147135 response.headers['Content-Security-Policy'] = "default-src: 'self'"148 response.headers['Content-Security-Policy'] = "default-src 'self'"149150- https://csp.withgoogle.com/docs/index.html151- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy152
69 unmodified lines