Skip to content
Myr

security.rst

This file

Path

docs/security.rst

250 lines · 9.5 KiB

Last changed by

fix linksDavid Lordbefore adoption97ad085

History of this file

Take it

The walk stopped after 200 commits: lines older than afb613c point at it.

Added some missing documentation information.Armin Ronacherbefore adoptionafb613c

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>`_.

Added some missing documentation information.Armin Ronacherbefore adoptionafb613c

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.

Added some missing documentation information.Armin Ronacherbefore adoptionafb613c

lines 29–40why, 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+jinja40

Merge pull request #2364 from davidism/xss-noteDavid Lordbefore adoption9d82993

line 41why, one step back
41 <input value="{{ value }}">

Added some missing documentation information.Armin Ronacherbefore adoptionafb613c

lines 42–48why, one step back
42
43Why 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:: html48

Merge pull request #2364 from davidism/xss-noteDavid Lordbefore adoption9d82993

line 49why, one step back
49 onmouseover=alert(document.cookie)

Added some missing documentation information.Armin Ronacherbefore adoptionafb613c

line 50why, one step back
50

Merge pull request #2364 from davidism/xss-noteDavid Lordbefore adoption9d82993

line 51why, one step back
51When the user would then move with the mouse over the input, the cookie

Added some missing documentation information.Armin Ronacherbefore adoptionafb613c

lines 52–57why, one step back
52would be presented to the user in an alert window. But instead of showing
53the 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.57

Merge pull request #2364 from davidism/xss-noteDavid Lordbefore adoption9d82993

lines 58–68why, one step back
58There is one class of XSS issues that Jinja's escaping does not protect
59against. 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.68

Added some missing documentation information.Armin Ronacherbefore adoptionafb613c

lines 69–83why, one step back
69Cross-Site Request Forgery (CSRF)
70---------------------------------7172Another big problem is CSRF. This is a very complex topic and I won't73outline it here in detail just mention what it is and how to theoretically74prevent it.7576If your authentication information is stored in cookies, you have implicit77state management. The state of "being logged in" is controlled by a78cookie, and that cookie is sent with each request to a page.79Unfortunately that includes requests triggered by 3rd party sites. If you80don't keep that in mind, some people might be able to trick your81application's users with social engineering to do stupid things without82them knowing.83

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

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

Added some missing documentation information.Armin Ronacherbefore adoptionafb613c

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

resolve merge conflictsKenneth Reitzbefore adoption31174fe

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

Added some missing documentation information.Armin Ronacherbefore adoptionafb613c

lines 88–108why, one step back
88their profiles will end up being deleted.
8990Imagine you were to run Facebook with millions of concurrent users and91someone would send out links to images of little kittens. When users92would go to that page, their profiles would get deleted while they are93looking at images of fluffy cats.9495How can you prevent that? Basically for each request that modifies96content on the server you would have to either use a one-time token and97store that in the cookie **and** also transmit it with the form data.98After receiving the data on the server again, you would then have to99compare the two tokens and ensure they are equal.100101Why does Flask not do that for you? The ideal place for this to happen is102the form validation framework, which does not exist in Flask.103104.. _json-security:105106JSON Security107-------------108

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 109–110why, one step back
109In Flask 0.10 and lower, :func:`~flask.jsonify` did not serialize top-level
110arrays to JSON. This was because of a security vulnerability in ECMAScript 4.

Added some missing documentation information.Armin Ronacherbefore adoptionafb613c

line 111why, one step back
111

resolve merge conflictsKenneth Reitzbefore adoption31174fe

lines 112–117why, one step back
112ECMAScript 5 closed this vulnerability, so only extremely old browsers are
113still vulnerable. All of these browsers have `other more serious114vulnerabilities115<https://github.com/pallets/flask/issues/248#issuecomment-59934857>`_, so116this behavior was changed and :func:`~flask.jsonify` now supports serializing117arrays.

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

lines 118–121why, one step back
118
119Security Headers120----------------121

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 122–125why, one step back
122Browsers recognize various response headers in order to control security. We
123recommend reviewing each of the headers below for use in your application.124The `Flask-Talisman`_ extension can be used to manage HTTPS and the security125headers for you.

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 126why, one step back
126

clean up security header docsDavid Lordbefore adoption8eff9bd

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

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

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

clean up security header docsDavid Lordbefore adoption8eff9bd

line 130why, one step back
130~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 131why, one step back
131

clean up security header docsDavid Lordbefore adoption8eff9bd

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

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 134why, one step back
134

