Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Make third partiness computation much faster
Browse files Browse the repository at this point in the history
Based on the method used in Privacy Badger Chrome.
Also changes from using hosts (which include port) to hostnames.
Fix #696

Auditors: @bbondy
  • Loading branch information
diracdeltas committed Feb 10, 2016
1 parent 6b7173b commit d368f70
Show file tree
Hide file tree
Showing 5 changed files with 5,890 additions and 9 deletions.
16 changes: 10 additions & 6 deletions app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const BrowserWindow = electron.BrowserWindow
const AppStore = require('../js/stores/appStore')
const AppConfig = require('../js/constants/appConfig')
const urlParse = require('url').parse
const psl = require('psl')
const getBaseDomain = require('../js/lib/baseDomain').getBaseDomain

const filteringFns = []

Expand Down Expand Up @@ -46,8 +46,8 @@ function registerForSession (session) {

let requestHeaders = details.requestHeaders
if (module.exports.isResourceEnabled(AppConfig.resourceNames.COOKIEBLOCK) &&
module.exports.isThirdPartyHost(urlParse(details.firstPartyUrl || '').host,
urlParse(details.url || '').host)) {
module.exports.isThirdPartyHost(urlParse(details.firstPartyUrl || '').hostname,
urlParse(details.url || '').hostname)) {
// Clear cookie and referer on third-party requests
if (requestHeaders['Cookie']) {
requestHeaders['Cookie'] = undefined
Expand All @@ -73,12 +73,16 @@ function registerForSession (session) {
}

module.exports.isThirdPartyHost = (baseContextHost, testHost) => {
// TODO: Always return true if these are IP addresses that aren't the same
if (!testHost || !baseContextHost) {
return true
}
const testSuffix = psl.parse(testHost).domain
const baseContextSuffix = psl.parse(baseContextHost).domain
return testSuffix !== baseContextSuffix || !testSuffix || !baseContextSuffix
const documentDomain = getBaseDomain(baseContextHost)
if (testHost.length > documentDomain.length) {
return (testHost.substr(testHost.length - documentDomain.length - 1) !== '.' + documentDomain)
} else {
return (testHost !== documentDomain)
}
}

module.exports.init = () => {
Expand Down
4 changes: 2 additions & 2 deletions app/trackingProtection.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const startTrackingProtection = (wnd) => {
cachedFirstParty = {}
}
const firstPartyUrl = URL.parse(details.firstPartyUrl)
let firstPartyUrlHost = firstPartyUrl.host || ''
let firstPartyUrlHost = firstPartyUrl.hostname || ''
if (firstPartyUrlHost.startsWith('www.')) {
firstPartyUrlHost = firstPartyUrlHost.substring(4)
}
Expand All @@ -40,7 +40,7 @@ const startTrackingProtection = (wnd) => {
++cachedFirstPartyCount
}
}
const urlHost = URL.parse(details.url).host
const urlHost = URL.parse(details.url).hostname
const shouldBlock = firstPartyUrl.protocol &&
details.resourceType !== 'mainFrame' &&
firstPartyUrl.protocol.startsWith('http') &&
Expand Down
48 changes: 48 additions & 0 deletions js/lib/baseDomain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const punycode = require('punycode')
const publicSuffixes = require('./psl')

/**
* Returns base domain for specified host based on Public Suffix List.
* @param {string} hostname The name of the host to get the base domain for
*/
module.exports.getBaseDomain = function (hostname) {
// decode punycode if exists
if (hostname.indexOf('xn--') >= 0) {
hostname = punycode.toUnicode(hostname)
}

// search through PSL
var prevDomains = []
var curDomain = hostname
var nextDot = curDomain.indexOf('.')
var tld = 0
var suffix

while (true) {
suffix = publicSuffixes[curDomain]
if (typeof suffix !== 'undefined') {
tld = suffix
break
}

if (nextDot < 0) {
tld = 1
break
}

prevDomains.push(curDomain.substring(0, nextDot))
curDomain = curDomain.substring(nextDot + 1)
nextDot = curDomain.indexOf('.')
}

while (tld > 0 && prevDomains.length > 0) {
curDomain = prevDomains.pop() + '.' + curDomain
tld--
}

return curDomain
}
Loading

2 comments on commit d368f70

@diracdeltas
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could still be made faster by caching the most recently-checked domains, i think

@bbondy
Copy link
Member

@bbondy bbondy commented on d368f70 Feb 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please post an issue for that, we should because benchmarks alone do repeated reloading of the same site. Also useful in general.

Please sign in to comment.