Skip to content

Commit 767b483

Browse files
committed
Allow overidding samesite value for session cookie
1 parent fb5554c commit 767b483

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

djangosaml2/middleware.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def process_response(self, request, response):
2626
session every time, save the changes and set a session cookie or delete
2727
the session cookie if the session has been emptied.
2828
"""
29+
SAMESITE = getattr(settings, "SAML_SESSION_COOKIE_SAMESITE", SAMESITE_NONE)
30+
2931
try:
3032
accessed = request.saml_session.accessed
3133
modified = request.saml_session.modified
@@ -39,7 +41,7 @@ def process_response(self, request, response):
3941
self.cookie_name,
4042
path=settings.SESSION_COOKIE_PATH,
4143
domain=settings.SESSION_COOKIE_DOMAIN,
42-
samesite=SAMESITE_NONE,
44+
samesite=SAMESITE,
4345
)
4446
patch_vary_headers(response, ("Cookie",))
4547
else:
@@ -74,6 +76,6 @@ def process_response(self, request, response):
7476
path=settings.SESSION_COOKIE_PATH,
7577
secure=settings.SESSION_COOKIE_SECURE or None,
7678
httponly=settings.SESSION_COOKIE_HTTPONLY or None,
77-
samesite=SAMESITE_NONE,
79+
samesite=SAMESITE,
7880
)
7981
return response

djangosaml2/tests/__init__.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,3 +1030,31 @@ def test_middleware_cookie_with_expiry(self):
10301030
self.assertIsNotNone(cookie["expires"])
10311031
self.assertNotEqual(cookie["expires"], "")
10321032
self.assertNotEqual(cookie["max-age"], "")
1033+
1034+
def test_middleware_cookie_samesite(self):
1035+
with override_settings(SAML_SESSION_COOKIE_SAMESITE="Lax"):
1036+
session = self.get_session()
1037+
session.save()
1038+
self.set_session_cookies(session)
1039+
1040+
config_loader_path = "djangosaml2.tests.test_config_loader_with_real_conf"
1041+
request = RequestFactory().get("/login/")
1042+
request.user = AnonymousUser()
1043+
request.session = session
1044+
middleware = SamlSessionMiddleware(dummy_get_response)
1045+
middleware.process_request(request)
1046+
1047+
saml_session_name = getattr(
1048+
settings, "SAML_SESSION_COOKIE_NAME", "saml_session"
1049+
)
1050+
getattr(request, saml_session_name).save()
1051+
1052+
response = views.LoginView.as_view(config_loader_path=config_loader_path)(
1053+
request
1054+
)
1055+
1056+
response = middleware.process_response(request, response)
1057+
1058+
cookie = response.cookies[saml_session_name]
1059+
1060+
self.assertEqual(cookie["samesite"], "Lax")

docs/source/contents/setup.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,18 @@ You can even configure the SAML cookie name as follows::
6262

6363
SAML_SESSION_COOKIE_NAME = 'saml_session'
6464

65+
By default, djangosaml2 will set "SameSite=None" for the SAML session cookie. This value can be configured as follows::
66+
67+
SAML_SESSION_COOKIE_SAMESITE = 'Lax'
68+
6569
Remember that in your browser "SameSite=None" attribute MUST also
6670
have the "Secure" attribute, which is required in order to use "SameSite=None", otherwise the cookie will be blocked, so you must also set::
6771

6872
SESSION_COOKIE_SECURE = True
6973

7074
.. Note::
7175

72-
djangosaml2 will attempt to set the ``SameSite`` attribute of the SAML session cookie to ``None`` so that it can be
76+
djangosaml2 will by default attempt to set the ``SameSite`` attribute of the SAML session cookie to ``None`` so that it can be
7377
used in cross-site requests, but this is only possible with Django 3.1 or higher. If you are experiencing issues with
7478
unsolicited requests or cookies not being sent (particularly when using the HTTP-POST binding), consider upgrading
7579
to Django 3.1 or higher. If you can't do that, configure "allow_unsolicited" to True in pySAML2 configuration.

0 commit comments

Comments
 (0)