CVE-2020-15258: Improper Input Validation (CWE-20)
value: In Wire before 3.20.x, `shell.openExternal` was used without checking the URL. This vulnerability allows an attacker to execute code on the victims machine by sending messages containing links with arbitrary protocols. The victim has to interact with the link and sees the URL that is opened. The issue was patched by implementing a helper function which checks if the URL's protocol is common. If it is common, the URL will be opened externally. If not, the URL will not be opened and a warning appears for the user informing them that a probably insecure URL was blocked from being executed. The issue is patched in Wire 3.20.x. More technical details about exploitation are available in the linked advisory.
CWE description: The product receives input or data, but it does
        not validate or incorrectly validates that the input has the
        properties that are required to process the data safely and
        correctly.
CVSS v3 base score: 8
Published: 2020-10-16T17:15Z
Repository: https://github.com/wireapp/wire-desktop
Language: TypeScript
Files changed: electron/html/about.html, electron/src/lib/showDialog.ts, electron/src/main.ts, electron/src/menu/system.ts, electron/src/window/AboutWindow.ts, electron/src/window/WindowUtil.test.ts, electron/src/window/WindowUtil.ts, yarn.lock
Commit date: 2020-09-22 14:26:11 +0200
Commit message: Merge pull request from GHSA-5gpx-9976-ggpm

Diff: diff --git a/electron/html/about.html b/electron/html/about.html
index 2375befb..665fd596 100644
--- a/electron/html/about.html
+++ b/electron/html/about.html
@@ -9,14 +9,18 @@
       <img id="logo" />
       <h1 id="name"></h1>
       <p class="copy"><span data-string="aboutVersion"></span> <span id="version"></span></p>
       <p class="webappVersion copy"><span data-string="aboutWebappVersion"></span> <span id="webappVersion"></span></p>
       <p>
-        <a href="https://support.wire.com/hc/articles/115001919905"><span data-string="aboutUpdate"></span></a>
+        <a href="https://support.wire.com/hc/articles/115001919905" target="_blank">
+          <span data-string="aboutUpdate"></span>
+        </a>
       </p>
       <p>
-        <a href="https://medium.com/wire-news/webapp-updates/home"><span data-string="aboutReleases"></span></a>
+        <a href="https://medium.com/wire-news/webapp-updates/home" target="_blank">
+          <span data-string="aboutReleases"></span>
+        </a>
       </p>
       <p><span id="copyright"></span></p>
     </div>
   </body>
 </html>
diff --git a/electron/src/lib/showDialog.ts b/electron/src/lib/showDialog.ts
index ab9ed447..6a5e0ddd 100644
--- a/electron/src/lib/showDialog.ts
+++ b/electron/src/lib/showDialog.ts
@@ -16,10 +16,11 @@
  * along with this program. If not, see http://www.gnu.org/licenses/.
  *
  */
 
 import {dialog, MessageBoxSyncOptions} from 'electron';
