From 8c18452cce858cd64585900cc5ba1eac6688b647 Mon Sep 17 00:00:00 2001 From: Ben Banfield-Zanin Date: Mon, 24 Feb 2025 11:15:13 +0000 Subject: [PATCH 1/6] Refresh the standard CORS headers for OPTIONS requests from Synapse --- charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl b/charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl index effb317d..fe2cfac2 100644 --- a/charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl +++ b/charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl @@ -116,7 +116,7 @@ frontend http-blackhole # a backend which responds to everything with a 204 backend return_204 - http-request return status 204 hdr "Access-Control-Allow-Origin" "*" hdr "Access-Control-Allow-Methods" "GET, POST, PUT, DELETE, OPTIONS" hdr "Access-Control-Allow-Headers" "Origin, X-Requested-With, Content-Type, Accept, Authorization" + http-request return status 204 hdr "Access-Control-Allow-Origin" "*" hdr "Access-Control-Allow-Methods" "GET, HEAD, POST, PUT, DELETE, OPTIONS" hdr "Access-Control-Allow-Headers" "Origin, X-Requested-With, Content-Type, Accept, Authorization, Date" hdr "Access-Control-Expose-Headers" "Synapse-Trace-Id, Server" # a fake backend which fonxes every request with a 500. Useful for # handling overloads etc. From 7b20d99f1c3a46bcf6e7d45cc5b1352adf38ee86 Mon Sep 17 00:00:00 2001 From: Ben Banfield-Zanin Date: Mon, 24 Feb 2025 11:19:52 +0000 Subject: [PATCH 2/6] Support QR code login --- .../configs/synapse/partial-haproxy.cfg.tpl | 11 +++++++++++ .../templates/synapse/_synapse_config.tpl | 2 ++ newsfragments/232.added.md | 1 + 3 files changed, 14 insertions(+) create mode 100644 newsfragments/232.added.md diff --git a/charts/matrix-stack/configs/synapse/partial-haproxy.cfg.tpl b/charts/matrix-stack/configs/synapse/partial-haproxy.cfg.tpl index 791d228a..c9a7b2f7 100644 --- a/charts/matrix-stack/configs/synapse/partial-haproxy.cfg.tpl +++ b/charts/matrix-stack/configs/synapse/partial-haproxy.cfg.tpl @@ -73,6 +73,11 @@ frontend synapse-http-in http-request set-var(req.backend) path,map_reg(/synapse/path_map_file_get,main) if has_get_map METH_GET http-request set-var(req.backend) path,map_reg(/synapse/path_map_file,main) unless { var(req.backend) -m found } +{{- if $root.Values.matrixAuthenticationService.enabled }} + acl rendezvous path_beg /_matrix/client/unstable/org.matrix.msc4108/rendezvous + acl rendezvous path_beg /_synapse/client/rendezvous + use_backend return_204_rendezvous if { method OPTIONS } rendezvous +{{- end }} use_backend return_204 if { method OPTIONS } {{- range .ingress.additionalPaths -}} @@ -195,3 +200,9 @@ backend synapse-be_{{ $additionalPathId }} {{- end }} {{- end -}} + +{{- if $root.Values.matrixAuthenticationService.enabled }} + +backend return_204_rendezvous + http-request return status 204 hdr "Access-Control-Allow-Origin" "*" hdr "Access-Control-Allow-Methods" "GET, HEAD, POST, PUT, DELETE, OPTIONS" hdr "Access-Control-Allow-Headers" "Origin, Content-Type, Accept, Content-Type, If-Match, If-None-Match" hdr "Access-Control-Expose-Headers" "Synapse-Trace-Id, Server, ETag" +{{- end -}} diff --git a/charts/matrix-stack/templates/synapse/_synapse_config.tpl b/charts/matrix-stack/templates/synapse/_synapse_config.tpl index 40a9f537..9c95f29a 100644 --- a/charts/matrix-stack/templates/synapse/_synapse_config.tpl +++ b/charts/matrix-stack/templates/synapse/_synapse_config.tpl @@ -114,6 +114,8 @@ experimental_features: admin_token: ${MAS_SHARED_SECRET} introspection_endpoint: "http://{{ $root.Release.Name }}-matrix-authentication-service.{{ $root.Release.Namespace }}.svc.cluster.local:8080/oauth2/introspect" + # QR Code Login. Requires MAS + msc4108_enabled: true password_config: localdb_enabled: false enabled: false diff --git a/newsfragments/232.added.md b/newsfragments/232.added.md new file mode 100644 index 00000000..81179117 --- /dev/null +++ b/newsfragments/232.added.md @@ -0,0 +1 @@ +Support QR code login when MAS is enabled. From bd35bfbc8112ec06659531de193589e93db6066f Mon Sep 17 00:00:00 2001 From: Ben Banfield-Zanin Date: Mon, 24 Feb 2025 13:44:43 +0000 Subject: [PATCH 3/6] The 204 backend is highly Synapse specific so put it in the Synapse fragment --- charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl | 4 ---- .../matrix-stack/configs/synapse/partial-haproxy.cfg.tpl | 8 ++++++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl b/charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl index fe2cfac2..84e66579 100644 --- a/charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl +++ b/charts/matrix-stack/configs/haproxy/haproxy.cfg.tpl @@ -114,10 +114,6 @@ frontend http-blackhole {{- end -}} -# a backend which responds to everything with a 204 -backend return_204 - http-request return status 204 hdr "Access-Control-Allow-Origin" "*" hdr "Access-Control-Allow-Methods" "GET, HEAD, POST, PUT, DELETE, OPTIONS" hdr "Access-Control-Allow-Headers" "Origin, X-Requested-With, Content-Type, Accept, Authorization, Date" hdr "Access-Control-Expose-Headers" "Synapse-Trace-Id, Server" - # a fake backend which fonxes every request with a 500. Useful for # handling overloads etc. backend return_500 diff --git a/charts/matrix-stack/configs/synapse/partial-haproxy.cfg.tpl b/charts/matrix-stack/configs/synapse/partial-haproxy.cfg.tpl index c9a7b2f7..06839e90 100644 --- a/charts/matrix-stack/configs/synapse/partial-haproxy.cfg.tpl +++ b/charts/matrix-stack/configs/synapse/partial-haproxy.cfg.tpl @@ -78,7 +78,7 @@ frontend synapse-http-in acl rendezvous path_beg /_synapse/client/rendezvous use_backend return_204_rendezvous if { method OPTIONS } rendezvous {{- end }} - use_backend return_204 if { method OPTIONS } + use_backend return_204_synapse if { method OPTIONS } {{- range .ingress.additionalPaths -}} {{- if eq .availability "internally_and_externally" }} @@ -199,7 +199,11 @@ backend synapse-be_{{ $additionalPathId }} {{- end }} {{- end }} -{{- end -}} +{{- end }} + +# a backend which responds to everything with a 204 mirroring https://github.com/element-hq/synapse/blob/v1.124.0/synapse/http/server.py#L901-L932 +backend return_204_synapse + http-request return status 204 hdr "Access-Control-Allow-Origin" "*" hdr "Access-Control-Allow-Methods" "GET, HEAD, POST, PUT, DELETE, OPTIONS" hdr "Access-Control-Allow-Headers" "Origin, X-Requested-With, Content-Type, Accept, Authorization, Date" hdr "Access-Control-Expose-Headers" "Synapse-Trace-Id, Server" {{- if $root.Values.matrixAuthenticationService.enabled }} From d3c687e0ef94c92c4a499d3d68516c54aef288d2 Mon Sep 17 00:00:00 2001 From: Ben Banfield-Zanin Date: Mon, 24 Feb 2025 14:26:40 +0000 Subject: [PATCH 4/6] Add a shared async context manager for getting a AIOHTTP Client --- tests/integration/lib/utils.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/tests/integration/lib/utils.py b/tests/integration/lib/utils.py index daa9247c..aa137cb2 100644 --- a/tests/integration/lib/utils.py +++ b/tests/integration/lib/utils.py @@ -6,6 +6,8 @@ import asyncio import base64 import json import os +from collections.abc import AsyncGenerator +from contextlib import asynccontextmanager from dataclasses import dataclass from pathlib import Path from ssl import SSLContext @@ -57,6 +59,15 @@ def b64encode(value: str): return base64.b64encode(value.encode("utf-8")).decode("utf-8") +@asynccontextmanager +async def aiohttp_client(ssl_context: SSLContext) -> AsyncGenerator[RetryClient]: + async with ( + aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, + RetryClient(session, retry_options=retry_options, raise_for_status=True) as client, + ): + yield client + + async def aiottp_get_json(url: str, ssl_context: SSLContext) -> Any: """Do an async HTTP GET against a url, retry exponentially on 429s. It expects a JSON response. @@ -70,9 +81,8 @@ async def aiottp_get_json(url: str, ssl_context: SSLContext) -> Any: host = urlparse(url).hostname async with ( - aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, - RetryClient(session, retry_options=retry_options, raise_for_status=True) as retry, - retry.get( + aiohttp_client(ssl_context) as client, + client.get( url.replace(host, "127.0.0.1"), headers={"Host": host}, server_hostname=host, @@ -99,9 +109,8 @@ async def aiohttp_post_json(url: str, data: dict, headers: dict, ssl_context: SS host = urlparse(url).hostname async with ( - aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ssl_context)) as session, - RetryClient(session, retry_options=retry_options, raise_for_status=True) as retry, - retry.post( + aiohttp_client(ssl_context) as client, + client.post( url.replace(host, "127.0.0.1"), headers=headers | {"Host": host}, server_hostname=host, json=data ) as response, ): From 39eee365868dee9b4af9302b86117259aa6ea496 Mon Sep 17 00:00:00 2001 From: Ben Banfield-Zanin Date: Mon, 24 Feb 2025 14:28:15 +0000 Subject: [PATCH 5/6] Verify unstable features and required CORS headers for QR Code Login --- tests/integration/test_synapse.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/tests/integration/test_synapse.py b/tests/integration/test_synapse.py index bdda3aba..26e4cb72 100644 --- a/tests/integration/test_synapse.py +++ b/tests/integration/test_synapse.py @@ -9,7 +9,7 @@ import pytest from .fixtures import ESSData from .lib.synapse import assert_downloaded_content, download_media, upload_media -from .lib.utils import KubeCtl, aiohttp_post_json, aiottp_get_json, value_file_has +from .lib.utils import KubeCtl, aiohttp_client, aiohttp_post_json, aiottp_get_json, value_file_has @pytest.mark.skipif(value_file_has("synapse.enabled", False), reason="Synapse not deployed") @@ -26,6 +26,9 @@ async def test_synapse_can_access_client_api( ) assert "unstable_features" in json_content + supports_qr_code_login = value_file_has("matrixAuthenticationService.enabled", True) + assert supports_qr_code_login == json_content["unstable_features"]["org.matrix.msc4108"] + @pytest.mark.skipif(value_file_has("synapse.enabled", False), reason="Synapse not deployed") @pytest.mark.parametrize("users", [("sliding-sync-user",)], indirect=True) @@ -88,3 +91,26 @@ async def test_synapse_media_upload_fetch_authenticated( content_upload_json["content_uri"].replace(f"mxc://{generated_data.server_name}/", ""), content_download_sha256, ) + + +@pytest.mark.skipif(value_file_has("synapse.enabled", False), reason="MAS not deployed") +@pytest.mark.asyncio_cooperative +async def test_rendezvous_cors_headers_are_only_set_with_mas(ingress_ready, generated_data: ESSData, ssl_context): + await ingress_ready("synapse") + async with ( + aiohttp_client(ssl_context) as client, + client.options( + f"https://synapse.{generated_data.server_name}/_matrix/client/unstable/org.matrix.msc4108/rendezvous", + ) as response, + ): + assert "Access-Control-Allow-Origin" in response.headers + assert response.headers["Access-Control-Allow-Origin"] == "*" + + assert "Access-Control-Allow-Headers" in response.headers + supports_qr_code_login = value_file_has("matrixAuthenticationService.enabled", True) + assert ("If-Match" in response.headers["Access-Control-Allow-Headers"]) == supports_qr_code_login + + assert "Access-Control-Expose-Headers" in response.headers + assert "Synapse-Trace-Id" in response.headers["Access-Control-Expose-Headers"] + assert "Server" in response.headers["Access-Control-Expose-Headers"] + assert ("ETag" in response.headers["Access-Control-Expose-Headers"]) == supports_qr_code_login From 77a33406a5756a4c2174f5a87a9d0809dcb1be9a Mon Sep 17 00:00:00 2001 From: Ben Banfield-Zanin Date: Mon, 24 Feb 2025 14:59:58 +0000 Subject: [PATCH 6/6] Update test_configs_and_mount_consistency to not look at mounts as part of e.g. URLs --- tests/manifests/test_configs_and_mounts_consistency.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/manifests/test_configs_and_mounts_consistency.py b/tests/manifests/test_configs_and_mounts_consistency.py index 7cc67e55..e695beb4 100644 --- a/tests/manifests/test_configs_and_mounts_consistency.py +++ b/tests/manifests/test_configs_and_mounts_consistency.py @@ -57,7 +57,7 @@ def get_volume_from_mount(template, volume_mount): def match_in_content(container_name, mounted_keys, mount_path, matches_in): found_mount = False for match_in in matches_in: - for match in re.findall(rf"{mount_path}/([^\s\n\")`;,]+(?!.*noqa))", match_in): + for match in re.findall(rf"(?:^|\s|\"){mount_path}/([^\s\n\")`;,]+(?!.*noqa))", match_in): assert f"{mount_path}/{match}" in mounted_keys, ( f"{mount_path}/{match} used in {container_name} but it is not found " f"from any mounted secret or configmap"