Skip to content
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

dom tame poc #29056

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
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
111 changes: 111 additions & 0 deletions app/scripts/use-snow.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,116 @@ Changing this code must be done cautiously to avoid breaking the app!

// eslint-disable-next-line import/unambiguous
(function () {
function tameDOM(win) {
// return;
function generateTamedWindow(win) {
return new Proxy(win, {
get: (a,b,c) => {
if (b === 'document') {
return proxy;
}
if (![
"nodeName",
"Element",
"HTMLElement",
"getComputedStyle",
"visualViewport",
"addEventListener",
"removeEventListener",
"toString",
"pageXOffset",
"pageYOffset",
].includes(b)) {
throw 'NOT ALLOWED(window): ' + b;
}
let ret = Reflect.get(a,b);
if (typeof ret === 'function') {
ret = ret.bind(win);
}
return ret;
}
});
}
function generateTamedDocument(doc, winp) {
return new Proxy(doc, {
get: (a,b,c) => {
if (b === 'defaultView') {
// return window;
return winp;
}
if (![
"body",
"host",
"shadowRoot",
"scrollingElement",
"nodeType",
"documentElement",
"activeElement",
"parentNode",
"addEventListener",
"createElement",
"createElementNS",
"parentWindow",
"compatMode",
"elementFromPoint",
"elementsFromPoint",
"createEvent",
"createTextNode"
].includes(b) &&
!b.startsWith('__reactInternalInstance') &&
!b.startsWith('__reactContainer')
) {
throw 'NOT ALLOWED(document): ' + b;
}
let ret = Reflect.get(a,b);
if (typeof ret === 'function') {
ret = ret.bind(doc);
}
return ret;
}
});
}
try {win.Node} catch {return;}
const {document, Object, Document, Node, MouseEvent} = win;
const winp = generateTamedWindow(win);
const proxy = generateTamedDocument(document, winp);
Object.defineProperty(Document.prototype, 'defaultView', {get: function() {
return winp;
}}
);
Object.defineProperty(Node.prototype, 'ownerDocument', {get: function() {
return proxy;
}}
);
const getRootNode = Object.getOwnPropertyDescriptor(Node.prototype, 'getRootNode').value;
Object.defineProperty(Node.prototype, 'getRootNode', {value: function() {
const that = this === proxy ? document : this;
const ret = getRootNode.call(that);
if (ret === document) {
return proxy;
}
return ret;
}}
);
const parentNode = Object.getOwnPropertyDescriptor(Node.prototype, 'parentNode').get;
Object.defineProperty(Node.prototype, 'parentNode', {get: function() {
const that = this === proxy ? document : this;
const ret = parentNode.call(that);
if (ret === document) {
return proxy;
}
return ret;
}}
);
const initMouseEvent = Object.getOwnPropertyDescriptor(MouseEvent.prototype, 'initMouseEvent').value;
Object.defineProperty(MouseEvent.prototype, 'initMouseEvent', {value: function (a,b,c,w,e,f,g,h,i,j,k,l,m,n,o) {
if (winp === w) {
w = win;
}
return initMouseEvent.call(this, a,b,c,w,e,f,g,h,i,j,k,l,m,n,o);
}}
);
}
const log = console.log.bind(console);
// eslint-disable-next-line no-undef
const isWorker = !self.document;
Expand All @@ -21,6 +131,7 @@ Changing this code must be done cautiously to avoid breaking the app!
// eslint-disable-next-line no-undef
self.SNOW((win) => {
log(msg, win);
tameDOM(win);
scuttle(win);
}, realm);
}
Expand Down
Loading