Skip to content

Commit d01a8ab

Browse files
committed
Allow subpaths in MAS endpoints (#19186)
Fixes #19184 ### Pull Request Checklist <!-- Please read https://element-hq.github.io/synapse/latest/development/contributing_guide.html before submitting your pull request --> * [X] Pull request is based on the develop branch * [X] Pull request includes a [changelog file](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#changelog). The entry should: - Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from `EventStore` to `EventWorkerStore`.". - Use markdown where necessary, mostly for `code blocks`. - End with either a period (.) or an exclamation mark (!). - Start with a capital letter. - Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry. * [X] [Code style](https://element-hq.github.io/synapse/latest/code_style.html) is correct (run the [linters](https://element-hq.github.io/synapse/latest/development/contributing_guide.html#run-the-linters))
1 parent 8feb862 commit d01a8ab

File tree

3 files changed

+30
-24
lines changed

3 files changed

+30
-24
lines changed

changelog.d/19186.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix regression preventing subpaths in MAS endpoints.

synapse/api/auth/mas.py

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from urllib.parse import urlencode
1818

1919
from pydantic import (
20-
AnyHttpUrl,
2120
BaseModel,
2221
ConfigDict,
2322
StrictBool,
@@ -147,33 +146,13 @@ def __init__(self, hs: "HomeServer"):
147146

148147
@property
149148
def _metadata_url(self) -> str:
150-
return str(
151-
AnyHttpUrl.build(
152-
scheme=self._config.endpoint.scheme,
153-
username=self._config.endpoint.username,
154-
password=self._config.endpoint.password,
155-
host=self._config.endpoint.host or "",
156-
port=self._config.endpoint.port,
157-
path=".well-known/openid-configuration",
158-
query=None,
159-
fragment=None,
160-
)
149+
return (
150+
f"{str(self._config.endpoint).rstrip('/')}/.well-known/openid-configuration"
161151
)
162152

163153
@property
164154
def _introspection_endpoint(self) -> str:
165-
return str(
166-
AnyHttpUrl.build(
167-
scheme=self._config.endpoint.scheme,
168-
username=self._config.endpoint.username,
169-
password=self._config.endpoint.password,
170-
host=self._config.endpoint.host or "",
171-
port=self._config.endpoint.port,
172-
path="oauth2/introspect",
173-
query=None,
174-
fragment=None,
175-
)
176-
)
155+
return f"{str(self._config.endpoint).rstrip('/')}/oauth2/introspect"
177156

178157
async def _load_metadata(self) -> ServerMetadata:
179158
response = await self._http_client.get_json(self._metadata_url)

tests/handlers/test_oauth_delegation.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,32 @@ def test_cached_expired_introspection(self) -> None:
10571057
self.assertEqual(self.server.calls, 1)
10581058

10591059

1060+
class MasAuthDelegationWithSubpath(MasAuthDelegation):
1061+
"""Test MAS delegation when the MAS server is hosted on a subpath."""
1062+
1063+
def default_config(self) -> dict[str, Any]:
1064+
config = super().default_config()
1065+
# Override the endpoint to include a subpath
1066+
config["matrix_authentication_service"]["endpoint"] = (
1067+
self.server.endpoint + "auth/path/"
1068+
)
1069+
return config
1070+
1071+
def test_introspection_endpoint_uses_subpath(self) -> None:
1072+
"""Test that the introspection endpoint correctly uses the configured subpath."""
1073+
expected_introspection_url = (
1074+
self.server.endpoint + "auth/path/oauth2/introspect"
1075+
)
1076+
self.assertEqual(self._auth._introspection_endpoint, expected_introspection_url)
1077+
1078+
def test_metadata_url_uses_subpath(self) -> None:
1079+
"""Test that the metadata URL correctly uses the configured subpath."""
1080+
expected_metadata_url = (
1081+
self.server.endpoint + "auth/path/.well-known/openid-configuration"
1082+
)
1083+
self.assertEqual(self._auth._metadata_url, expected_metadata_url)
1084+
1085+
10601086
@parameterized_class(
10611087
("config",),
10621088
[

0 commit comments

Comments
 (0)