security.rst
This file
Path
docs/security.rst
208 lines · 8.0 KiB
Last changed by
clean up security header docsDavid Lordbefore adoption8eff9bd
History of this fileTake it
The walk stopped after 200 commits: lines older than bd51d2b point at it.
Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
lines 1–17why, 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 Scriptingresolve merge conflictsKenneth Reitzbefore adoption31174fe
line 18why, one step back18<https://en.wikipedia.org/wiki/Cross-site_scripting>`_.Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
lines 19–27why, one step back1920Flask 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 theMerge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
line 28why, one step back28 ``Content-Disposition: attachment`` header to prevent that problem.Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
lines 29–72why, one step back29- 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.72Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae
lines 73–74why, one step back73Say you have a specific URL that, when you sent ``POST`` requests to will74delete a user's profile (say ``http://example.com/user/delete``). If anMerge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
line 75why, one step back75attacker now creates a page that sends a post request to that page withresolve merge conflictsKenneth Reitzbefore adoption31174fe
line 76why, one step back76some JavaScript they just have to trick some users to load that page andMerge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
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-------------97resolve merge conflictsKenneth Reitzbefore adoption31174fe
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 #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b
line 100why, one step back100resolve merge conflictsKenneth Reitzbefore adoption31174fe
lines 101–106why, one step back101ECMAScript 5 closed this vulnerability, so only extremely old browsers are102still vulnerable. All of these browsers have `other more serious103vulnerabilities104<https://github.com/pallets/flask/issues/248#issuecomment-59934857>`_, so105this behavior was changed and :func:`~flask.jsonify` now supports serializing106arrays.Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
lines 107–110why, one step back107108Security Headers109----------------110clean up security header docsDavid Lordbefore adoption8eff9bd
lines 111–114why, one step back111Browsers recognize various response headers in order to control security. We112recommend reviewing each of the headers below for use in your application.113The `Flask-Talisman`_ extension can be used to manage HTTPS and the security114headers for you.Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 115why, one step back115clean up security header docsDavid Lordbefore adoption8eff9bd
lines 116–117why, one step back116.. _Flask-Talisman: https://github.com/GoogleCloudPlatform/flask-talisman117Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 118why, one step back118HTTP Strict Transport Security (HSTS)clean up security header docsDavid Lordbefore adoption8eff9bd
line 119why, one step back119~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 120why, one step back120clean up security header docsDavid Lordbefore adoption8eff9bd
lines 121–122why, one step back121Tells the browser to convert all HTTP requests to HTTPS, preventing122man-in-the-middle (MITM) attacks. ::Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 123why, one step back123clean up security header docsDavid Lordbefore adoption8eff9bd
line 124why, one step back124 response.haders['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains'Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 125why, one step back125clean up security header docsDavid Lordbefore adoption8eff9bd
line 126why, one step back126- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-SecurityMerge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 127why, one step back127clean up security header docsDavid Lordbefore adoption8eff9bd
lines 128–129why, one step back128Content Security Policy (CSP)129~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 130why, one step back130clean up security header docsDavid Lordbefore adoption8eff9bd
lines 131–133why, one step back131Tell the browser where it can load various types of resource from. This header132should be used whenever possible, but requires some work to define the correct133policy for your site. A very strict policy would be::Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 134why, one step back134clean up security header docsDavid Lordbefore adoption8eff9bd
line 135why, one step back135 response.headers['Content-Security-Policy'] = "default-src: 'self'"Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 136why, one step back136clean up security header docsDavid Lordbefore adoption8eff9bd
lines 137–138why, one step back137- https://csp.withgoogle.com/docs/index.html138- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-PolicyMerge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 139why, one step back139clean up security header docsDavid Lordbefore adoption8eff9bd
lines 140–141why, one step back140X-Content-Type-Options141~~~~~~~~~~~~~~~~~~~~~~Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 142why, one step back142clean up security header docsDavid Lordbefore adoption8eff9bd
lines 143–145why, one step back143Forces the browser to honor the response content type instead of trying to144detect it, which can be abused to generate a cross-site scripting (XSS)145attack. ::Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 146why, one step back146clean up security header docsDavid Lordbefore adoption8eff9bd
line 147why, one step back147 response.headers['X-Content-Type-Options'] = 'nosniff'Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 148why, one step back148clean up security header docsDavid Lordbefore adoption8eff9bd
line 149why, one step back149- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-OptionsMerge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 150why, one step back150clean up security header docsDavid Lordbefore adoption8eff9bd
lines 151–152why, one step back151X-Frame-Options152~~~~~~~~~~~~~~~Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 153why, one step back153clean up security header docsDavid Lordbefore adoption8eff9bd
lines 154–157why, one step back154Prevents external sites from embedding your site in an ``iframe``. This155prevents a class of attacks where clicks in the outer frame can be translated156invisibly to clicks on your page's elements. This is also known as157"clickjacking". ::Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 158why, one step back158clean up security header docsDavid Lordbefore adoption8eff9bd
line 159why, one step back159 response.headers['X-Frame-Options'] = 'SAMEORIGIN'Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 160why, one step back160clean up security header docsDavid Lordbefore adoption8eff9bd
line 161why, one step back161- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-OptionsMerge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 162why, one step back162clean up security header docsDavid Lordbefore adoption8eff9bd
lines 163–164why, one step back163X-XSS-Protection164~~~~~~~~~~~~~~~~Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 165why, one step back165clean up security header docsDavid Lordbefore adoption8eff9bd
lines 166–168why, one step back166The browser will try to prevent reflected XSS attacks by not loading the page167if the request contains something that looks like JavaScript and the response168contains the same data. ::Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 169why, one step back169clean up security header docsDavid Lordbefore adoption8eff9bd
line 170why, one step back170 response.headers['X-XSS-Protection'] = '1; mode=block'Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 171why, one step back171clean up security header docsDavid Lordbefore adoption8eff9bd
line 172why, one step back172- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-ProtectionMerge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 173why, one step back173clean up security header docsDavid Lordbefore adoption8eff9bd
lines 174–175why, one step back174Set-Cookie options175~~~~~~~~~~~~~~~~~~Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 176why, one step back176clean up security header docsDavid Lordbefore adoption8eff9bd
lines 177–179why, one step back177These options can be added to a ``Set-Cookie`` header to improve their178security. Flask has configuration options to set these on the session cookie.179They can be set on other cookies too.Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 180why, one step back180clean up security header docsDavid Lordbefore adoption8eff9bd
lines 181–185why, one step back181- ``Secure`` limits cookies to HTTPS traffic only.182- ``HttpOnly`` protects the contents of cookies from being read with183 JavaScript.184- ``SameSite`` ensures that cookies can only be requested from the same185 domain that created them. It is not supported by Flask yet.Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 186why, one step back186clean up security header docsDavid Lordbefore adoption8eff9bd
line 187why, one step back187::Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 188why, one step back188clean up security header docsDavid Lordbefore adoption8eff9bd
lines 189–192why, one step back189 app.config.update(190 SESSION_COOKIE_SECURE=True,191 SESSION_COOKIE_HTTPONLY=True,192 )Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 193why, one step back193clean up security header docsDavid Lordbefore adoption8eff9bd
line 194why, one step back194 response.set_cookie('username', 'flask', secure=True, httponly=True)Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 195why, one step back195clean up security header docsDavid Lordbefore adoption8eff9bd
line 196why, one step back196- https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#Secure_and_HttpOnly_cookiesMerge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 197why, one step back197clean up security header docsDavid Lordbefore adoption8eff9bd
lines 198–199why, one step back198HTTP Public Key Pinning (HPKP)199~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 200why, one step back200clean up security header docsDavid Lordbefore adoption8eff9bd
lines 201–202why, one step back201This tells the browser to authenticate with the server using only the specific202certificate key to prevent MITM attacks.Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 203why, one step back203clean up security header docsDavid Lordbefore adoption8eff9bd
lines 204–206why, one step back204.. warning::205 Be careful when enabling this, as it is very difficult to undo if you set up206 or upgrade your key incorrectly.Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f
line 207why, one step back207clean up security header docsDavid Lordbefore adoption8eff9bd
line 208why, one step back208- https://developer.mozilla.org/en-US/docs/Web/HTTP/Public_Key_Pinning