Skip to content

Commit cda3970

Browse files
committed
Properly fail on bad network filter pattern
Related issue: - uBlockOrigin/uBlock-issues#1146
1 parent 46e90b2 commit cda3970

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/js/codemirror/ubo-static-filtering.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,13 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
727727
if ( astParser.hasError() === false ) { return; }
728728
let error = 'Invalid filter';
729729
if ( astParser.isCosmeticFilter() && astParser.result.error ) {
730-
error = `${error}: ${astParser.result.error}`;
730+
return `${error}: ${astParser.result.error}`;
731+
}
732+
if ( astParser.astError === sfp.AST_ERROR_BAD_REGEX ) {
733+
return `${error}: Bad regular expression`;
734+
}
735+
if ( astParser.astError === sfp.AST_ERROR_BAD_PATTERN ) {
736+
return `${error}: Bad pattern`;
731737
}
732738
return error;
733739
};

src/js/static-filtering-parser.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ export const AST_FLAG_NET_PATTERN_LEFT_ANCHOR = 1 << iota++;
9090
export const AST_FLAG_NET_PATTERN_RIGHT_ANCHOR = 1 << iota++;
9191
export const AST_FLAG_HAS_OPTIONS = 1 << iota++;
9292

93+
iota = 0;
94+
export const AST_ERROR_NONE = 1 << iota++;
95+
export const AST_ERROR_BAD_REGEX = 1 << iota++;
96+
export const AST_ERROR_BAD_PATTERN = 1 << iota++;
97+
9398
iota = 0;
9499
const NODE_RIGHT_INDEX = iota++;
95100
const NOOP_NODE_SIZE = iota;
@@ -736,7 +741,7 @@ export class AstFilterParser {
736741
this.reHasWhitespaceChar = /\s/;
737742
this.reHasUppercaseChar = /[A-Z]/;
738743
this.reHasUnicodeChar = /[^\x00-\x7F]/;
739-
this.reUnicodeChars = /[^\x00-\x7F]/g;
744+
this.reUnicodeChars = /\P{ASCII}/gu;
740745
this.reBadHostnameChars = /[\x00-\x24\x26-\x29\x2b\x2c\x2f\x3b-\x40\x5c\x5e\x60\x7b-\x7f]/;
741746
this.reIsEntity = /^[^*]+\.\*$/;
742747
this.rePreparseDirectiveIf = /^!#if /;
@@ -765,6 +770,7 @@ export class AstFilterParser {
765770
this.astType = AST_TYPE_NONE;
766771
this.astTypeFlavor = AST_TYPE_NONE;
767772
this.astFlags = 0;
773+
this.astError = 0;
768774
this.rootNode = this.allocTypedNode(NODE_TYPE_LINE_RAW, 0, this.rawEnd);
769775
if ( this.rawEnd === 0 ) { return; }
770776

@@ -1475,6 +1481,7 @@ export class AstFilterParser {
14751481
}
14761482
} else {
14771483
this.astTypeFlavor = AST_TYPE_NETWORK_PATTERN_BAD;
1484+
this.astError = AST_ERROR_BAD_REGEX;
14781485
this.addFlags(AST_FLAG_HAS_ERROR);
14791486
this.addNodeFlags(next, NODE_FLAG_ERROR);
14801487
}
@@ -1592,18 +1599,19 @@ export class AstFilterParser {
15921599
? this.normalizePattern(pattern)
15931600
: pattern;
15941601
next = this.allocTypedNode(NODE_TYPE_NET_PATTERN, patternBeg, patternEnd);
1595-
if ( normal === '' || pattern === '*' ) {
1602+
if ( normal === undefined ) {
1603+
this.astTypeFlavor = AST_TYPE_NETWORK_PATTERN_BAD;
1604+
this.addFlags(AST_FLAG_HAS_ERROR);
1605+
this.astError = AST_ERROR_BAD_PATTERN;
1606+
this.addNodeFlags(next, NODE_FLAG_ERROR);
1607+
} else if ( normal === '' || pattern === '*' ) {
15961608
this.astTypeFlavor = AST_TYPE_NETWORK_PATTERN_ANY;
15971609
} else if ( this.reHostnameAscii.test(normal) ) {
15981610
this.astTypeFlavor = AST_TYPE_NETWORK_PATTERN_HOSTNAME;
15991611
} else if ( this.reHasPatternSpecialChars.test(normal) ) {
16001612
this.astTypeFlavor = AST_TYPE_NETWORK_PATTERN_GENERIC;
1601-
} else if ( normal !== undefined ) {
1602-
this.astTypeFlavor = AST_TYPE_NETWORK_PATTERN_PLAIN;
16031613
} else {
1604-
this.astTypeFlavor = AST_TYPE_NETWORK_PATTERN_BAD;
1605-
this.addFlags(AST_FLAG_HAS_ERROR);
1606-
this.addNodeFlags(next, NODE_FLAG_ERROR);
1614+
this.astTypeFlavor = AST_TYPE_NETWORK_PATTERN_PLAIN;
16071615
}
16081616
this.addNodeToRegister(NODE_TYPE_NET_PATTERN, next);
16091617
if ( needNormalization && normal !== undefined ) {
@@ -1692,9 +1700,8 @@ export class AstFilterParser {
16921700
if ( this.reHasUnicodeChar.test(normal) === false ) { return normal; }
16931701
// Percent-encode remaining Unicode characters.
16941702
try {
1695-
normal = normal.replace(
1696-
this.reUnicodeChars,
1697-
s => encodeURIComponent(s).toLowerCase()
1703+
normal = normal.replace(this.reUnicodeChars, s =>
1704+
encodeURIComponent(s).toLowerCase()
16981705
);
16991706
} catch (ex) {
17001707
return;

0 commit comments

Comments
 (0)