From f0baf0e81b96c5ec04dc0d5a64a656ad0864f161 Mon Sep 17 00:00:00 2001 From: nobody Date: Sat, 4 Dec 2021 07:46:03 +0100 Subject: [PATCH] Refactor request handling (#787) --- core/constants.js | 4 ++-- core/interceptor.js | 26 +++++++++++--------------- core/request-analyzer.js | 35 ++++++++++++++++++++++++++++------- pages/updates/updates.html | 4 ++++ 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/core/constants.js b/core/constants.js index 51ba3831..1f852418 100644 --- a/core/constants.js +++ b/core/constants.js @@ -401,8 +401,7 @@ const MathJaxFiles = { /** * To block bad resources, e.g. fingerprint * - * Necessary if the user has selected the option "Block requests for missing - * resources Advanced" option is not enabled. + * Required if the user has not enabled the "Block requests for missing resources" option. * * https://codeberg.org/nobody/LocalCDN/issues/703 */ @@ -420,6 +419,7 @@ const LogString = { 'YANDEX': 'Workaround. Disable LocalCDN if website and CDN are the same', 'REPLACED_RESOURCE': 'Replaced resource:', 'MISSING_RESOURCE': 'Missing resource:', + 'EVIL_RESOURCE_BLOCKED': 'Evil resource blocked:', }; // Supported charsets for TextDecoder() diff --git a/core/interceptor.js b/core/interceptor.js index c49bf487..d03f3651 100644 --- a/core/interceptor.js +++ b/core/interceptor.js @@ -31,10 +31,9 @@ var interceptor = {}; */ interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) { - let validCandidate, targetDetails, targetPath, targetDetailURL; + let validCandidate, targetDetails, targetDomain; validCandidate = requestAnalyzer.isValidCandidate(requestDetails, tab); - if (!validCandidate) { return { 'cancel': false @@ -42,20 +41,17 @@ interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) { } if (interceptor._isBadResource(requestDetails.url)) { - console.log(`[ LocalCDN ] Evil resource blocked: ${requestDetails.url}`); + console.log(`${LogString.PREFIX} ${LogString.EVIL_RESOURCE_BLOCKED} ${requestDetails.url}`); log.append(tab.url, requestDetails.url, '-', true); return { 'cancel': true }; } - targetDetails = requestAnalyzer.getLocalTarget(requestDetails, tab.url); - targetPath = targetDetails.path; + targetDomain = helpers.extractDomainFromUrl(requestDetails.url, true); - - if (Regex.GOOGLE_FONTS.test(requestDetails.url)) { + if (requestAnalyzer.isGoogleFont(targetDomain) && !requestAnalyzer.isGoogleMaterialIcons(requestDetails.url)) { let initiatorDomain, isListed; - initiatorDomain = helpers.extractDomainFromUrl(tab.url, true); isListed = helpers.checkAllowlisted(initiatorDomain, interceptor.allowedDomainsGoogleFonts); // Check if the website is allowed to load Google Fonts @@ -70,13 +66,13 @@ interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) { } } - targetDetailURL = helpers.extractDomainFromUrl(requestDetails.url, true); + targetDetails = requestAnalyzer.getLocalTarget(requestDetails, tab.url); + console.log(targetDetails['result']); - if (targetDetails === false && !IgnoredHost[targetDetailURL]) { - ++stateManager.tabs[tabIdentifier].missing; - } - - if (!targetDetails) { + if (targetDetails['result'] === false) { + if (!IgnoredHost[targetDomain]) { + ++stateManager.tabs[tabIdentifier].missing; + } return interceptor._handleMissingCandidate(requestDetails.url, tabIdentifier); } @@ -85,7 +81,7 @@ interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) { }; return { - 'redirectUrl': chrome.runtime.getURL(targetPath + fileGuard.secret) + 'redirectUrl': chrome.runtime.getURL(targetDetails.path + fileGuard.secret) }; }; diff --git a/core/request-analyzer.js b/core/request-analyzer.js index a0e8f6a0..dae6dc3d 100644 --- a/core/request-analyzer.js +++ b/core/request-analyzer.js @@ -31,7 +31,7 @@ var requestAnalyzer = {}; */ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) { - let initiatorDomain, isAllowlisted; + let initiatorDomain, requestedDomain, isAllowlisted; initiatorDomain = helpers.extractDomainFromUrl(tabDetails.url, true); @@ -39,6 +39,12 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) { initiatorDomain = Address.EXAMPLE; } + // If requested Domain not in mappings.js it is not relevant + requestedDomain = helpers.extractDomainFromUrl(requestDetails.url, true); + if (mappings['cdn'][requestedDomain] === undefined) { + return false; + } + isAllowlisted = helpers.checkAllowlisted(initiatorDomain, requestAnalyzer.allowlistedDomains); if (isAllowlisted) { return false; @@ -46,11 +52,12 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) { // Font Awesome injections in Chromium deactivated (https://gitlab.com/nobody42/localcdn/-/issues/67) if (BrowserType.CHROMIUM) { - if ((/(font-awesome|fontawesome)/).test(requestDetails.url)) { + if (requestDetails.url.includes('font-awesome') || requestDetails.url.includes('fontawesome')) { console.warn(`${LogString.PREFIX} ${LogString.FONT_AWESOME}`); log.append(tabDetails.url, requestDetails.url, LogString.FONT_AWESOME, true); return false; - } else if (requestDetails.url.startsWith('https://fonts.googleapis.com')) { + } + if (requestAnalyzer._isGoogleMaterialIcons(requestedDomain, requestDetails.url)) { // also valid for Google Material icons console.warn(`${LogString.PREFIX} ${LogString.GOOGLE_MATERIAL_ICONS}`); log.append(tabDetails.url, requestDetails.url, LogString.GOOGLE_MATERIAL_ICONS, true); @@ -58,7 +65,7 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) { } } - // Disable LocalCDN if website is 'yandex.com' and CDN is 'yastatic.net', because website and CDN are the same. + // Ignore requests if website is 'yandex.com' and CDN is 'yastatic.net', because website and CDN are the same. if (tabDetails.url.includes('yandex.com') && requestDetails.url.includes('yastatic.net')) { log.append(tabDetails.url, requestDetails.url, LogString.YANDEX, true); return false; @@ -68,6 +75,14 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) { return requestDetails.method === WebRequest.GET; }; +requestAnalyzer.isGoogleMaterialIcons = function (url) { + return url.includes('Material+Icons') || url.includes('materialicons'); +}; + +requestAnalyzer.isGoogleFont = function (domain) { + return domain.includes('fonts.googleapis.com') || domain.includes('fonts.gstatic.com'); +}; + requestAnalyzer.getLocalTarget = function (requestDetails, initiator) { let destinationUrl, destinationHost, destinationPath, hostMappings, basePath, resourceMappings, destinationSearchString; @@ -155,7 +170,9 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel } if (resourcePattern === undefined) { - return false; + return { + 'result': false, + }; } for (let resourceMold of Object.keys(resourceMappings)) { @@ -166,7 +183,9 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel // Replace the requested version with the latest depending on major version versionDelivered = targets.setLastVersion(targetPath, versionNumber); if (versionDelivered === false) { - return false; + return { + 'result': false, + }; } versionDelivered = versionDelivered.toString(); targetPath = targetPath.replace(versionNumber, versionDelivered); @@ -206,7 +225,9 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel console.warn(`${LogString.PREFIX} ${LogString.MISSING_RESOURCE} ${channelHost}${channelPath}`); log.append(initiator, channelHost + channelPath, '-', true); } - return false; + return { + 'result': false, + }; }; requestAnalyzer._getPathOfBundle = function (initiator, channelHost, channelPath, targetPath, bundle) { diff --git a/pages/updates/updates.html b/pages/updates/updates.html index d904fe70..5f367a12 100644 --- a/pages/updates/updates.html +++ b/pages/updates/updates.html @@ -29,6 +29,10 @@ +

Improved

+