Skip to content
Snippets Groups Projects
Verified Commit b3931feb authored by Thomas Rientjes's avatar Thomas Rientjes
Browse files

Improve domain parsing logic

parent 391eb4e6
No related branches found
No related tags found
No related merge requests found
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment