Microsoft has acquired GitHub. Decentraleyes has left GitHub. Welcome to its new home!

To participate, please register, or sign in with an existing GitLab.com, Bitbucket, or GitHub account.

Past contributions on GitHub? Be sure to reclaim your Comments, Issues, and Pull Requests.

Improve domain parsing logic

parent 391eb4e6
......@@ -21,10 +21,9 @@ const Address = {
'ANY': '*://*/*',
'ANY_PATH': '/*',
'ANY_PROTOCOL': '*://',
'DOMAIN_EXPRESSION': /:\/\/(.[^/]+)(.*)/,
'EXAMPLE': 'example.org',
'HTTP_EXPRESSION': /^http?:\/\//,
'HTTPS': 'https://',
'HTTP': 'http:',
'HTTPS': 'https:',
'WWW_PREFIX': 'www.',
'WWW_PREFIX_LENGTH': 4
};
......
......@@ -36,10 +36,9 @@ interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) {
};
}
try {
tabDomain = tab.url.match(Address.DOMAIN_EXPRESSION)[1];
tabDomain = requestAnalyzer._normalizeDomain(tabDomain);
} catch (exception) {
tabDomain = helpers.extractDomainFromUrl(tab.url, true);
if (tabDomain === null) {
tabDomain = Address.EXAMPLE;
}
......@@ -98,12 +97,15 @@ interceptor._handleMissingCandidate = function (requestUrl) {
};
}
if (requestUrl.match(Address.HTTP_EXPRESSION)) {
let requestUrlSegments = new URL(requestUrl);
if (requestUrlSegments.protocol === Address.HTTP) {
let secureRequestUrl = requestUrl.replace(Address.HTTP_EXPRESSION, Address.HTTPS);
requestUrlSegments.protocol = Address.HTTPS;
requestUrl = requestUrlSegments.toString();
return {
'redirectUrl': secureRequestUrl
'redirectUrl': requestUrl
};
} else {
......
......@@ -25,15 +25,17 @@ var requestAnalyzer = {};
requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) {
let initiatorHost;
let initiatorDomain, isWhitelisted;
try {
initiatorHost = tabDetails.url.match(Address.DOMAIN_EXPRESSION)[1];
} catch (exception) {
initiatorHost = Address.EXAMPLE;
initiatorDomain = helpers.extractDomainFromUrl(tabDetails.url, true);
if (initiatorDomain === null) {
initiatorDomain = Address.EXAMPLE;
}
if (initiatorHost && requestAnalyzer.whitelistedDomains[requestAnalyzer._normalizeDomain(initiatorHost)]) {
isWhitelisted = requestAnalyzer.whitelistedDomains[initiatorDomain];
if (isWhitelisted) {
return false;
}
......@@ -43,10 +45,12 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) {
requestAnalyzer.getLocalTarget = function (requestDetails) {
let destinationHost, destinationPath, hostMappings, basePath, resourceMappings;
let destinationUrl, destinationHost, destinationPath, hostMappings, basePath, resourceMappings;
destinationUrl = new URL(requestDetails.url);
destinationHost = requestDetails.url.match(Address.DOMAIN_EXPRESSION)[1];
destinationPath = requestDetails.url.match(Address.DOMAIN_EXPRESSION)[2];
destinationHost = destinationUrl.host;
destinationPath = destinationUrl.pathname;
// Use the proper mappings for the targeted host.
hostMappings = mappings[destinationHost];
......
......@@ -66,11 +66,7 @@ document.addEventListener('DOMContentLoaded', function () {
injections = backgroundPage.stateManager.tabs[tabs[0].id].injections;
injectionOverview = {};
try {
domain = tabs[0].url.match(Address.DOMAIN_EXPRESSION)[1];
} catch (exception) {
domain = null;
}
domain = helpers.extractDomainFromUrl(tabs[0].url);
if (domain !== null) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment