Skip to content

Add export global module #507

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 2 commits into from
May 31, 2023
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
16 changes: 16 additions & 0 deletions src/captured-globals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Capture prototype to prevent overloading
function captureGlobals (object) {
const newGlobal = object
for (const key in object) {
const value = object[key]
newGlobal[key] = value
}
for (const key in object.prototype) {
const value = object.prototype[key]
newGlobal.prototype[key] = value
}
return newGlobal
}

export const Set = captureGlobals(globalThis.Set)
export const Reflect = captureGlobals(globalThis.Reflect)
3 changes: 2 additions & 1 deletion src/features/runtime-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ContentFeature from '../content-feature.js'
import { DDGProxy, getStackTraceOrigins, getStack, matchHostname, injectGlobalStyles, createStyleElement, postDebugMessage, taintSymbol, hasTaintedMethod } from '../utils.js'
import { wrapScriptCodeOverload } from './runtime-checks/script-overload.js'
import { Reflect } from '../captured-globals.js'

let stackDomains = []
let matchAllStackDomains = false
Expand Down Expand Up @@ -490,7 +491,7 @@ export default class RuntimeChecks extends ContentFeature {
// This shouldn't happen, but if it does we don't want to break the page
try {
// @ts-expect-error TS node return here
customElements.define('ddg-runtime-checks', DDGRuntimeChecks)
globalThis.customElements.define('ddg-runtime-checks', DDGRuntimeChecks)
} catch {}
}

Expand Down
9 changes: 4 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* global cloneInto, exportFunction, mozProxies */
import { Set } from './captured-globals.js'

// Only use globalThis for testing this breaks window.wrappedJSObject code in Firefox
// eslint-disable-next-line no-global-assign
let globalObj = typeof window === 'undefined' ? globalThis : window
let Error = globalObj.Error
let messageSecret
const CapturedSet = globalObj.Set
// Capture prototype to prevent overloading
const createSet = () => hasMozProxies ? new Set() : new CapturedSet()

export const taintSymbol = Symbol('taint')

Expand Down Expand Up @@ -171,7 +170,7 @@ export function matchHostname (hostname, exceptionDomain) {

const lineTest = /(\()?(https?:[^)]+):[0-9]+:[0-9]+(\))?/
export function getStackTraceUrls (stack) {
const urls = createSet()
const urls = new Set()
try {
const errorLines = stack.split('\n')
// Should cater for Chrome and Firefox stacks, we only care about https? resources.
Expand All @@ -189,7 +188,7 @@ export function getStackTraceUrls (stack) {

export function getStackTraceOrigins (stack) {
const urls = getStackTraceUrls(stack)
const origins = createSet()
const origins = new Set()
for (const url of urls) {
origins.add(url.hostname)
}
Expand Down
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
"integration-test",
"inject",
"packages",
"playwright.config.js"
, "script-overload-snapshots" ],
"playwright.config.js",
"script-overload-snapshots"
],
"exclude": [
"snapshots/script-overload-snapshots",
"integration-test/pages",
Expand Down