fix missing trailing asterisk in filter representation in the logger

Issue unearthed in https://github.com/uBlockOrigin/uAssets/issues/4083#issuecomment-436914727
This commit is contained in:
Raymond Hill 2018-11-08 09:01:41 -02:00
parent 6d00583a50
commit 9eba215961
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 38 additions and 26 deletions

View File

@ -135,7 +135,7 @@ const isFirstParty = function(domain, hostname) {
const normalizeRegexSource = function(s) { const normalizeRegexSource = function(s) {
try { try {
var re = new RegExp(s); const re = new RegExp(s);
return re.source; return re.source;
} catch (ex) { } catch (ex) {
normalizeRegexSource.message = ex.toString(); normalizeRegexSource.message = ex.toString();
@ -144,24 +144,23 @@ const normalizeRegexSource = function(s) {
}; };
const rawToRegexStr = function(s, anchor) { const rawToRegexStr = function(s, anchor) {
let me = rawToRegexStr;
// https://www.loggly.com/blog/five-invaluable-techniques-to-improve-regex-performance/ // https://www.loggly.com/blog/five-invaluable-techniques-to-improve-regex-performance/
// https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions // https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions
// Also: remove leading/trailing wildcards -- there is no point. // Also: remove leading/trailing wildcards -- there is no point.
let reStr = s.replace(me.escape1, '\\$&') let reStr = s.replace(rawToRegexStr.escape1, '\\$&')
.replace(me.escape2, '(?:[^%.0-9a-z_-]|$)') .replace(rawToRegexStr.escape2, '(?:[^%.0-9a-z_-]|$)')
.replace(me.escape3, '') .replace(rawToRegexStr.escape3, '')
.replace(me.escape4, '[^ ]*?'); .replace(rawToRegexStr.escape4, '[^ ]*?');
if ( anchor & 0x4 ) { if ( anchor & 0b100 ) {
reStr = ( reStr = (
reStr.startsWith('\\.') ? reStr.startsWith('\\.') ?
rawToRegexStr.reTextHostnameAnchor2 : rawToRegexStr.reTextHostnameAnchor2 :
rawToRegexStr.reTextHostnameAnchor1 rawToRegexStr.reTextHostnameAnchor1
) + reStr; ) + reStr;
} else if ( anchor & 0x2 ) { } else if ( anchor & 0b010 ) {
reStr = '^' + reStr; reStr = '^' + reStr;
} }
if ( anchor & 0x1 ) { if ( anchor & 0b001 ) {
reStr += '$'; reStr += '$';
} }
return reStr; return reStr;
@ -173,6 +172,19 @@ rawToRegexStr.escape4 = /\*/g;
rawToRegexStr.reTextHostnameAnchor1 = '^[a-z-]+://(?:[^/?#]+\\.)?'; rawToRegexStr.reTextHostnameAnchor1 = '^[a-z-]+://(?:[^/?#]+\\.)?';
rawToRegexStr.reTextHostnameAnchor2 = '^[a-z-]+://(?:[^/?#]+)?'; rawToRegexStr.reTextHostnameAnchor2 = '^[a-z-]+://(?:[^/?#]+)?';
// https://github.com/uBlockOrigin/uAssets/issues/4083#issuecomment-436914727
const rawToPlainStr = function(s, anchor) {
if (
anchor === 0 &&
s.charCodeAt(0) === 0x2F /* '/' */ &&
s.length > 2 &&
s.charCodeAt(s.length-1) === 0x2F /* '/' */
) {
s = s + '*';
}
return s;
};
const filterDataSerialize = µb.CompiledLineIO.serialize; const filterDataSerialize = µb.CompiledLineIO.serialize;
const toLogDataInternal = function(categoryBits, tokenHash, filter) { const toLogDataInternal = function(categoryBits, tokenHash, filter) {
@ -308,8 +320,8 @@ FilterPlain.prototype.match = function(url, tokenBeg) {
FilterPlain.prototype.logData = function() { FilterPlain.prototype.logData = function() {
return { return {
raw: this.s, raw: rawToPlainStr(this.s, 0),
regex: rawToRegexStr(this.s), regex: rawToRegexStr(this.s, 0),
compiled: this.compile() compiled: this.compile()
}; };
}; };
@ -341,7 +353,7 @@ FilterPlainPrefix0.prototype.match = function(url, tokenBeg) {
FilterPlainPrefix0.prototype.logData = function() { FilterPlainPrefix0.prototype.logData = function() {
return { return {
raw: this.s, raw: this.s,
regex: rawToRegexStr(this.s), regex: rawToRegexStr(this.s, 0),
compiled: this.compile() compiled: this.compile()
}; };
}; };
@ -372,8 +384,8 @@ FilterPlainPrefix1.prototype.match = function(url, tokenBeg) {
FilterPlainPrefix1.prototype.logData = function() { FilterPlainPrefix1.prototype.logData = function() {
return { return {
raw: this.s, raw: rawToPlainStr(this.s, 0),
regex: rawToRegexStr(this.s), regex: rawToRegexStr(this.s, 0),
compiled: this.compile() compiled: this.compile()
}; };
}; };
@ -408,7 +420,7 @@ FilterPlainHostname.prototype.match = function() {
FilterPlainHostname.prototype.logData = function() { FilterPlainHostname.prototype.logData = function() {
return { return {
raw: '||' + this.s + '^', raw: '||' + this.s + '^',
regex: rawToRegexStr(this.s + '^'), regex: rawToRegexStr(this.s + '^', 0),
compiled: this.compile() compiled: this.compile()
}; };
}; };
@ -440,7 +452,7 @@ FilterPlainLeftAnchored.prototype.match = function(url) {
FilterPlainLeftAnchored.prototype.logData = function() { FilterPlainLeftAnchored.prototype.logData = function() {
return { return {
raw: '|' + this.s, raw: '|' + this.s,
regex: rawToRegexStr(this.s, 0x2), regex: rawToRegexStr(this.s, 0b010),
compiled: this.compile() compiled: this.compile()
}; };
}; };
@ -472,7 +484,7 @@ FilterPlainRightAnchored.prototype.match = function(url) {
FilterPlainRightAnchored.prototype.logData = function() { FilterPlainRightAnchored.prototype.logData = function() {
return { return {
raw: this.s + '|', raw: this.s + '|',
regex: rawToRegexStr(this.s, 0x1), regex: rawToRegexStr(this.s, 0b001),
compiled: this.compile() compiled: this.compile()
}; };
}; };
@ -504,7 +516,7 @@ FilterExactMatch.prototype.match = function(url) {
FilterExactMatch.prototype.logData = function() { FilterExactMatch.prototype.logData = function() {
return { return {
raw: '|' + this.s + '|', raw: '|' + this.s + '|',
regex: rawToRegexStr(this.s, 0x3), regex: rawToRegexStr(this.s, 0b011),
compiled: this.compile() compiled: this.compile()
}; };
}; };
@ -537,7 +549,7 @@ FilterPlainHnAnchored.prototype.match = function(url, tokenBeg) {
FilterPlainHnAnchored.prototype.logData = function() { FilterPlainHnAnchored.prototype.logData = function() {
return { return {
raw: '||' + this.s, raw: '||' + this.s,
regex: rawToRegexStr(this.s), regex: rawToRegexStr(this.s, 0),
compiled: this.compile() compiled: this.compile()
}; };
}; };
@ -574,7 +586,7 @@ FilterGeneric.prototype.match = function(url) {
FilterGeneric.prototype.logData = function() { FilterGeneric.prototype.logData = function() {
var out = { var out = {
raw: this.s, raw: rawToPlainStr(this.s, this.anchor),
regex: this.re.source, regex: this.re.source,
compiled: this.compile() compiled: this.compile()
}; };
@ -620,7 +632,7 @@ FilterGenericHnAnchored.prototype.match = function(url) {
FilterGenericHnAnchored.prototype.logData = function() { FilterGenericHnAnchored.prototype.logData = function() {
var out = { var out = {
raw: '||' + this.s, raw: '||' + this.s,
regex: rawToRegexStr(this.s, this.anchor & ~0x4), regex: rawToRegexStr(this.s, this.anchor & 0b001),
compiled: this.compile() compiled: this.compile()
}; };
return out; return out;
@ -1088,7 +1100,7 @@ FilterHostnameDict.prototype.match = function() {
FilterHostnameDict.prototype.logData = function() { FilterHostnameDict.prototype.logData = function() {
return { return {
raw: '||' + this.h + '^', raw: '||' + this.h + '^',
regex: rawToRegexStr(this.h) + '(?:[^%.0-9a-z_-]|$)', regex: rawToRegexStr(this.h, 0) + '(?:[^%.0-9a-z_-]|$)',
compiled: this.h compiled: this.h
}; };
}; };