-
Notifications
You must be signed in to change notification settings - Fork 19
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
Capture globals generically #719
Draft
jonathanKingston
wants to merge
7
commits into
main
Choose a base branch
from
jkt/type-globals
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7bdbf1a
Move to a safe-globals package
jonathanKingston 5413e73
Capture globals
jonathanKingston 7844d45
Add string capture for get stack
jonathanKingston b095421
Fix up extension testing
jonathanKingston 6bc060d
Account for extensions setting the globals
jonathanKingston 3823ad6
Change ordering of setGlobal
jonathanKingston c45f6fa
Fix script-overload testing
jonathanKingston File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { captureGlobal, cleanup } from './scope.js' | ||
|
||
export const String = captureGlobal('String') | ||
export const Set = captureGlobal('Set') | ||
export const Reflect = captureGlobal('Reflect') | ||
export const Object = captureGlobal('Object') | ||
export const RegExp = captureGlobal('RegExp') | ||
|
||
/** | ||
* Returns a string using the prototype of our safe iframe. | ||
*/ | ||
export function getSafeString (stringIn) { | ||
// eslint-disable-next-line no-new-wrappers | ||
return new String(stringIn) | ||
} | ||
|
||
// Both of these aren't using our safe iframe as we need to have registered side effects on the page | ||
// We still want to not have a tampered method though. | ||
export const customElementsGet = globalThis.customElements?.get.bind(globalThis.customElements) | ||
export const customElementsDefine = globalThis.customElements?.define.bind(globalThis.customElements) | ||
|
||
cleanup() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "@duckduckgo/safe-globals", | ||
"private": "true", | ||
"version": "0.0.1", | ||
"description": "Exposes a set of globals that aren't touched by a web page", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
}, | ||
"license": "ISC", | ||
"devDependencies": { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* global mozProxies */ | ||
import { getInjectionElement } from '../../src/utils.js' | ||
export const hasMozProxies = typeof mozProxies !== 'undefined' ? mozProxies : false | ||
|
||
let dummyWindow | ||
if (globalThis && 'document' in globalThis && | ||
// Prevent infinate recursion of injection into Chrome | ||
globalThis.location.href !== 'about:blank') { | ||
const injectionElement = getInjectionElement() | ||
// injectionElement is null in some playwright context tests | ||
if (injectionElement) { | ||
dummyWindow = globalThis.document.createElement('iframe') | ||
dummyWindow.style.display = 'none' | ||
injectionElement.appendChild(dummyWindow) | ||
} | ||
} | ||
|
||
let dummyContentWindow = dummyWindow?.contentWindow | ||
if (hasMozProxies) { | ||
// Purposefully prevent Firefox using the iframe as we can't hold stale references to iframes there. | ||
// dummyContentWindow = undefined | ||
// @ts-expect-error - mozProxies is not defined on window | ||
dummyContentWindow = dummyContentWindow?.wrappedJSObject | ||
} | ||
// @ts-expect-error - Symbol is not defined on window | ||
const dummySymbol = dummyContentWindow?.Symbol | ||
const iteratorSymbol = dummySymbol?.iterator | ||
|
||
/** | ||
* Capture prototype to prevent overloading | ||
* @template {string} T | ||
* @param {T} globalName | ||
* @returns {globalThis[T]} | ||
*/ | ||
export function captureGlobal (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 | ||
if (!global) { | ||
// @ts-expect-error can't index typeof T | ||
return globalThis[globalName] | ||
} | ||
|
||
// Alias the iterator symbol to the local symbol so for loops work | ||
if (iteratorSymbol && | ||
global?.prototype && | ||
iteratorSymbol in global.prototype) { | ||
global.prototype[Symbol.iterator] = global.prototype[iteratorSymbol] | ||
} | ||
return global | ||
} | ||
|
||
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() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// This file is used to assist with ordering of setup of globals from utils.js with safe-globals | ||
// It's to allow for the extension to setGlobal in testing | ||
|
||
// Only use globalThis for testing this breaks window.wrappedJSObject code in Firefox | ||
// eslint-disable-next-line no-global-assign | ||
export let globalObj = typeof window === 'undefined' ? globalThis : window | ||
export let Error = globalObj.Error | ||
|
||
/** | ||
* Used for testing to override the globals used within this file. | ||
* @param {window} globalObjIn | ||
*/ | ||
export function setGlobal (globalObjIn) { | ||
globalObj = globalObjIn | ||
Error = globalObj.Error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this just noticed during testing and fixed on the fly?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the method signature to return null also.
The reason this was added is the utils.js is used within the extension testing which doesn't have a DOM, so the code falls back to just using the existing global (under a different alias so still some slight improvements).