🔒️ 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:
Gina Häußge authored and GitHub committed 2022-09-19 10:01:28 +02:00
1 parent 0db80dc0c9
commit 59a0c8e8d7
22 files changed
+402 -20

No files matched your search

@@ -291,6 +291,9 @@ class AnnouncementPlugin(
def is_blueprint_protected(self):
return False
def is_blueprint_csrf_protected(self):
return True
##~~ EventHandlerPlugin
def on_event(self, event, payload):
+6 -2
View File
@@ -161,6 +161,7 @@ class AppKeysPlugin(
return NO_CONTENT
@octoprint.plugin.BlueprintPlugin.route("/request", methods=["POST"])
@octoprint.plugin.BlueprintPlugin.csrf_exempt()
@no_firstrun_access
def handle_request(self):
data = flask.request.json
@@ -196,7 +197,7 @@ class AppKeysPlugin(
)
return response
@octoprint.plugin.BlueprintPlugin.route("/request/<app_token>")
@octoprint.plugin.BlueprintPlugin.route("/request/<app_token>", methods=["GET"])
@no_firstrun_access
def handle_decision_poll(self, app_token):
result = self._get_pending_by_app_token(app_token)
@@ -214,7 +215,7 @@ class AppKeysPlugin(
return flask.abort(404)
@octoprint.plugin.BlueprintPlugin.route("/auth/<app_token>")
@octoprint.plugin.BlueprintPlugin.route("/auth/<app_token>", methods=["GET"])
@no_firstrun_access
def handle_auth_dialog(self, app_token):
user_id = current_user.get_name()
@@ -284,6 +285,9 @@ class AppKeysPlugin(
def is_blueprint_protected(self):
return False # No API key required to request API access
def is_blueprint_csrf_protected(self):
return True # protect anything that isn't explicitly marked as exempt
##~~ SimpleApiPlugin mixin
def get_api_commands(self):
+3
View File
@@ -356,6 +356,9 @@ class BackupPlugin(
def is_blueprint_protected(self):
return False
def is_blueprint_csrf_protected(self):
return True
##~~ WizardPlugin
def is_wizard_required(self):
@@ -40,7 +40,6 @@ $(function () {
dataType: "json",
maxNumberOfFiles: 1,
autoUpload: false,
headers: OctoPrint.getRequestHeaders(),
add: function (e, data) {
if (data.files.length === 0) {
// no files? ignore
@@ -74,6 +74,11 @@ class CoreWizardPlugin(
else:
return {}
# ~~ BlueprintPlugin API
def is_blueprint_csrf_protected(self):
return True
# ~~ WizardPlugin API
def is_wizard_required(self):
@@ -134,6 +134,9 @@ class DiscoveryPlugin(
def is_blueprint_protected(self):
return False
def is_blueprint_csrf_protected(self):
return True
##~~ StartupPlugin API -- used for registering OctoPrint's Zeroconf and SSDP services upon application startup
def on_startup(self, host, port):
@@ -127,6 +127,9 @@ class LoggingPlugin(
def is_blueprint_protected(self):
return False
def is_blueprint_csrf_protected(self):
return True
def _get_usage(self):
import psutil
@@ -609,6 +609,9 @@ class PluginManagerPlugin(
def is_blueprint_protected(self):
return False
def is_blueprint_csrf_protected(self):
return True
##~~ EventHandlerPlugin
def on_event(self, event, payload):
@@ -1453,6 +1453,9 @@ class SoftwareUpdatePlugin(
def is_blueprint_protected(self):
return False
def is_blueprint_csrf_protected(self):
return True
# ~~ Asset API
def get_assets(self):