Fix parsing of `redirect=` option as per 67e06f53b4 (commitcomment-27803901)

This commit is contained in:
Raymond Hill 2018-12-17 07:46:04 -05:00
parent d65dcbc771
commit dfcd23197d
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 35 additions and 48 deletions

View File

@ -299,20 +299,17 @@ RedirectEngine.prototype.fromCompiledRule = function(line) {
/******************************************************************************/ /******************************************************************************/
RedirectEngine.prototype.compileRuleFromStaticFilter = function(line) { RedirectEngine.prototype.compileRuleFromStaticFilter = function(line) {
var matches = this.reFilterParser.exec(line); const matches = this.reFilterParser.exec(line);
if ( matches === null || matches.length !== 4 ) { if ( matches === null || matches.length !== 4 ) { return; }
return;
} let des = matches[1] || '';
var µburi = µBlock.URI, const pattern = (des + matches[2]).replace(/[.+?{}()|[\]\/\\]/g, '\\$&')
des = matches[1] || '', .replace(/\^/g, '[^\\w.%-]')
pattern = (des + matches[2]).replace(/[.+?{}()|[\]\/\\]/g, '\\$&') .replace(/\*/g, '.*?');
.replace(/\^/g, '[^\\w.%-]') let type,
.replace(/\*/g, '.*?'),
type,
redirect = '', redirect = '',
srcs = [], srcs = [];
options = matches[3].split(','), option; for ( const option of matches[3].split(',') ) {
while ( (option = options.pop()) ) {
if ( option.startsWith('redirect=') ) { if ( option.startsWith('redirect=') ) {
redirect = option.slice(9); redirect = option.slice(9);
continue; continue;
@ -321,29 +318,23 @@ RedirectEngine.prototype.compileRuleFromStaticFilter = function(line) {
srcs = option.slice(7).split('|'); srcs = option.slice(7).split('|');
continue; continue;
} }
if ( option === 'first-party' ) { if ( option === 'first-party' || option === '1p' ) {
srcs.push(µburi.domainFromHostnameNoCache(des) || des); srcs.push(µBlock.URI.domainFromHostnameNoCache(des) || des);
continue; continue;
} }
// One and only one type must be specified. // One and only one type must be specified.
if ( option in this.supportedTypes ) { if ( this.supportedTypes.has(option) ) {
if ( type !== undefined ) { if ( type !== undefined ) { return; }
return; type = this.supportedTypes.get(option);
}
type = this.supportedTypes[option];
continue; continue;
} }
} }
// Need a resource token. // Need a resource token.
if ( redirect === '' ) { if ( redirect === '' ) { return; }
return;
}
// Need one single type -- not negated. // Need one single type -- not negated.
if ( type === undefined || type.startsWith('~') ) { if ( type === undefined ) { return; }
return;
}
if ( des === '' ) { if ( des === '' ) {
des = '*'; des = '*';
@ -353,16 +344,10 @@ RedirectEngine.prototype.compileRuleFromStaticFilter = function(line) {
srcs.push('*'); srcs.push('*');
} }
var out = []; const out = [];
var i = srcs.length, src; for ( const src of srcs ) {
while ( i-- ) { if ( src === '' ) { continue; }
src = srcs[i]; if ( src.startsWith('~') ) { continue; }
if ( src === '' ) {
continue;
}
if ( src.startsWith('~') ) {
continue;
}
out.push(src + '\t' + des + '\t' + type + '\t' + pattern + '\t' + redirect); 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.reFilterParser = /^(?:\|\|([^\/:?#^*]+)|\*)([^$]+)\$([^$]+)$/;
RedirectEngine.prototype.supportedTypes = (function() { RedirectEngine.prototype.supportedTypes = new Map([
var types = Object.create(null); [ 'css', 'stylesheet' ],
types.font = 'font'; [ 'font', 'font' ],
types.image = 'image'; [ 'image', 'image' ],
types.media = 'media'; [ 'media', 'media' ],
types.object = 'object'; [ 'object', 'object' ],
types.script = 'script'; [ 'script', 'script' ],
types.stylesheet = 'stylesheet'; [ 'stylesheet', 'stylesheet' ],
types.subdocument = 'sub_frame'; [ 'frame', 'sub_frame' ],
types.xmlhttprequest = 'xmlhttprequest'; [ 'subdocument', 'sub_frame' ],
return types; [ 'xhr', 'xmlhttprequest' ],
})(); [ 'xmlhttprequest', 'xmlhttprequest' ],
]);
/******************************************************************************/ /******************************************************************************/

View File

@ -1385,6 +1385,7 @@ FilterParser.prototype.toNormalizedType = {
'beacon': 'other', 'beacon': 'other',
'css': 'stylesheet', 'css': 'stylesheet',
'data': 'data', 'data': 'data',
'doc': 'main_frame',
'document': 'main_frame', 'document': 'main_frame',
'elemhide': 'generichide', 'elemhide': 'generichide',
'font': 'font', 'font': 'font',