Electron-385 (Remove third party dependency and write custom implementation) (#350)

- Remove third party dependency and write custom implementation
- Add negative test case
This commit is contained in:
Kiran Niranjan 2018-05-01 15:21:00 +05:30 committed by Vishwas Shashidhar
parent c1e4016e93
commit 59a80f558c
3 changed files with 43 additions and 2 deletions

View File

@ -1,7 +1,6 @@
'use strict';
const { getGlobalConfigField } = require('./../config.js');
const parseDomain = require('parse-domain');
const isEqual = require('lodash.isequal');
const log = require('../log.js');
const logLevels = require('../enums/logLevels.js');
@ -21,6 +20,9 @@ getGlobalConfigField('customTlds')
log.send(logLevels.INFO, `error setting custom tlds -> ${err}`);
});
const urlParts = /^(https?:\/\/)?([^/]*@)?(.+?)(:\d{2,5})?([/?].*)?$/;
const dot = /\./g;
/**
* Loops through the list of whitelist urls
* @param url {String} - url the electron is navigated to
@ -128,6 +130,32 @@ function matchSubDomains(subDomainUrl, subDomainWhitelist) {
return lastCharSubDomainUrl === lastCharWhitelist;
}
/**
* Splits the url into tld, domain, subdomain
* @param url
* @return {{tld: string | *, domain: string | *, subdomain: string}}
*/
function parseDomain(url) {
let urlSplit = url.match(urlParts);
let domain = urlSplit[3];
// capture top level domain
const tld = domain.slice(domain.lastIndexOf('.'));
urlSplit = domain.slice(0, -tld.length).split(dot);
// capture domain
domain = urlSplit.pop();
// capture subdomain
const subdomain = urlSplit.join(".");
return {
tld: tld.trim(),
domain: domain.trim(),
subdomain: subdomain.trim()
}
}
module.exports = {
isWhitelisted,

View File

@ -121,7 +121,6 @@
"lodash.isequal": "4.5.0",
"lodash.omit": "4.5.0",
"lodash.pick": "4.4.0",
"parse-domain": "2.0.0",
"ref": "1.3.5",
"shell-path": "2.1.0",
"winreg": "1.2.4"

View File

@ -32,6 +32,13 @@ describe('validate url with whitelist', function() {
return expect(checkWhitelist(url, whitelist)).toBeTruthy();
});
it('should return true for non-standard TLDs', function() {
const whitelist = 'symphony.com, symphony.econet';
const url = 'https://my.symphony.econet/';
return expect(checkWhitelist(url, whitelist)).toBeTruthy();
});
});
describe('checkWhitelist falsity tests', function () {
@ -50,6 +57,13 @@ describe('validate url with whitelist', function() {
return expect(checkWhitelist(url, whitelist)).toBeFalsy();
});
it('should return false when TLD does not match', function () {
const whitelist = 'www.symphony.com, app.symphony.com, my.symphony.com';
const url = 'https://my.symphony.echonet/';
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';