Skip to content

Commit 90ca133

Browse files
author
Jono Xia
committed
Bugs 8 and 28 - added first pass at a site blocking/whitelisting feature, still experimental.
1 parent 4f875e0 commit 90ca133

File tree

5 files changed

+103
-9
lines changed

5 files changed

+103
-9
lines changed

data/index-content-script.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ unsafeWindow.getSavedGraph = function getSavedGraph() {
2525
self.port.emit('getSavedGraph');
2626
};
2727

28+
unsafeWindow.blockDomain = function blockDomain(domain) {
29+
self.port.emit('blockDomain', {"domain": domain});
30+
};
31+
32+
unsafeWindow.whitelistDomain = function whitelistDomain(domain) {
33+
self.port.emit('whitelistDomain', {"domain": domain});
34+
};
35+
2836
self.port.on("log", function(log) {
2937
log = JSON.parse(log);
3038
if (graphCallback) {

data/ui/collusion-addon.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ var CollusionAddon = (function() {
77
importGraph: window.importGraph,
88
resetGraph: window.resetGraph,
99
saveGraph: window.saveGraph,
10-
getSavedGraph: window.getSavedGraph
10+
getSavedGraph: window.getSavedGraph,
11+
blockDomain: window.blockDomain,
12+
whitelistDomain: window.whitelistDomain
1113
};
12-
14+
1315
return self;
1416
})();

data/ui/graphrunner.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ var GraphRunner = (function(jQuery, d3) {
7878

7979
$("#domain-infos .info").hide();
8080

81-
// TODO Why do we clone the div instead of just clearing the one and adding to it?
82-
// Oh, I see, we create a clone for each domain and then re-use it if it's already
81+
// Instead of just cleraing out the domain info div and puttig in the new info each time,
82+
// create a clone of the template for each new domain, and re-use that create a clone for each domain and then re-use it if it's already
8383
// created. An optimization?
8484
if (!info.length) {
8585
info = $("#templates .info").clone();
@@ -91,13 +91,22 @@ var GraphRunner = (function(jQuery, d3) {
9191
var trackerId = d.trackerInfo.network_id;
9292
info.find("h2.domain").empty();
9393
img.attr("src", TRACKER_LOGO + trackerId + ".jpg").addClass("tracker");
94-
} else
94+
} else {
9595
img.attr("src", 'http://' + d.name + '/favicon.ico')
9696
.addClass("favicon");
97+
}
9798
setDomainLink(info.find("a.domain"), d);
9899
info.find("h2.domain").prepend(img);
99100
img.error(function() { img.remove(); });
100101
$("#domain-infos").append(info);
102+
103+
// Set up callback functions for the block-this-site link and the whitelist link
104+
info.find(".block-link").click(function() {
105+
CollusionAddon.blockDomain(d.name);
106+
});
107+
info.find(".whitelist-link").click(function() {
108+
CollusionAddon.whitelistDomain(d.name);
109+
});
101110
}
102111

103112
// List referrers, if any (sites that set cookies read by this site)
@@ -276,6 +285,10 @@ var GraphRunner = (function(jQuery, d3) {
276285
.attr("xlink:href", function(d) {return 'http://' + d.name + '/favicon.ico'; } );
277286
}
278287

288+
// Remove nodes if domain is removed from the data (e.g. user blocked it)
289+
node.exit().remove();
290+
// TODO: this doesn't seem to work at all. Debug more.
291+
279292
return node;
280293
}
281294

data/ui/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ <h1>Collusion</h1>
4141
<div id="templates">
4242
<div class="info" style="display: none;">
4343
<h2 class="domain"></h2>
44-
44+
<a class="block-link">Block 3rd Party Cookies For This Site</a> | <a class="whitelist-link">This Site is OK</a>
4545
<div class="referrees">
4646
<p>When you visit <a class="domain"></a>, it informs the following websites about you.</p>
4747
<ul></ul>

lib/main.js

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ var log = {};
3030
// Array to maintain connections to any pages running collusion UI:
3131
var workers = [];
3232

33+
// Whitelist of sites that user has told us they don't care about tracking them:
34+
var whitelist = [];
35+
3336
var startTime = new Date();
3437
var collusionPanel = null;
3538

3639

37-
3840
function attachToCollusionPage(worker) {
3941
/* Set up channel of communcation between this add-on and the script (index-content-script.js)
4042
* that we attached to the web page running the Collusion UI. */
@@ -78,6 +80,12 @@ function attachToCollusionPage(worker) {
7880
startTime = new Date() - maxTime;
7981
log = graph;
8082
});
83+
worker.port.on("blockDomain", function(data) {
84+
blockDomain(data.domain);
85+
});
86+
worker.port.on("whitelistDomain", function(data) {
87+
whitelistDomain(data.domain);
88+
});
8189
}
8290

8391

@@ -138,6 +146,57 @@ function getDomain(host) {
138146
}
139147
}
140148

149+
function blockDomain(domain) {
150+
console.log("Blocking domain " + domain);
151+
deleteNode(domain);
152+
// Clear all current cookies to this domain, and block them so they don't come back
153+
// TODO: are we blocking cookies TO this domain or blocking cookies FROM this domain?
154+
var cookieEnumerator = cookieMgr.getCookiesFromHost();
155+
while (cookieEnumerator.hasMoreElements()) {
156+
var cookie = cookieEnumerator.getNext().QueryInterface(Ci.nsICookie2);
157+
cookieMgr.remove(cookie.host, cookie.name, cookie.path, true);
158+
// The final true argument means cookies from this host are permanently blocked
159+
// TODO does this also block first-party cookies? we don't want that.
160+
// Look into nsICookiePermission...
161+
}
162+
163+
/* See https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsICookieManager2#getCookiesFromHost%28%29
164+
* and see https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsICookieManager
165+
*/
166+
}
167+
168+
function whitelistDomain(domain) {
169+
console.log("Whitelisting domain " + domain);
170+
deleteNode(domain);
171+
whitelist.push(domain); // TODO TEST
172+
storage.whitelist = whitelist;
173+
}
174+
175+
function deleteNode(nodeDomain) {
176+
for (var domain in log) {
177+
if (log[domain].referrers[nodeDomain]) {
178+
console.log("Removed link to " + domain + " from " + nodeDomain);
179+
delete log[domain].referrers[nodeDomain];
180+
}
181+
}
182+
if (log[nodeDomain]) {
183+
delete log[nodeDomain];
184+
console.log("Removed all links to " + nodeDomain);
185+
}
186+
187+
/* TODO it doesn't look like sending the newly-reduced log back to graphrunner.js
188+
* does anything - even though I call node.exit().remove(), I think the way the data is passed
189+
* around over there (it was not expected for nodes to ever leave the graph, seems like) means
190+
* that nothing is removed. However, reloading the page results in a graph without the removed
191+
* node, so that much at least works.
192+
*/
193+
194+
workers.forEach(function(worker) {
195+
worker.port.emit("log", JSON.stringify(log));
196+
});
197+
}
198+
199+
141200
// Main entry point. Will be called when Firefox starts or when Collusion is installed:
142201
function initCollusion() {
143202

@@ -178,7 +237,7 @@ function initCollusion() {
178237
});
179238

180239
// Set up the status bar button to open the main UI page:
181-
require("widget").Widget({
240+
var widget = require("widget").Widget({
182241
id: "collusion",
183242
label: "Display Collusion Diagram",
184243
contentURL: data.url("favicon.ico"),
@@ -192,6 +251,12 @@ function initCollusion() {
192251
log = JSON.parse(storage.graph);
193252
}
194253

254+
// Load any whitelist we have stored from last time
255+
if (storage.whitelist) {
256+
whitelist = JSON.parse(storage.whitelist);
257+
}
258+
259+
195260
// Set up an observer to record third-party cookies. This callback
196261
// right here is the crux of Collusion.
197262
obSvc.add("http-on-examine-response", function(subject, topic, data) {
@@ -207,6 +272,10 @@ function initCollusion() {
207272
if (channel.referrer.host in baseHosts)
208273
return;
209274

275+
// Ignore cookies from whitelisted sites. TODO: Or is that cookies TO whitelisted sites?
276+
if (channel.referrer.host in whitelist)
277+
return;
278+
210279
if (domain != referrerDomain) {
211280
try {
212281
type = subject.getResponseHeader("Content-Type");
@@ -298,5 +367,7 @@ function initCollusion() {
298367
attachToExistingCollusionPages();
299368
}
300369

370+
301371
// Start!
302-
initCollusion();
372+
initCollusion();
373+

0 commit comments

Comments
 (0)