Skip to content

Commit

Permalink
Fix script-overload testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanKingston committed Sep 7, 2023
1 parent 3823ad6 commit c45f6fa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
9 changes: 7 additions & 2 deletions packages/safe-globals/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ if (globalThis && 'document' in globalThis &&

let dummyContentWindow = dummyWindow?.contentWindow
if (hasMozProxies) {
// Purposefully prevent Firefox using the iframe as we can't hold stale references to iframes there.

Check failure on line 20 in packages/safe-globals/scope.js

View workflow job for this annotation

GitHub Actions / unit (ubuntu-20.04)

Irregular whitespace not allowed
// dummyContentWindow = undefined
// @ts-expect-error - mozProxies is not defined on window
dummyContentWindow = dummyContentWindow.wrappedJSObject
dummyContentWindow = dummyContentWindow?.wrappedJSObject
}
// @ts-expect-error - Symbol is not defined on window
const dummySymbol = dummyContentWindow?.Symbol
Expand All @@ -31,7 +33,7 @@ const iteratorSymbol = dummySymbol?.iterator
* @returns {globalThis[T]}
*/
export function captureGlobal (globalName) {
const global = dummyWindow?.contentWindow?.[globalName]
const global = dummyContentWindow?.[globalName]

// if we were unable to create a dummy window, return the global
// this still has the advantage of preventing aliasing of the global through shawdowing
Expand All @@ -50,6 +52,9 @@ export function captureGlobal (globalName) {
}

export function cleanup () {
// We can't remove the iframe in firefox as we get dead object issues.
if (import.meta.injectName === 'firefox') return

// Clean up the dummy window
dummyWindow?.remove()
}
5 changes: 4 additions & 1 deletion src/features/runtime-checks/script-overload.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,16 @@ export function wrapScriptCodeOverload (code, config) {
// Ensure globalThis === window
const globalThis = window
`
// Hack to use default capture instead of rollups replaced variable names.
// This is covered by testing so should break if rollup is changed.
const proxyString = constructProxy.toString().replaceAll('Object$1', 'Object').replaceAll('Reflect$1', 'Reflect')
return removeIndent(`(function (parentScope) {
/**
* DuckDuckGo Runtime Checks injected code.
* If you're reading this, you're probably trying to debug a site that is breaking due to our runtime checks.
* Please raise an issues on our GitHub repo: https://github.com/duckduckgo/content-scope-scripts/
*/
${constructProxy.toString()}
${proxyString}
${prepend}
${getContextId.toString()}
Expand Down
10 changes: 5 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,16 @@ export function hasThirdPartyOrigin (scriptOrigins) {
export function getTabHostname () {
let framingOrigin = null
try {
// @ts-expect-error - globalThis.top is possibly 'null' here
framingOrigin = globalThis.top.location.href
// @ts-expect-error - globalObj.top is possibly 'null' here
framingOrigin = globalObj.top.location.href
} catch {
framingOrigin = globalThis.document.referrer
framingOrigin = globalObj.document.referrer
}

// Not supported in Firefox
if ('ancestorOrigins' in globalThis.location && globalThis.location.ancestorOrigins.length) {
if ('ancestorOrigins' in globalObj.location && globalObj.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)
framingOrigin = globalObj.location.ancestorOrigins.item(globalObj.location.ancestorOrigins.length - 1)
}

try {
Expand Down

0 comments on commit c45f6fa

Please sign in to comment.