[MM-54872] Ensure matched subpaths are exact and not substrings of each other (#2958)

This commit is contained in:
Devin Binnie
2024-02-12 08:45:08 -05:00
committed by GitHub
parent 1a7516aac9
commit 68034b166f
2 changed files with 8 additions and 3 deletions

View File

@@ -13,7 +13,7 @@ jest.mock('common/config', () => ({
jest.mock('common/utils/url', () => ({ jest.mock('common/utils/url', () => ({
parseURL: jest.fn(), parseURL: jest.fn(),
isInternalURL: jest.fn(), isInternalURL: jest.fn(),
getFormattedPathName: (pathname) => (pathname.length ? pathname : '/'), getFormattedPathName: (pathname) => (pathname.endsWith('/') ? pathname : `${pathname}/`),
})); }));
jest.mock('common/utils/util', () => ({ jest.mock('common/utils/util', () => ({
isVersionGreaterThanOrEqualTo: jest.fn(), 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')}); 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', () => { it('should match the correct server with a subpath - base view', () => {
const inputURL = new URL('http://server-2.com/subpath/server'); 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')}); expect(serverManager.lookupViewByURL(inputURL)).toStrictEqual({id: 'view-2', url: new URL('http://server-2.com/subpath')});

View File

@@ -123,7 +123,7 @@ export class ServerManager extends EventEmitter {
} }
const server = this.getAllServers().find((server) => { const server = this.getAllServers().find((server) => {
return isInternalURL(parsedURL, server.url, ignoreScheme) && return isInternalURL(parsedURL, server.url, ignoreScheme) &&
getFormattedPathName(parsedURL.pathname).startsWith(server.url.pathname); getFormattedPathName(parsedURL.pathname).startsWith(getFormattedPathName(server.url.pathname));
}); });
if (!server) { if (!server) {
return undefined; return undefined;
@@ -134,7 +134,7 @@ export class ServerManager extends EventEmitter {
views. views.
filter((view) => view && view.type !== TAB_MESSAGING). filter((view) => view && view.type !== TAB_MESSAGING).
forEach((view) => { forEach((view) => {
if (getFormattedPathName(parsedURL.pathname).startsWith(view.url.pathname)) { if (getFormattedPathName(parsedURL.pathname).startsWith(getFormattedPathName(view.url.pathname))) {
selectedView = view; selectedView = view;
} }
}); });