[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
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 3 deletions

View file

@ -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')});

View file

@ -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;
}
});