Skip to content

Commit 9eba215

Browse files
committed
fix missing trailing asterisk in filter representation in the logger
Issue unearthed in uBlockOrigin/uAssets#4083 (comment)
1 parent 6d00583 commit 9eba215

1 file changed

Lines changed: 38 additions & 26 deletions

File tree

src/js/static-net-filtering.js

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ const isFirstParty = function(domain, hostname) {
135135

136136
const normalizeRegexSource = function(s) {
137137
try {
138-
var re = new RegExp(s);
138+
const re = new RegExp(s);
139139
return re.source;
140140
} catch (ex) {
141141
normalizeRegexSource.message = ex.toString();
@@ -144,24 +144,23 @@ const normalizeRegexSource = function(s) {
144144
};
145145

146146
const rawToRegexStr = function(s, anchor) {
147-
let me = rawToRegexStr;
148147
// https://www.loggly.com/blog/five-invaluable-techniques-to-improve-regex-performance/
149148
// https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
150149
// Also: remove leading/trailing wildcards -- there is no point.
151-
let reStr = s.replace(me.escape1, '\\$&')
152-
.replace(me.escape2, '(?:[^%.0-9a-z_-]|$)')
153-
.replace(me.escape3, '')
154-
.replace(me.escape4, '[^ ]*?');
155-
if ( anchor & 0x4 ) {
150+
let reStr = s.replace(rawToRegexStr.escape1, '\\$&')
151+
.replace(rawToRegexStr.escape2, '(?:[^%.0-9a-z_-]|$)')
152+
.replace(rawToRegexStr.escape3, '')
153+
.replace(rawToRegexStr.escape4, '[^ ]*?');
154+
if ( anchor & 0b100 ) {
156155
reStr = (
157-
reStr.startsWith('\\.') ?
158-
rawToRegexStr.reTextHostnameAnchor2 :
159-
rawToRegexStr.reTextHostnameAnchor1
160-
) + reStr;
161-
} else if ( anchor & 0x2 ) {
156+
reStr.startsWith('\\.') ?
157+
rawToRegexStr.reTextHostnameAnchor2 :
158+
rawToRegexStr.reTextHostnameAnchor1
159+
) + reStr;
160+
} else if ( anchor & 0b010 ) {
162161
reStr = '^' + reStr;
163162
}
164-
if ( anchor & 0x1 ) {
163+
if ( anchor & 0b001 ) {
165164
reStr += '$';
166165
}
167166
return reStr;
@@ -173,6 +172,19 @@ rawToRegexStr.escape4 = /\*/g;
173172
rawToRegexStr.reTextHostnameAnchor1 = '^[a-z-]+://(?:[^/?#]+\\.)?';
174173
rawToRegexStr.reTextHostnameAnchor2 = '^[a-z-]+://(?:[^/?#]+)?';
175174

175+
// https://github.com/uBlockOrigin/uAssets/issues/4083#issuecomment-436914727
176+
const rawToPlainStr = function(s, anchor) {
177+
if (
178+
anchor === 0 &&
179+
s.charCodeAt(0) === 0x2F /* '/' */ &&
180+
s.length > 2 &&
181+
s.charCodeAt(s.length-1) === 0x2F /* '/' */
182+
) {
183+
s = s + '*';
184+
}
185+
return s;
186+
};
187+
176188
const filterDataSerialize = µb.CompiledLineIO.serialize;
177189

178190
const toLogDataInternal = function(categoryBits, tokenHash, filter) {
@@ -308,8 +320,8 @@ FilterPlain.prototype.match = function(url, tokenBeg) {
308320

309321
FilterPlain.prototype.logData = function() {
310322
return {
311-
raw: this.s,
312-
regex: rawToRegexStr(this.s),
323+
raw: rawToPlainStr(this.s, 0),
324+
regex: rawToRegexStr(this.s, 0),
313325
compiled: this.compile()
314326
};
315327
};
@@ -341,7 +353,7 @@ FilterPlainPrefix0.prototype.match = function(url, tokenBeg) {
341353
FilterPlainPrefix0.prototype.logData = function() {
342354
return {
343355
raw: this.s,
344-
regex: rawToRegexStr(this.s),
356+
regex: rawToRegexStr(this.s, 0),
345357
compiled: this.compile()
346358
};
347359
};
@@ -372,8 +384,8 @@ FilterPlainPrefix1.prototype.match = function(url, tokenBeg) {
372384

373385
FilterPlainPrefix1.prototype.logData = function() {
374386
return {
375-
raw: this.s,
376-
regex: rawToRegexStr(this.s),
387+
raw: rawToPlainStr(this.s, 0),
388+
regex: rawToRegexStr(this.s, 0),
377389
compiled: this.compile()
378390
};
379391
};
@@ -408,7 +420,7 @@ FilterPlainHostname.prototype.match = function() {
408420
FilterPlainHostname.prototype.logData = function() {
409421
return {
410422
raw: '||' + this.s + '^',
411-
regex: rawToRegexStr(this.s + '^'),
423+
regex: rawToRegexStr(this.s + '^', 0),
412424
compiled: this.compile()
413425
};
414426
};
@@ -440,7 +452,7 @@ FilterPlainLeftAnchored.prototype.match = function(url) {
440452
FilterPlainLeftAnchored.prototype.logData = function() {
441453
return {
442454
raw: '|' + this.s,
443-
regex: rawToRegexStr(this.s, 0x2),
455+
regex: rawToRegexStr(this.s, 0b010),
444456
compiled: this.compile()
445457
};
446458
};
@@ -472,7 +484,7 @@ FilterPlainRightAnchored.prototype.match = function(url) {
472484
FilterPlainRightAnchored.prototype.logData = function() {
473485
return {
474486
raw: this.s + '|',
475-
regex: rawToRegexStr(this.s, 0x1),
487+
regex: rawToRegexStr(this.s, 0b001),
476488
compiled: this.compile()
477489
};
478490
};
@@ -504,7 +516,7 @@ FilterExactMatch.prototype.match = function(url) {
504516
FilterExactMatch.prototype.logData = function() {
505517
return {
506518
raw: '|' + this.s + '|',
507-
regex: rawToRegexStr(this.s, 0x3),
519+
regex: rawToRegexStr(this.s, 0b011),
508520
compiled: this.compile()
509521
};
510522
};
@@ -537,7 +549,7 @@ FilterPlainHnAnchored.prototype.match = function(url, tokenBeg) {
537549
FilterPlainHnAnchored.prototype.logData = function() {
538550
return {
539551
raw: '||' + this.s,
540-
regex: rawToRegexStr(this.s),
552+
regex: rawToRegexStr(this.s, 0),
541553
compiled: this.compile()
542554
};
543555
};
@@ -574,7 +586,7 @@ FilterGeneric.prototype.match = function(url) {
574586

575587
FilterGeneric.prototype.logData = function() {
576588
var out = {
577-
raw: this.s,
589+
raw: rawToPlainStr(this.s, this.anchor),
578590
regex: this.re.source,
579591
compiled: this.compile()
580592
};
@@ -620,7 +632,7 @@ FilterGenericHnAnchored.prototype.match = function(url) {
620632
FilterGenericHnAnchored.prototype.logData = function() {
621633
var out = {
622634
raw: '||' + this.s,
623-
regex: rawToRegexStr(this.s, this.anchor & ~0x4),
635+
regex: rawToRegexStr(this.s, this.anchor & 0b001),
624636
compiled: this.compile()
625637
};
626638
return out;
@@ -1088,7 +1100,7 @@ FilterHostnameDict.prototype.match = function() {
10881100
FilterHostnameDict.prototype.logData = function() {
10891101
return {
10901102
raw: '||' + this.h + '^',
1091-
regex: rawToRegexStr(this.h) + '(?:[^%.0-9a-z_-]|$)',
1103+
regex: rawToRegexStr(this.h, 0) + '(?:[^%.0-9a-z_-]|$)',
10921104
compiled: this.h
10931105
};
10941106
};

0 commit comments

Comments
 (0)