[MM-44607] Only cleanse subpath if it occurs at the beginning (#2129)

* [MM-44607] Only cleanse subpath if it occurs at the beginning

* Fix tests
This commit is contained in:
Devin Binnie 2022-05-27 11:37:07 -04:00 committed by GitHub
parent 42a53e31b5
commit b4a779a666
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 1 deletions

View file

@ -303,6 +303,20 @@ describe('common/utils/url', () => {
});
});
describe('cleanPathName', () => {
it('should not clean path name if it occurs other than the beginning', () => {
expect(urlUtils.cleanPathName('/mattermost', '/home/channels/mattermost/test')).toBe('/home/channels/mattermost/test');
});
it('should clean path name if it occurs at the beginning', () => {
expect(urlUtils.cleanPathName('/mattermost', '/mattermost/channels/home/test')).toBe('/channels/home/test');
});
it('should do nothing if it doesnt occur', () => {
expect(urlUtils.cleanPathName('/mattermost', '/channels/home/test')).toBe('/channels/home/test');
});
});
describe('isCustomLoginURL', () => {
it('should match correct URL', () => {
expect(urlUtils.isCustomLoginURL(

View file

@ -208,6 +208,18 @@ function isChannelExportUrl(serverUrl: URL | string, inputUrl: URL | string): bo
return isUrlType('plugins/com.mattermost.plugin-channel-export/api/v1/export', serverUrl, inputUrl);
}
function cleanPathName(basePathName: string, pathName: string) {
if (basePathName === '/') {
return pathName;
}
if (pathName.startsWith(basePathName)) {
return pathName.replace(basePathName, '');
}
return pathName;
}
export default {
isValidURL,
isValidURI,
@ -224,4 +236,5 @@ export default {
isCustomLoginURL,
isChannelExportUrl,
isUrlType,
cleanPathName,
};

View file

@ -46,6 +46,7 @@ jest.mock('common/utils/url', () => ({
isTeamUrl: jest.fn(),
isAdminUrl: jest.fn(),
getView: jest.fn(),
cleanPathName: jest.fn(),
}));
jest.mock('common/tabs/TabView', () => ({
getTabViewName: jest.fn(),
@ -770,6 +771,7 @@ describe('main/windows/windowManager', () => {
],
},
];
urlUtils.cleanPathName.mockImplementation((base, path) => path);
});
afterEach(() => {

View file

@ -588,7 +588,7 @@ export class WindowManager {
log.debug('WwindowManager.handleBrowserHistoryPush', {viewName, pathName});
const currentView = this.viewManager?.views.get(viewName);
const cleanedPathName = currentView?.tab.server.url.pathname === '/' ? pathName : pathName.replace(currentView?.tab.server.url.pathname || '', '');
const cleanedPathName = urlUtils.cleanPathName(currentView?.tab.server.url.pathname || '', pathName);
const redirectedViewName = urlUtils.getView(`${currentView?.tab.server.url}${cleanedPathName}`, Config.teams)?.name || viewName;
if (this.viewManager?.closedViews.has(redirectedViewName)) {
// If it's a closed view, just open it and stop