Outright reject patterns with more than one space character

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/1118

This is not a complete fix for the reported issue, but this
should catch many reported cases of invalid filters in the
wild.
This commit is contained in:
Raymond Hill 2020-06-21 08:08:18 -04:00
parent 2ea6769065
commit 86d28b57c3
No known key found for this signature in database
GPG key ID: 25E1490B761470C2

View file

@ -338,7 +338,7 @@ const Parser = class {
// optionsAnchorSpan: first slice to options anchor // optionsAnchorSpan: first slice to options anchor
// optionsSpan: first slice to options // optionsSpan: first slice to options
analyzeNet() { analyzeNet() {
let islice = this.leftSpaceSpan.i; let islice = this.leftSpaceSpan.len;
// Assume no exception // Assume no exception
this.exceptionSpan.i = this.leftSpaceSpan.len; this.exceptionSpan.i = this.leftSpaceSpan.len;
@ -499,8 +499,10 @@ const Parser = class {
// https://github.com/gorhill/httpswitchboard/issues/15 // https://github.com/gorhill/httpswitchboard/issues/15
// When parsing a hosts file, ensure localhost et al. don't end up // When parsing a hosts file, ensure localhost et al. don't end up
// in the pattern. To accomplish this we establish the rule that // in the pattern. To accomplish this we establish the rule that
// if a pattern contains space characters, the pattern will be only // if a pattern contains a space character, the pattern will be only
// the part following the last space occurrence. // the part following the space character.
// https://github.com/uBlockOrigin/uBlock-issues/issues/1118
// Patterns with more than one space are dubious.
{ {
const { i, len } = this.patternSpan; const { i, len } = this.patternSpan;
let j = len; let j = len;
@ -512,6 +514,17 @@ const Parser = class {
this.patternBits |= bits; this.patternBits |= bits;
} }
if ( j !== 0 ) { if ( j !== 0 ) {
let dubious = false;
for ( let k = this.patternSpan.i; k < j; k += 3 ) {
if ( hasNoBits(this.slices[k], BITSpace) ) { continue; }
this.patternBits |= BITSpace;
if ( this.interactive ) {
this.markSlices(this.patternSpan.i, j, BITError);
}
dubious = true;
break;
}
if ( dubious === false ) {
this.patternSpan.i += j + 3; this.patternSpan.i += j + 3;
this.patternSpan.len -= j + 3; this.patternSpan.len -= j + 3;
if ( this.reIsLocalhostRedirect.test(this.getNetPattern()) ) { if ( this.reIsLocalhostRedirect.test(this.getNetPattern()) ) {
@ -520,6 +533,7 @@ const Parser = class {
if ( this.interactive ) { if ( this.interactive ) {
this.markSlices(0, this.patternSpan.i, BITIgnore); this.markSlices(0, this.patternSpan.i, BITIgnore);
} }
}
// TODO: test again for regex? // TODO: test again for regex?
} }
} }
@ -885,12 +899,15 @@ const Parser = class {
// https://github.com/chrisaljoudi/uBlock/issues/1096 // https://github.com/chrisaljoudi/uBlock/issues/1096
// Examples of dubious filter content: // Examples of dubious filter content:
// - Spaces characters
// - Single character other than `*` wildcard // - Single character other than `*` wildcard
patternIsDubious() { patternIsDubious() {
return this.patternBits !== BITAsterisk && return hasBits(this.patternBits, BITSpace) || (
this.patternBits !== BITAsterisk &&
this.optionsSpan.len === 0 && this.optionsSpan.len === 0 &&
this.patternSpan.len === 3 && this.patternSpan.len === 3 &&
this.slices[this.patternSpan.i+2] === 1; this.slices[this.patternSpan.i+2] === 1
);
} }
patternIsMatchAll() { patternIsMatchAll() {