[MM-20832] prevent dialog stacking, move to promises (#1169)

* [MM-20832] fix dialog stacking, move to promises

* put line in the right place

* Fix linting

* fix lint x2

* show details

* don't store the whole URL, just the server

* fix CR comments
This commit is contained in:
Guillermo Vayá
2020-02-06 12:41:37 +01:00
committed by GitHub
parent 33c869edae
commit ac3ac24c42

View File

@@ -51,6 +51,7 @@ const assetsDir = path.resolve(app.getAppPath(), 'assets');
const loginCallbackMap = new Map(); const loginCallbackMap = new Map();
const certificateRequests = new Map(); const certificateRequests = new Map();
const userActivityMonitor = new UserActivityMonitor(); const userActivityMonitor = new UserActivityMonitor();
const certificateErrorCallbacks = new Map();
// Keep a global reference of the window object, if you don't, the window will // Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected. // be closed automatically when the JavaScript object is garbage collected.
@@ -337,45 +338,65 @@ function handleSelectedCertificate(event, server, cert) {
} }
function handleAppCertificateError(event, webContents, url, error, certificate, callback) { function handleAppCertificateError(event, webContents, url, error, certificate, callback) {
if (certificateStore.isTrusted(url, certificate)) { const parsedURL = new URL(url);
if (!parsedURL) {
return;
}
const origin = parsedURL.origin;
if (certificateStore.isTrusted(origin, certificate)) {
event.preventDefault(); event.preventDefault();
callback(true); callback(true);
} else { } else {
let detail = `URL: ${url}\nError: ${error}`; // update the callback
if (certificateStore.isExisting(url)) { const errorID = `${origin}:${error}`;
detail = 'Certificate is different from previous one.\n\n' + detail;
// if we are already showing that error, don't add more dialogs
if (certificateErrorCallbacks.has(errorID)) {
console.log(`Ignoring already shown dialog for ${errorID}`);
certificateErrorCallbacks.set(errorID, callback);
return;
} }
const extraDetail = certificateStore.isExisting(origin) ? 'Certificate is different from previous one.\n\n' : '';
const detail = `${extraDetail}origin: ${origin}\nError: ${error}`;
certificateErrorCallbacks.set(errorID, callback);
dialog.showMessageBox(mainWindow, { dialog.showMessageBox(mainWindow, {
title: 'Certificate Error', title: 'Certificate Error',
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.', 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', type: 'error',
buttons: [ detail,
'More Details', buttons: ['More Details', 'Cancel Connection'],
'Cancel Connection',
],
cancelId: 1, cancelId: 1,
}, (response) => { }).then(
if (response === 0) { ({response}) => {
dialog.showMessageBox(mainWindow, { if (response === 0) {
title: 'Certificate Error', return dialog.showMessageBox(mainWindow, {
message: `Certificate from "${certificate.issuerName}" is not trusted.`, title: 'Certificate Not Trusted',
detail, message: `Certificate from "${certificate.issuerName}" is not trusted.`,
type: 'error', detail: extraDetail,
buttons: [ type: 'error',
'Trust Insecure Certificate', buttons: ['Trust Insecure Certificate', 'Cancel Connection'],
'Cancel Connection', cancelId: 1,
], });
cancelId: 1, }
}, (responseTwo) => { //eslint-disable-line max-nested-callbacks return {response};
if (responseTwo === 0) { }).then(
certificateStore.add(url, certificate); ({response: responseTwo}) => {
certificateStore.save(); if (responseTwo === 0) {
webContents.loadURL(url); certificateStore.add(origin, certificate);
} certificateStore.save();
}); certificateErrorCallbacks.get(errorID)(true);
} certificateErrorCallbacks.delete(errorID);
}); webContents.loadURL(url);
callback(false); } else {
certificateErrorCallbacks.get(errorID)(false);
certificateErrorCallbacks.delete(errorID);
}
}).catch(
(dialogError) => {
log.error(`There was an error with the Certificate Error dialog: ${dialogError}`);
certificateErrorCallbacks.delete(errorID);
});
} }
} }