Skip to content
This repository was archived by the owner on Nov 15, 2017. It is now read-only.

Commit 4095bb5

Browse files
committed
fixed false negatives: parent hostnames need to be tested too
1 parent 656a580 commit 4095bb5

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

doc/img/onbeforerequest-perf.png

102 Bytes
Loading

js/abp-filters.js

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ var FilterPlain = function(s, tokenBeg) {
8787
};
8888

8989
FilterPlain.prototype.match = function(url, tokenBeg) {
90+
// adbProfiler.countTest();
9091
return url.substr(tokenBeg - this.tokenBeg, this.s.length) === this.s;
9192
};
9293

@@ -99,6 +100,7 @@ var FilterPlainHostname = function(s, tokenBeg, hostname) {
99100
};
100101

101102
FilterPlainHostname.prototype.match = function(url, tokenBeg) {
103+
// adbProfiler.countTest();
102104
return pageHostname.slice(-this.hostname.length) === this.hostname &&
103105
url.substr(tokenBeg - this.tokenBeg, this.s.length) === this.s;
104106
};
@@ -112,6 +114,7 @@ var FilterPlainNotHostname = function(s, tokenBeg, hostname) {
112114
};
113115

114116
FilterPlainNotHostname.prototype.match = function(url, tokenBeg) {
117+
// adbProfiler.countTest();
115118
return pageHostname.slice(-this.hostname.length) !== this.hostname &&
116119
url.substr(tokenBeg - this.tokenBeg, this.s.length) === this.s;
117120
};
@@ -123,6 +126,7 @@ var FilterPlainPrefix0 = function(s) {
123126
};
124127

125128
FilterPlainPrefix0.prototype.match = function(url, tokenBeg) {
129+
// adbProfiler.countTest();
126130
return url.substr(tokenBeg, this.s.length) === this.s;
127131
};
128132

@@ -134,6 +138,7 @@ var FilterPlainPrefix0Hostname = function(s, hostname) {
134138
};
135139

136140
FilterPlainPrefix0Hostname.prototype.match = function(url, tokenBeg) {
141+
// adbProfiler.countTest();
137142
return pageHostname.slice(-this.hostname.length) === this.hostname &&
138143
url.substr(tokenBeg, this.s.length) === this.s;
139144
};
@@ -146,6 +151,7 @@ var FilterPlainPrefix0NotHostname = function(s, hostname) {
146151
};
147152

148153
FilterPlainPrefix0NotHostname.prototype.match = function(url, tokenBeg) {
154+
// adbProfiler.countTest();
149155
return pageHostname.slice(-this.hostname.length) !== this.hostname &&
150156
url.substr(tokenBeg, this.s.length) === this.s;
151157
};
@@ -157,6 +163,7 @@ var FilterPlainPrefix1 = function(s) {
157163
};
158164

159165
FilterPlainPrefix1.prototype.match = function(url, tokenBeg) {
166+
// adbProfiler.countTest();
160167
return url.substr(tokenBeg - 1, this.s.length) === this.s;
161168
};
162169

@@ -168,6 +175,7 @@ var FilterPlainPrefix1Hostname = function(s, hostname) {
168175
};
169176

170177
FilterPlainPrefix1Hostname.prototype.match = function(url, tokenBeg) {
178+
// adbProfiler.countTest();
171179
return pageHostname.slice(-this.hostname.length) === this.hostname &&
172180
url.substr(tokenBeg - 1, this.s.length) === this.s;
173181
};
@@ -180,6 +188,7 @@ var FilterPlainPrefix1NotHostname = function(s, hostname) {
180188
};
181189

182190
FilterPlainPrefix1NotHostname.prototype.match = function(url, tokenBeg) {
191+
// adbProfiler.countTest();
183192
return pageHostname.slice(-this.hostname.length) !== this.hostname &&
184193
url.substr(tokenBeg - 1, this.s.length) === this.s;
185194
};
@@ -200,6 +209,7 @@ var FilterSingleWildcard = function(s, tokenBeg) {
200209
};
201210

