From f0281886d7c84d35591b7d06fe68f045466573f4 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Tue, 14 May 2024 13:40:10 +0100 Subject: [PATCH] Fix well-known lookup for sliding sync labs check (#12519) * Fix well-known lookup for sliding sync labs check Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Add test Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Iterate Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --------- Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/SlidingSyncManager.ts | 6 +++--- test/SlidingSyncManager-test.ts | 32 +++++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/SlidingSyncManager.ts b/src/SlidingSyncManager.ts index 8ccdfe868c..c4387e85d6 100644 --- a/src/SlidingSyncManager.ts +++ b/src/SlidingSyncManager.ts @@ -359,10 +359,10 @@ export class SlidingSyncManager { let proxyUrl: string | undefined; try { - const clientWellKnown = await AutoDiscovery.findClientConfig(client.baseUrl); + const clientWellKnown = await AutoDiscovery.findClientConfig(client.getDomain()!); proxyUrl = clientWellKnown?.["org.matrix.msc3575.proxy"]?.url; } catch (e) { - // client.baseUrl is invalid, `AutoDiscovery.findClientConfig` has thrown + // client.getDomain() is invalid, `AutoDiscovery.findClientConfig` has thrown } if (proxyUrl != undefined) { @@ -401,7 +401,7 @@ export class SlidingSyncManager { const proxyUrl = await this.getProxyFromWellKnown(client); if (proxyUrl != undefined) { - const response = await fetch(proxyUrl + "/client/server.json", { + const response = await fetch(new URL("/client/server.json", proxyUrl), { method: Method.Get, signal: timeoutSignal(10 * 1000), // 10s }); diff --git a/test/SlidingSyncManager-test.ts b/test/SlidingSyncManager-test.ts index d93de8f572..9cb30c5b6f 100644 --- a/test/SlidingSyncManager-test.ts +++ b/test/SlidingSyncManager-test.ts @@ -17,6 +17,7 @@ limitations under the License. import { SlidingSync } from "matrix-js-sdk/src/sliding-sync"; import { mocked } from "jest-mock"; import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix"; +import fetchMockJest from "fetch-mock-jest"; import { SlidingSyncManager } from "../src/SlidingSyncManager"; import { stubClient } from "./test-utils"; @@ -39,6 +40,8 @@ describe("SlidingSyncManager", () => { mocked(client.getRoom).mockReturnValue(null); manager.configure(client, "invalid"); manager.slidingSync = slidingSync; + fetchMockJest.reset(); + fetchMockJest.get("https://proxy/client/server.json", {}); }); describe("setRoomVisible", () => { @@ -236,7 +239,7 @@ describe("SlidingSyncManager", () => { describe("checkSupport", () => { beforeEach(() => { SlidingSyncController.serverSupportsSlidingSync = false; - jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy"); + jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("https://proxy/"); }); it("shorts out if the server has 'native' sliding sync support", async () => { jest.spyOn(manager, "nativeSlidingSyncSupport").mockResolvedValue(true); @@ -252,6 +255,25 @@ describe("SlidingSyncManager", () => { expect(manager.getProxyFromWellKnown).toHaveBeenCalled(); expect(SlidingSyncController.serverSupportsSlidingSync).toBeTruthy(); }); + it("should query well-known on server_name not baseUrl", async () => { + fetchMockJest.get("https://matrix.org/.well-known/matrix/client", { + "m.homeserver": { + base_url: "https://matrix-client.matrix.org", + server: "matrix.org", + }, + "org.matrix.msc3575.proxy": { + url: "https://proxy/", + }, + }); + fetchMockJest.get("https://matrix-client.matrix.org/_matrix/client/versions", { versions: ["v1.4"] }); + + mocked(manager.getProxyFromWellKnown).mockRestore(); + jest.spyOn(manager, "nativeSlidingSyncSupport").mockResolvedValue(false); + expect(SlidingSyncController.serverSupportsSlidingSync).toBeFalsy(); + await manager.checkSupport(client); + expect(SlidingSyncController.serverSupportsSlidingSync).toBeTruthy(); + expect(fetchMockJest).not.toHaveFetched("https://matrix-client.matrix.org/.well-known/matrix/client"); + }); }); describe("nativeSlidingSyncSupport", () => { beforeEach(() => { @@ -266,7 +288,7 @@ describe("SlidingSyncManager", () => { expect(feature).toBe("org.matrix.msc3575"); return true; }); - const proxySpy = jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy"); + const proxySpy = jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("https://proxy/"); expect(SlidingSyncController.serverSupportsSlidingSync).toBeFalsy(); await manager.checkSupport(client); // first thing it does is call nativeSlidingSyncSupport @@ -287,14 +309,14 @@ describe("SlidingSyncManager", () => { expect(manager.startSpidering).toHaveBeenCalled(); }); it("uses the proxy declared in the client well-known", async () => { - jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy"); + jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("https://proxy/"); await manager.setup(client); expect(manager.configure).toHaveBeenCalled(); - expect(manager.configure).toHaveBeenCalledWith(client, "proxy"); + expect(manager.configure).toHaveBeenCalledWith(client, "https://proxy/"); expect(manager.startSpidering).toHaveBeenCalled(); }); it("uses the legacy `feature_sliding_sync_proxy_url` if it was set", async () => { - jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy"); + jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("https://proxy/"); jest.spyOn(SettingsStore, "getValue").mockImplementation((name: string) => { if (name === "feature_sliding_sync_proxy_url") return "legacy-proxy"; });