From dfcd23197d026d1e6662a4e905aef580c37110b3 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 17 Dec 2018 07:46:04 -0500 Subject: [PATCH] Fix parsing of `redirect=` option as per https://github.com/gorhill/uBlock/commit/67e06f53b4d73df6179f6d320553a55da4ead40e#commitcomment-27803901 --- src/js/redirect-engine.js | 82 ++++++++++++++-------------------- src/js/static-net-filtering.js | 1 + 2 files changed, 35 insertions(+), 48 deletions(-) diff --git a/src/js/redirect-engine.js b/src/js/redirect-engine.js index bfcdfb5c0..2faf39cd5 100644 --- a/src/js/redirect-engine.js +++ b/src/js/redirect-engine.js @@ -299,20 +299,17 @@ RedirectEngine.prototype.fromCompiledRule = function(line) { /******************************************************************************/ RedirectEngine.prototype.compileRuleFromStaticFilter = function(line) { - var matches = this.reFilterParser.exec(line); - if ( matches === null || matches.length !== 4 ) { - return; - } - var µburi = µBlock.URI, - des = matches[1] || '', - pattern = (des + matches[2]).replace(/[.+?{}()|[\]\/\\]/g, '\\$&') - .replace(/\^/g, '[^\\w.%-]') - .replace(/\*/g, '.*?'), - type, + const matches = this.reFilterParser.exec(line); + if ( matches === null || matches.length !== 4 ) { return; } + + let des = matches[1] || ''; + const pattern = (des + matches[2]).replace(/[.+?{}()|[\]\/\\]/g, '\\$&') + .replace(/\^/g, '[^\\w.%-]') + .replace(/\*/g, '.*?'); + let type, redirect = '', - srcs = [], - options = matches[3].split(','), option; - while ( (option = options.pop()) ) { + srcs = []; + for ( const option of matches[3].split(',') ) { if ( option.startsWith('redirect=') ) { redirect = option.slice(9); continue; @@ -321,29 +318,23 @@ RedirectEngine.prototype.compileRuleFromStaticFilter = function(line) { srcs = option.slice(7).split('|'); continue; } - if ( option === 'first-party' ) { - srcs.push(µburi.domainFromHostnameNoCache(des) || des); + if ( option === 'first-party' || option === '1p' ) { + srcs.push(µBlock.URI.domainFromHostnameNoCache(des) || des); continue; } // One and only one type must be specified. - if ( option in this.supportedTypes ) { - if ( type !== undefined ) { - return; - } - type = this.supportedTypes[option]; + if ( this.supportedTypes.has(option) ) { + if ( type !== undefined ) { return; } + type = this.supportedTypes.get(option); continue; } } // Need a resource token. - if ( redirect === '' ) { - return; - } + if ( redirect === '' ) { return; } // Need one single type -- not negated. - if ( type === undefined || type.startsWith('~') ) { - return; - } + if ( type === undefined ) { return; } if ( des === '' ) { des = '*'; @@ -353,16 +344,10 @@ RedirectEngine.prototype.compileRuleFromStaticFilter = function(line) { srcs.push('*'); } - var out = []; - var i = srcs.length, src; - while ( i-- ) { - src = srcs[i]; - if ( src === '' ) { - continue; - } - if ( src.startsWith('~') ) { - continue; - } + const out = []; + for ( const src of srcs ) { + if ( src === '' ) { continue; } + if ( src.startsWith('~') ) { continue; } out.push(src + '\t' + des + '\t' + type + '\t' + pattern + '\t' + redirect); } @@ -373,18 +358,19 @@ RedirectEngine.prototype.compileRuleFromStaticFilter = function(line) { RedirectEngine.prototype.reFilterParser = /^(?:\|\|([^\/:?#^*]+)|\*)([^$]+)\$([^$]+)$/; -RedirectEngine.prototype.supportedTypes = (function() { - var types = Object.create(null); - types.font = 'font'; - types.image = 'image'; - types.media = 'media'; - types.object = 'object'; - types.script = 'script'; - types.stylesheet = 'stylesheet'; - types.subdocument = 'sub_frame'; - types.xmlhttprequest = 'xmlhttprequest'; - return types; -})(); +RedirectEngine.prototype.supportedTypes = new Map([ + [ 'css', 'stylesheet' ], + [ 'font', 'font' ], + [ 'image', 'image' ], + [ 'media', 'media' ], + [ 'object', 'object' ], + [ 'script', 'script' ], + [ 'stylesheet', 'stylesheet' ], + [ 'frame', 'sub_frame' ], + [ 'subdocument', 'sub_frame' ], + [ 'xhr', 'xmlhttprequest' ], + [ 'xmlhttprequest', 'xmlhttprequest' ], +]); /******************************************************************************/ diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index b78239e5a..dc8382d48 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -1385,6 +1385,7 @@ FilterParser.prototype.toNormalizedType = { 'beacon': 'other', 'css': 'stylesheet', 'data': 'data', + 'doc': 'main_frame', 'document': 'main_frame', 'elemhide': 'generichide', 'font': 'font',