This commit is contained in:
gorhill 2015-03-16 14:58:35 -04:00
parent 6b8118bb68
commit b55a9458f1
1 changed files with 28 additions and 3 deletions

View File

@ -237,7 +237,9 @@ var FilterParser = function() {
this.unhide = 0; this.unhide = 0;
this.hostnames = []; this.hostnames = [];
this.invalid = false; this.invalid = false;
this.cosmetic = true;
this.reParser = /^\s*([^#]*)(##|#@#)(.+)\s*$/; this.reParser = /^\s*([^#]*)(##|#@#)(.+)\s*$/;
this.div = document.createElement('div');
}; };
/******************************************************************************/ /******************************************************************************/
@ -248,18 +250,31 @@ FilterParser.prototype.reset = function() {
this.unhide = 0; this.unhide = 0;
this.hostnames.length = 0; this.hostnames.length = 0;
this.invalid = false; this.invalid = false;
this.cosmetic = true;
return this; return this;
}; };
/******************************************************************************/ /******************************************************************************/
FilterParser.prototype.isValidSelector = function(s) {
try {
this.div.matches(s);
} catch (e) {
console.error('µBlock> invalid cosmetic filter:', s);
return false;
}
return true;
};
/******************************************************************************/
FilterParser.prototype.parse = function(s) { FilterParser.prototype.parse = function(s) {
// important! // important!
this.reset(); this.reset();
var matches = this.reParser.exec(s); var matches = this.reParser.exec(s);
if ( matches === null || matches.length !== 4 ) { if ( matches === null || matches.length !== 4 ) {
this.invalid = true; this.cosmetic = false;
return this; return this;
} }
@ -272,7 +287,7 @@ FilterParser.prototype.parse = function(s) {
// Any sequence of `#` longer than one means the line is not a valid // Any sequence of `#` longer than one means the line is not a valid
// cosmetic filter. // cosmetic filter.
if ( this.suffix.indexOf('##') !== -1 ) { if ( this.suffix.indexOf('##') !== -1 ) {
this.invalid = true; this.cosmetic = false;
return this; return this;
} }
@ -283,6 +298,13 @@ FilterParser.prototype.parse = function(s) {
this.suffix = this.suffix.slice(1); this.suffix = this.suffix.slice(1);
} }
// https://github.com/gorhill/uBlock/issues/1004
// Detect and report invalid CSS selectors.
if ( this.isValidSelector(this.suffix) === false ) {
this.invalid = true;
return this;
}
this.unhide = matches[2].charAt(1) === '@' ? 1 : 0; this.unhide = matches[2].charAt(1) === '@' ? 1 : 0;
if ( this.prefix !== '' ) { if ( this.prefix !== '' ) {
this.hostnames = this.prefix.split(/\s*,\s*/); this.hostnames = this.prefix.split(/\s*,\s*/);
@ -559,9 +581,12 @@ FilterContainer.prototype.reset = function() {
FilterContainer.prototype.compile = function(s, out) { FilterContainer.prototype.compile = function(s, out) {
var parsed = this.parser.parse(s); var parsed = this.parser.parse(s);
if ( parsed.invalid ) { if ( parsed.cosmetic === false ) {
return false; return false;
} }
if ( parsed.invalid ) {
return true;
}
var hostnames = parsed.hostnames; var hostnames = parsed.hostnames;
var i = hostnames.length; var i = hostnames.length;