Skip to content
Myr

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 file

Take 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 back
1Security Considerations
2=======================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 Scripting

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 18why, one step back
18<https://en.wikipedia.org/wiki/Cross-site_scripting>`_.

Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b

lines 19–27why, one step back
19
20Flask 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 the

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

line 28why, one step back
28 ``Content-Disposition: attachment`` header to prevent that problem.

Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b

lines 29–72why, one step back
29- sending out textfiles from uploaded files. Some browsers are using
30 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.72

Merge pull request #1240 from defuz/docs-style-fix-2Markus Unterwaditzerbefore adoption6fcc2ae

lines 73–74why, one step back
73Say you have a specific URL that, when you sent ``POST`` requests to will
74delete a user's profile (say ``http://example.com/user/delete``). If an

Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b

line 75why, one step back
75attacker now creates a page that sends a post request to that page with

resolve merge conflictsKenneth Reitzbefore adoption31174fe

line 76why, one step back
76some JavaScript they just have to trick some users to load that page and

Merge pull request #1052 from miguelgrinberg/masterKenneth Reitzbefore adoptionbd51d2b

lines 77–97why, one step back
77their 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-------------97

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 98–99why, one step back
98In Flask 0.10 and lower, :func:`~flask.jsonify` did not serialize top-level
99arrays 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 back
100

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 101–106why, one step back
101ECMAScript 5 closed this vulnerability, so only extremely old browsers are
102still 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 back
107
108Security Headers109----------------110

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 111–114why, one step back
111Browsers recognize various response headers in order to control security. We
112recommend 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 back
115

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 116–117why, one step back
116.. _Flask-Talisman: https://github.com/GoogleCloudPlatform/flask-talisman
117

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 118why, one step back
118HTTP Strict Transport Security (HSTS)

clean up security header docsDavid Lordbefore adoption8eff9bd

line 119why, one step back
119~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 120why, one step back
120

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 121–122why, one step back
121Tells the browser to convert all HTTP requests to HTTPS, preventing
122man-in-the-middle (MITM) attacks. ::

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 123why, one step back
123

clean up security header docsDavid Lordbefore adoption8eff9bd

line 124why, one step back
124 response.haders['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains'

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 125why, one step back
125

clean up security header docsDavid Lordbefore adoption8eff9bd

line 126why, one step back
126- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 127why, one step back
127

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 128–129why, one step back
128Content Security Policy (CSP)
129~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 130why, one step back
130

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 131–133why, one step back
131Tell the browser where it can load various types of resource from. This header
132should 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 back
134

clean up security header docsDavid Lordbefore adoption8eff9bd

line 135why, one step back
135 response.headers['Content-Security-Policy'] = "default-src: 'self'"

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 136why, one step back
136

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 137–138why, one step back
137- https://csp.withgoogle.com/docs/index.html
138- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 139why, one step back
139

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 140–141why, one step back
140X-Content-Type-Options
141~~~~~~~~~~~~~~~~~~~~~~

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 142why, one step back
142

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 143–145why, one step back
143Forces the browser to honor the response content type instead of trying to
144detect 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 back
146

clean up security header docsDavid Lordbefore adoption8eff9bd

line 147why, one step back
147 response.headers['X-Content-Type-Options'] = 'nosniff'

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 148why, one step back
148

clean up security header docsDavid Lordbefore adoption8eff9bd

line 149why, one step back
149- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 150why, one step back
150

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 151–152why, one step back
151X-Frame-Options
152~~~~~~~~~~~~~~~

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 153why, one step back
153

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 154–157why, one step back
154Prevents external sites from embedding your site in an ``iframe``. This
155prevents 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 back
158

clean up security header docsDavid Lordbefore adoption8eff9bd

line 159why, one step back
159 response.headers['X-Frame-Options'] = 'SAMEORIGIN'

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 160why, one step back
160

clean up security header docsDavid Lordbefore adoption8eff9bd

line 161why, one step back
161- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 162why, one step back
162

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 163–164why, one step back
163X-XSS-Protection
164~~~~~~~~~~~~~~~~

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 165why, one step back
165

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 166–168why, one step back
166The browser will try to prevent reflected XSS attacks by not loading the page
167if 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 back
169

clean up security header docsDavid Lordbefore adoption8eff9bd

line 170why, one step back
170 response.headers['X-XSS-Protection'] = '1; mode=block'

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 171why, one step back
171

clean up security header docsDavid Lordbefore adoption8eff9bd

line 172why, one step back
172- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 173why, one step back
173

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 174–175why, one step back
174Set-Cookie options
175~~~~~~~~~~~~~~~~~~

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 176why, one step back
176

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 177–179why, one step back
177These options can be added to a ``Set-Cookie`` header to improve their
178security. 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 back
180

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 181–185why, one step back
181- ``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 back
186

clean up security header docsDavid Lordbefore adoption8eff9bd

line 187why, one step back
187::

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 188why, one step back
188

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 189–192why, one step back
189 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 back
193

clean up security header docsDavid Lordbefore adoption8eff9bd

line 194why, one step back
194 response.set_cookie('username', 'flask', secure=True, httponly=True)

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 195why, one step back
195

clean up security header docsDavid Lordbefore adoption8eff9bd

line 196why, one step back
196- https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#Secure_and_HttpOnly_cookies

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 197why, one step back
197

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 198–199why, one step back
198HTTP Public Key Pinning (HPKP)
199~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 200why, one step back
200

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 201–202why, one step back
201This tells the browser to authenticate with the server using only the specific
202certificate key to prevent MITM attacks.

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 203why, one step back
203

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 204–206why, one step back
204.. 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 back
207

clean up security header docsDavid Lordbefore adoption8eff9bd

line 208why, one step back
208- https://developer.mozilla.org/en-US/docs/Web/HTTP/Public_Key_Pinning