+import * as locale from '../locale/locale';
 
 export const showDialog = (message: string, title: string, type?: string): void => {
   const options: MessageBoxSyncOptions = {
     message,
     title,
@@ -29,5 +30,15 @@ export const showDialog = (message: string, title: string, type?: string): void
 };
 
 export const showErrorDialog = (message: string): void => {
   showDialog(message, 'Error', 'error');
 };
+
+export const showWarningDialog = (message: string): void => {
+  const options: MessageBoxSyncOptions = {
+    buttons: ['OK'],
+    message,
+    title: 'Warning',
+    type: 'warning',
+  };
+  dialog.showMessageBoxSync(options);
+};
diff --git a/electron/src/main.ts b/electron/src/main.ts
index 373d2eeb..883fcc7d 100644
--- a/electron/src/main.ts
+++ b/electron/src/main.ts
@@ -26,11 +26,10 @@ import {
   Filter,
   HeadersReceivedResponse,
   ipcMain,
   Menu,
   OnHeadersReceivedListenerDetails,
-  shell,
   WebContents,
 } from 'electron';
 import * as fs from 'fs-extra';
 import {getProxySettings} from 'get-proxy-settings';
 import * as logdown from 'logdown';
@@ -262,11 +261,11 @@ const showMainWindow = async (mainWindowState: windowStateKeeper.State): Promise
     if (typeof (event as any).sender.viewInstanceId !== 'undefined') {
       logger.log('New window was created from a webview, aborting.');
       return;
     }
 
-    await shell.openExternal(url);
+    return WindowUtil.openExternal(url);
   });
 
   main.on('focus', () => {
     systemMenu.registerGlobalShortcuts();
     main.flashFrame(false);
@@ -505,11 +504,11 @@ class ElectronWrapperInit {
       if (SingleSignOn.isSingleSignOnLoginWindow(frameName)) {
         return new SingleSignOn(main, event, url, options).init();
       }
 
       this.logger.log('Opening an external window from a webview.');
-      return shell.openExternal(url);
+      return WindowUtil.openExternal(url);
     };
 
     const willNavigateInWebview = (event: ElectronEvent, url: string, baseUrl: string): void => {
       // Ensure navigation is to an allowed domain
       if (OriginValidator.isMatchingHost(url, baseUrl)) {
diff --git a/electron/src/menu/system.ts b/electron/src/menu/system.ts
index 3a867666..a18b639d 100644
--- a/electron/src/menu/system.ts
+++ b/electron/src/menu/system.ts
@@ -16,11 +16,11 @@
  * along with this program. If not, see http://www.gnu.org/licenses/.
  *
  */
 
 import autoLaunch = require('auto-launch');
-import {dialog, globalShortcut, ipcMain, Menu, MenuItemConstructorOptions, shell} from 'electron';
+import {dialog, globalShortcut, ipcMain, Menu, MenuItemConstructorOptions} from 'electron';
 import * as path from 'path';
 
 import {EVENT_TYPE} from '../lib/eventType';
 import * as locale from '../locale/locale';
 import {getLogger} from '../logging/getLogger';
@@ -31,10 +31,11 @@ import {config} from '../settings/config';
 import {settings} from '../settings/ConfigurationPersistence';
 import {SettingsType} from '../settings/SettingsType';
 import {WindowManager} from '../window/WindowManager';
 import {downloadLogs} from '../lib/download';
 import {zipFiles, createFile} from '../lib/zip';
+import * as WindowUtil from '../window/WindowUtil';
 
 const launchCmd = process.env.APPIMAGE || process.execPath;
 
 const logger = getLogger(path.basename(__filename));
 
@@ -249,27 +250,27 @@ const downloadLogsTemplate: MenuItemConstructorOptions = {
 const helpTemplate: MenuItemConstructorOptions = {
   label: `&${locale.getText('menuHelp')}`,
   role: 'help',
   submenu: [
     {
-      click: () => shell.openExternal(config.legalUrl),
+      click: () => WindowUtil.openExternal(config.legalUrl, true),
       label: locale.getText('menuLegal'),
     },
     {
-      click: () => shell.openExternal(config.privacyUrl),
+      click: () => WindowUtil.openExternal(config.privacyUrl, true),
       label: locale.getText('menuPrivacy'),
     },
     {
-      click: () => shell.openExternal(config.licensesUrl),
+      click: () => WindowUtil.openExternal(config.licensesUrl, true),
       label: locale.getText('menuLicense'),
     },
     {
-      click: () => shell.openExternal(config.supportUrl),
+      click: () => WindowUtil.openExternal(config.supportUrl, true),
       label: locale.getText('menuSupport'),
     },
     {
-      click: () => shell.openExternal(EnvironmentUtil.web.getWebsiteUrl()),
+      click: () => WindowUtil.openExternal(EnvironmentUtil.web.getWebsiteUrl(), true),
       label: locale.getText('menuAppURL'),
     },
     downloadLogsTemplate,
   ],
 };
diff --git a/electron/src/window/AboutWindow.ts b/electron/src/window/AboutWindow.ts
index c8c3d2f3..058e7420 100644
--- a/electron/src/window/AboutWindow.ts
+++ b/electron/src/window/AboutWindow.ts
@@ -15,19 +15,20 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see http://www.gnu.org/licenses/.
  *
  */
 
-import {app, BrowserWindow, ipcMain, session, shell} from 'electron';
+import {app, BrowserWindow, ipcMain, session} from 'electron';
 import fileUrl = require('file-url');
 import * as path from 'path';
 
 import {EVENT_TYPE} from '../lib/eventType';
 import * as locale from '../locale/locale';
 import * as EnvironmentUtil from '../runtime/EnvironmentUtil';
 import {config} from '../settings/config';
 import {getLogger} from '../logging/getLogger';
+import * as WindowUtil from '../window/WindowUtil';
 
 const logger = getLogger(path.basename(__filename));
 
 let webappVersion: string;
 
@@ -87,18 +88,23 @@ const showWindow = async () => {
     aboutWindow.webContents.session.webRequest.onBeforeRequest(async ({url}, callback) => {
       // Only allow those URLs to be opened within the window
       if (ABOUT_WINDOW_ALLOWLIST.includes(url)) {
         return callback({cancel: false});
       }
+    });
+
+    // Handle the new window event in the About Window
+    aboutWindow.webContents.on('new-window', (event, url) => {
+      event.preventDefault();
 
-      // Open HTTPS links in browser instead
-      if (url.startsWith('https://')) {
-        await shell.openExternal(url);
-      } else {
-        logger.info(`Attempt to open URL "${url}" in window prevented.`);
-        callback({redirectURL: ABOUT_HTML});
+      // Ensure the link does not come from a webview
+      if (typeof (event as any).sender.viewInstanceId !== 'undefined') {
+        logger.log('New window was created from a webview, aborting.');
+        return;
       }
+
+      return WindowUtil.openExternal(url, true);
     });
 
     // Locales
     ipcMain.on(EVENT_TYPE.ABOUT.LOCALE_VALUES, (event, labels: locale.i18nLanguageIdentifier[]) => {
       if (aboutWindow) {
diff --git a/electron/src/window/WindowUtil.test.ts b/electron/src/window/WindowUtil.test.ts
new file mode 100644
index 00000000..d0f4a4ed
--- /dev/null
+++ b/electron/src/window/WindowUtil.test.ts
@@ -0,0 +1,73 @@
+/*
+ * Wire
+ * Copyright (C) 2020 Wire Swiss GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/.
+ *
+ */
+
+import {dialog, shell} from 'electron';
+import * as assert from 'assert';
+import * as sinon from 'sinon';
+
+import * as WindowUtil from './WindowUtil';
+
+describe('WindowUtil', () => {
+  describe('openExternal', () => {
+    let dialogStub: sinon.SinonStub;
+    let shellStub: sinon.SinonStub;
+
+    beforeEach(() => {
+      shellStub = sinon.stub(shell, 'openExternal').returns(Promise.resolve());
+      dialogStub = sinon.stub(dialog, 'showMessageBoxSync').returns(0);
+    });
+
+    afterEach(() => {
+      dialogStub.restore();
+      shellStub.restore();
+    });
+
+    it('opens secure URLs externally', async () => {
+      const url = 'https://github.com/wireapp/wire-desktop';
+      await WindowUtil.openExternal(url);
+
+      assert.ok(shellStub.firstCall.calledWith(url));
+      assert.ok(dialogStub.notCalled);
+    });
+
+    it('opens insecure URLs externally if allowed', async () => {
+      const url = 'http://github.com/wireapp/wire-desktop';
+      await WindowUtil.openExternal(url, false);
+
+      assert.ok(shellStub.firstCall.calledWith(url));
+      assert.ok(dialogStub.notCalled);
+    });
+
+    it(`doesn't open insecure URLs externally if not allowed`, async () => {
+      const url = 'http://example.org';
+      await WindowUtil.openExternal(url, true);
+
+      assert.ok(shellStub.notCalled);
+      assert.ok(dialogStub.called);
+    });
+
+    it(`doesn't open non-website URLs externally`, async () => {
+      const url = 'smb://attacker.tld/public/pwn.desktop';
+      await WindowUtil.openExternal(url);
+
+      assert.ok(shellStub.notCalled);
+      assert.ok(dialogStub.called);
+    });
+  });
+});
diff --git a/electron/src/window/WindowUtil.ts b/electron/src/window/WindowUtil.ts
index 022e903d..82540f36 100644
--- a/electron/src/window/WindowUtil.ts
+++ b/electron/src/window/WindowUtil.ts
@@ -15,11 +15,18 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see http://www.gnu.org/licenses/.
  *
  */
 
-import {BrowserWindow, screen} from 'electron';
+import {BrowserWindow, screen, shell} from 'electron';
+import * as path from 'path';
+import * as URL from 'url';
+
+import {getLogger} from '../logging/getLogger';
+import {showWarningDialog} from '../lib/showDialog';
+
+const logger = getLogger(path.basename(__filename));
 
 interface Rectangle {
   height: number;
   width: number;
   x: number;
@@ -44,5 +51,26 @@ export const isInView = (win: BrowserWindow): boolean => {
     nearestWorkArea,
   );
 
   return upperLeftVisible || lowerRightVisible;
 };
+
+export const openExternal = async (url: string, httpsOnly: boolean = false): Promise<void> => {
+  try {
+    const urlProtocol = URL.parse(url).protocol || '';
+    const allowedProtocols = ['https:'];
+
+    if (!httpsOnly) {
+      allowedProtocols.push('ftp:', 'http:', 'mailto:');
+    }
+
+    if (!allowedProtocols.includes(urlProtocol)) {
+      logger.warn(`Prevented opening external URL "${url}".`);
+      showWarningDialog(`Prevented opening external URL "${url}".`);
+      return;
+    }
+
+    await shell.openExternal(url);
+  } catch (error) {
+    logger.error(error);
+  }
+};
diff --git a/yarn.lock b/yarn.lock
index f12b6434..7b300825 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5343,20 +5343,10 @@ fs-extra-p@^8.0.2:
   integrity sha512-sCLpU5kk5CvrWZvFM9dUlqPgHrE02AEt6XYzF7kDscr5COc7DHfhNfODTXt0bkVNmt5DkvU2uJSYjorxY3bRKA==
   dependencies:
     bluebird-lst "^1.0.9"
     fs-extra "^8.1.0"
 
-fs-extra@9.0.0:
-  version "9.0.0"
-  resolved "https://registry.npmjs.org/fs-extra/-/fs-extra-9.0.0.tgz#b6afc31036e247b2466dc99c29ae797d5d4580a3"
-  integrity sha512-pmEYSk3vYsG/bF651KPUXZ+hvjpgWYw/Gc7W9NFUe3ZVLczKKWIij3IKpOrQcdw4TILtibFslZ0UmR8Vvzig4g==
-  dependencies:
-    at-least-node "^1.0.0"
-    graceful-fs "^4.2.0"
-    jsonfile "^6.0.1"
-    universalify "^1.0.0"
-
 fs-extra@9.0.1, fs-extra@^9.0.0, fs-extra@^9.0.1:
   version "9.0.1"
   resolved "https://registry.npmjs.org/fs-extra/-/fs-extra-9.0.1.tgz#910da0062437ba4c39fedd863f1675ccfefcb9fc"
   integrity sha512-h2iAoN838FqAFJY2/qVpzFXy+EBxfVE220PalAqQLDVsFOHLJrZvut5puAbCdNv6WJk+B8ihI+k0c7JK5erwqQ==
   dependencies:

Vulnerable code:         <a href="https://support.wire.com/hc/articles/115001919905"><span data-string="aboutUpdate"></span></a>
        <a href="https://medium.com/wire-news/webapp-updates/home"><span data-string="aboutReleases"></span></a>
  shell,
    await shell.openExternal(url);
      return shell.openExternal(url);
import {dialog, globalShortcut, ipcMain, Menu, MenuItemConstructorOptions, shell} from 'electron';
      click: () => shell.openExternal(config.legalUrl),
      click: () => shell.openExternal(config.privacyUrl),
      click: () => shell.openExternal(config.licensesUrl),
      click: () => shell.openExternal(config.supportUrl),
      click: () => shell.openExternal(EnvironmentUtil.web.getWebsiteUrl()),
import {app, BrowserWindow, ipcMain, session, shell} from 'electron';
      // Open HTTPS links in browser instead
      if (url.startsWith('https://')) {
        await shell.openExternal(url);
      } else {
        logger.info(`Attempt to open URL "${url}" in window prevented.`);
        callback({redirectURL: ABOUT_HTML});
import {BrowserWindow, screen} from 'electron';
fs-extra@9.0.0:
  version "9.0.0"
  resolved "https://registry.npmjs.org/fs-extra/-/fs-extra-9.0.0.tgz#b6afc31036e247b2466dc99c29ae797d5d4580a3"
  integrity sha512-pmEYSk3vYsG/bF651KPUXZ+hvjpgWYw/Gc7W9NFUe3ZVLczKKWIij3IKpOrQcdw4TILtibFslZ0UmR8Vvzig4g==
  dependencies:
    at-least-node "^1.0.0"
    graceful-fs "^4.2.0"
    jsonfile "^6.0.1"
    universalify "^1.0.0"

Fixed code:         <a href="https://support.wire.com/hc/articles/115001919905" target="_blank">
          <span data-string="aboutUpdate"></span>
        </a>
        <a href="https://medium.com/wire-news/webapp-updates/home" target="_blank">
          <span data-string="aboutReleases"></span>
        </a>
import * as locale from '../locale/locale';

export const showWarningDialog = (message: string): void => {
  const options: MessageBoxSyncOptions = {
    buttons: ['OK'],
    message,
    title: 'Warning',
    type: 'warning',
  };
  dialog.showMessageBoxSync(options);
};
    return WindowUtil.openExternal(url);
      return WindowUtil.openExternal(url);
import {dialog, globalShortcut, ipcMain, Menu, MenuItemConstructorOptions} from 'electron';
import * as WindowUtil from '../window/WindowUtil';
      click: () => WindowUtil.openExternal(config.legalUrl, true),
      click: () => WindowUtil.openExternal(config.privacyUrl, true),
      click: () => WindowUtil.openExternal(config.licensesUrl, true),
      click: () => WindowUtil.openExternal(config.supportUrl, true),
      click: () => WindowUtil.openExternal(EnvironmentUtil.web.getWebsiteUrl(), true),
import {app, BrowserWindow, ipcMain, session} from 'electron';
import * as WindowUtil from '../window/WindowUtil';
    });

    // Handle the new window event in the About Window
    aboutWindow.webContents.on('new-window', (event, url) => {
      event.preventDefault();
      // Ensure the link does not come from a webview
      if (typeof (event as any).sender.viewInstanceId !== 'undefined') {
        logger.log('New window was created from a webview, aborting.');
        return;

      return WindowUtil.openExternal(url, true);
/*
 * Wire
 * Copyright (C) 2020 Wire Swiss GmbH
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 *
 */

import {dialog, shell} from 'electron';
import * as assert from 'assert';
import * as sinon from 'sinon';

import * as WindowUtil from './WindowUtil';

describe('WindowUtil', () => {
  describe('openExternal', () => {
    let dialogStub: sinon.SinonStub;
    let shellStub: sinon.SinonStub;

    beforeEach(() => {
      shellStub = sinon.stub(shell, 'openExternal').returns(Promise.resolve());
      dialogStub = sinon.stub(dialog, 'showMessageBoxSync').returns(0);
    });

    afterEach(() => {
      dialogStub.restore();
      shellStub.restore();
    });

    it('opens secure URLs externally', async () => {
      const url = 'https://github.com/wireapp/wire-desktop';
      await WindowUtil.openExternal(url);

      assert.ok(shellStub.firstCall.calledWith(url));
      assert.ok(dialogStub.notCalled);
    });

    it('opens insecure URLs externally if allowed', async () => {
      const url = 'http://github.com/wireapp/wire-desktop';
      await WindowUtil.openExternal(url, false);

      assert.ok(shellStub.firstCall.calledWith(url));
      assert.ok(dialogStub.notCalled);
    });

    it(`doesn't open insecure URLs externally if not allowed`, async () => {
      const url = 'http://example.org';
      await WindowUtil.openExternal(url, true);

      assert.ok(shellStub.notCalled);
      assert.ok(dialogStub.called);
    });

    it(`doesn't open non-website URLs externally`, async () => {
      const url = 'smb://attacker.tld/public/pwn.desktop';
      await WindowUtil.openExternal(url);

      assert.ok(shellStub.notCalled);
      assert.ok(dialogStub.called);
    });
  });
});
import {BrowserWindow, screen, shell} from 'electron';
import * as path from 'path';
import * as URL from 'url';

import {getLogger} from '../logging/getLogger';
import {showWarningDialog} from '../lib/showDialog';

const logger = getLogger(path.basename(__filename));

export const openExternal = async (url: string, httpsOnly: boolean = false): Promise<void> => {
  try {
    const urlProtocol = URL.parse(url).protocol || '';
    const allowedProtocols = ['https:'];

    if (!httpsOnly) {
      allowedProtocols.push('ftp:', 'http:', 'mailto:');
    }

    if (!allowedProtocols.includes(urlProtocol)) {
      logger.warn(`Prevented opening external URL "${url}".`);
      showWarningDialog(`Prevented opening external URL "${url}".`);
      return;
    }

    await shell.openExternal(url);
  } catch (error) {
    logger.error(error);
  }
};