From 00a5243ec9b57d51f68a67948324a10ce891dd72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 15 Apr 2020 13:04:42 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20config=20flag=20to=20configure=20Sa?= =?UTF-8?q?meSite=20and=20Secure=20cookie=20headers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Provides one part of the solution to #3482. The other part is sadly on the shoulders of the user, who needs to setup either a whole domain hierarchy to not run into "third party page" issues that would trigger SameSite=Lax blocking, or needs to reconfigure their setup to use https and make sure to set Secure to true. Closes #3482 (there's nothing else I can do here) --- docs/configuration/config_yaml.rst | 34 ++++++++++++++++++++++++++++++ src/octoprint/server/util/flask.py | 12 +++++++++++ src/octoprint/settings.py | 9 ++++++-- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/docs/configuration/config_yaml.rst b/docs/configuration/config_yaml.rst index a18bf3d7c..8c5d61618 100644 --- a/docs/configuration/config_yaml.rst +++ b/docs/configuration/config_yaml.rst @@ -1025,6 +1025,25 @@ Use the following settings to configure the server: - 192.168.1.254 - 192.168.23.42 + # Whether to allow OctoPrint to be embedded in a frame or not. Note that depending on your setup you might + # have to set SameSite to None, Secure to true and serve OctoPrint through a reverse proxy that enables https + # for cookies and thus logging in to work + allowFraming: true + + # Settings for further configuration of the cookies that OctoPrint sets (login, remember me, ...) + cookies: + # SameSite setting to use on the cookies. Possible values are None, Lax and Strict. Defaults to None but + # be advised that browsers will soon force this to Lax unless also being set as Secure and served over + # https, which will cause issues with embedding OctoPrint in frames. + # + # See also https://www.chromestatus.com/feature/5088147346030592, + # https://www.chromestatus.com/feature/5633521622188032 and issue #3482 + samesite: lax + + # Whether to set the Secure flag to true on cookies. Defaults to false. Only set to true if you are running + # OctoPrint behind a reverse proxy taking care of SSL termination. + secure: false + # Settings for file uploads to OctoPrint, such as maximum allowed file size and # header suffixes to use for streaming uploads. OctoPrint does some nifty things internally in # order to allow streaming of large file uploads to the application rather than just storing @@ -1129,6 +1148,21 @@ Use the following settings to configure the server: `into OctoPrint's wiki `_ for some examples on how to configure this. +.. note:: + + If you want to embed OctoPrint in a frame, you'll need to set ``allowFraming`` to ``true`` or your browser will + prevent this. + + In future browser builds you will also have to make sure you frame is on the same domain as OctoPrint or that + OctoPrint is served via https through a reverse proxy and has set ``cookies.secure`` to ``true`` or your browser + will refuse to persist cookies and logging in will not work. + + See also `Cookies default to SameSite=Lax `_ and + `Reject insecure SameSite=None cookies `_ as well as + `this ticket `_, + and `this twitter thread `_ on why OctoPrint cannot + solve this on its own/ship with https that doesn't cause scary warnings in your browser. + .. _sec-configuration-config_yaml-slicing: Slicing diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index 4318a598e..f2d2dd5fe 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -505,6 +505,18 @@ class OctoPrintFlaskResponse(flask.Response): # restrict cookie path to script root kwargs["path"] = flask.request.script_root + kwargs.get("path", "/") + # set same-site header + samesite = settings().get(["server", "cookies", "samesite"]) + if samesite is not None: + samesite = samesite.lower() + if samesite == "none": + samesite = None + if samesite in (None, "strict", "lax"): + kwargs["samesite"] = samesite + + # set secure if necessary + kwargs["secure"] = settings().getBoolean(["server", "cookies", "secure"]) + # add request specific cookie suffix to name flask.Response.set_cookie(self, key + flask.request.cookie_suffix, *args, **kwargs) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 1b1f2ef22..7739a7a89 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -223,7 +223,11 @@ default_settings = { "enabled": True, "trustedSubnets": [] }, - "allowFraming": False + "allowFraming": False, + "cookies": { + "secure": False, + "samesite": None + }, }, "webcam": { "webcamEnabled": True, @@ -410,7 +414,8 @@ default_settings = { }, "webassets": { "bundle": True, - "clean_on_startup": True + "clean_on_startup": True, + "minify": True }, "useFrozenDictForPrinterState": True, "showLoadingAnimation": True,