Ensure FLoC is opt-in by default

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/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)
This commit is contained in:
Raymond Hill 2021-04-11 09:36:56 -04:00
parent 5a48917b80
commit bfdc81e9e4
No known key found for this signature in database
GPG key ID: 25E1490B761470C2
4 changed files with 68 additions and 6 deletions

View file

@ -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, {
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();
}
});
}
);
})();

View file

@ -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 = {

View file

@ -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),

View file

@ -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.
@ -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,
@ -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