From 2c03ecdfff8f111fd2f9d9db1bdb6a8d01fdfe84 Mon Sep 17 00:00:00 2001 From: cp2004 <31997505+cp2004@users.noreply.github.com> Date: Wed, 12 May 2021 00:08:32 +0100 Subject: [PATCH] :bug: Fix SameSite=None cookie When passing Python's NoneType down to Werkzeug, it uses this logic: ```python if samesite is not None: samesite = samesite.title() if samesite not in {"Strict", "Lax", "None"}: raise ValueError("SameSite must be 'Strict', 'Lax', or 'None'.") ``` This means that when OctoPrint converted any strings 'None' into NoneType, they would not show up in the Set-Cookie header. Investigation: https://community.octoprint.org/t/unable-to-log-into-octoprint-through-an-iframe-on-home-assistant/33977/20?u=charlie_powell Link to werkzeug source: https://github.com/pallets/werkzeug/blob/1dde4b1790f9c46b7122bb8225e6b48a5b22a615/src/werkzeug/http.py#L1213-L1217 Docs, while not explicitly clear, here: https://werkzeug.palletsprojects.com/en/1.0.x/http/?highlight=dump_cookie#werkzeug.http.dump_cookie --- src/octoprint/server/util/flask.py | 6 ++++-- tests/server/util/test_flask.py | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index 4afa58051..cf7434f0b 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -567,8 +567,10 @@ class OctoPrintFlaskResponse(flask.Response): if samesite is not None: samesite = samesite.lower() if samesite == "none": - samesite = None - if samesite not in (None, "strict", "lax"): + # Must be string "None" + samesite = "None" + if samesite not in ("None", "strict", "lax"): + # If NoneType, the cookie is not set samesite = None kwargs["samesite"] = samesite diff --git a/tests/server/util/test_flask.py b/tests/server/util/test_flask.py index 6596e232c..397eeccf7 100644 --- a/tests/server/util/test_flask.py +++ b/tests/server/util/test_flask.py @@ -503,7 +503,7 @@ class OctoPrintFlaskResponseTest(unittest.TestCase): @data( [None, None, False, None, None], - [None, None, False, "none", None], + [None, None, False, "none", "None"], [None, None, False, "lax", "lax"], [None, None, False, "StRiCt", "strict"], [None, None, False, "INVALID", None],