Skip to content

Commit bfdc81e

Browse files
committed
Ensure FLoC is opt-in by default
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)
1 parent 5a48917 commit bfdc81e

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

assets/resources/scriptlets.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -668,12 +668,18 @@
668668
/// no-floc.js
669669
// https://github.com/uBlockOrigin/uBlock-issues/issues/1553
670670
(function() {
671-
if ( document.interestCohort instanceof Function === false ) { return; }
672-
document.interestCohort = new Proxy(document.interestCohort, {
673-
apply: function() {
674-
return Promise.reject();
671+
if ( Document instanceof Object === false ) { return; }
672+
if ( Document.prototype.interestCohort instanceof Function === false ) {
673+
return;
674+
}
675+
Document.prototype.interestCohort = new Proxy(
676+
Document.prototype.interestCohort,
677+
{
678+
apply: function() {
679+
return Promise.reject();
680+
}
675681
}
676-
});
682+
);
677683
})();
678684

679685

src/js/scriptlet-filtering.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,10 @@
382382
return out.join('\n');
383383
};
384384

385+
api.hasScriptlet = function(hostname, exceptionBit, scriptlet) {
386+
return scriptletDB.hasStr(hostname, exceptionBit, scriptlet);
387+
};
388+
385389
api.injectNow = function(details) {
386390
if ( typeof details.frameId !== 'number' ) { return; }
387391
const request = {

src/js/static-ext-filtering.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,35 @@
173173
}
174174
}
175175

176+
hasStr(hostname, exceptionBit, value) {
177+
let found = false;
178+
for (;;) {
179+
let iHn = this.hostnameToSlotIdMap.get(hostname);
180+
if ( iHn !== undefined ) {
181+
do {
182+
const strId = this.hostnameSlots[iHn+0];
183+
if ( this.strSlots[strId >>> this.nBits] === value ) {
184+
if ( (strId & exceptionBit) !== 0 ) {
185+
return false;
186+
}
187+
found = true;
188+
}
189+
iHn = this.hostnameSlots[iHn+1];
190+
} while ( iHn !== 0 );
191+
}
192+
if ( hostname === '' ) { break; }
193+
const pos = hostname.indexOf('.');
194+
if ( pos !== -1 ) {
195+
hostname = hostname.slice(pos + 1);
196+
} else if ( hostname !== '*' ) {
197+
hostname = '*';
198+
} else {
199+
hostname = '';
200+
}
201+
}
202+
return found;
203+
}
204+
176205
toSelfie() {
177206
return {
178207
hostnameToSlotIdMap: Array.from(this.hostnameToSlotIdMap),

src/js/traffic.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ window.addEventListener('webextFlavor', function() {
5353
vAPI.webextFlavor.major < 59;
5454
}, { once: true });
5555

56+
// https://github.com/uBlockOrigin/uBlock-issues/issues/1553
57+
const supportsFloc = document.interestCohort instanceof Function;
58+
5659
/******************************************************************************/
5760

5861
// Intercept and filter web requests.
@@ -540,7 +543,7 @@ const onHeadersReceived = function(details) {
540543
}
541544

542545
// At this point we have a HTML document.
543-
546+
544547
const filteredHTML =
545548
µb.canFilterResponseData && filterDocument(fctxt, details) === true;
546549

@@ -551,6 +554,9 @@ const onHeadersReceived = function(details) {
551554
if ( injectCSP(fctxt, pageStore, responseHeaders) === true ) {
552555
modifiedHeaders = true;
553556
}
557+
if ( supportsFloc && foilFloc(fctxt, responseHeaders) ) {
558+
modifiedHeaders = true;
559+
}
554560

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

10131019
/******************************************************************************/
10141020

1021+
// https://github.com/uBlockOrigin/uBlock-issues/issues/1553
1022+
// https://github.com/WICG/floc#opting-out-of-computation
1023+
1024+
const foilFloc = function(fctxt, responseHeaders) {
1025+
const hn = fctxt.getHostname();
1026+
if ( µBlock.scriptletFilteringEngine.hasScriptlet(hn, 1, 'no-floc') === false ) {
1027+
return false;
1028+
}
1029+
responseHeaders.push({
1030+
name: 'Permissions-Policy',
1031+
value: 'interest-cohort=()' }
1032+
);
1033+
return true;
1034+
};
1035+
1036+
/******************************************************************************/
1037+
10151038
// https://github.com/gorhill/uBlock/issues/1163
10161039
// "Block elements by size".
10171040
// https://github.com/gorhill/uBlock/issues/1390#issuecomment-187310719

0 commit comments

Comments
 (0)