-
-
Notifications
You must be signed in to change notification settings - Fork 386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Surrogates (fixes #400) #935
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
ad30f20
Refactor common property accessors into variables.
ghostwords 08f9cf5
Update copyright year for src/webrequest.js
ghostwords dbc3773
Add stub surrogate lookup.
ghostwords 75888cc
Fix background script loading order.
ghostwords 0772489
Switch to more compact data URI encoding.
ghostwords 23ea583
Add surrogate for legacy Google Analytics (ga.js).
ghostwords d3f67b0
Add a unit test.
ghostwords 2302469
Fix indentation.
ghostwords 6bcad20
Fix nit (related to b6c34272).
ghostwords a1a997e
Add note to fix synchronous XHR in main thread.
ghostwords 3f814a9
Move socialwidgets.json from src/ to data/
ghostwords d084db4
Split out surrogate definitions into own file.
ghostwords 52c37b7
Fix indentation.
ghostwords 4f27efe
Upgrade to hostname + suffix token-based checking.
ghostwords 0910a93
Replace repeated calls to get request hostname.
ghostwords 773091e
ES6 tweaks.
ghostwords 156a2ab
Add more unit tests.
ghostwords 1e1ac8d
Ignore querystrings when suffix matching.
ghostwords 8bd1a08
Add some documentation.
ghostwords 733eb8f
Improve variable name.
ghostwords 8b81fea
Add TODO.
ghostwords 4158e9d
Make tests more readable.
ghostwords 19f54f4
Add test for returned string being a JS data URI.
ghostwords 0c02006
Update ga.js surrogate.
ghostwords a88a76a
Add b.scorecardresearch.com surrogates.
ghostwords f9ab56d
Merge branch 'master' into surrogates
ghostwords 3864d77
Fix typo.
ghostwords 44f5b97
Add WIP integration test.
ghostwords 636c06d
Integration test WIP.
ghostwords 0438f8d
Finish integration test.
ghostwords 23f0e60
Rename integration test file.
ghostwords 7adb3f0
Merge branch 'master' into surrogates
ghostwords d5abfc6
Merge branch 'master' into surrogates
ghostwords 1a94f69
Add attribution link to comScore surrogate.
ghostwords 7d15ebb
Merge branch 'master' into surrogates
ghostwords 95ea557
Move integration test to own servers.
ghostwords f11cdf6
Tweak global var declaration.
ghostwords 8e290cd
Merge branch 'master' into surrogates
ghostwords 40d0a84
Fix integration test.
ghostwords File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* | ||
* | ||
* This file is part of Privacy Badger <https://www.eff.org/privacybadger> | ||
* Copyright (C) 2016 Electronic Frontier Foundation | ||
* | ||
* Privacy Badger is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* Privacy Badger is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Privacy Badger. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
require.scopes.surrogatedb = (function() { | ||
|
||
// "hostnames" maps hostnames to arrays of surrogate pattern tokens. | ||
// | ||
// A hostname can have one or more surrogate scripts. | ||
// | ||
// Surrogate pattern tokens are used to look up the actual | ||
// surrogate script code (stored in "surrogates" object below). | ||
const hostnames = { | ||
'b.scorecardresearch.com': [ | ||
'/beacon.js', | ||
'/c2/plugins/streamsense_plugin_html5.js', | ||
], | ||
'ssl.google-analytics.com': [ | ||
'/ga.js', | ||
], | ||
'www.google-analytics.com': [ | ||
'/ga.js', | ||
], | ||
}; | ||
|
||
// "surrogates" maps surrogate pattern tokens to surrogate script code. | ||
// | ||
// There is currently one type of surrogate pattern token: suffix. | ||
// Does the script URL (querystring excluded) end with the token? | ||
const surrogates = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More documentation needed here as well. |
||
/* eslint-disable no-extra-semi */ | ||
|
||
// Google Analytics (legacy ga.js) | ||
// | ||
// sourced from https://github.com/uBlockOrigin/uAssets/ under GPLv3 | ||
// https://github.com/uBlockOrigin/uAssets/blob/f79f3e69c1e20c47df1876efe2dd43027bf05b89/filters/resources.txt#L162-L256 | ||
// | ||
// test cases: | ||
// http://checkin.avianca.com/ | ||
// https://www.vmware.com/support/pubs/ws_pubs.html (release notes links) | ||
// | ||
// API reference: | ||
// https://developers.google.com/analytics/devguides/collection/gajs/methods/ | ||
'/ga.js': '(' + | ||
function() { | ||
var noopfn = function() { | ||
; | ||
}; | ||
// | ||
var Gaq = function() { | ||
; | ||
}; | ||
Gaq.prototype.Na = noopfn; | ||
Gaq.prototype.O = noopfn; | ||
Gaq.prototype.Sa = noopfn; | ||
Gaq.prototype.Ta = noopfn; | ||
Gaq.prototype.Va = noopfn; | ||
Gaq.prototype._createAsyncTracker = noopfn; | ||
Gaq.prototype._getAsyncTracker = noopfn; | ||
Gaq.prototype._getPlugin = noopfn; | ||
Gaq.prototype.push = function(a) { | ||
if ( typeof a === 'function' ) { | ||
a(); return; | ||
} | ||
if ( Array.isArray(a) === false ) { | ||
return; | ||
} | ||
// https://twitter.com/catovitch/status/776442930345218048 | ||
// https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiDomainDirectory#_gat.GA_Tracker_._link | ||
if ( a[0] === '_link' && typeof a[1] === 'string' ) { | ||
window.location.assign(a[1]); | ||
} | ||
}; | ||
// | ||
var tracker = (function() { | ||
var out = {}; | ||
var api = [ | ||
'_addIgnoredOrganic _addIgnoredRef _addItem _addOrganic', | ||
'_addTrans _clearIgnoredOrganic _clearIgnoredRef _clearOrganic', | ||
'_cookiePathCopy _deleteCustomVar _getName _setAccount', | ||
'_getAccount _getClientInfo _getDetectFlash _getDetectTitle', | ||
'_getLinkerUrl _getLocalGifPath _getServiceMode _getVersion', | ||
'_getVisitorCustomVar _initData _link _linkByPost', | ||
'_setAllowAnchor _setAllowHash _setAllowLinker _setCampContentKey', | ||
'_setCampMediumKey _setCampNameKey _setCampNOKey _setCampSourceKey', | ||
'_setCampTermKey _setCampaignCookieTimeout _setCampaignTrack _setClientInfo', | ||
'_setCookiePath _setCookiePersistence _setCookieTimeout _setCustomVar', | ||
'_setDetectFlash _setDetectTitle _setDomainName _setLocalGifPath', | ||
'_setLocalRemoteServerMode _setLocalServerMode _setReferrerOverride _setRemoteServerMode', | ||
'_setSampleRate _setSessionTimeout _setSiteSpeedSampleRate _setSessionCookieTimeout', | ||
'_setVar _setVisitorCookieTimeout _trackEvent _trackPageLoadTime', | ||
'_trackPageview _trackSocial _trackTiming _trackTrans', | ||
'_visitCode' | ||
].join(' ').split(/\s+/); | ||
var i = api.length; | ||
while ( i-- ) { | ||
out[api[i]] = noopfn; | ||
} | ||
out._getLinkerUrl = function(a) { | ||
return a; | ||
}; | ||
return out; | ||
})(); | ||
// | ||
var Gat = function() { | ||
; | ||
}; | ||
Gat.prototype._anonymizeIP = noopfn; | ||
Gat.prototype._createTracker = noopfn; | ||
Gat.prototype._forceSSL = noopfn; | ||
Gat.prototype._getPlugin = noopfn; | ||
Gat.prototype._getTracker = function() { | ||
return tracker; | ||
}; | ||
Gat.prototype._getTrackerByName = function() { | ||
return tracker; | ||
}; | ||
Gat.prototype._getTrackers = noopfn; | ||
Gat.prototype.aa = noopfn; | ||
Gat.prototype.ab = noopfn; | ||
Gat.prototype.hb = noopfn; | ||
Gat.prototype.la = noopfn; | ||
Gat.prototype.oa = noopfn; | ||
Gat.prototype.pa = noopfn; | ||
Gat.prototype.u = noopfn; | ||
var gat = new Gat(); | ||
window._gat = gat; | ||
// | ||
var gaq = new Gaq(); | ||
(function() { | ||
var aa = window._gaq || []; | ||
if ( Array.isArray(aa) ) { | ||
while ( aa[0] ) { | ||
gaq.push(aa.shift()); | ||
} | ||
} | ||
})(); | ||
window._gaq = gaq.qf = gaq; | ||
} + ')();', | ||
|
||
// https://github.com/gorhill/uBlock/issues/1265 | ||
// https://github.com/uBlockOrigin/uAssets/blob/581f2c93eeca0e55991aa331721b6942f3162615/filters/resources.txt#L736-L746 | ||
'/beacon.js': '(' + | ||
function() { | ||
window.COMSCORE = { | ||
purge: function() { | ||
_comscore = []; // eslint-disable-line no-undef | ||
}, | ||
beacon: function() { | ||
; | ||
} | ||
}; | ||
} + ')();', | ||
|
||
// http://www.dplay.se/ett-jobb-for-berg/ (videos) | ||
'/c2/plugins/streamsense_plugin_html5.js': '(' + | ||
function() { | ||
} + ')();', | ||
|
||
/* eslint-enable no-extra-semi */ | ||
}; | ||
|
||
const exports = { | ||
hostnames: hostnames, | ||
surrogates: surrogates, | ||
}; | ||
|
||
return exports; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* | ||
* This file is part of Privacy Badger <https://www.eff.org/privacybadger> | ||
* Copyright (C) 2016 Electronic Frontier Foundation | ||
* | ||
* Privacy Badger is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* Privacy Badger is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Privacy Badger. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
require.scopes.surrogates = (function() { | ||
|
||
const db = require('surrogatedb'); | ||
|
||
/** | ||
* Blocking tracking scripts (trackers) can cause parts of webpages to break. | ||
* Surrogate scripts are dummy pieces of JavaScript meant to supply just enough | ||
* of the original tracker's functionality to allow pages to continue working. | ||
* | ||
* This method gets called within request-blocking listeners: | ||
* It needs to be fast! | ||
* | ||
* @param {String} script_url The full URL of the script resource being requested. | ||
* | ||
* @param {String} script_hostname The hostname component of the script_url | ||
* parameter. This is an optimization: the calling context should already have | ||
* this information. | ||
* | ||
* @return {String|Boolean} The surrogate script as a data URI when there is a | ||
* match, or boolean false when there is no match. | ||
*/ | ||
function getSurrogateURI(script_url, script_hostname) { | ||
// do we have an entry for the script hostname? | ||
if (db.hostnames.hasOwnProperty(script_hostname)) { | ||
const tokens = db.hostnames[script_hostname]; | ||
|
||
// do any of the pattern tokens for that hostname match the script URL? | ||
for (let i = 0; i < tokens.length; i++) { | ||
const token = tokens[i], | ||
qs_start = script_url.indexOf('?'); | ||
|
||
let match = false; | ||
|
||
if (qs_start == -1) { | ||
if (script_url.endsWith(token)) { | ||
match = true; | ||
} | ||
} else { | ||
if (script_url.endsWith(token, qs_start)) { | ||
match = true; | ||
} | ||
} | ||
|
||
if (match) { | ||
// there is a match, return the surrogate code | ||
return 'data:application/javascript;base64,' + btoa(db.surrogates[token]); | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
const exports = { | ||
getSurrogateURI: getSurrogateURI, | ||
}; | ||
|
||
return exports; | ||
})(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This data structure should be better commented.