From 68034b166fd21e141ebcee25f0465ce256e01361 Mon Sep 17 00:00:00 2001 From: Devin Binnie <52460000+devinbinnie@users.noreply.github.com> Date: Mon, 12 Feb 2024 08:45:08 -0500 Subject: [PATCH] [MM-54872] Ensure matched subpaths are exact and not substrings of each other (#2958) --- src/common/servers/serverManager.test.js | 7 ++++++- src/common/servers/serverManager.ts | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/common/servers/serverManager.test.js b/src/common/servers/serverManager.test.js index 72213e95..4f0a53eb 100644 --- a/src/common/servers/serverManager.test.js +++ b/src/common/servers/serverManager.test.js @@ -13,7 +13,7 @@ jest.mock('common/config', () => ({ jest.mock('common/utils/url', () => ({ parseURL: jest.fn(), isInternalURL: jest.fn(), - getFormattedPathName: (pathname) => (pathname.length ? pathname : '/'), + getFormattedPathName: (pathname) => (pathname.endsWith('/') ? pathname : `${pathname}/`), })); jest.mock('common/utils/util', () => ({ isVersionGreaterThanOrEqualTo: jest.fn(), @@ -128,6 +128,11 @@ describe('common/servers/serverManager', () => { expect(serverManager.lookupViewByURL(inputURL)).toStrictEqual({id: 'view-2', url: new URL('http://server-2.com/subpath')}); }); + it('should not match a server where the subpaths are substrings of each other ', () => { + const inputURL = new URL('http://server-2.com/subpath2'); + expect(serverManager.lookupViewByURL(inputURL)).toBe(undefined); + }); + it('should match the correct server with a subpath - base view', () => { const inputURL = new URL('http://server-2.com/subpath/server'); expect(serverManager.lookupViewByURL(inputURL)).toStrictEqual({id: 'view-2', url: new URL('http://server-2.com/subpath')}); diff --git a/src/common/servers/serverManager.ts b/src/common/servers/serverManager.ts index 1a957322..f6d5896b 100644 --- a/src/common/servers/serverManager.ts +++ b/src/common/servers/serverManager.ts @@ -123,7 +123,7 @@ export class ServerManager extends EventEmitter { } const server = this.getAllServers().find((server) => { return isInternalURL(parsedURL, server.url, ignoreScheme) && - getFormattedPathName(parsedURL.pathname).startsWith(server.url.pathname); + getFormattedPathName(parsedURL.pathname).startsWith(getFormattedPathName(server.url.pathname)); }); if (!server) { return undefined; @@ -134,7 +134,7 @@ export class ServerManager extends EventEmitter { views. filter((view) => view && view.type !== TAB_MESSAGING). forEach((view) => { - if (getFormattedPathName(parsedURL.pathname).startsWith(view.url.pathname)) { + if (getFormattedPathName(parsedURL.pathname).startsWith(getFormattedPathName(view.url.pathname))) { selectedView = view; } });