mirror of
https://github.com/finos/SymphonyElectron.git
synced 2025-02-25 18:55:29 -06:00
Electron-217
Updated the PR as per the review comments
This commit is contained in:
parent
919882a5b3
commit
78c466516c
@ -3,7 +3,7 @@
|
||||
"minimizeOnClose" : true,
|
||||
"launchOnStartup" : true,
|
||||
"alwaysOnTop" : false,
|
||||
"whiteListURL": "*",
|
||||
"whitelistUrl": "*",
|
||||
"notificationSettings": {
|
||||
"position": "upper-right",
|
||||
"display": ""
|
||||
|
@ -1,120 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const { getGlobalConfigField } = require('./../config.js');
|
||||
const parseDomain = require('parse-domain');
|
||||
const isEqual = require('lodash.isequal');
|
||||
|
||||
/**
|
||||
* Loops through the list of whitelist urls
|
||||
* @param url {String} - url the electron is navigated to
|
||||
* @returns {Promise}
|
||||
*/
|
||||
function isWhiteList(url) {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
getGlobalConfigField('whiteListURL').then((whiteList) => {
|
||||
|
||||
if (checkWhiteList(url, whiteList)) {
|
||||
return resolve();
|
||||
}
|
||||
|
||||
return reject(new Error('URL does not match with the whiteList'));
|
||||
|
||||
}).catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that compares url against a list of whitelist
|
||||
* returns true if hostName or domain present in the whitelist
|
||||
* @param url {String} - url the electron is navigated to
|
||||
* @param whiteList {String} - coma separated whitelists
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function checkWhiteList(url, whiteList) {
|
||||
let whiteLists = whiteList.split(',');
|
||||
const parsedURL = parseDomain(url);
|
||||
|
||||
if (!parsedURL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!whiteList) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!whiteLists.length || whiteLists.indexOf('*') !== -1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return whiteLists.some((whiteListHost) => {
|
||||
let parsedWhiteList = parseDomain(whiteListHost);
|
||||
|
||||
if (!parsedWhiteList) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return matchDomains(parsedURL, parsedWhiteList);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches the respective hostName
|
||||
* @param parsedURL {Object} - parsed url
|
||||
* @param parsedWhiteList {Object} - parsed whitelist
|
||||
*
|
||||
* example:
|
||||
* matchDomain({ subdomain: www, domain: example, tld: com }, { subdomain: app, domain: example, tld: com })
|
||||
*
|
||||
* @returns {*}
|
||||
*/
|
||||
function matchDomains(parsedURL, parsedWhiteList) {
|
||||
|
||||
if (isEqual(parsedURL, parsedWhiteList)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const hostNameFromURL = parsedURL.domain + parsedURL.tld;
|
||||
const hostNameFromWhiteList = parsedWhiteList.domain + parsedWhiteList.tld;
|
||||
|
||||
if (!parsedWhiteList.subdomain) {
|
||||
return hostNameFromURL === hostNameFromWhiteList
|
||||
}
|
||||
|
||||
return hostNameFromURL === hostNameFromWhiteList && matchSubDomains(parsedURL.subdomain, parsedWhiteList.subdomain);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches the last occurrence in the sub-domain
|
||||
* @param subDomainURL {String} - sub-domain from url
|
||||
* @param subDomainWhiteList {String} - sub-domain from whitelist
|
||||
*
|
||||
* example: matchSubDomains('www', 'app')
|
||||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function matchSubDomains(subDomainURL, subDomainWhiteList) {
|
||||
|
||||
if (subDomainURL === subDomainWhiteList) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const subDomainURLArray = subDomainURL.split('.');
|
||||
const lastCharSubDomainURL = subDomainURLArray[subDomainURLArray.length - 1];
|
||||
|
||||
const subDomainWhiteListArray = subDomainWhiteList.split('.');
|
||||
const lastCharWhiteList = subDomainWhiteListArray[subDomainWhiteListArray.length - 1];
|
||||
|
||||
return lastCharSubDomainURL === lastCharWhiteList;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
isWhiteList,
|
||||
|
||||
// items below here are only exported for testing, do NOT use!
|
||||
checkWhiteList
|
||||
|
||||
};
|
120
js/utils/whitelistHandler.js
Normal file
120
js/utils/whitelistHandler.js
Normal file
@ -0,0 +1,120 @@
|
||||
'use strict';
|
||||
|
||||
const { getGlobalConfigField } = require('./../config.js');
|
||||
const parseDomain = require('parse-domain');
|
||||
const isEqual = require('lodash.isequal');
|
||||
|
||||
/**
|
||||
* Loops through the list of whitelist urls
|
||||
* @param url {String} - url the electron is navigated to
|
||||
* @returns {Promise}
|
||||
*/
|
||||
function isWhitelisted(url) {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
getGlobalConfigField('whitelistUrl').then((whitelist) => {
|
||||
|
||||
if (checkWhitelist(url, whitelist)) {
|
||||
return resolve();
|
||||
}
|
||||
|
||||
return reject(new Error('URL does not match with the whitelist'));
|
||||
|
||||
}).catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that compares url against a list of whitelist
|
||||
* returns true if hostName or domain present in the whitelist
|
||||
* @param url {String} - url the electron is navigated to
|
||||
* @param whitelist {String} - coma separated whitelists
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function checkWhitelist(url, whitelist) {
|
||||
let whitelistArray = whitelist.split(',');
|
||||
const parsedUrl = parseDomain(url);
|
||||
|
||||
if (!parsedUrl) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!whitelist) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!whitelistArray.length || whitelistArray.indexOf('*') !== -1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return whitelistArray.some((whitelistHost) => {
|
||||
let parsedWhitelist = parseDomain(whitelistHost);
|
||||
|
||||
if (!parsedWhitelist) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return matchDomains(parsedUrl, parsedWhitelist);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches the respective hostName
|
||||
* @param parsedUrl {Object} - parsed url
|
||||
* @param parsedWhitelist {Object} - parsed whitelist
|
||||
*
|
||||
* example:
|
||||
* matchDomain({ subdomain: www, domain: example, tld: com }, { subdomain: app, domain: example, tld: com })
|
||||
*
|
||||
* @returns {*}
|
||||
*/
|
||||
function matchDomains(parsedUrl, parsedWhitelist) {
|
||||
|
||||
if (isEqual(parsedUrl, parsedWhitelist)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const hostNameFromUrl = parsedUrl.domain + parsedUrl.tld;
|
||||
const hostNameFromWhitelist = parsedWhitelist.domain + parsedWhitelist.tld;
|
||||
|
||||
if (!parsedWhitelist.subdomain) {
|
||||
return hostNameFromUrl === hostNameFromWhitelist
|
||||
}
|
||||
|
||||
return hostNameFromUrl === hostNameFromWhitelist && matchSubDomains(parsedUrl.subdomain, parsedWhitelist.subdomain);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches the last occurrence in the sub-domain
|
||||
* @param subDomainUrl {String} - sub-domain from url
|
||||
* @param subDomainWhitelist {String} - sub-domain from whitelist
|
||||
*
|
||||
* example: matchSubDomains('www', 'app')
|
||||
*
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function matchSubDomains(subDomainUrl, subDomainWhitelist) {
|
||||
|
||||
if (subDomainUrl === subDomainWhitelist) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const subDomainUrlArray = subDomainUrl.split('.');
|
||||
const lastCharSubDomainUrl = subDomainUrlArray[subDomainUrlArray.length - 1];
|
||||
|
||||
const subDomainWhitelistArray = subDomainWhitelist.split('.');
|
||||
const lastCharWhitelist = subDomainWhitelistArray[subDomainWhitelistArray.length - 1];
|
||||
|
||||
return lastCharSubDomainUrl === lastCharWhitelist;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
isWhitelisted,
|
||||
|
||||
// items below here are only exported for testing, do NOT use!
|
||||
checkWhitelist
|
||||
|
||||
};
|
@ -21,7 +21,7 @@ const eventEmitter = require('./eventEmitter');
|
||||
const throttle = require('./utils/throttle.js');
|
||||
const { getConfigField, updateConfigField } = require('./config.js');
|
||||
const { isMac, isNodeEnv } = require('./utils/misc');
|
||||
const { isWhiteList } = require('./utils/isWhiteList');
|
||||
const { isWhitelisted } = require('./utils/whitelistHandler');
|
||||
|
||||
// show dialog when certificate errors occur
|
||||
require('./dialogs/showCertError.js');
|
||||
@ -465,7 +465,7 @@ function doCreateMainWindow(initialUrl, initialBounds) {
|
||||
|
||||
// whenever the main window is navigated for ex: window.location.href or url redirect
|
||||
mainWindow.webContents.on('will-navigate', function(event, navigatedURL) {
|
||||
isWhiteList(navigatedURL)
|
||||
isWhitelisted(navigatedURL)
|
||||
.catch(() => {
|
||||
event.preventDefault();
|
||||
electron.dialog.showMessageBox(mainWindow, {
|
||||
|
@ -1,74 +1,74 @@
|
||||
const { checkWhiteList } = require('../../js/utils/isWhiteList');
|
||||
const { checkWhitelist } = require('../../js/utils/whitelistHandler');
|
||||
|
||||
describe('validate url with whiteList', function() {
|
||||
describe('validate url with whitelist', function() {
|
||||
|
||||
describe('checkWhiteList truth tests', function() {
|
||||
describe('checkWhitelist truth tests', function() {
|
||||
|
||||
it('should return true when the url is valid', function() {
|
||||
const whiteList = 'www.symphony.com, app.symphony.com, my.symphony.com';
|
||||
const whitelist = 'www.symphony.com, app.symphony.com, my.symphony.com';
|
||||
const url = 'https://my.symphony.com/';
|
||||
|
||||
return expect(checkWhiteList(url, whiteList)).toBeTruthy();
|
||||
return expect(checkWhitelist(url, whitelist)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return true when if hostName is defined', function() {
|
||||
const whiteList = 'www.symphony.com, app.symphony.com, symphony.com';
|
||||
const whitelist = 'www.symphony.com, app.symphony.com, symphony.com';
|
||||
const url = 'https://xyz.symphony.com/';
|
||||
|
||||
return expect(checkWhiteList(url, whiteList)).toBeTruthy();
|
||||
return expect(checkWhitelist(url, whitelist)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return true when the first occurrence of sub-domain is matched', function() {
|
||||
const whiteList = 'www.symphony.com, app.symphony.com, my.symphony.com';
|
||||
const whitelist = 'www.symphony.com, app.symphony.com, my.symphony.com';
|
||||
const url = 'https://xyz.my.symphony.com/';
|
||||
|
||||
return expect(checkWhiteList(url, whiteList)).toBeTruthy();
|
||||
return expect(checkWhitelist(url, whitelist)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return true when for any URL if whiteList has *', function() {
|
||||
const whiteList = '*';
|
||||
it('should return true when for any URL if whitelist has *', function() {
|
||||
const whitelist = '*';
|
||||
const url = 'https://www.example.com/';
|
||||
|
||||
return expect(checkWhiteList(url, whiteList)).toBeTruthy();
|
||||
return expect(checkWhitelist(url, whitelist)).toBeTruthy();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('checkWhiteList falsity tests', function () {
|
||||
describe('checkWhitelist falsity tests', function () {
|
||||
|
||||
it('should return false when sub-domain does not match', function () {
|
||||
const whiteList = 'www.symphony.com, app.symphony.com, my.symphony.com';
|
||||
const whitelist = 'www.symphony.com, app.symphony.com, my.symphony.com';
|
||||
const url = 'https://xyz.symphony.com/';
|
||||
|
||||
return expect(checkWhiteList(url, whiteList)).toBeFalsy();
|
||||
return expect(checkWhitelist(url, whitelist)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return false when hostName does not match', function () {
|
||||
const whiteList = 'www.symphony.com, app.symphony.com, my.symphony.com';
|
||||
const whitelist = 'www.symphony.com, app.symphony.com, my.symphony.com';
|
||||
const url = 'https://my.example.com/';
|
||||
|
||||
return expect(checkWhiteList(url, whiteList)).toBeFalsy();
|
||||
return expect(checkWhitelist(url, whitelist)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return false when the URL is invalid', function () {
|
||||
const whiteList = 'www.symphony.com, app.symphony.com, my.symphony.com';
|
||||
const whitelist = 'www.symphony.com, app.symphony.com, my.symphony.com';
|
||||
const url = 'invalidUrl';
|
||||
|
||||
return expect(checkWhiteList(url, whiteList)).toBeFalsy();
|
||||
return expect(checkWhitelist(url, whitelist)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return false when the whiteList is invalid', function () {
|
||||
const whiteList = 'invalidWhiteList';
|
||||
it('should return false when the whitelist is invalid', function () {
|
||||
const whitelist = 'invalidWhitelist';
|
||||
const url = 'https://www.symphony.com';
|
||||
|
||||
return expect(checkWhiteList(url, whiteList)).toBeFalsy();
|
||||
return expect(checkWhitelist(url, whitelist)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should return false if whiteList is empty', function() {
|
||||
const whiteList = '';
|
||||
it('should return false if whitelist is empty', function() {
|
||||
const whitelist = '';
|
||||
const url = 'https://www.example.com/';
|
||||
|
||||
return expect(checkWhiteList(url, whiteList)).toBeFalsy();
|
||||
return expect(checkWhitelist(url, whitelist)).toBeFalsy();
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user