🔒️ Protect against CSRF attacks (#5)
* 🔒️ Protect against CSRF attacks CORS and the JSON API heavily reduced the attack surface here, but especially the upload endpoints were still vulnerable to CSRF attacks. All endpoints on the API that use methods beyond GET, HEAD or OPTIONS now require the presence of a csrf token (generated by the UI on first load) AND a X-CSRF-Token header with a matching value (Double Submit Cookie), unless authorization is done through an API-Key instead of a browser session. Additionally, the SameSite attribute now is set to "Strict" by default. * 🔒️ Expand CSRF protection to plugins Plugins are currently by default *opted out* of CSRF protection, to ensure backwards compatibility with third party plugins in the field. However, in a future OctoPrint version this will become a default *opt in*. Plugin authors are advised to adjust the implementation with an explicit decision via a logged warning generated in the default implementation. Additionally, plugins that have opted out their whole plugin will generate a warning in the startup messages. A decorator to exempt individual endpoints from CSRF protection as certain workflows require (see for example the auth request in the appkeys plugin) is provided. The general rule of thumb should be to opt in and only exempt those non GET/HEAD/OPTIONS endpoints that really need to be usable without an API key or CSRF cookie/header combo. In practice, that should be pretty much non. * 🐛 Fix a left-over from a local test * 🐛 Fix wrong setting in E2E test config.yaml * 🧑💻 Set CSRF header for $.ajax as well We already ensure the header is set in the client lib and on the file upload widgets, but many third party plugins don't use the client lib but rather $.ajax directly and thus break without this. * 📝 Improve docs on CSRF protection
This commit is contained in:
1 parent
0db80dc0c9
commit
59a0c8e8d7
22 files changed
+402
-20
No files matched your search
+24
-1
@@ -135,7 +135,7 @@ check the corresponding checkbox in the API settings dialog.
|
||||
|
||||
.. warning::
|
||||
|
||||
This means any browser page can send requests to the OctoPrint API. Authorization is still required however.
|
||||
This means any browser page can send requests to the OctoPrint API. Authorization via an API-Key is still required however.
|
||||
|
||||
If CORS is not enabled you will get errors like the following::
|
||||
|
||||
@@ -151,6 +151,29 @@ If CORS is not enabled you will get errors like the following::
|
||||
the login mechanism (or reusing an existing login session). When accessing OctoPrint
|
||||
via CORS, you'll therefore always need to use an API key.
|
||||
|
||||
.. _sec-api-general-csrf:
|
||||
|
||||
CSRF Protection
|
||||
===============
|
||||
|
||||
.. versionadded:: 1.8.3
|
||||
|
||||
To protect OctoPrint against `CSRF attacks <https://owasp.org/www-community/attacks/csrf>`_ against the non CORS affected upload endpoints, in case of browser session based authorization the API
|
||||
is protected using the `Double Submit Cookie mitigation strategy <https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#double-submit-cookie>`_.
|
||||
On first page load of the UI, the login page or the recovery page, a ``csrf_token_P<port>`` or ``csrf_token_P<port>_R<root>`` cookie is set
|
||||
that can be read via client-side JavaScript. All requests towards the API that are not ``GET``, ``HEAD`` or ``OPTIONS``
|
||||
and rely on cookie based authorization (so not on an API key but rather an active login session) are required
|
||||
to send both the ``csrf_token`` cookie as well as an ``X-CSRF-Token`` header containing its value.
|
||||
|
||||
.. note::
|
||||
|
||||
If you use the :ref:`JS Client library <sec-jsclientlib>`, this will take care of doing the needful for you. Any code in the *Core UI* calling
|
||||
API functions through ``$.ajax`` or ``$.get`` or ``$.post`` will also take care of this for you. If you use another library for
|
||||
accessing OctoPrint's API in a browser context, you'll need to make sure to send the ``X-CSRF-Token`` header yourself. Examples for
|
||||
several JS frameworks can be found in the `OWASP cheatsheet on CSRF attacks <https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html#javascript-guidance-for-auto-inclusion-of-csrf-tokens-as-an-ajax-request-header>`_.
|
||||
Take a look at the implementations of ``OctoPrintClient.getCookie`` and ``OctoPrintClient.getHeaders`` in ``src/octoprint/static/js/client/base.js``
|
||||
for details on how to retrieve the cookie value and how to construct the header.
|
||||
|
||||
.. _sec-api-general-login:
|
||||
|
||||
Login
|
||||
|
||||
Reference in new issue
Block a user