202211
FilterSingleWildcard.prototype.match = function(url, tokenBeg) {
212+
// adbProfiler.countTest();
203213
tokenBeg -= this.tokenBeg;
204214
return url.substr(tokenBeg, this.lSegment.length) === this.lSegment &&
205215
url.indexOf(this.rSegment, tokenBeg + this.lSegment.length) > 0;
@@ -217,6 +227,7 @@ var FilterSingleWildcardHostname = function(s, tokenBeg, hostname) {
217227
};
218228

219229
FilterSingleWildcardHostname.prototype.match = function(url, tokenBeg) {
230+
// adbProfiler.countTest();
220231
tokenBeg -= this.tokenBeg;
221232
return pageHostname.slice(-this.hostname.length) === this.hostname &&
222233
url.substr(tokenBeg, this.lSegment.length) === this.lSegment &&
@@ -235,6 +246,7 @@ var FilterSingleWildcardNotHostname = function(s, tokenBeg, hostname) {
235246
};
236247

237248
FilterSingleWildcardNotHostname.prototype.match = function(url, tokenBeg) {
249+
// adbProfiler.countTest();
238250
tokenBeg -= this.tokenBeg;
239251
return pageHostname.slice(-this.hostname.length) !== this.hostname &&
240252
url.substr(tokenBeg, this.lSegment.length) === this.lSegment &&
@@ -251,6 +263,7 @@ var FilterSingleWildcardPrefix0 = function(s) {
251263
};
252264

253265
FilterSingleWildcardPrefix0.prototype.match = function(url, tokenBeg) {
266+
// adbProfiler.countTest();
254267
return url.substr(tokenBeg, this.lSegment.length) === this.lSegment &&
255268
url.indexOf(this.rSegment, tokenBeg + this.lSegment.length) > 0;
256269
};
@@ -266,6 +279,7 @@ var FilterSingleWildcardPrefix0Hostname = function(s, hostname) {
266279
};
267280

268281
FilterSingleWildcardPrefix0Hostname.prototype.match = function(url, tokenBeg) {
282+
// adbProfiler.countTest();
269283
return pageHostname.slice(-this.hostname.length) === this.hostname &&
270284
url.substr(tokenBeg, this.lSegment.length) === this.lSegment &&
271285
url.indexOf(this.rSegment, tokenBeg + this.lSegment.length) > 0;
@@ -282,6 +296,7 @@ var FilterSingleWildcardPrefix0NotHostname = function(s, hostname) {
282296
};
283297

284298
FilterSingleWildcardPrefix0NotHostname.prototype.match = function(url, tokenBeg) {
299+
// adbProfiler.countTest();
285300
return pageHostname.slice(-this.hostname.length) !== this.hostname &&
286301
url.substr(tokenBeg, this.lSegment.length) === this.lSegment &&
287302
url.indexOf(this.rSegment, tokenBeg + this.lSegment.length) > 0;
@@ -302,6 +317,7 @@ var FilterManyWildcards = function(s, tokenBeg) {
302317
};
303318

304319
FilterManyWildcards.prototype.match = function(url, tokenBeg) {
320+
// adbProfiler.countTest();
305321
return this.re.test(url.slice(tokenBeg - this.tokenBeg));
306322
};
307323

@@ -315,6 +331,7 @@ var FilterManyWildcardsHostname = function(s, tokenBeg, hostname) {
315331
};
316332

317333
FilterManyWildcardsHostname.prototype.match = function(url, tokenBeg) {
334+
// adbProfiler.countTest();
318335
return pageHostname.slice(-this.hostname.length) === this.hostname &&
319336
this.re.test(url.slice(tokenBeg - this.tokenBeg));
320337
};
@@ -329,6 +346,7 @@ var FilterManyWildcardsNotHostname = function(s, tokenBeg, hostname) {
329346
};
330347

331348
FilterManyWildcardsNotHostname.prototype.match = function(url, tokenBeg) {
349+
// adbProfiler.countTest();
332350
return pageHostname.slice(-this.hostname.length) !== this.hostname &&
333351
this.re.test(url.slice(tokenBeg - this.tokenBeg));
334352
};
@@ -864,7 +882,6 @@ FilterContainer.prototype.reset = function() {
864882
/******************************************************************************/
865883
/*
866884
var adbProfiler = {
867-
testSwitch: false,
868885
testCount: 0,
869886
urlCount: 0,
870887
dumpEach: 200,
@@ -874,13 +891,8 @@ var adbProfiler = {
874891
this.dump();
875892
}
876893
},
877-
testCounter: function(on) {
878-
this.testSwitch = on;
879-
},
880894
countTest: function() {
881-
if ( this.testSwitch ) {
882-
this.testCount += 1;
883-
}
895+
this.testCount += 1;
884896
},
885897
dump: function() {
886898
console.log('HTTPSB.adbProfiler> number or filters tested per URL: %d (sample: %d URLs)', this.testCount / this.urlCount, this.urlCount);
@@ -1135,9 +1147,33 @@ FilterContainer.prototype.matchTokens = function() {
11351147

11361148
/******************************************************************************/
11371149

1150+
// This is where we test filters which have the form:
1151+
//
1152+
// `||www.example.com^$third-party`
1153+
//
1154+
// Because LiquidDict is well optimized to deal with plain hostname, we gain
1155+
// reusing it here for these sort of filters rather than using filters
1156+
// specialized to deal with other complex filters.
1157+
1158+
FilterContainer.prototype.match3rdPartyHostname = function(requestHostname) {
1159+
// Quick test first
1160+
if ( this.blocked3rdPartyHostnames.test(requestHostname) ) {
1161+
return '||' + requestHostname + '^$third-party';
1162+
}
1163+
// Check parent hostnames if quick test failed
1164+
var hostnames = HTTPSB.URI.parentHostnamesFromHostname(requestHostname);
1165+
for ( var i = 0, n = hostnames.length; i < n; i++ ) {
1166+
if ( this.blocked3rdPartyHostnames.test(hostnames[i]) ) {
1167+
return '||' + hostnames[i] + '^$third-party';
1168+
}
1169+
}
1170+
return false;
1171+
};
1172+
1173+
/******************************************************************************/
1174+
11381175
FilterContainer.prototype.matchString = function(pageStats, url, requestType, requestHostname) {
11391176
// adbProfiler.countUrl();
1140-
// adbProfiler.testCounter(true);
11411177

11421178
// https://github.com/gorhill/httpswitchboard/issues/239
11431179
// Convert url to lower case:
@@ -1170,8 +1206,8 @@ FilterContainer.prototype.matchString = function(pageStats, url, requestType, re
11701206
pageHostname = pageStats.pageHostname;
11711207

11721208
var bf = false;
1173-
if ( party === ThirdParty && this.blocked3rdPartyHostnames.test(requestHostname) ) {
1174-
bf = '||' + requestHostname + '^$third-party';
1209+
if ( party === ThirdParty ) {
1210+
bf = this.match3rdPartyHostname(requestHostname);
11751211
}
11761212

11771213
// Test against block filters

0 commit comments

Comments
 (0)