Skip to content

Commit

Permalink
[MM-61040] Handle custom protocols in plugins popouts (#3166)
Browse files Browse the repository at this point in the history
* Handle custom protocols in plugins popouts

* Improve
  • Loading branch information
streamer45 authored Oct 14, 2024
1 parent 4ac0d68 commit 071e9d3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/main/views/pluginsPopUps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import ViewManager from 'main/views/viewManager';

import PluginsPopUpsManager from './pluginsPopUps';

import allowProtocolDialog from '../allowProtocolDialog';

jest.mock('electron', () => ({
shell: {
openExternal: jest.fn(),
Expand Down Expand Up @@ -41,6 +43,10 @@ jest.mock('../contextMenu', () => {
});
});

jest.mock('../allowProtocolDialog', () => ({
handleDialogEvent: jest.fn(),
}));

describe('PluginsPopUpsManager', () => {
afterEach(() => {
jest.resetAllMocks();
Expand Down Expand Up @@ -121,6 +127,11 @@ describe('PluginsPopUpsManager', () => {
expect(ServerViewState.switchServer).toHaveBeenCalledWith(4545);
expect(ViewManager.handleDeepLink).toHaveBeenCalledWith(parseURL('http://localhost:8065/team/channel'));

// Verify opening custom protocols is handled through allowProtocolDialog
expect(handlers['window-open']({url: 'custom:somelink'})).toEqual({action: 'deny'});
expect(allowProtocolDialog.handleDialogEvent).toBeCalledWith('custom:', 'custom:somelink');
expect(shell.openExternal).not.toHaveBeenCalledWith('custom:somelink');

// Verify opening external links is allowed through browser
expect(handlers['window-open']({url: 'https://www.example.com'})).toEqual({action: 'deny'});
expect(shell.openExternal).toHaveBeenCalledWith('https://www.example.com');
Expand Down
12 changes: 10 additions & 2 deletions src/main/views/pluginsPopUps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import {
} from 'common/utils/url';
import ContextMenu from 'main/contextMenu';
import ViewManager from 'main/views/viewManager';
import {generateHandleConsoleMessage} from 'main/views/webContentEventsCommon';
import {generateHandleConsoleMessage, isCustomProtocol} from 'main/views/webContentEventsCommon';
import MainWindow from 'main/windows/mainWindow';

import allowProtocolDialog from '../allowProtocolDialog';

const log = new Logger('PluginsPopUpsManager');

type PluginPopUp = {
Expand Down Expand Up @@ -76,8 +78,14 @@ export class PluginsPopUpsManager {

const serverView = ViewManager.getViewByWebContentsId(parentId)?.view;

// Check for custom protocol
if (isCustomProtocol(parsedURL)) {
allowProtocolDialog.handleDialogEvent(parsedURL.protocol, url);
return {action: 'deny'};
}

// We allow internal (i.e., same server) links to be routed as expected.
if (serverView && parsedURL && isTeamUrl(serverView.server.url, parsedURL, true)) {
if (serverView && isTeamUrl(serverView.server.url, parsedURL, true)) {
ServerViewState.switchServer(serverView.server.id);
MainWindow.get()?.focus();
ViewManager.handleDeepLink(parsedURL);
Expand Down
6 changes: 2 additions & 4 deletions src/main/views/webContentEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ import ViewManager from 'main/views/viewManager';
import CallsWidgetWindow from 'main/windows/callsWidgetWindow';
import MainWindow from 'main/windows/mainWindow';

import {generateHandleConsoleMessage} from './webContentEventsCommon';
import {generateHandleConsoleMessage, isCustomProtocol} from './webContentEventsCommon';

import {protocols} from '../../../electron-builder.json';
import allowProtocolDialog from '../allowProtocolDialog';
import {composeUserAgent} from '../utils';

Expand All @@ -42,7 +41,6 @@ type CustomLogin = {
}

const log = new Logger('WebContentsEventManager');
const scheme = protocols && protocols[0] && protocols[0].schemes && protocols[0].schemes[0];

export class WebContentsEventManager {
customLogins: Record<number, CustomLogin>;
Expand Down Expand Up @@ -169,7 +167,7 @@ export class WebContentsEventManager {
}

// Check for custom protocol
if (parsedURL.protocol !== 'http:' && parsedURL.protocol !== 'https:' && parsedURL.protocol !== `${scheme}:`) {
if (isCustomProtocol(parsedURL)) {
allowProtocolDialog.handleDialogEvent(parsedURL.protocol, details.url);
return {action: 'deny'};
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/views/webContentEventsCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type {Event} from 'electron';
import type {Logger} from 'common/log';
import {getLevel} from 'common/log';

import {protocols} from '../../../electron-builder.json';

enum ConsoleMessageLevel {
Verbose,
Info,
Expand Down Expand Up @@ -34,3 +36,8 @@ export const generateHandleConsoleMessage = (log: Logger) => (_: Event, level: n

logFn(...entries);
};

export function isCustomProtocol(url: URL) {
const scheme = protocols && protocols[0] && protocols[0].schemes && protocols[0].schemes[0];
return url.protocol !== 'http:' && url.protocol !== 'https:' && url.protocol !== `${scheme}:`;
}

0 comments on commit 071e9d3

Please sign in to comment.