This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 971
/
Copy pathtrackingProtection.js
66 lines (57 loc) · 2.7 KB
/
trackingProtection.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* 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/. */
'use strict'
const urlParse = require('./common/urlParse')
const TrackingProtection = require('tracking-protection').CTPParser
const dataFile = require('./dataFile')
const Filtering = require('./filtering')
const isThirdPartyHost = require('./browser/isThirdPartyHost')
const LRUCache = require('lru-cache')
module.exports.resourceName = 'trackingProtection'
let trackingProtection
let cachedFirstParty = new LRUCache(50)
// Temporary whitelist until we find a better solution
const whitelistHosts = ['connect.facebook.net', 'connect.facebook.com', 'staticxx.facebook.com', 'www.facebook.com', 'scontent.xx.fbcdn.net', 'pbs.twimg.com', 'scontent-sjc2-1.xx.fbcdn.net', 'platform.twitter.com', 'syndication.twitter.com', 'cdn.syndication.twimg.com']
const startTrackingProtection = (wnd) => {
Filtering.registerBeforeRequestFilteringCB((details) => {
const mainFrameUrl = Filtering.getMainFrameUrl(details)
// this can happen if the tab is closed and the webContents is no longer available
if (!mainFrameUrl) {
return {
resourceName: module.exports.resourceName
}
}
const firstPartyUrl = urlParse(mainFrameUrl)
let firstPartyUrlHost = firstPartyUrl.hostname || ''
if (firstPartyUrlHost.startsWith('www.')) {
firstPartyUrlHost = firstPartyUrlHost.substring(4)
}
if (firstPartyUrl.protocol && firstPartyUrl.protocol.startsWith('http')) {
if (!cachedFirstParty.get(firstPartyUrlHost)) {
let firstPartyHosts = trackingProtection.findFirstPartyHosts(firstPartyUrlHost)
cachedFirstParty.set(firstPartyUrlHost, (firstPartyHosts && firstPartyHosts.split(',')) || [])
}
}
const urlHost = urlParse(details.url).hostname
const cancel = firstPartyUrl.protocol &&
details.resourceType !== 'mainFrame' &&
firstPartyUrl.protocol.startsWith('http') &&
!whitelistHosts.includes(urlHost) &&
cachedFirstParty.get(firstPartyUrlHost) &&
trackingProtection.matchesTracker(firstPartyUrlHost, urlHost) &&
urlHost !== firstPartyUrl.hostname &&
!cachedFirstParty.get(firstPartyUrlHost).find((baseHost) =>
!isThirdPartyHost(baseHost, urlHost))
dataFile.debug(module.exports.resourceName, details, cancel)
return {
cancel,
resourceName: module.exports.resourceName
}
})
}
module.exports.init = () => {
trackingProtection = new TrackingProtection()
dataFile.init(module.exports.resourceName, undefined, startTrackingProtection,
(data) => trackingProtection.deserialize(data))
}