Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 695b73c

Browse files
Allow OIDC cookies to work on non-root public baseurls (#9726)
Applied a (slightly modified) patch from #9574. As far as I understand this would allow the cookie set during the OIDC flow to work on deployments using public baseurls that do not sit at the URL path root.
1 parent 59d24c5 commit 695b73c

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

changelog.d/9726.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixes the OIDC SSO flow when using a `public_baseurl` value including a non-root URL path.

synapse/config/server.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,11 @@ def read_config(self, config, **kwargs):
235235
self.print_pidfile = config.get("print_pidfile")
236236
self.user_agent_suffix = config.get("user_agent_suffix")
237237
self.use_frozen_dicts = config.get("use_frozen_dicts", False)
238+
238239
self.public_baseurl = config.get("public_baseurl")
240+
if self.public_baseurl is not None:
241+
if self.public_baseurl[-1] != "/":
242+
self.public_baseurl += "/"
239243

240244
# Whether to enable user presence.
241245
presence_config = config.get("presence") or {}
@@ -407,10 +411,6 @@ def read_config(self, config, **kwargs):
407411
config_path=("federation_ip_range_blacklist",),
408412
)
409413

410-
if self.public_baseurl is not None:
411-
if self.public_baseurl[-1] != "/":
412-
self.public_baseurl += "/"
413-
414414
# (undocumented) option for torturing the worker-mode replication a bit,
415415
# for testing. The value defines the number of milliseconds to pause before
416416
# sending out any replication updates.

synapse/handlers/oidc.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import inspect
1616
import logging
1717
from typing import TYPE_CHECKING, Dict, Generic, List, Optional, TypeVar, Union
18-
from urllib.parse import urlencode
18+
from urllib.parse import urlencode, urlparse
1919

2020
import attr
2121
import pymacaroons
@@ -68,8 +68,8 @@
6868
#
6969
# Here we have the names of the cookies, and the options we use to set them.
7070
_SESSION_COOKIES = [
71-
(b"oidc_session", b"Path=/_synapse/client/oidc; HttpOnly; Secure; SameSite=None"),
72-
(b"oidc_session_no_samesite", b"Path=/_synapse/client/oidc; HttpOnly"),
71+
(b"oidc_session", b"HttpOnly; Secure; SameSite=None"),
72+
(b"oidc_session_no_samesite", b"HttpOnly"),
7373
]
7474

7575
#: A token exchanged from the token endpoint, as per RFC6749 sec 5.1. and
@@ -279,6 +279,13 @@ def __init__(
279279
self._config = provider
280280
self._callback_url = hs.config.oidc_callback_url # type: str
281281

282+
# Calculate the prefix for OIDC callback paths based on the public_baseurl.
283+
# We'll insert this into the Path= parameter of any session cookies we set.
284+
public_baseurl_path = urlparse(hs.config.server.public_baseurl).path
285+
self._callback_path_prefix = (
286+
public_baseurl_path.encode("utf-8") + b"_synapse/client/oidc"
287+
)
288+
282289
self._oidc_attribute_requirements = provider.attribute_requirements
283290
self._scopes = provider.scopes
284291
self._user_profile_method = provider.user_profile_method
@@ -779,8 +786,13 @@ async def handle_redirect_request(
779786

780787
for cookie_name, options in _SESSION_COOKIES:
781788
request.cookies.append(
782-
b"%s=%s; Max-Age=3600; %s"
783-
% (cookie_name, cookie.encode("utf-8"), options)
789+
b"%s=%s; Max-Age=3600; Path=%s; %s"
790+
% (
791+
cookie_name,
792+
cookie.encode("utf-8"),
793+
self._callback_path_prefix,
794+
options,
795+
)
784796
)
785797

786798
metadata = await self.load_metadata()

0 commit comments

Comments
 (0)