1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-24 19:33:01 +01:00

code review: first-party determination must be based on root domain in dyna-filtering

This commit is contained in:
gorhill 2014-10-07 16:30:40 -04:00
parent fe486b7cb7
commit 500660bf18
3 changed files with 36 additions and 24 deletions

View File

@ -202,6 +202,8 @@ var tagNameToRequestTypeMap = {
var filterRequests = function(pageStore, details) {
details.pageDomain = µb.URI.domainFromHostname(details.pageHostname);
details.rootHostname = pageStore.rootHostname;
details.rootDomain = pageStore.rootDomain;
var inRequests = details.requests;
var outRequests = [];
@ -236,6 +238,8 @@ var filterRequest = function(pageStore, details) {
return;
}
details.pageDomain = µb.URI.domainFromHostname(details.pageHostname);
details.rootHostname = pageStore.rootHostname;
details.rootDomain = pageStore.rootDomain;
var result = pageStore.filterRequest(
details,
tagNameToRequestTypeMap[details.tagName],

View File

@ -140,6 +140,15 @@ var atoi = function(s) {
return cachedParseInt(s, 10);
};
var isFirstParty = function(firstPartyDomain, hostname) {
if ( hostname.slice(0 - firstPartyDomain.length) !== firstPartyDomain ) {
return false;
}
// Be sure to not confuse 'example.com' with 'anotherexample.com'
var c = hostname.charAt(hostname.length - firstPartyDomain.length - 1);
return c === '.' || c === '';
};
/*******************************************************************************
Filters family tree:
@ -1945,19 +1954,21 @@ FilterContainer.prototype.match3rdPartyHostname = function(requestHostname) {
FilterContainer.prototype.matchStringExactType = function(pageDetails, requestURL, requestType) {
var url = requestURL.toLowerCase();
var pageDomain = pageDetails.pageDomain || '';
var requestHostname = µb.URI.hostnameFromURI(requestURL);
var party = requestHostname.slice(-pageDomain.length) === pageDomain ?
FirstParty :
ThirdParty;
// Evaluate dynamic filters first. "Block" dynamic filters are always
// "important", they override everything else.
var bf = this.matchDynamicFilters(pageDetails.rootHostname, requestType, party === FirstParty);
var bf = this.matchDynamicFilters(
pageDetails.rootHostname,
requestType,
isFirstParty(pageDetails.rootDomain, requestHostname)
);
if ( bf !== '' && bf.slice(0, 2) !== '@@' ) {
return bf;
}
var party = isFirstParty(pageDetails.pageDomain, requestHostname) ? FirstParty : ThirdParty;
// This will be used by hostname-based filters
pageHostname = pageDetails.pageHostname || '';
@ -2030,26 +2041,21 @@ FilterContainer.prototype.matchString = function(pageDetails, requestURL, reques
// filters are tested *only* if there is a (unlikely) hit on a block
// filter.
var pageDomain = pageDetails.pageDomain || '';
var requestHostname = µb.URI.hostnameFromURI(requestURL);
// Find out the relation between the page and request
var party = ThirdParty;
if ( requestHostname.slice(0 - pageDomain.length) === pageDomain ) {
// Be sure to not confuse 'example.com' with 'anotherexample.com'
var c = requestHostname.charAt(requestHostname.length - pageDomain.length - 1);
if ( c === '' || c === '.' ) {
party = FirstParty;
}
}
// Evaluate dynamic filters first. "Block" dynamic filters are always
// "important", they override everything else.
var bf = this.matchDynamicFilters(pageDetails.rootHostname, requestType, party === FirstParty);
var bf = this.matchDynamicFilters(
pageDetails.rootHostname,
requestType,
isFirstParty(pageDetails.rootDomain, requestHostname)
);
if ( bf !== '' && bf.slice(0, 2) !== '@@' ) {
return bf;
}
var party = isFirstParty(pageDetails.pageDomain, requestHostname) ? FirstParty : ThirdParty ;
// This will be used by hostname-based filters
pageHostname = pageDetails.pageHostname || '';

View File

@ -240,15 +240,17 @@ FrameStore.factory = function(rootHostname, frameURL) {
FrameStore.prototype.init = function(rootHostname, frameURL) {
var µburi = µb.URI;
this.pageHostname = µburi.hostnameFromURI(frameURL);
this.pageDomain = µburi.domainFromHostname(this.pageHostname);
this.pageDomain = µburi.domainFromHostname(this.pageHostname) || this.pageHostname;
this.rootHostname = rootHostname;
this.rootDomain = µburi.domainFromHostname(rootHostname) || rootHostname;
return this;
};
/******************************************************************************/
FrameStore.prototype.dispose = function() {
this.pageHostname = this.pageDomain = this.rootHostname = '';
this.pageHostname = this.pageDomain =
this.rootHostname = this.rootDomain = '';
if ( frameStoreJunkyard.length < frameStoreJunkyardMax ) {
frameStoreJunkyard.push(this);
}
@ -298,6 +300,7 @@ PageStore.prototype.init = function(tabId, pageURL) {
// Use hostname if no domain can be extracted
this.pageDomain = µb.URI.domainFromHostname(this.pageHostname) || this.pageHostname;
this.rootHostname = this.pageHostname;
this.rootDomain = this.pageDomain;
this.frames = {};
this.netFiltering = true;
@ -327,6 +330,7 @@ PageStore.prototype.reuse = function(pageURL, context) {
this.pageHostname = µb.URI.hostnameFromURI(pageURL);
this.pageDomain = µb.URI.domainFromHostname(this.pageHostname) || this.pageHostname;
this.rootHostname = this.pageHostname;
this.rootDomain = this.pageDomain;
return this;
}
// A new page is completely reloaded from scratch, reset all.
@ -347,11 +351,9 @@ PageStore.prototype.dispose = function() {
// need to release the memory taken by these, which can amount to
// sizeable enough chunks (especially requests, through the request URL
// used as a key).
this.pageURL = '';
this.previousPageURL = '';
this.pageHostname = '';
this.pageDomain = '';
this.rootHostname = '';
this.pageURL = this.previousPageURL =
this.pageHostname = this.pageDomain =
this.rootHostname = this.rootDomain = '';
this.disposeFrameStores();
this.netFilteringCache = this.netFilteringCache.dispose();
if ( pageStoreJunkyard.length < pageStoreJunkyardMax ) {