Skip to content
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

Change tab URL calculation for contentblockerrules.js and surrogates.js #1021

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,37 @@
// "use strict";

(function () {
const topLevelUrl = getTopLevelURL()


/**
* Best guess effort of the tabs hostname. Cribbed from getTabHostname in: https://github.com/duckduckgo/content-scope-scripts/
* @returns {string|null} inferred tab hostname
*/
function getTabURL () {
let framingOrigin = null
try {
// @ts-expect-error - globalThis.top is possibly 'null' here
framingOrigin = globalThis.top.location.href
} catch {
framingOrigin = globalThis.document.referrer
}

// Not supported in Firefox
if ('ancestorOrigins' in globalThis.location && globalThis.location.ancestorOrigins.length) {
// ancestorOrigins is reverse order, with the last item being the top frame
framingOrigin = globalThis.location.ancestorOrigins.item(globalThis.location.ancestorOrigins.length - 1)
}

try {
// @ts-expect-error - framingOrigin is possibly 'null' here
framingOrigin = new URL(framingOrigin)
} catch {
framingOrigin = null
}
return framingOrigin
}

const topLevelUrl = getTabURL()

let unprotectedDomain = false
const domainParts = topLevelUrl && topLevelUrl.host ? topLevelUrl.host.split('.') : []
Expand Down Expand Up @@ -133,21 +163,6 @@
return false
}

// private
function getTopLevelURL () {
try {
// FROM: https://stackoverflow.com/a/7739035/73479
// FIX: Better capturing of top level URL so that trackers in embedded documents are not considered first party
if (window.location !== window.parent.location) {
return new URL(window.location.href !== 'about:blank' ? document.referrer : window.parent.location.href)
} else {
return new URL(document.location.href)
}
} catch (error) {
return new URL(location.href)
}
}

if (!window.__firefox__) {
Object.defineProperty(window, '__firefox__', {
enumerable: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,35 @@
}
])

const topLevelUrl = getTopLevelURL()
/**
* Best guess effort of the tabs hostname. Cribbed from getTabHostname in: https://github.com/duckduckgo/content-scope-scripts/
* @returns {string|null} inferred tab hostname
*/
function getTabURL () {
let framingOrigin = null
try {
// @ts-expect-error - globalThis.top is possibly 'null' here
framingOrigin = globalThis.top.location.href
} catch {
framingOrigin = globalThis.document.referrer
}

// Not supported in Firefox
if ('ancestorOrigins' in globalThis.location && globalThis.location.ancestorOrigins.length) {
// ancestorOrigins is reverse order, with the last item being the top frame
framingOrigin = globalThis.location.ancestorOrigins.item(globalThis.location.ancestorOrigins.length - 1)
}

try {
// @ts-expect-error - framingOrigin is possibly 'null' here
framingOrigin = new URL(framingOrigin)
} catch {
framingOrigin = null
}
return framingOrigin
}

const topLevelUrl = getTabURL()

let unprotectedDomain = false
const domainParts = topLevelUrl && topLevelUrl.host ? topLevelUrl.host.split('.') : []
Expand Down Expand Up @@ -466,17 +494,6 @@
return false
}

// private
function getTopLevelURL () {
try {
// FROM: https://stackoverflow.com/a/7739035/73479
// FIX: Better capturing of top level URL so that trackers in embedded documents are not considered first party
return new URL(window.location !== window.parent.location ? document.referrer : document.location.href)
} catch (error) {
return new URL(location.href)
}
}

// private
function loadSurrogate (surrogatePattern) {
trackers.surrogateList[surrogatePattern]()
Expand Down
Loading