Skip to content

Commit

Permalink
🐛 createElement hijack must be paired to avoid rewriting leak (#2777)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuitos authored Nov 2, 2023
1 parent 46e4921 commit a38b1af
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,37 @@ function patchDocument(cfg: { sandbox: SandBox; speedy: boolean }) {
};

if (speedy) {
const modifications: {
createElement?: typeof document.createElement;
querySelector?: typeof document.querySelector;
} = {};

const proxyDocument = new Proxy(document, {
/**
* Read and write must be paired, otherwise the write operation will leak to the global
*/
set: (target, p, value) => {
(<any>target)[p] = value;
switch (p) {
case 'createElement': {
modifications.createElement = value;
break;
}
case 'querySelector': {
modifications.querySelector = value;
break;
}
default:
(<any>target)[p] = value;
break;
}

return true;
},
get: (target, p, receiver) => {
switch (p) {
case 'createElement': {
// Must store the original createElement function to avoid error in nested sandbox
const targetCreateElement = target.createElement;
const targetCreateElement = modifications.createElement || target.createElement;
return function createElement(...args: Parameters<typeof document.createElement>) {
if (!nativeGlobal.__currentLockingSandbox__) {
nativeGlobal.__currentLockingSandbox__ = sandbox.name;
Expand All @@ -87,7 +108,7 @@ function patchDocument(cfg: { sandbox: SandBox; speedy: boolean }) {
}

case 'querySelector': {
const targetQuerySelector = target.querySelector;
const targetQuerySelector = modifications.querySelector || target.querySelector;
return function querySelector(...args: Parameters<typeof document.querySelector>) {
const selector = args[0];
switch (selector) {
Expand Down

1 comment on commit a38b1af

@vercel
Copy link

@vercel vercel bot commented on a38b1af Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.