From 6b7d8e75f4c2af0ab6e96b7ec99fe8be88632323 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 21 Feb 2018 08:19:43 -0500 Subject: [PATCH] bring out of band fixes (c5cbf5db47, 2999dbee5c) for Firefox/webext into master --- src/js/background.js | 1 + src/js/messaging.js | 1 + src/js/traffic.js | 47 +++++++++++++++++++++++++++++++++----------- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/js/background.js b/src/js/background.js index b25c459bc..ec6f6bb61 100644 --- a/src/js/background.js +++ b/src/js/background.js @@ -37,6 +37,7 @@ var µBlock = (function() { // jshint ignore:line autoUpdatePeriod: 7, ignoreRedirectFilters: false, ignoreScriptInjectFilters: false, + streamScriptInjectFilters: false, manualUpdateAssetFetchPeriod: 2000, popupFontSize: 'unset', suspendTabsUntilReady: false, diff --git a/src/js/messaging.js b/src/js/messaging.js index 667d5572b..c38ab15d3 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -517,6 +517,7 @@ var onMessage = function(request, sender, callback) { // already been injected. if ( µb.canFilterResponseBody === false || + µb.hiddenSettings.streamScriptInjectFilters !== true || µb.textEncode === undefined || µb.textEncode.normalizeCharset(request.charset) === undefined ) { diff --git a/src/js/traffic.js b/src/js/traffic.js index 704204f39..3d6c2b4fc 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -652,15 +652,26 @@ var filterDocument = (function() { } // Case insensitively test for '!doctype'. if ( - bb[i++] !== 0x21 /* '!' */ || - ( bb[i++] | 0x20 ) !== 0x64 /* 'd' */ || - ( bb[i++] | 0x20 ) !== 0x6F /* 'o' */ || - ( bb[i++] | 0x20 ) !== 0x63 /* 'c' */ || - ( bb[i++] | 0x20 ) !== 0x74 /* 't' */ || - ( bb[i++] | 0x20 ) !== 0x79 /* 'y' */ || - ( bb[i++] | 0x20 ) !== 0x70 /* 'p' */ || - ( bb[i++] | 0x20 ) !== 0x65 /* 'e' */ + bb[i+0] === 0x21 /* '!' */ && + ( bb[i+1] | 0x20 ) === 0x64 /* 'd' */ && + ( bb[i+2] | 0x20 ) === 0x6F /* 'o' */ && + ( bb[i+3] | 0x20 ) === 0x63 /* 'c' */ && + ( bb[i+4] | 0x20 ) === 0x74 /* 't' */ && + ( bb[i+5] | 0x20 ) === 0x79 /* 'y' */ && + ( bb[i+6] | 0x20 ) === 0x70 /* 'p' */ && + ( bb[i+7] | 0x20 ) === 0x65 /* 'e' */ ) { + i += 8; + } + // Case insensitively test for 'html'. + else if ( + ( bb[i+0] | 0x20 ) === 0x68 /* 'h' */ && + ( bb[i+1] | 0x20 ) === 0x74 /* 't' */ && + ( bb[i+2] | 0x20 ) === 0x6D /* 'm' */ && + ( bb[i+3] | 0x20 ) === 0x6C /* 'l' */ + ) { + i += 4; + } else { return false; } // Scan for '>'. @@ -680,7 +691,6 @@ var filterDocument = (function() { textEncoder.encode('') ); filterer.stream.write(new Uint8Array(responseBytes, i)); - filterer.stream.disconnect(); return true; }; @@ -720,7 +730,11 @@ var filterDocument = (function() { // confirmed, there is nothing which can be done uBO-side to reduce // overhead. if ( filterer.buffer === null ) { - if ( streamJobDone(filterer, ev.data) ) { return; } + if ( streamJobDone(filterer, ev.data) ) { + filterers.delete(this); + this.disconnect(); + return; + } filterer.buffer = new Uint8Array(ev.data); return; } @@ -856,7 +870,18 @@ var filterDocument = (function() { buffer: null, charset: undefined }; - request.selectors = µb.htmlFilteringEngine.retrieve(request); + + // https://github.com/gorhill/uBlock/issues/3526 + // https://github.com/uBlockOrigin/uAssets/issues/1492 + // Two distinct issues, but both are arising as a result of + // injecting scriptlets through stream filtering. So falling back + // to "slow" scriplet injection for the time being. Stream filtering + // (`##^`) should be used for when scriptlets are defeated by early + // script tags on a page. + // + if ( µb.hiddenSettings.streamScriptInjectFilters ) { + request.selectors = µb.htmlFilteringEngine.retrieve(request); + } request.scriptlets = µb.scriptletFilteringEngine.retrieve(request); if (