Fix typo in docs (#2419)asmith26before adoption8e2ef5b

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

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 136why, one step back
136

clean up security header docsDavid Lordbefore adoption8eff9bd

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

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 138why, one step back
138

Merge pull request #2364 from davidism/xss-noteDavid Lordbefore adoption9d82993

lines 139–140why, one step back
139.. _security-csp:
140

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 141–142why, one step back
141Content Security Policy (CSP)
142~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 143why, one step back
143

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 144–146why, one step back
144Tell the browser where it can load various types of resource from. This header
145should be used whenever possible, but requires some work to define the correct146policy for your site. A very strict policy would be::

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 147why, one step back
147

Merge pull request #2364 from davidism/xss-noteDavid Lordbefore adoption9d82993

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

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 149why, one step back
149

clean up security header docsDavid Lordbefore adoption8eff9bd

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

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 152why, one step back
152

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 153–154why, one step back
153X-Content-Type-Options
154~~~~~~~~~~~~~~~~~~~~~~

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 155why, one step back
155

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 156–158why, one step back
156Forces the browser to honor the response content type instead of trying to
157detect it, which can be abused to generate a cross-site scripting (XSS)158attack. ::

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 159why, one step back
159

clean up security header docsDavid Lordbefore adoption8eff9bd

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

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 161why, one step back
161

clean up security header docsDavid Lordbefore adoption8eff9bd

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

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 163why, one step back
163

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 164–165why, one step back
164X-Frame-Options
165~~~~~~~~~~~~~~~

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 166why, one step back
166

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 167–170why, one step back
167Prevents external sites from embedding your site in an ``iframe``. This
168prevents a class of attacks where clicks in the outer frame can be translated169invisibly to clicks on your page's elements. This is also known as170"clickjacking". ::

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 response.headers['X-Frame-Options'] = 'SAMEORIGIN'

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 173why, one step back
173

clean up security header docsDavid Lordbefore adoption8eff9bd

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

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 175why, one step back
175

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 176–177why, one step back
176X-XSS-Protection
177~~~~~~~~~~~~~~~~

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 178why, one step back
178

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 179–181why, one step back
179The browser will try to prevent reflected XSS attacks by not loading the page
180if the request contains something that looks like JavaScript and the response181contains the same data. ::

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 182why, one step back
182

clean up security header docsDavid Lordbefore adoption8eff9bd

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

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 184why, one step back
184

clean up security header docsDavid Lordbefore adoption8eff9bd

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

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 186why, one step back
186

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 187–188why, one step back
187Set-Cookie options
188~~~~~~~~~~~~~~~~~~

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 189why, one step back
189

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 190–192why, one step back
190These options can be added to a ``Set-Cookie`` header to improve their
191security. Flask has configuration options to set these on the session cookie.192They can be set on other cookies too.

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 193why, one step back
193

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 194–198why, one step back
194- ``Secure`` limits cookies to HTTPS traffic only.
195- ``HttpOnly`` protects the contents of cookies from being read with196 JavaScript.197- ``SameSite`` ensures that cookies can only be requested from the same198 domain that created them. It is not supported by Flask yet.

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 199why, one step back
199

clean up security header docsDavid Lordbefore adoption8eff9bd

line 200why, one step back
200::

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 201why, one step back
201

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 202–205why, one step back
202 app.config.update(
203 SESSION_COOKIE_SECURE=True,204 SESSION_COOKIE_HTTPONLY=True,205 )

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 206why, one step back
206

clean up security header docsDavid Lordbefore adoption8eff9bd

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

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 208why, one step back
208

mention that session signature checks max ageDavid Lordbefore adoption2efb565

lines 209–215why, one step back
209Specifying ``Expires`` or ``Max-Age`` options, will remove the cookie after
210the given time, or the current time plus the age, respectively. If neither211option is set, the cookie will be removed when the browser is closed. ::212213 # cookie expires after 10 minutes214 response.set_cookie('snakes', '3', max_age=600)215

fix linksDavid Lordbefore adoption97ad085

lines 216–220why, one step back
216For the session cookie, if :attr:`session.permanent <flask.session.permanent>`
217is set, then :data:`PERMANENT_SESSION_LIFETIME` is used to set the expiration.218Flask's default cookie implementation validates that the cryptographic219signature is not older than this value. Lowering this value may help mitigate220replay attacks, where intercepted cookies can be sent at a later time. ::

mention that session signature checks max ageDavid Lordbefore adoption2efb565

lines 221–233why, one step back
221
222 app.config.update(223 PERMANENT_SESSION_LIFETIME=600224 )225226 @app.route('/login', methods=['POST'])227 def login():228 ...229 session.clear()230 session['user_id'] = user.id231 session.permanent = True232 ...233

fix linksDavid Lordbefore adoption97ad085

lines 234–235why, one step back
234Use :class:`itsdangerous.TimedSerializer` to sign and validate other cookie
235values (or any values that need secure signatures).

mention that session signature checks max ageDavid Lordbefore adoption2efb565

lines 236–238why, one step back
236
237- https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies238- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 239why, one step back
239

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 240–241why, one step back
240HTTP Public Key Pinning (HPKP)
241~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 242why, one step back
242

clean up security header docsDavid Lordbefore adoption8eff9bd

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

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 245why, one step back
245

clean up security header docsDavid Lordbefore adoption8eff9bd

lines 246–248why, one step back
246.. warning::
247 Be careful when enabling this, as it is very difficult to undo if you set up248 or upgrade your key incorrectly.

Merge pull request #2309 from aqt01/masterKenneth Reitzbefore adoption3acec5f

line 249why, one step back
249

clean up security header docsDavid Lordbefore adoption8eff9bd

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