Skip to content

Commit 85bfd6b

Browse files
committed
Create in-memory cache to prevent duplicate reports
1 parent 9d537a6 commit 85bfd6b

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

src/js/background.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ Badger.prototype = {
10041004
},
10051005

10061006
/**
1007-
* Checks if local storage ( in dict) has any high-entropy keys
1007+
* Checks if local storage (in dict) has any high-entropy keys
10081008
*
10091009
* @param {Object} lsItems Local storage dict
10101010
* @returns {boolean} true if it seems there are supercookies

src/js/constants.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ var exports = {
4242

4343
// The max amount of time (in milliseconds) that PB will wait before sharing a
4444
// tracking action with EFF for community learning
45-
MAX_CL_WAIT_TIME: 0, //30 * 60 * 1000, // one half hour
45+
MAX_CL_WAIT_TIME: 5 * 60 * 1000, // five minutes
4646

4747
// The probability that any given tracking action will be logged to the
4848
// community server, as a float from 0.0 to 1.0
4949
CL_PROBABILITY: 1.0,
5050

51+
// size of the in-memory community learning cache
52+
CL_CACHE_SIZE: 5000,
53+
5154
DNT_POLICY_CHECK_INTERVAL: 1000, // one second
5255
};
5356

src/js/heuristicblocking.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ function HeuristicBlocker(pbStorage) {
3737
// impossible to attribute to a tab.
3838
this.tabOrigins = {};
3939
this.tabUrls = {};
40+
41+
// in-memory cache for community learning
42+
this.previouslySharedTrackers = new Set();
4043
}
4144

4245
HeuristicBlocker.prototype = {
@@ -356,6 +359,29 @@ HeuristicBlocker.prototype = {
356359
shareTrackerInfo: function(page_host, tracker_host, tracker_type) {
357360
// Share a random sample of trackers we observe
358361
if (Math.random() < constants.CL_PROBABILITY) {
362+
// check if we've shared this tracker recently
363+
// note that this check comes after checking against the snitch map
364+
let tr_str = page_host + '+' + tracker_host + '+' + tracker_type;
365+
if (this.previouslySharedTrackers.has(tr_str)) {
366+
return;
367+
}
368+
369+
// add this entry to the cache
370+
this.previouslySharedTrackers.add(tr_str);
371+
372+
// if the cache gets too big, cut it in half
373+
if (this.previouslySharedTrackers.size > constants.CL_CACHE_SIZE) {
374+
this.previouslySharedTrackers = new Set(
375+
// An array created from the set will have all of its entries ordered
376+
// by when they were added
377+
Array.from(this.previouslySharedTrackers).slice(
378+
// keep the most recent half of the cache entries
379+
Math.floor(constants.CL_CACHE_SIZE / 2)
380+
)
381+
);
382+
}
383+
384+
// now make the request to the database server
359385
setTimeout(function() {
360386
fetch("http://localhost:8080", {
361387
method: "POST",

0 commit comments

Comments
 (0)