From 500660bf182cd25c97832316cc46f0aaf9b5b727 Mon Sep 17 00:00:00 2001 From: gorhill Date: Tue, 7 Oct 2014 16:30:40 -0400 Subject: [PATCH] code review: first-party determination must be based on root domain in dyna-filtering --- js/messaging-handlers.js | 4 ++++ js/net-filtering.js | 40 +++++++++++++++++++++++----------------- js/pagestore.js | 16 +++++++++------- 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/js/messaging-handlers.js b/js/messaging-handlers.js index 5c061865c..727645c27 100644 --- a/js/messaging-handlers.js +++ b/js/messaging-handlers.js @@ -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], diff --git a/js/net-filtering.js b/js/net-filtering.js index 68680eee9..342bf4e85 100644 --- a/js/net-filtering.js +++ b/js/net-filtering.js @@ -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 || ''; diff --git a/js/pagestore.js b/js/pagestore.js index ddd2940d2..6d8b73788 100644 --- a/js/pagestore.js +++ b/js/pagestore.js @@ -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 ) {