From 6b09eec4c51ffb02422ade4c866581aef30d0953 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Thu, 29 Nov 2018 12:04:25 -0800 Subject: [PATCH 1/3] Some improvments to navigation and messaging. --- src/main.js | 64 ++++++++++++++++++++++++++++++++++-------- src/main/mainWindow.js | 1 + 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/main.js b/src/main.js index 93345a3d..9e4eaa14 100644 --- a/src/main.js +++ b/src/main.js @@ -6,6 +6,8 @@ import os from 'os'; import path from 'path'; +import {URL} from 'url'; + import electron from 'electron'; const { app, @@ -307,22 +309,34 @@ app.on('certificate-error', (event, webContents, url, error, certificate, callba if (certificateStore.isExisting(url)) { detail = 'Certificate is different from previous one.\n\n' + detail; } - dialog.showMessageBox(mainWindow, { - title: 'Certificate error', - message: `Do you trust certificate from "${certificate.issuerName}"?`, - detail, - type: 'warning', + title: 'Certificate Error', + message: 'There is a configuration issue with the Mattermost server you are trying to connect to, or someone is trying to intercept your connection. You also may need to sign into the Wi-Fi you are connected to using your web browser.', + type: 'error', buttons: [ - 'Yes', - 'No', + 'More Details', + 'Cancel Connection', ], cancelId: 1, }, (response) => { if (response === 0) { - certificateStore.add(url, certificate); - certificateStore.save(); - webContents.loadURL(url); + dialog.showMessageBox(mainWindow, { + title: 'Certificate Error', + message: `Certificate from "${certificate.issuerName}" is not trusted.`, + detail, + type: 'error', + buttons: [ + 'Continue Insecurely', + 'Cancel Connection', + ], + cancelId: 1, + }, (responseTwo) => { //eslint-disable-line max-nested-callbacks + if (responseTwo === 0) { + certificateStore.add(url, certificate); + certificateStore.save(); + webContents.loadURL(url); + } + }); } }); callback(false); @@ -350,8 +364,8 @@ app.on('login', (event, webContents, request, authInfo, callback) => { allowProtocolDialog.init(mainWindow); -ipcMain.on('download-url', (event, URL) => { - downloadURL(mainWindow, URL, (err) => { +ipcMain.on('download-url', (event, url) => { + downloadURL(mainWindow, url, (err) => { if (err) { dialog.showMessageBox(mainWindow, { type: 'error', @@ -650,3 +664,29 @@ app.on('ready', () => { // Open the DevTools. // mainWindow.openDevTools(); }); + +app.on('web-contents-created', (dc, contents) => { + contents.on('will-attach-webview', (event, webPreferences) => { + webPreferences.nodeIntegration = false; + webPreferences.contextIsolation = true; + }); + contents.on('will-navigate', (event, navigationUrl) => { + const parsedUrl = new URL(navigationUrl); + const trustedURLs = settings.mergeDefaultTeams(config.teams).map((team) => new URL(team.url)); //eslint-disable-line max-nested-callbacks + + let trusted = false; + for (const url of trustedURLs) { + if (parsedUrl.origin === url.origin) { + trusted = true; + break; + } + } + + if (!trusted) { + event.preventDefault(); + } + }); + contents.on('new-window', (event) => { + event.preventDefault(); + }); +}); diff --git a/src/main/mainWindow.js b/src/main/mainWindow.js index 47adb886..4b970353 100644 --- a/src/main/mainWindow.js +++ b/src/main/mainWindow.js @@ -67,6 +67,7 @@ function createMainWindow(config, options) { mainWindow.webContents.on('will-attach-webview', (event, webPreferences) => { webPreferences.nodeIntegration = false; + webPreferences.contextIsolation = true; }); mainWindow.once('ready-to-show', () => { From 51dd3718944098edfdf093c988ba5d92e14c8e75 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Fri, 7 Dec 2018 08:11:27 -0800 Subject: [PATCH 2/3] Wording fixes. Co-Authored-By: crspeller --- src/main.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.js b/src/main.js index 9e4eaa14..6a019e7c 100644 --- a/src/main.js +++ b/src/main.js @@ -311,7 +311,7 @@ app.on('certificate-error', (event, webContents, url, error, certificate, callba } dialog.showMessageBox(mainWindow, { title: 'Certificate Error', - message: 'There is a configuration issue with the Mattermost server you are trying to connect to, or someone is trying to intercept your connection. You also may need to sign into the Wi-Fi you are connected to using your web browser.', + message: 'There is a configuration issue with this Mattermost server, or someone is trying to intercept your connection. You also may need to sign into the Wi-Fi you are connected to using your web browser.', type: 'error', buttons: [ 'More Details', @@ -326,7 +326,7 @@ app.on('certificate-error', (event, webContents, url, error, certificate, callba detail, type: 'error', buttons: [ - 'Continue Insecurely', + 'Trust Insecure Certificate', 'Cancel Connection', ], cancelId: 1, From 753ad3dfdc00c0eed1b8a9f9298cd6c615d13edc Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Wed, 2 Jan 2019 08:09:29 -0800 Subject: [PATCH 3/3] Remove context isolation. --- src/main.js | 1 - src/main/mainWindow.js | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main.js b/src/main.js index 6a019e7c..4b4f1fc1 100644 --- a/src/main.js +++ b/src/main.js @@ -668,7 +668,6 @@ app.on('ready', () => { app.on('web-contents-created', (dc, contents) => { contents.on('will-attach-webview', (event, webPreferences) => { webPreferences.nodeIntegration = false; - webPreferences.contextIsolation = true; }); contents.on('will-navigate', (event, navigationUrl) => { const parsedUrl = new URL(navigationUrl); diff --git a/src/main/mainWindow.js b/src/main/mainWindow.js index 4b970353..47adb886 100644 --- a/src/main/mainWindow.js +++ b/src/main/mainWindow.js @@ -67,7 +67,6 @@ function createMainWindow(config, options) { mainWindow.webContents.on('will-attach-webview', (event, webPreferences) => { webPreferences.nodeIntegration = false; - webPreferences.contextIsolation = true; }); mainWindow.once('ready-to-show', () => {