diff options
author | Jesús <heckyel@hyperbola.info> | 2020-03-15 12:57:08 -0500 |
---|---|---|
committer | Jesús <heckyel@hyperbola.info> | 2020-03-15 12:57:08 -0500 |
commit | d62a0f38eeb36866a59f165d7f9bfb8dfd6ac3aa (patch) | |
tree | ec12ca4ea7a4a9a06fafc75ef6421c9954145b8f | |
parent | f167a8d5c1a1ed092f6379e333db53b3349ded10 (diff) | |
download | ematrix-d62a0f38eeb36866a59f165d7f9bfb8dfd6ac3aa.tar.lz ematrix-d62a0f38eeb36866a59f165d7f9bfb8dfd6ac3aa.tar.xz ematrix-d62a0f38eeb36866a59f165d7f9bfb8dfd6ac3aa.zip |
Style changes
-rw-r--r-- | js/traffic.js | 333 |
1 files changed, 160 insertions, 173 deletions
diff --git a/js/traffic.js b/js/traffic.js index 8e408cf..eafaa08 100644 --- a/js/traffic.js +++ b/js/traffic.js @@ -25,37 +25,33 @@ /******************************************************************************/ -// Start isolation from global scope - ηMatrix.webRequest = (function() { Cu.import('chrome://ematrix/content/lib/UriTools.jsm'); - /******************************************************************************/ - // Intercept and filter web requests according to white and black lists. - - var onBeforeRootFrameRequestHandler = function(details) { - var ηm = ηMatrix; - var requestURL = details.url; - var requestHostname = UriTools.hostnameFromURI(requestURL); - var tabId = details.tabId; + var onBeforeRootFrameRequestHandler = function (details) { + let ηm = ηMatrix; + let requestURL = details.url; + let requestHostname = UriTools.hostnameFromURI(requestURL); + let tabId = details.tabId; ηm.tabContextManager.push(tabId, requestURL); - var tabContext = ηm.tabContextManager.mustLookup(tabId); - var rootHostname = tabContext.rootHostname; + let tabContext = ηm.tabContextManager.mustLookup(tabId); + let rootHostname = tabContext.rootHostname; // Disallow request as per matrix? - var block = ηm.mustBlock(rootHostname, requestHostname, 'doc'); + let block = ηm.mustBlock(rootHostname, requestHostname, 'doc'); - var pageStore = ηm.pageStoreFromTabId(tabId); + let pageStore = ηm.pageStoreFromTabId(tabId); pageStore.recordRequest('doc', requestURL, block); ηm.logger.writeOne(tabId, 'net', rootHostname, requestURL, 'doc', block); // Not blocked - if ( !block ) { - // rhill 2013-11-07: Senseless to do this for behind-the-scene requests. + if (!block) { + // rhill 2013-11-07: Senseless to do this for + // behind-the-scene requests. ηm.cookieHunter.recordPageCookies(pageStore); return; } @@ -67,110 +63,113 @@ why: '?' })); - vAPI.tabs.replace(tabId, vAPI.getURL('main-blocked.html?details=') + query); + vAPI.tabs.replace(tabId, + vAPI.getURL('main-blocked.html?details=') + query); - return { cancel: true }; + return { + cancel: true + }; }; - /******************************************************************************/ - // Intercept and filter web requests according to white and black lists. + var onBeforeRequestHandler = function (details) { + let ηm = ηMatrix; + let requestURL = details.url; + let requestScheme = UriTools.schemeFromURI(requestURL); - var onBeforeRequestHandler = function(details) { - var ηm = ηMatrix, - ηmuri = UriTools, - requestURL = details.url, - requestScheme = ηmuri.schemeFromURI(requestURL); - - if ( ηmuri.isNetworkScheme(requestScheme) === false ) { return; } + if (UriTools.isNetworkScheme(requestScheme) === false) { + return; + } var requestType = requestTypeNormalizer[details.type] || 'other'; // https://github.com/gorhill/httpswitchboard/issues/303 - // Wherever the main doc comes from, create a receiver page URL: synthetize - // one if needed. - if ( requestType === 'doc' && details.parentFrameId === -1 ) { + // Wherever the main doc comes from, create a receiver page + // URL: synthetize one if needed. + if (requestType === 'doc' && details.parentFrameId === -1) { return onBeforeRootFrameRequestHandler(details); } - // Re-classify orphan HTTP requests as behind-the-scene requests. There is - // not much else which can be done, because there are URLs - // which cannot be handled by ηMatrix, i.e. `opera://startpage`, - // as this would lead to complications with no obvious solution, like how - // to scope on unknown scheme? Etc. + // Re-classify orphan HTTP requests as behind-the-scene + // requests. There is not much else which can be done, because + // there are URLs which cannot be handled by ηMatrix, + // i.e. `opera://startpage`, as this would lead to + // complications with no obvious solution, like how to scope + // on unknown scheme? Etc. // https://github.com/gorhill/httpswitchboard/issues/191 // https://github.com/gorhill/httpswitchboard/issues/91#issuecomment-37180275 - var tabContext = ηm.tabContextManager.mustLookup(details.tabId), - tabId = tabContext.tabId, - rootHostname = tabContext.rootHostname, - specificity = 0; + let tabContext = ηm.tabContextManager.mustLookup(details.tabId); + let tabId = tabContext.tabId; + let rootHostname = tabContext.rootHostname; + let specificity = 0; // Filter through matrix - var block = ηm.tMatrix.mustBlock( - rootHostname, - ηmuri.hostnameFromURI(requestURL), - requestType - ); - if ( block ) { + let block = ηm.tMatrix.mustBlock(rootHostname, + UriTools.hostnameFromURI(requestURL), + requestType); + if (block) { specificity = ηm.tMatrix.specificityRegister; } // Record request. // https://github.com/gorhill/httpswitchboard/issues/342 - // The way requests are handled now, it may happen at this point some - // processing has already been performed, and that a synthetic URL has - // been constructed for logging purpose. Use this synthetic URL if - // it is available. - var pageStore = ηm.mustPageStoreFromTabId(tabId); + // The way requests are handled now, it may happen at this + // point some processing has already been performed, and that + // a synthetic URL has been constructed for logging + // purpose. Use this synthetic URL if it is available. + let pageStore = ηm.mustPageStoreFromTabId(tabId); // Enforce strict secure connection? - if ( tabContext.secure && ηmuri.isSecureScheme(requestScheme) === false ) { + if (tabContext.secure + && UriTools.isSecureScheme(requestScheme) === false) { pageStore.hasMixedContent = true; - if ( block === false ) { + if (block === false) { block = ηm.tMatrix.evaluateSwitchZ('https-strict', rootHostname); } } pageStore.recordRequest(requestType, requestURL, block); - ηm.logger.writeOne(tabId, 'net', rootHostname, requestURL, details.type, block); - - if ( block ) { - pageStore.cacheBlockedCollapsible(requestType, requestURL, specificity); - return { 'cancel': true }; + ηm.logger.writeOne(tabId, 'net', rootHostname, + requestURL, details.type, block); + + if (block) { + pageStore.cacheBlockedCollapsible(requestType, + requestURL, specificity); + return { + 'cancel': true + }; } }; - /******************************************************************************/ - // Sanitize outgoing headers as per user settings. - - var onBeforeSendHeadersHandler = function(details) { - var ηm = ηMatrix, - ηmuri = UriTools, - requestURL = details.url, - requestScheme = ηmuri.schemeFromURI(requestURL); + var onBeforeSendHeadersHandler = function (details) { + let ηm = ηMatrix; + let requestURL = details.url; + let requestScheme = UriTools.schemeFromURI(requestURL); // Ignore non-network schemes - if ( ηmuri.isNetworkScheme(requestScheme) === false ) { return; } + if (UriTools.isNetworkScheme(requestScheme) === false) { + return; + } - // Re-classify orphan HTTP requests as behind-the-scene requests. There is - // not much else which can be done, because there are URLs - // which cannot be handled by HTTP Switchboard, i.e. `opera://startpage`, - // as this would lead to complications with no obvious solution, like how - // to scope on unknown scheme? Etc. + // Re-classify orphan HTTP requests as behind-the-scene + // requests. There is not much else which can be done, because + // there are URLs which cannot be handled by HTTP Switchboard, + // i.e. `opera://startpage`, as this would lead to + // complications with no obvious solution, like how to scope + // on unknown scheme? Etc. // https://github.com/gorhill/httpswitchboard/issues/191 // https://github.com/gorhill/httpswitchboard/issues/91#issuecomment-37180275 - var tabId = details.tabId, - pageStore = ηm.mustPageStoreFromTabId(tabId), - requestType = requestTypeNormalizer[details.type] || 'other', - requestHeaders = details.requestHeaders, - headerIndex, headerValue; + let tabId = details.tabId; + let pageStore = ηm.mustPageStoreFromTabId(tabId); + let requestType = requestTypeNormalizer[details.type] || 'other'; + let requestHeaders = details.requestHeaders; + let headerIndex, headerValue; // https://github.com/gorhill/httpswitchboard/issues/342 - // Is this hyperlink auditing? - // If yes, create a synthetic URL for reporting hyperlink auditing - // in request log. This way the user is better informed of what went - // on. + // Is this hyperlink auditing? If yes, create a synthetic URL + // for reporting hyperlink auditing in request log. This way + // the user is better informed of what went on. // https://html.spec.whatwg.org/multipage/links.html#hyperlink-auditing // @@ -186,46 +185,46 @@ // // With hyperlink-auditing, removing header(s) is pointless, the whole // request must be cancelled. - headerIndex = headerIndexFromName('ping-to', requestHeaders); - if ( headerIndex !== -1 ) { + if (headerIndex !== -1) { headerValue = requestHeaders[headerIndex].value; - if ( headerValue !== '' ) { - var block = ηm.userSettings.processHyperlinkAuditing; - pageStore.recordRequest('other', requestURL + '{Ping-To:' + headerValue + '}', block); + if (headerValue !== '') { + let block = ηm.userSettings.processHyperlinkAuditing; + pageStore.recordRequest('other', + requestURL + + '{Ping-To:' + + headerValue + + '}', block); ηm.logger.writeOne(tabId, 'net', '', requestURL, 'ping', block); - if ( block ) { + if (block) { ηm.hyperlinkAuditingFoiledCounter += 1; - return { 'cancel': true }; + return { + 'cancel': true + }; } } } - // If we reach this point, request is not blocked, so what is left to do - // is to sanitize headers. - - var rootHostname = pageStore.pageHostname, - requestHostname = ηmuri.hostnameFromURI(requestURL), - modified = false; + // If we reach this point, request is not blocked, so what is + // left to do is to sanitize headers. + let rootHostname = pageStore.pageHostname; + let requestHostname = UriTools.hostnameFromURI(requestURL); + let modified = false; // Process `Cookie` header. - headerIndex = headerIndexFromName('cookie', requestHeaders); - if ( - headerIndex !== -1 && - ηm.mustBlock(rootHostname, requestHostname, 'cookie') - ) { + if (headerIndex !== -1 + && ηm.mustBlock(rootHostname, requestHostname, 'cookie')) { modified = true; headerValue = requestHeaders[headerIndex].value; requestHeaders.splice(headerIndex, 1); ηm.cookieHeaderFoiledCounter++; - if ( requestType === 'doc' ) { + if (requestType === 'doc') { ηm.logger.writeOne(tabId, 'net', '', headerValue, 'COOKIE', true); } } // Process `Referer` header. - // https://github.com/gorhill/httpswitchboard/issues/222#issuecomment-44828402 // https://github.com/gorhill/uMatrix/issues/320 @@ -239,32 +238,36 @@ // "Origin header field." // https://github.com/gorhill/uMatrix/issues/358 - // Do not spoof `Origin` header for the time being. + // Do not spoof `Origin` header for the time being. // https://github.com/gorhill/uMatrix/issues/773 - // For non-GET requests, remove `Referer` header instead of spoofing it. + // For non-GET requests, remove `Referer` header instead of spoofing it. headerIndex = headerIndexFromName('referer', requestHeaders); - if ( headerIndex !== -1 ) { + if (headerIndex !== -1) { headerValue = requestHeaders[headerIndex].value; - if ( headerValue !== '' ) { - var toDomain = ηmuri.domainFromHostname(requestHostname); - if ( toDomain !== '' && toDomain !== ηmuri.domainFromURI(headerValue) ) { + if (headerValue !== '') { + let toDomain = UriTools.domainFromHostname(requestHostname); + if (toDomain !== '' + && toDomain !== UriTools.domainFromURI(headerValue)) { pageStore.has3pReferrer = true; - if ( ηm.tMatrix.evaluateSwitchZ('referrer-spoof', rootHostname) ) { + if (ηm.tMatrix.evaluateSwitchZ('referrer-spoof', + rootHostname)) { modified = true; - var newValue; - if ( details.method === 'GET' ) { + let newValue; + if (details.method === 'GET') { newValue = requestHeaders[headerIndex].value = requestScheme + '://' + requestHostname + '/'; } else { requestHeaders.splice(headerIndex, 1); } ηm.refererHeaderFoiledCounter++; - if ( requestType === 'doc' ) { - ηm.logger.writeOne(tabId, 'net', '', headerValue, 'REFERER', true); - if ( newValue !== undefined ) { - ηm.logger.writeOne(tabId, 'net', '', newValue, 'REFERER', false); + if (requestType === 'doc') { + ηm.logger.writeOne(tabId, 'net', '', + headerValue, 'REFERER', true); + if (newValue !== undefined) { + ηm.logger.writeOne(tabId, 'net', '', + newValue, 'REFERER', false); } } } @@ -272,13 +275,13 @@ } } - if ( modified ) { - return { requestHeaders: requestHeaders }; + if (modified) { + return { + requestHeaders: requestHeaders + }; } }; - /******************************************************************************/ - // To prevent inline javascript from being executed. // Prevent inline scripting using `Content-Security-Policy`: @@ -286,57 +289,58 @@ // This fixes: // https://github.com/gorhill/httpswitchboard/issues/35 - - var onHeadersReceived = function(details) { + var onHeadersReceived = function (details) { // Ignore schemes other than 'http...' - var ηm = ηMatrix, - tabId = details.tabId, - requestURL = details.url, - requestType = requestTypeNormalizer[details.type] || 'other'; + let ηm = ηMatrix; + let tabId = details.tabId; + let requestURL = details.url; + let requestType = requestTypeNormalizer[details.type] || 'other'; // https://github.com/gorhill/uMatrix/issues/145 // Check if the main_frame is a download - if ( requestType === 'doc' ) { + if (requestType === 'doc') { ηm.tabContextManager.push(tabId, requestURL); } - var tabContext = ηm.tabContextManager.lookup(tabId); - if ( tabContext === null ) { return; } + let tabContext = ηm.tabContextManager.lookup(tabId); + if (tabContext === null) { + return; + } - var csp = [], - cspReport = [], - rootHostname = tabContext.rootHostname, - requestHostname = UriTools.hostnameFromURI(requestURL); + let csp = []; + let cspReport = []; + let rootHostname = tabContext.rootHostname; + let requestHostname = UriTools.hostnameFromURI(requestURL); // Inline script tags. - if ( ηm.mustAllow(rootHostname, requestHostname, 'script' ) !== true ) { + if (ηm.mustAllow(rootHostname, requestHostname, 'script') !== true) { csp.push(ηm.cspNoInlineScript); } // Inline style tags. - if ( ηm.mustAllow(rootHostname, requestHostname, 'css' ) !== true ) { + if (ηm.mustAllow(rootHostname, requestHostname, 'css') !== true) { csp.push(ηm.cspNoInlineStyle); } // https://bugzilla.mozilla.org/show_bug.cgi?id=1302667 - var cspNoWorker = ηm.cspNoWorker; - if ( cspNoWorker === undefined ) { + let cspNoWorker = ηm.cspNoWorker; + if (cspNoWorker === undefined) { cspNoWorker = cspNoWorkerInit(); } - if ( ηm.tMatrix.evaluateSwitchZ('no-workers', rootHostname) ) { + if (ηm.tMatrix.evaluateSwitchZ('no-workers', rootHostname)) { csp.push(cspNoWorker); - } else if ( ηm.rawSettings.disableCSPReportInjection === false ) { + } else if (ηm.rawSettings.disableCSPReportInjection === false) { cspReport.push(cspNoWorker); } - var headers = details.responseHeaders, - cspDirectives, i; + let headers = details.responseHeaders; + let cspDirectives, i; - if ( csp.length !== 0 ) { + if (csp.length !== 0) { cspDirectives = csp.join(','); i = headerIndexFromName('content-security-policy', headers); - if ( i !== -1 ) { + if (i !== -1) { headers[i].value += ',' + cspDirectives; } else { headers.push({ @@ -344,15 +348,16 @@ value: cspDirectives }); } - if ( requestType === 'doc' ) { + if (requestType === 'doc') { ηm.logger.writeOne(tabId, 'net', '', cspDirectives, 'CSP', false); } } - if ( cspReport.length !== 0 ) { + if (cspReport.length !== 0) { cspDirectives = cspReport.join(','); - i = headerIndexFromName('content-security-policy-report-only', headers); - if ( i !== -1 ) { + i = headerIndexFromName('content-security-policy-report-only', + headers); + if (i !== -1) { headers[i].value += ',' + cspDirectives; } else { headers.push({ @@ -362,12 +367,12 @@ } } - return { responseHeaders: headers }; + return { + responseHeaders: headers + }; }; - /******************************************************************************/ - - var cspNoWorkerInit = function() { + let cspNoWorkerInit = function () { if (ηMatrix.cspNoWorker === undefined) { ηMatrix.cspNoWorker = "worker-src 'none'; " +"frame-src data: blob: *; " @@ -377,23 +382,18 @@ return ηMatrix.cspNoWorker; }; - /******************************************************************************/ - // Caller must ensure headerName is normalized to lower case. - - var headerIndexFromName = function(headerName, headers) { - var i = headers.length; - while ( i-- ) { + let headerIndexFromName = function (headerName, headers) { + for (let i=0; i<headers.length; ++i) { if ( headers[i].name.toLowerCase() === headerName ) { return i; } } + return -1; }; - /******************************************************************************/ - - var requestTypeNormalizer = { + let requestTypeNormalizer = { 'font' : 'css', 'image' : 'image', 'imageset' : 'image', @@ -408,8 +408,6 @@ 'xmlhttprequest': 'xhr' }; - /******************************************************************************/ - vAPI.net.onBeforeRequest = { extra: [ 'blocking' ], callback: onBeforeRequestHandler @@ -427,20 +425,9 @@ callback: onHeadersReceived }; - /******************************************************************************/ - - var start = function() { - vAPI.net.registerListeners(); - }; - - /******************************************************************************/ - return { - start: start + start: function () { + vAPI.net.registerListeners(); + }, }; - - /******************************************************************************/ - })(); - -/******************************************************************************/ |