Electron-217

* Wrote some unit tests for isWhiteList
* Fixed some validation issues
This commit is contained in:
Kiran Niranjan 2017-12-12 13:00:32 +05:30 committed by Kiran Niranjan
parent 84cb11e629
commit d7f62a38b0
4 changed files with 113 additions and 11 deletions

View File

@ -1,7 +1,8 @@
'use strict';
const { getConfigField } = require('./../config.js');
const parse = require('parse-domain');
const parseDomain = require('parse-domain');
const isEqual = require('lodash.isequal');
/**
* Loops through the list of whitelist urls
@ -11,12 +12,16 @@ const parse = require('parse-domain');
function isWhiteList(url) {
return new Promise((resolve, reject) => {
getConfigField('whitelist').then((whiteList) => {
getConfigField('whiteListURL').then((whiteList) => {
if (checkWhiteList(url, whiteList)) {
resolve();
} else {
reject();
return resolve();
}
return reject(new Error('URL does not match with the whiteList'));
}).catch((err) => {
reject(err);
});
});
}
@ -30,14 +35,22 @@ function isWhiteList(url) {
*/
function checkWhiteList(url, whiteList) {
let whiteLists = whiteList.split(',');
const parsedURL = parse(url);
const parsedURL = parseDomain(url);
if (whiteLists.indexOf('*') !== -1) {
if (!parsedURL) {
return false;
}
if (!whiteList) {
return false;
}
if (!whiteLists.length || whiteLists.indexOf('*') !== -1) {
return true;
}
return whiteLists.some((whiteListHost) => {
let parsedWhiteList = parse(whiteListHost);
let parsedWhiteList = parseDomain(whiteListHost);
if (!parsedWhiteList) {
return false;
@ -59,7 +72,7 @@ function checkWhiteList(url, whiteList) {
*/
function matchDomains(parsedURL, parsedWhiteList) {
if (_.isEqual(parsedURL, parsedWhiteList)) {
if (isEqual(parsedURL, parsedWhiteList)) {
return true;
}
@ -70,7 +83,7 @@ function matchDomains(parsedURL, parsedWhiteList) {
return hostNameFromURL === hostNameFromWhiteList
}
return matchSubDomains(parsedURL.subdomain, parsedWhiteList.subdomain);
return hostNameFromURL === hostNameFromWhiteList && matchSubDomains(parsedURL.subdomain, parsedWhiteList.subdomain);
}
@ -98,4 +111,10 @@ function matchSubDomains(subDomainURL, subDomainWhiteList) {
return lastCharSubDomainURL === lastCharWhiteList;
}
module.exports = isWhiteList;
module.exports = {
isWhiteList,
// items below here are only exported for testing, do NOT use!
checkWhiteList
};

View File

@ -21,6 +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');
// show dialog when certificate errors occur
require('./dialogs/showCertError.js');
@ -462,6 +463,13 @@ function doCreateMainWindow(initialUrl, initialBounds) {
}
});
mainWindow.webContents.on('will-navigate', function(event, navigatedUrl){
// TODO: need inputs from design to implement error dialog
isWhiteList(navigatedUrl).catch(() => {
event.preventDefault();
});
});
}
/**

View File

@ -105,6 +105,7 @@
"filesize": "^3.5.10",
"keymirror": "0.1.1",
"lodash.difference": "^4.5.0",
"lodash.isequal": "^4.5.0",
"lodash.omit": "^4.5.0",
"lodash.pick": "^4.4.0",
"parse-domain": "^2.0.0",

View File

@ -0,0 +1,74 @@
const { checkWhiteList } = require('../../js/utils/isWhiteList');
describe('validate url with whiteList', 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 url = 'https://my.symphony.com/';
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 url = 'https://xyz.symphony.com/';
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 url = 'https://xyz.my.symphony.com/';
return expect(checkWhiteList(url, whiteList)).toBeTruthy();
});
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();
});
});
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 url = 'https://xyz.symphony.com/';
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 url = 'https://my.example.com/';
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 url = 'invalidUrl';
return expect(checkWhiteList(url, whiteList)).toBeFalsy();
});
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();
});
it('should return false if whiteList is empty', function() {
const whiteList = '';
const url = 'https://www.example.com/';
return expect(checkWhiteList(url, whiteList)).toBeFalsy();
});
});
});