[MM-16516] Add a "don't trust" option for certificates (#1733)

* Added checkbox to certificate not trusted modal

* Added functionality to store a dontTrust boolean with the certificate store
This commit is contained in:
John Willis
2021-09-14 05:16:04 -04:00
committed by GitHub
parent 20ec525819
commit 375da5bdb3
3 changed files with 26 additions and 6 deletions

View File

@@ -13,10 +13,11 @@ import urlUtils from 'common/utils/url';
import * as Validator from './Validator'; import * as Validator from './Validator';
function comparableCertificate(certificate: Certificate): ComparableCertificate { function comparableCertificate(certificate: Certificate, dontTrust = false): ComparableCertificate {
return { return {
data: certificate.data.toString(), data: certificate.data.toString(),
issuerName: certificate.issuerName, issuerName: certificate.issuerName,
dontTrust,
}; };
} }
@@ -53,8 +54,8 @@ export default class CertificateStore {
fs.writeFileSync(this.storeFile, JSON.stringify(this.data, null, ' ')); fs.writeFileSync(this.storeFile, JSON.stringify(this.data, null, ' '));
}; };
add = (targetURL: string, certificate: Certificate) => { add = (targetURL: string, certificate: Certificate, dontTrust = false) => {
this.data[urlUtils.getHost(targetURL)] = comparableCertificate(certificate); this.data[urlUtils.getHost(targetURL)] = comparableCertificate(certificate, dontTrust);
}; };
isExisting = (targetURL: string) => { isExisting = (targetURL: string) => {
@@ -68,4 +69,12 @@ export default class CertificateStore {
} }
return areEqual(this.data[host], comparableCertificate(certificate)); return areEqual(this.data[host], comparableCertificate(certificate));
}; };
isExplicitlyUntrusted = (targetURL: string) => {
// Whether or not the certificate was explicitly marked as untrusted by
// clicking "Don't ask again" checkbox before cancelling the connection.
const host = urlUtils.getHost(targetURL);
const dontTrust = this.data[host]?.dontTrust;
return dontTrust === undefined ? false : dontTrust;
}
} }

View File

@@ -396,7 +396,11 @@ function handleAppCertificateError(event: electron.Event, webContents: electron.
return; return;
} }
const origin = parsedURL.origin; const origin = parsedURL.origin;
if (certificateStore.isTrusted(origin, certificate)) { if (certificateStore.isExplicitlyUntrusted(origin)) {
event.preventDefault();
log.warn(`Ignoring previously untrusted certificate for ${origin}`);
callback(false);
} else if (certificateStore.isTrusted(origin, certificate)) {
event.preventDefault(); event.preventDefault();
callback(true); callback(true);
} else { } else {
@@ -436,11 +440,13 @@ function handleAppCertificateError(event: electron.Event, webContents: electron.
type: 'error', type: 'error',
buttons: ['Trust Insecure Certificate', 'Cancel Connection'], buttons: ['Trust Insecure Certificate', 'Cancel Connection'],
cancelId: 1, cancelId: 1,
checkboxChecked: false,
checkboxLabel: "Don't ask again",
}); });
} }
return {response}; return {response, checkboxChecked: false};
}).then( }).then(
({response: responseTwo}) => { ({response: responseTwo, checkboxChecked}) => {
if (responseTwo === 0) { if (responseTwo === 0) {
certificateStore.add(origin, certificate); certificateStore.add(origin, certificate);
certificateStore.save(); certificateStore.save();
@@ -448,6 +454,10 @@ function handleAppCertificateError(event: electron.Event, webContents: electron.
certificateErrorCallbacks.delete(errorID); certificateErrorCallbacks.delete(errorID);
webContents.loadURL(url); webContents.loadURL(url);
} else { } else {
if (checkboxChecked) {
certificateStore.add(origin, certificate, true);
certificateStore.save();
}
certificateErrorCallbacks.get(errorID)(false); certificateErrorCallbacks.get(errorID)(false);
certificateErrorCallbacks.delete(errorID); certificateErrorCallbacks.delete(errorID);
} }

View File

@@ -6,6 +6,7 @@ import {Certificate} from 'electron/common';
export type ComparableCertificate = { export type ComparableCertificate = {
data: string; data: string;
issuerName: string; issuerName: string;
dontTrust: boolean;
} }
export type CertificateModalData = { export type CertificateModalData = {