security.rst
This file
Path
docs/security.rst
106 lines · 4.6 KiB
Last changed by
Change to Pallets projectJosé Carlos Garcíabefore adoption567fff9
History of this fileTake it
The walk stopped after 200 commits: lines older than eca5306 point at it.
Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 1–75why, one step back1Security Considerations2=======================34Web applications usually face all kinds of security problems and it's very5hard to get everything right. Flask tries to solve a few of these things6for you, but there are a couple more you have to take care of yourself.78.. _xss:910Cross-Site Scripting (XSS)11--------------------------1213Cross site scripting is the concept of injecting arbitrary HTML (and with14it JavaScript) into the context of a website. To remedy this, developers15have to properly escape text so that it cannot include arbitrary HTML16tags. For more information on that have a look at the Wikipedia article17on `Cross-Site Scripting18<http://en.wikipedia.org/wiki/Cross-site_scripting>`_.1920Flask configures Jinja2 to automatically escape all values unless21explicitly told otherwise. This should rule out all XSS problems caused22in templates, but there are still other places where you have to be23careful:2425- generating HTML without the help of Jinja226- calling :class:`~flask.Markup` on data submitted by users27- sending out HTML from uploaded files, never do that, use the28 ``Content-Disposition: attachment`` header to prevent that problem.29- sending out textfiles from uploaded files. Some browsers are using30 content-type guessing based on the first few bytes so users could31 trick a browser to execute HTML.3233Another thing that is very important are unquoted attributes. While34Jinja2 can protect you from XSS issues by escaping HTML, there is one35thing it cannot protect you from: XSS by attribute injection. To counter36this possible attack vector, be sure to always quote your attributes with37either double or single quotes when using Jinja expressions in them:3839.. sourcecode:: html+jinja4041 <a href="{{ href }}">the text</a>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)5051When the user would then move with the mouse over the link, 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.5758Cross-Site Request Forgery (CSRF)59---------------------------------6061Another big problem is CSRF. This is a very complex topic and I won't62outline it here in detail just mention what it is and how to theoretically63prevent it.6465If your authentication information is stored in cookies, you have implicit66state management. The state of "being logged in" is controlled by a67cookie, and that cookie is sent with each request to a page.68Unfortunately that includes requests triggered by 3rd party sites. If you69don't keep that in mind, some people might be able to trick your70application's users with social engineering to do stupid things without71them knowing.7273Say you have a specific URL that, when you sent ``POST`` requests to will74delete a user's profile (say ``http://example.com/user/delete``). If an75attacker now creates a page that sends a post request to that page withMerge pull request #1750 from erogers6264/masterMarkus Unterwaditzerbefore adoptionbdadd97
line 76why, one step back76some JavaScript they just have to trick some users to load that page andMerge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
lines 77–97why, one step back77their profiles will end up being deleted.7879Imagine you were to run Facebook with millions of concurrent users and80someone would send out links to images of little kittens. When users81would go to that page, their profiles would get deleted while they are82looking at images of fluffy cats.8384How can you prevent that? Basically for each request that modifies85content on the server you would have to either use a one-time token and86store that in the cookie **and** also transmit it with the form data.87After receiving the data on the server again, you would then have to88compare the two tokens and ensure they are equal.8990Why does Flask not do that for you? The ideal place for this to happen is91the form validation framework, which does not exist in Flask.9293.. _json-security:9495JSON Security96-------------97Merge pull request #1671 from jeffwidman/support-json-top-level-arraysDavid Lordbefore adoption431db28
lines 98–99why, one step back98In Flask 0.10 and lower, :func:`~flask.jsonify` did not serialize top-level99arrays to JSON. This was because of a security vulnerability in ECMAScript 4.Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINEMarkus Unterwaditzerbefore adoptioneca5306
line 100why, one step back100Merge pull request #1671 from jeffwidman/support-json-top-level-arraysDavid Lordbefore adoption431db28
lines 101–103why, one step back101ECMAScript 5 closed this vulnerability, so only extremely old browsers are102still vulnerable. All of these browsers have `other more serious103vulnerabilitiesChange to Pallets projectJosé Carlos Garcíabefore adoption567fff9
line 104why, one step back104<https://github.com/pallets/flask/issues/248#issuecomment-59934857>`_, soMerge pull request #1671 from jeffwidman/support-json-top-level-arraysDavid Lordbefore adoption431db28
lines 105–106why, one step back105this behavior was changed and :func:`~flask.jsonify` now supports serializing106arrays.