Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit 4b5e53f

Browse files
Change tab URL calculation for contentblockerrules.js and surrogates.js (#1021)
<!-- Note: This checklist is a reminder of our shared engineering expectations. --> Please review the release process for BrowserServicesKit [here](https://app.asana.com/0/1200194497630846/1200837094583426). **Required**: Task/Issue URL: #1019 and https://app.asana.com/0/1200437802575119/1208550540369943/f iOS PR: TODO macOS PR: TODO What kind of version bump will this require?: Minor **Optional**: Tech Design URL: CC: **Description**: This PR changes the tab calculation for the page, currently it doesn't account for grand child frames at all. This code has mostly been cribbed from: https://github.com/duckduckgo/content-scope-scripts/blob/655d12d9e77f93cc36875142aa2ff3e06c52608d/injected/src/utils.js#L135-L157 Full disclosure: I tested this manually in the console rather than doing a full build, we should verify before releasing. Thanks to @alisha for spotting this problem in #1019, producing a well written explanation and PR! **Steps to test this PR**: For testing the blocking follow the steps in: #1019 using the console. For surrogates the same should work for a tracker on the surrogate list: - replacing https://rtb.openx.net/openrtbb/prebidjs with https://google-analytics.com/gtm/js - validating that frame has the script executed: cframe.contentWindow.ga should be defined <!-- Before submitting a PR, please ensure you have tested the combinations you expect the reviewer to test, then delete configurations you *know* do not need explicit testing. Using a simulator where a physical device is unavailable is acceptable. --> **OS Testing**: * [ ] iOS 14 * [ ] iOS 15 * [ ] iOS 16 * [ ] macOS 10.15 * [ ] macOS 11 * [ ] macOS 12 --- ###### Internal references: [Software Engineering Expectations](https://app.asana.com/0/59792373528535/199064865822552) [Technical Design Template](https://app.asana.com/0/59792373528535/184709971311943)
1 parent ffcbeb2 commit 4b5e53f

File tree

2 files changed

+60
-28
lines changed

2 files changed

+60
-28
lines changed

Sources/BrowserServicesKit/ContentBlocking/UserScripts/contentblockerrules.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,37 @@
1919
// "use strict";
2020

2121
(function () {
22-
const topLevelUrl = getTopLevelURL()
22+
23+
24+
/**
25+
* Best guess effort of the tabs hostname. Cribbed from getTabHostname in: https://github.com/duckduckgo/content-scope-scripts/
26+
* @returns {string|null} inferred tab hostname
27+
*/
28+
function getTabURL () {
29+
let framingOrigin = null
30+
try {
31+
// @ts-expect-error - globalThis.top is possibly 'null' here
32+
framingOrigin = globalThis.top.location.href
33+
} catch {
34+
framingOrigin = globalThis.document.referrer
35+
}
36+
37+
// Not supported in Firefox
38+
if ('ancestorOrigins' in globalThis.location && globalThis.location.ancestorOrigins.length) {
39+
// ancestorOrigins is reverse order, with the last item being the top frame
40+
framingOrigin = globalThis.location.ancestorOrigins.item(globalThis.location.ancestorOrigins.length - 1)
41+
}
42+
43+
try {
44+
// @ts-expect-error - framingOrigin is possibly 'null' here
45+
framingOrigin = new URL(framingOrigin)
46+
} catch {
47+
framingOrigin = null
48+
}
49+
return framingOrigin
50+
}
51+
52+
const topLevelUrl = getTabURL()
2353

2454
let unprotectedDomain = false
2555
const domainParts = topLevelUrl && topLevelUrl.host ? topLevelUrl.host.split('.') : []
@@ -133,21 +163,6 @@
133163
return false
134164
}
135165

136-
// private
137-
function getTopLevelURL () {
138-
try {
139-
// FROM: https://stackoverflow.com/a/7739035/73479
140-
// FIX: Better capturing of top level URL so that trackers in embedded documents are not considered first party
141-
if (window.location !== window.parent.location) {
142-
return new URL(window.location.href !== 'about:blank' ? document.referrer : window.parent.location.href)
143-
} else {
144-
return new URL(document.location.href)
145-
}
146-
} catch (error) {
147-
return new URL(location.href)
148-
}
149-
}
150-
151166
if (!window.__firefox__) {
152167
Object.defineProperty(window, '__firefox__', {
153168
enumerable: false,

Sources/BrowserServicesKit/ContentBlocking/UserScripts/surrogates.js

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,35 @@
377377
}
378378
])
379379

380-
const topLevelUrl = getTopLevelURL()
380+
/**
381+
* Best guess effort of the tabs hostname. Cribbed from getTabHostname in: https://github.com/duckduckgo/content-scope-scripts/
382+
* @returns {string|null} inferred tab hostname
383+
*/
384+
function getTabURL () {
385+
let framingOrigin = null
386+
try {
387+
// @ts-expect-error - globalThis.top is possibly 'null' here
388+
framingOrigin = globalThis.top.location.href
389+
} catch {
390+
framingOrigin = globalThis.document.referrer
391+
}
392+
393+
// Not supported in Firefox
394+
if ('ancestorOrigins' in globalThis.location && globalThis.location.ancestorOrigins.length) {
395+
// ancestorOrigins is reverse order, with the last item being the top frame
396+
framingOrigin = globalThis.location.ancestorOrigins.item(globalThis.location.ancestorOrigins.length - 1)
397+
}
398+
399+
try {
400+
// @ts-expect-error - framingOrigin is possibly 'null' here
401+
framingOrigin = new URL(framingOrigin)
402+
} catch {
403+
framingOrigin = null
404+
}
405+
return framingOrigin
406+
}
407+
408+
const topLevelUrl = getTabURL()
381409

382410
let unprotectedDomain = false
383411
const domainParts = topLevelUrl && topLevelUrl.host ? topLevelUrl.host.split('.') : []
@@ -466,17 +494,6 @@
466494
return false
467495
}
468496

469-
// private
470-
function getTopLevelURL () {
471-
try {
472-
// FROM: https://stackoverflow.com/a/7739035/73479
473-
// FIX: Better capturing of top level URL so that trackers in embedded documents are not considered first party
474-
return new URL(window.location !== window.parent.location ? document.referrer : document.location.href)
475-
} catch (error) {
476-
return new URL(location.href)
477-
}
478-
}
479-
480497
// private
481498
function loadSurrogate (surrogatePattern) {
482499
trackers.surrogateList[surrogatePattern]()

0 commit comments

Comments
 (0)