Skip to content

Commit

Permalink
Ensure FLoC is opt-in by default
Browse files Browse the repository at this point in the history
Related issue:
- uBlockOrigin/uBlock-issues#1553

This commit ensures FLoC is opt-in. The generic filter
`*##+js(no-floc)` in "uBlock filters -- Privacy" ensures
the feature is disabled when using default settings/lists.

Users can opt-in to FLoC by adding a generic exception
filter to their custom filters, `#@#+js(no-floc)`; or they
can opt-in only for a specific set of websites through a
more specific exception filter:

    example.com,shopping.example#@#+js(no-floc)
  • Loading branch information
gorhill committed Apr 11, 2021
1 parent 5a48917 commit bfdc81e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
16 changes: 11 additions & 5 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,12 +668,18 @@
/// no-floc.js
// https://github.com/uBlockOrigin/uBlock-issues/issues/1553
(function() {
if ( document.interestCohort instanceof Function === false ) { return; }
document.interestCohort = new Proxy(document.interestCohort, {
apply: function() {
return Promise.reject();
if ( Document instanceof Object === false ) { return; }
if ( Document.prototype.interestCohort instanceof Function === false ) {
return;
}
Document.prototype.interestCohort = new Proxy(
Document.prototype.interestCohort,
{
apply: function() {
return Promise.reject();
}
}
});
);
})();


Expand Down
4 changes: 4 additions & 0 deletions src/js/scriptlet-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,10 @@
return out.join('\n');
};

api.hasScriptlet = function(hostname, exceptionBit, scriptlet) {
return scriptletDB.hasStr(hostname, exceptionBit, scriptlet);
};

api.injectNow = function(details) {
if ( typeof details.frameId !== 'number' ) { return; }
const request = {
Expand Down
29 changes: 29 additions & 0 deletions src/js/static-ext-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,35 @@
}
}

hasStr(hostname, exceptionBit, value) {
let found = false;
for (;;) {
let iHn = this.hostnameToSlotIdMap.get(hostname);
if ( iHn !== undefined ) {
do {
const strId = this.hostnameSlots[iHn+0];
if ( this.strSlots[strId >>> this.nBits] === value ) {
if ( (strId & exceptionBit) !== 0 ) {
return false;
}
found = true;
}
iHn = this.hostnameSlots[iHn+1];
} while ( iHn !== 0 );
}
if ( hostname === '' ) { break; }
const pos = hostname.indexOf('.');
if ( pos !== -1 ) {
hostname = hostname.slice(pos + 1);
} else if ( hostname !== '*' ) {
hostname = '*';
} else {
hostname = '';
}
}
return found;
}

toSelfie() {
return {
hostnameToSlotIdMap: Array.from(this.hostnameToSlotIdMap),
Expand Down
25 changes: 24 additions & 1 deletion src/js/traffic.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ window.addEventListener('webextFlavor', function() {
vAPI.webextFlavor.major < 59;
}, { once: true });

// https://github.com/uBlockOrigin/uBlock-issues/issues/1553
const supportsFloc = document.interestCohort instanceof Function;

/******************************************************************************/

// Intercept and filter web requests.
Expand Down Expand Up @@ -540,7 +543,7 @@ const onHeadersReceived = function(details) {
}

// At this point we have a HTML document.

const filteredHTML =
µb.canFilterResponseData && filterDocument(fctxt, details) === true;

Expand All @@ -551,6 +554,9 @@ const onHeadersReceived = function(details) {
if ( injectCSP(fctxt, pageStore, responseHeaders) === true ) {
modifiedHeaders = true;
}
if ( supportsFloc && foilFloc(fctxt, responseHeaders) ) {
modifiedHeaders = true;
}

// https://bugzilla.mozilla.org/show_bug.cgi?id=1376932
// Prevent document from being cached by the browser if we modified it,
Expand Down Expand Up @@ -1012,6 +1018,23 @@ const injectCSP = function(fctxt, pageStore, responseHeaders) {

/******************************************************************************/

// https://github.com/uBlockOrigin/uBlock-issues/issues/1553
// https://github.com/WICG/floc#opting-out-of-computation

const foilFloc = function(fctxt, responseHeaders) {
const hn = fctxt.getHostname();
if ( µBlock.scriptletFilteringEngine.hasScriptlet(hn, 1, 'no-floc') === false ) {
return false;
}
responseHeaders.push({
name: 'Permissions-Policy',
value: 'interest-cohort=()' }
);
return true;
};

/******************************************************************************/

// https://github.com/gorhill/uBlock/issues/1163
// "Block elements by size".
// https://github.com/gorhill/uBlock/issues/1390#issuecomment-187310719
Expand Down

0 comments on commit bfdc81e

Please sign in to comment.