Electron-217

* Implemented basic logic on whitelisting urls
* Removed a space in PULL_REQUEST_TEMPLATE.md
This commit is contained in:
Kiran Niranjan 2017-12-11 22:41:34 +05:30 committed by Kiran Niranjan
parent bc231a8022
commit 84cb11e629
4 changed files with 105 additions and 2 deletions

View File

@ -5,7 +5,7 @@ A few sentences describing the overall goals of the pull request's commits. Desc
## Approach ## Approach
How does this change address the problem? How does this change address the problem?
- #### Problem with the code: - #### Problem with the code:
- #### Fix : - #### Fix:
## Learning ## Learning

View File

@ -3,6 +3,7 @@
"minimizeOnClose" : true, "minimizeOnClose" : true,
"launchOnStartup" : true, "launchOnStartup" : true,
"alwaysOnTop" : false, "alwaysOnTop" : false,
"whiteListURL": "*",
"notificationSettings": { "notificationSettings": {
"position": "upper-right", "position": "upper-right",
"display": "" "display": ""

101
js/utils/isWhiteList.js Normal file
View File

@ -0,0 +1,101 @@
'use strict';
const { getConfigField } = require('./../config.js');
const parse = require('parse-domain');
/**
* 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) => {
getConfigField('whitelist').then((whiteList) => {
if (checkWhiteList(url, whiteList)) {
resolve();
} else {
reject();
}
});
});
}
/**
* 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 = parse(url);
if (whiteLists.indexOf('*') !== -1) {
return true;
}
return whiteLists.some((whiteListHost) => {
let parsedWhiteList = parse(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 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;

View File

@ -104,9 +104,10 @@
"electron-squirrel-startup": "^1.0.0", "electron-squirrel-startup": "^1.0.0",
"filesize": "^3.5.10", "filesize": "^3.5.10",
"keymirror": "0.1.1", "keymirror": "0.1.1",
"lodash.difference": "^4.5.0",
"lodash.omit": "^4.5.0", "lodash.omit": "^4.5.0",
"lodash.pick": "^4.4.0", "lodash.pick": "^4.4.0",
"lodash.difference": "^4.5.0", "parse-domain": "^2.0.0",
"winreg": "^1.2.3" "winreg": "^1.2.3"
}, },
"optionalDependencies": { "optionalDependencies": {