Skip to content
Myr

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

1 file+66 added−55 removed

modified:docs/security.rst+66 added-55 removed
106 unmodified lines
107108Security Headers109----------------110111This section contains a list of HTTP security headers supported by Flask.112To configure HTTPS and handle the headers listed below we suggest the package `flask-talisman <https://github.com/GoogleCloudPlatform/flask-talisman>`_. 111Browsers 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.115116.. _Flask-Talisman: https://github.com/GoogleCloudPlatform/flask-talisman117118HTTP Strict Transport Security (HSTS)115-------------------------------------119~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~120117Redirects HTTP requests to HTTPS on all URLs, preventing man-in-the-middle (MITM) attacks.121Tells the browser to convert all HTTP requests to HTTPS, preventing122man-in-the-middle (MITM) attacks. ::123119Example:124 response.haders['Strict-Transport-Security'] = 'max-age=31536000; includeSubDomains'125121.. 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-Security127127See also `Strict Transport Security <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security>`_. 128Content Security Policy (CSP)129~~~~~~~~~~~~~~~~~~~~~~~~~~~~~130129HTTP Public Key Pinning (HPKP)130------------------------------131Tell 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::134132This 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'"136134Example:137- https://csp.withgoogle.com/docs/index.html138- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy139136.. sourcecode:: none140X-Content-Type-Options141~~~~~~~~~~~~~~~~~~~~~~142138 Public-Key-Pins: pin-sha256="base64=="; max-age=expireTime [; includeSubDomains][; report-uri="reportURI"] 143Forces 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. ::146140See also `Public Key Pinning <https://developer.mozilla.org/en-US/docs/Web/HTTP/Public_Key_Pinning>`_.147 response.headers['X-Content-Type-Options'] = 'nosniff'148142X-Frame-Options (Clickjacking Protection)143-----------------------------------------149- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options150145Prevents the client from clicking page elements outside of the website, avoiding hijacking or UI redress attacks.151X-Frame-Options152~~~~~~~~~~~~~~~153147.. sourcecode:: none148 149 X-Frame-Options: DENY 150 X-Frame-Options: SAMEORIGIN151 X-Frame-Options: ALLOW-FROM https://example.com/154Prevents 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". ::158153See also `X-Frame-Options <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options>`_. 159 response.headers['X-Frame-Options'] = 'SAMEORIGIN'160155X-Content-Type-Options156----------------------161- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options162158This 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.163X-XSS-Protection164~~~~~~~~~~~~~~~~165160.. sourcecode:: none161 162 X-Content-Type-Options: nosniff166The 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. ::169164See 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'171166Content Security Policy (CSP)167-----------------------------172- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-XSS-Protection173169Enhances security and prevents common web vulnerabilities such as cross-site scripting (XSS) and man-in-the-middle (MITM) related attacks.174Set-Cookie options175~~~~~~~~~~~~~~~~~~176171Example:177These 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.180173.. 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.186177See also `Content Security Policy <https://csp.withgoogle.com/docs/index.html>`_.187::188179Cookie Options180--------------189 app.config.update(190 SESSION_COOKIE_SECURE=True,191 SESSION_COOKIE_HTTPONLY=True,192 )193182While 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)195184- ``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_cookies197188Example:198HTTP Public Key Pinning (HPKP)199~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~200190.. sourcecode:: none191 192 Set-Cookie: [cookie-name]=[cookie-value] 201This tells the browser to authenticate with the server using only the specific202certificate key to prevent MITM attacks.203194See 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.207196- 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