Skip to content

Commit

Permalink
Merge pull request #3039
Browse files Browse the repository at this point in the history
Prefer MessageSender.origin for getting message sender's frame host
  • Loading branch information
ghostwords committed Dec 19, 2024
2 parents e9bb64d + c3a2bf2 commit 4bf849b
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 203 deletions.
21 changes: 10 additions & 11 deletions src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,7 @@ Badger.prototype = {

// block the domains
for (let domain of domains) {
self.heuristicBlocking.blocklistOrigin(
getBaseDomain(domain), domain);
self.heuristicBlocking.blocklistDomain(getBaseDomain(domain), domain);
}
},

Expand All @@ -539,24 +538,24 @@ Badger.prototype = {
*/
blockPanopticlickDomains() {
for (let domain of constants.PANOPTICLICK_DOMAINS) {
this.heuristicBlocking.blocklistOrigin(domain, domain);
this.heuristicBlocking.blocklistDomain(domain, domain);
}
},

/**
* Saves a user preference for an origin, overriding the default setting.
* Saves a user preference for a domain, overriding the default setting.
*
* @param {String} userAction enum of block, cookieblock, noaction
* @param {String} origin the third party origin to take action on
* @param {String} domain the third party domain to take action on
*/
saveAction: function(userAction, origin) {
var allUserActions = {
saveAction: function(userAction, domain) {
let allUserActions = {
block: constants.USER_BLOCK,
cookieblock: constants.USER_COOKIEBLOCK,
allow: constants.USER_ALLOW
};
this.storage.setupUserAction(origin, allUserActions[userAction]);
log("Finished saving action " + userAction + " for " + origin);
this.storage.setupUserAction(domain, allUserActions[userAction]);
log(`Finished saving action ${userAction} for ${domain}`);
},

initializeCnames: function () {
Expand Down Expand Up @@ -1128,10 +1127,10 @@ Badger.prototype = {
* and if necessary updates the badge.
*
* @param {Integer} tab_id the tab we are on
* @param {String} fqdn the third party origin to add
* @param {String} fqdn the third party domain to add
* @param {String} action the action we are taking
*/
logThirdPartyOriginOnTab: function (tab_id, fqdn, action) {
logThirdParty: function (tab_id, fqdn, action) {
let self = this,
is_blocked = (
action == constants.BLOCK ||
Expand Down
2 changes: 1 addition & 1 deletion src/js/contentscripts/clobbercookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if (window.top == window) {

// TODO race condition; fix waiting on https://crbug.com/478183
chrome.runtime.sendMessage({
type: "checkLocation",
type: "checkClobberingEnabled",
frameUrl: window.FRAME_URL
}, function (blocked) {
if (blocked) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/contentscripts/clobberlocalstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if (window.top == window) {

// TODO race condition; fix waiting on https://crbug.com/478183
chrome.runtime.sendMessage({
type: "checkLocation",
type: "checkClobberingEnabled",
frameUrl: window.FRAME_URL
}, function (blocked) {
if (blocked) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/contentscripts/supercookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function getPageScript(event_id) {
// TODO sometimes contentscripts/utils.js isn't here?!
// TODO window.FRAME_URL / window.injectScript are undefined ...
chrome.runtime.sendMessage({
type: "inspectLocalStorage",
type: "detectSupercookies",
frameUrl: window.FRAME_URL
}, function (enabledAndThirdParty) {
if (!enabledAndThirdParty) {
Expand Down
29 changes: 15 additions & 14 deletions src/js/heuristicblocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ function HeuristicBlocker(pbStorage) {
self.storage = pbStorage;

// TODO roll into tabData? -- 6/10/2019 not for now, since tabData is populated
// by the synchronous listeners in webrequests.js and tabOrigins is used by the
// by the synchronous listeners in webrequests.js and tabBases is used by the
// async listeners here; there's no way to enforce ordering of requests among
// those two. Also, tabData is cleaned up every time a tab is closed, so
// dangling requests that don't trigger listeners until after the tab closes are
// impossible to attribute to a tab.
self.tabOrigins = {};
self.tabBases = {};
self.tabUrls = {};

// initialize tab bases and URLs for already-open tabs
Expand All @@ -46,7 +46,7 @@ function HeuristicBlocker(pbStorage) {
if (utils.isRestrictedUrl(tab.url)) {
continue;
}
self.tabOrigins[tab.id] = getBaseDomain((new URI(tab.url)).host);
self.tabBases[tab.id] = getBaseDomain((new URI(tab.url)).host);
self.tabUrls[tab.id] = tab.url;
}
});
Expand All @@ -55,16 +55,17 @@ function HeuristicBlocker(pbStorage) {
HeuristicBlocker.prototype = {

/**
* Blocklists a domain.
* Blocklists a domain:
*
* - Blocks or cookieblocks an FQDN.
* - Blocks or cookieblocks its base domain (eTLD+1).
* - Cookieblocks any yellowlisted subdomains that share the base domain with the FQDN.
* - Blocks or cookieblocks the given domain.
* - Blocks or cookieblocks its eTLD+1 ("base" domain).
* - Cookieblocks any yellowlisted subdomains that
* share the base domain with the given domain.
*
* @param {String} base The base domain (eTLD+1) to blocklist
* @param {String} fqdn The FQDN
* @param {String} fqdn The domain to blocklist
*/
blocklistOrigin: function (base, fqdn) {
blocklistDomain: function (base, fqdn) {
let self = this,
ylistStorage = self.storage.getStore("cookieblock_list");

Expand Down Expand Up @@ -132,12 +133,12 @@ HeuristicBlocker.prototype = {
// if this is a main window request, update tab data and quit
if (details.type == "main_frame") {
let tab_host = (new URI(details.url)).host;
self.tabOrigins[tab_id] = getBaseDomain(tab_host);
self.tabBases[tab_id] = getBaseDomain(tab_host);
self.tabUrls[tab_id] = details.url;
return;
}

let tab_base = self.tabOrigins[tab_id];
let tab_base = self.tabBases[tab_id];
if (!tab_base) {
return;
}
Expand Down Expand Up @@ -179,7 +180,7 @@ HeuristicBlocker.prototype = {
badger.storage.recordTrackingDetails(request_base, tab_base, 'beacon');
// log in popup
if (from_current_tab) {
badger.logThirdPartyOriginOnTab(
badger.logThirdParty(
tab_id, request_host, badger.storage.getBestAction(request_host));
}
// don't bother checking for tracking cookies
Expand Down Expand Up @@ -209,7 +210,7 @@ HeuristicBlocker.prototype = {
}

let self = this,
tab_base = self.tabOrigins[details.tabId];
tab_base = self.tabBases[details.tabId];
if (!tab_base) {
return;
}
Expand Down Expand Up @@ -438,7 +439,7 @@ HeuristicBlocker.prototype = {
// (cookie)block if domain was seen tracking on enough first party domains
if (firstParties.length >=
self.storage.getStore('private_storage').getItem('blockThreshold')) {
self.blocklistOrigin(tracker_base, tracker_fqdn);
self.blocklistDomain(tracker_base, tracker_fqdn);
}
}
};
Expand Down
76 changes: 38 additions & 38 deletions src/js/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,21 @@ function loadOptions() {
$("#tracking-domains-show-not-yet-blocked").on("change", filterTrackingDomains);
$("#tracking-domains-hide-in-seed").on("change", filterTrackingDomains);

// Add event listeners for origins container.
// Add event listeners for domain toggles container.
$('#blockedResourcesContainer').on('change', 'input:radio', function () {
let $radio = $(this),
$clicker = $radio.parents('.clicker').first(),
origin = $clicker.data('origin'),
domain = $clicker.data('origin'),
action = $radio.val();

// update domain slider row tooltip/status indicators
updateOrigin(origin, action, true);
updateOrigin(domain, action, true);

// persist the change
saveToggle(origin, action);
saveToggle(domain, action);
});
$('#blockedResourcesContainer').on('click', '.userset .honeybadgerPowered', revertDomainControl);
$('#blockedResourcesContainer').on('click', '.removeOrigin', removeOrigin);
$('#blockedResourcesContainer').on('click', '.removeOrigin', removeDomain);
$('#blockedResourcesInner').on('scroll', function () {
activateDomainListTooltips();
});
Expand Down Expand Up @@ -510,9 +510,9 @@ function updateCheckingDNTPolicy() {
type: "getOptionsData",
}, (response) => {
// update DNT-compliant domains
updateSliders(response.origins);
updateSliders(response.trackers);
// update cached domain data
OPTIONS_DATA.origins = response.origins;
OPTIONS_DATA.trackers = response.trackers;
// update count of blocked domains
updateSummary();
// toggle the "dnt" filter
Expand Down Expand Up @@ -609,26 +609,26 @@ function removeWidgetSiteExceptions(event) {
// Tracking Domains slider functions

/**
* Gets action for given origin.
* @param {String} origin - Origin to get action for.
* Gets action for given domain.
* @param {String} domain - Domain to get action for.
*/
function getOriginAction(origin) {
return OPTIONS_DATA.origins[origin];
function getOriginAction(domain) {
return OPTIONS_DATA.trackers[domain];
}

function revertDomainControl(event) {
event.preventDefault();

let origin = $(event.target).parent().data('origin');
let domain = $(event.target).parent().data('origin');

chrome.runtime.sendMessage({
type: "revertDomainControl",
origin
domain
}, (response) => {
// update any sliders that changed as a result
updateSliders(response.origins);
updateSliders(response.trackers);
// update cached domain data
OPTIONS_DATA.origins = response.origins;
OPTIONS_DATA.trackers = response.trackers;
});
}

Expand All @@ -637,7 +637,7 @@ function revertDomainControl(event) {
*/
function updateSummary() {
// if there are no tracking domains
let allTrackingDomains = Object.keys(OPTIONS_DATA.origins);
let allTrackingDomains = Object.keys(OPTIONS_DATA.trackers);
if (!allTrackingDomains || !allTrackingDomains.length) {
// hide the number of trackers message
$("#options_domain_list_trackers").hide();
Expand All @@ -658,7 +658,7 @@ function updateSummary() {

// count unique (cookie)blocked tracking base domains
let blockedBases = new Set(
filterDomains(OPTIONS_DATA.origins, { typeFilter: '-dnt' })
filterDomains(OPTIONS_DATA.trackers, { typeFilter: '-dnt' })
.map(d => getBaseDomain(d)));
$("#options_domain_list_trackers").html(i18n.getMessage(
"options_domain_list_trackers", [
Expand Down Expand Up @@ -780,7 +780,7 @@ let filterTrackingDomains = (function () {

_maybeFetchSeed(!hide_in_seed, function () {
renderTrackingDomains(
filterDomains(OPTIONS_DATA.origins, {
filterDomains(OPTIONS_DATA.trackers, {
searchFilter: $searchFilter.val().toLowerCase(),
typeFilter: $typeFilter.val(),
statusFilter: $statusFilter.val(),
Expand Down Expand Up @@ -948,8 +948,8 @@ function updatePrivacyOverride(setting_name, setting_value) {
* Updates domain tooltip, slider color.
* Also toggles status indicators like breakage warnings.
*/
function updateOrigin(origin, action, userset) {
let $clicker = $('#blockedResourcesInner div.clicker[data-origin="' + origin + '"]'),
function updateOrigin(domain, action, userset) {
let $clicker = $('#blockedResourcesInner div.clicker[data-origin="' + domain + '"]'),
$switchContainer = $clicker.find('.switch-container').first();

// update slider color via CSS
Expand All @@ -976,7 +976,7 @@ function updateOrigin(origin, action, userset) {

let show_breakage_warning = (
action == constants.BLOCK &&
utils.hasOwn(OPTIONS_DATA.cookieblocked, origin)
utils.hasOwn(OPTIONS_DATA.cookieblocked, domain)
);

htmlUtils.toggleBlockedStatus($clicker, userset, show_breakage_warning);
Expand All @@ -988,13 +988,13 @@ function updateOrigin(origin, action, userset) {
* For example, moving the slider for example.com should move the sliders
* for www.example.com and cdn.example.com
*/
function updateSliders(updatedOriginData) {
let updated_domains = Object.keys(updatedOriginData);
function updateSliders(updatedTrackerData) {
let updated_domains = Object.keys(updatedTrackerData);

// update any sliders that changed
for (let domain of updated_domains) {
let action = updatedOriginData[domain];
if (action == OPTIONS_DATA.origins[domain]) {
let action = updatedTrackerData[domain];
if (action == OPTIONS_DATA.trackers[domain]) {
continue;
}

Expand All @@ -1016,7 +1016,7 @@ function updateSliders(updatedOriginData) {
}

// remove sliders that are no longer present
let removed = Object.keys(OPTIONS_DATA.origins).filter(
let removed = Object.keys(OPTIONS_DATA.trackers).filter(
x => !updated_domains.includes(x));
for (let domain of removed) {
let $clicker = $('#blockedResourcesInner div.clicker[data-origin="' + domain + '"]');
Expand All @@ -1027,45 +1027,45 @@ function updateSliders(updatedOriginData) {
/**
* Save the user setting for a domain by messaging the background page.
*/
function saveToggle(origin, action) {
function saveToggle(domain, action) {
chrome.runtime.sendMessage({
type: "saveOptionsToggle",
origin,
domain,
action
}, (response) => {
// first update the cache for the slider
// that was just changed by the user
// to avoid redundantly updating it below
OPTIONS_DATA.origins[origin] = response.origins[origin];
OPTIONS_DATA.trackers[domain] = response.trackers[domain];
// update any sliders that changed as a result
updateSliders(response.origins);
updateSliders(response.trackers);
// update cached domain data
OPTIONS_DATA.origins = response.origins;
OPTIONS_DATA.trackers = response.trackers;
});
}

/**
* Remove origin from Privacy Badger.
* Remove domain from Privacy Badger.
* @param {Event} event Click event triggered by user.
*/
function removeOrigin(event) {
function removeDomain(event) {
event.preventDefault();

// confirm removal before proceeding
if (!confirm(i18n.getMessage("options_remove_origin_confirm"))) {
return;
}

let origin = $(event.target).parent().data('origin');
let domain = $(event.target).parent().data('origin');

chrome.runtime.sendMessage({
type: "removeOrigin",
origin
type: "removeDomain",
domain
}, (response) => {
// remove rows that are no longer here
updateSliders(response.origins);
updateSliders(response.trackers);
// update cached domain data
OPTIONS_DATA.origins = response.origins;
OPTIONS_DATA.trackers = response.trackers;
// if we removed domains, the summary text may have changed
updateSummary();
// and we probably now have new visible rows in the tracking domains list
Expand Down
Loading

0 comments on commit 4bf849b

Please sign in to comment.