clean up security header docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
[ci skip]
authored by David Lord · committed by David Lord · parent 11f463f · Browse files · 1 file·+66 added−55 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+66 added−55 removed
- other:+66 added−55 removed
Provenance
- Commit
8eff9bd- Tree
92f81a2- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
11f463f
1 file changed+66 added−55 removed
modified:docs/security.rst+66 added-55 removed
106 unmodified lines107 108 Security Headers109 ----------------110 111-This section contains a list of HTTP security headers supported by Flask.112-To configure HTTPS and handle the headers listed below we suggest the package `flask-talisman <https://github.com/GoogleCloudPlatform/flask-talisman>`_. 111+Browsers recognize various response headers in order to control security. We112+recommend reviewing each of the headers below for use in your application.113+The `Flask-Talisman`_ extension can be used to manage HTTPS and the security114+headers for you.115 116+.. _Flask-Talisman: https://github.com/GoogleCloudPlatform/flask-talisman117+118 HTTP Strict Transport Security (HSTS)115--------------------------------------119+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~120 117-Redirects HTTP requests to HTTPS on all URLs, preventing man-in-the-middle (MITM) attacks.121+Tells the browser to convert all HTTP requests to HTTPS, preventing122+man-in-the-middle (MITM) attacks. ::123 119-Example:124+ response.haders['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains'125 121-.. sourcecode:: none122- 123- Strict-Transport-Security: max-age=<expire-time 124- Strict-Transport-Security: max-age=<expire-time>; includeSubDomains 125- Strict-Transport-Security: max-age=<expire-time>; preload 126+- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security127 127-See also `Strict Transport Security <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security>`_. 128+Content Security Policy (CSP)129+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~130 129-HTTP Public Key Pinning (HPKP)130-------------------------------131+Tell the browser where it can load various types of resource from. This header132+should be used whenever possible, but requires some work to define the correct133+policy for your site. A very strict policy would be::134 132-This enables your web server to authenticate with a client browser using a specific certificate key to prevent man-in-the-middle (MITM) attacks.135+ response.headers['Content-Security-Policy'] = "default-src: 'self'"136 134-Example:137+- https://csp.withgoogle.com/docs/index.html138+- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy139 136-.. sourcecode:: none140+X-Content-Type-Options141+~~~~~~~~~~~~~~~~~~~~~~142 138- Public-Key-Pins: pin-sha256="base64=="; max-age=expireTime [; includeSubDomains][; report-uri="reportURI"] 143+Forces the browser to honor the response content type instead of trying to144+detect it, which can be abused to generate a cross-site scripting (XSS)145+attack. ::146 140-See also `Public Key Pinning <https://developer.mozilla.org/en-US/docs/Web/HTTP/Public_Key_Pinning>`_.147+ response.headers['X-Content-Type-Options'] = 'nosniff'148 142-X-Frame-Options (Clickjacking Protection)143------------------------------------------149+- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options150 145-Prevents the client from clicking page elements outside of the website, avoiding hijacking or UI redress attacks.151+X-Frame-Options152+~~~~~~~~~~~~~~~153 147-.. sourcecode:: none148- 149- X-Frame-Options: DENY 150- X-Frame-Options: SAMEORIGIN151- X-Frame-Options: ALLOW-FROM https://example.com/154+Prevents external sites from embedding your site in an ``iframe``. This155+prevents a class of attacks where clicks in the outer frame can be translated156+invisibly to clicks on your page's elements. This is also known as157+"clickjacking". ::158 153-See also `X-Frame-Options <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options>`_. 159+ response.headers['X-Frame-Options'] = 'SAMEORIGIN'160 155-X-Content-Type-Options156-----------------------161+- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options162 158-This header prevents Cross-site scripting (XSS) by blocking requests on clients and forcing them to first read and validate the content-type before reading any of the contents of the request.163+X-XSS-Protection164+~~~~~~~~~~~~~~~~165 160-.. sourcecode:: none161- 162- X-Content-Type-Options: nosniff166+The browser will try to prevent reflected XSS attacks by not loading the page167+if the request contains something that looks like JavaScript and the response168+contains the same data. ::169 164-See also `X-Content-Type-Options <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options>`_. 170+ response.headers['X-XSS-Protection'] = '1; mode=block'171 166-Content Security Policy (CSP)167------------------------------172+- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection173 169-Enhances security and prevents common web vulnerabilities such as cross-site scripting (XSS) and man-in-the-middle (MITM) related attacks.174+Set-Cookie options175+~~~~~~~~~~~~~~~~~~176 171-Example:177+These options can be added to a ``Set-Cookie`` header to improve their178+security. Flask has configuration options to set these on the session cookie.179+They can be set on other cookies too.180 173-.. sourcecode:: none174- 175- Content-Security-Policy: default-src https:; script-src 'nonce-{random}'; object-src 'none'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.186 177-See also `Content Security Policy <https://csp.withgoogle.com/docs/index.html>`_.187+::188 179-Cookie Options180---------------189+ app.config.update(190+ SESSION_COOKIE_SECURE=True,191+ SESSION_COOKIE_HTTPONLY=True,192+ )193 182-While these headers are not directly security related, they have important options that may affect your Flask application.194+ response.set_cookie('username', 'flask', secure=True, httponly=True)195 184-- ``Secure`` limits your cookies to HTTPS traffic only.185-- ``HttpOnly`` protects the contents of your cookie from being visible to XSS.186-- ``SameSite`` ensures that cookies can only be requested from the same domain that created them but this feature is not yet fully supported across all browsers. 196+- https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#Secure_and_HttpOnly_cookies197 188-Example:198+HTTP Public Key Pinning (HPKP)199+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~200 190-.. sourcecode:: none191- 192- Set-Cookie: [cookie-name]=[cookie-value] 201+This tells the browser to authenticate with the server using only the specific202+certificate key to prevent MITM attacks.203 194-See also:204+.. warning::205+ Be careful when enabling this, as it is very difficult to undo if you set up206+ or upgrade your key incorrectly.207 196-- Mozilla guide to `HTTP cookies <https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#Secure_and_HttpOnly_cookies>`_.197-- `OWASP HTTP Only <https://www.owasp.org/index.php/HttpOnly>`_. 208+- https://developer.mozilla.org/en-US/docs/Web/HTTP/Public_Key_Pinning