Skip to content

Disable FLoC #591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions shared/js/background/chrome-events.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@ function blockingExperimentActive () {
// return false
}

// we determine if FLoC is enabled by testing for availability of its JS API
const isFlocEnabled = ('interestCohort' in document)

// Overwrite FLoC JS API
if (isFlocEnabled) {
chrome.webNavigation.onCommitted.addListener(details => {
const tab = tabManager.get({ tabId: details.tabId })
if (tab && tab.site.whitelisted) return

chrome.tabs.executeScript(details.tabId, {
file: 'public/js/content-scripts/floc.js',
allFrames: true,
matchAboutBlank: true,
runAt: 'document_start'
})
})
}

// Shallow copy of request types
// And add beacon type based on browser, so we can block it
chrome.webRequest.onBeforeRequest.addListener(
Expand All @@ -201,26 +219,32 @@ chrome.webRequest.onHeadersReceived.addListener(
return ATB.updateSetAtb(request)
}

let responseHeaders = request.responseHeaders

if (isFlocEnabled && (request.type === 'main_frame' || request.type === 'sub_frame')) {
// there can be multiple permissions-policy headers, so we are good always appending one
responseHeaders.push({ name: 'permissions-policy', value: 'interest-cohort=()' })
}

if (blockingExperimentActive()) {
let responseHeaders = request.responseHeaders
// Strip 3rd party response header
const tab = tabManager.get({ tabId: request.tabId })
if (!request.responseHeaders) return
if (tab && tab.site.whitelisted) return
if (!request.responseHeaders) return { responseHeaders: responseHeaders }
if (tab && tab.site.whitelisted) return { responseHeaders: responseHeaders }
if (!tab) {
const initiator = request.initiator || request.documentUrl
if (utils.isFirstParty(initiator, request.url)) {
return
return { responseHeaders: responseHeaders }
}
} else if (tab && utils.isFirstParty(request.url, tab.url)) {
return
return { responseHeaders: responseHeaders }
}
if (!cookieConfig.isExcluded(request.url) && trackerutils.isTracker(request.url)) {
responseHeaders = responseHeaders.filter(header => header.name.toLowerCase() !== 'set-cookie')
}

return { responseHeaders: responseHeaders }
}

return { responseHeaders: responseHeaders }
},
{
urls: ['<all_urls>']
Expand Down
18 changes: 18 additions & 0 deletions shared/js/content-scripts/floc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(function disableFlocAPI () {
// don't inject into non-HTML documents (such as XML documents)
// but do inject into XHTML documents
if (document instanceof HTMLDocument === false && (
document instanceof XMLDocument === false ||
document.createElement('div') instanceof HTMLDivElement === false
)) {
return
}

if ('interestCohort' in Document.prototype) {
const scriptElement = document.createElement('script')
scriptElement.innerHTML = 'delete Document.prototype.interestCohort'
document.documentElement.prepend(scriptElement)
// remove element immediately so it is not visible to scripts on the page
document.documentElement.removeChild(scriptElement)
}
})()