Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
soundofspace committed Aug 13, 2024
1 parent 769f965 commit f15de75
Show file tree
Hide file tree
Showing 28 changed files with 658 additions and 379 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
const triggerName = args.callbackName;
export type Args = {
callbackName: string;
};
const typedArgs = args as Args;
const triggerName = typedArgs.callbackName;

if (!self[triggerName]) throw new Error('No cookie trigger');
const cookieTrigger = ((self[triggerName] as unknown) as Function).bind(self);
const cookieTrigger = (self[triggerName] as unknown as Function).bind(self);

delete self[triggerName];

proxySetter(Document.prototype, 'cookie', (target, thisArg, cookie) => {
cookieTrigger(JSON.stringify({ cookie, origin: self.location.origin }));
return ProxyOverride.callOriginal;
proxySetter(Document.prototype, 'cookie', (target, thisArg, argArray) => {
const cookie = argArray.at(0);
if (cookie) {
cookieTrigger(JSON.stringify({ cookie, origin: self.location.origin }));
}
return ReflectCached.apply(target, thisArg, argArray!);
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
proxyFunction(JSON, 'stringify', (target, thisArg, argArray) => {
argArray[1] = null;
argArray[2] = 2;
export type Args = {};

const result = target.apply(thisArg, argArray);
proxyFunction(JSON, 'stringify', (target, thisArg, argArray) => {
const result = ReflectCached.apply(target, thisArg, [argArray.at(0), null, 2]);
console.log(result);

return result;
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
export type Args = {
deviceId?: string;
groupId?: string
};
const typedArgs = args as Args;

if (
navigator.mediaDevices &&
navigator.mediaDevices.enumerateDevices &&
navigator.mediaDevices.enumerateDevices.name !== 'bound reportBlock'
) {
const videoDevice = {
deviceId: args.deviceId,
groupId: args.groupId,
deviceId: typedArgs.deviceId,
groupId: typedArgs.groupId,
kind: 'videoinput',
label: '',
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// @ts-ignore
const { audioCodecs, videoCodecs } = args;
export type Args = {
audioCodecs: any;
videoCodecs: any;
};
const typedArgs = args as Args;

const { audioCodecs, videoCodecs } = typedArgs;

if ('RTCRtpSender' in self && RTCRtpSender.prototype) {
proxyFunction(RTCRtpSender, 'getCapabilities', function (target, thisArg, argArray) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type Args = {};

if (typeof SharedWorker === 'undefined') {
// @ts-ignore
return;
Expand Down Expand Up @@ -34,33 +36,32 @@ proxyConstructor(self, 'SharedWorker', (target, argArray) => {
setTimeout(()=> {
removeEventListener('connect', storeEvent);
original();
events.forEach(ev => onconnect(ev));
// new Event('connect', ev)
events.forEach(ev => dispatchEvent(ev));
delete events;
}, 0);
}
function isInjectedDone() {
// See proxyUtils
function getSharedStorage() {
try {
// We can use this to check if injected logic is loaded
return Error['${sourceUrl}'];
return Function.prototype.toString.call('${sourceUrl}');
} catch {
return false;
return undefined;
}
}
if (isInjectedDone()) {
if (getSharedStorage()?.ready) {
originalAsSync();
return;
}
// Keep checking until we are ready
const interval = setInterval(() => {
if (!isInjectedDone()) {
return
if (getSharedStorage()?.ready) {
clearInterval(interval);
originalAsSync();
}
clearInterval(interval);
originalAsSync();
}, 20);
})()
`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
self.addEventListener('error', preventDefault);
self.addEventListener('unhandledrejection', preventDefault);
export type Args = {
preventDefaultUncaughtError: boolean;
preventDefaultUnhandledRejection: boolean;
};

const typedArgs = args as Args;

if (typedArgs.preventDefaultUncaughtError) {
self.addEventListener('error', preventDefault);
}
if (typedArgs.preventDefaultUnhandledRejection) {
self.addEventListener('unhandledrejection', preventDefault);
}

function preventDefault(event: ErrorEvent | PromiseRejectionEvent) {
event.preventDefault();
Expand All @@ -14,7 +25,7 @@ function preventDefault(event: ErrorEvent | PromiseRejectionEvent) {
ReflectCached.apply(originalFunction, thisArg, argArray);
prevented = true;
},
true,
{ overrideOnlyForInstance: true },
);
proxyGetter(
event,
Expand All @@ -23,7 +34,7 @@ function preventDefault(event: ErrorEvent | PromiseRejectionEvent) {
ReflectCached.get(target, thisArg);
return prevented;
},
true,
{ overrideOnlyForInstance: true },
);

if (!('console' in self)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export type Args = Record<string, string | number | boolean>;
const typedArgs = args as Args;

const activatedDebugInfo = new WeakSet<WebGL2RenderingContext | WebGLRenderingContext>();

for (const context of [
Expand All @@ -18,12 +21,12 @@ for (const context of [
const parameter = argArray && argArray.length ? argArray[0] : null;
// call api to make sure signature goes through
const result = ReflectCached.apply(originalFunction, thisArg, argArray);
if (args[parameter]) {
if (typedArgs[parameter]) {
if (!result && !activatedDebugInfo.has(context)) {
return result;
}

return args[parameter];
return typedArgs[parameter];
}
return result;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ for (const symbol of ReflectCached.ownKeys(Symbol)) {
function createError(message: string, type?: { new (msg: string): any }) {
if (!type) {
const match = nativeErrorRegex.exec(message);
if (match.length) {
if (match?.length) {
message = message.replace(`${match[1]}: `, '');
try {
type = self[match[1]];
Expand Down Expand Up @@ -47,6 +47,7 @@ function newObjectConstructor(
}
const props = Object.entries(newProps);
const obj = {};
if (!newProps._$protos) throw new Error('newProps._$protos undefined');
Object.setPrototypeOf(
obj,
prototypesByPath[newProps._$protos[0]] ?? getObjectAtPath(newProps._$protos[0]),
Expand All @@ -55,7 +56,7 @@ function newObjectConstructor(
if (prop.startsWith('_$')) continue;
let propName: string | symbol = prop;
if (propName.startsWith('Symbol(')) {
propName = Symbol.for(propName.match(/Symbol\((.+)\)/)[1]);
propName = Symbol.for(propName.match(/Symbol\((.+)\)/)![1]);
}
Object.defineProperty(obj, propName, buildDescriptor(value, `${path}.${prop}`));
}
Expand All @@ -73,28 +74,28 @@ function buildDescriptor(entry: IDescriptor, path: string): PropertyDescriptor {
if (flags.includes('e')) attrs.enumerable = true;

if (entry._$get) {
attrs.get = new Proxy(Function.prototype.call.bind({}), {
attrs.get = new Proxy(function () {}, {
apply() {
if (entry._$accessException) throw createError(entry._$accessException);
if (entry._$value) return entry._$value;
if (entry['_$$value()']) return entry['_$$value()']();
},
});
overriddenFns.set(attrs.get, entry._$get);
overriddenFns.set(attrs.get!, entry._$get);
} else if (entry['_$$value()']) {
attrs.value = entry['_$$value()']();
} else if (entry._$value !== undefined) {
attrs.value = entry._$value;
}

if (entry._$set) {
attrs.set = new Proxy(Function.prototype.call.bind({}), {
attrs.set = new Proxy(function () {}, {
apply() {},
});
overriddenFns.set(attrs.set, entry._$set);
overriddenFns.set(attrs.set!, entry._$set);
}

let prototypeDescriptor: PropertyDescriptor;
let prototypeDescriptor: PropertyDescriptor | undefined;
if (entry.prototype) {
prototypeDescriptor = buildDescriptor(entry.prototype, `${path}.prototype`);

Expand All @@ -116,12 +117,12 @@ function buildDescriptor(entry: IDescriptor, path: string): PropertyDescriptor {
Object.keys(entry)
.filter((key): key is OtherInvocationKey => key.startsWith('_$otherInvocation'))
// Not supported currently
.filter((key)=> !key.includes('new()'))
.filter(key => !key.includes('new()'))
.forEach(key => OtherInvocationsTracker.addOtherInvocation(path, key, entry[key]));

// use function call just to get a function that doesn't create prototypes on new
// bind to an empty object so we don't modify the original
attrs.value = new Proxy(Function.prototype.call.bind({}), {
attrs.value = new Proxy(function () {}, {
apply(_target, thisArg) {
const invocation =
OtherInvocationsTracker.getOtherInvocation(path, thisArg)?.invocation ??
Expand Down Expand Up @@ -164,13 +165,13 @@ function buildDescriptor(entry: IDescriptor, path: string): PropertyDescriptor {
if (propName.startsWith('Symbol(')) {
propName = globalSymbols[propName];
if (!propName) {
const symbolName = (propName as string).match(/Symbol\((.+)\)/)[1];
const symbolName = (propName as string).match(/Symbol\((.+)\)/)![1];
propName = Symbol.for(symbolName);
}
}
let descriptor: PropertyDescriptor;
if (propName === 'prototype') {
descriptor = prototypeDescriptor;
descriptor = prototypeDescriptor!;
} else {
descriptor = buildDescriptor(value, `${path}.${prop}`);
}
Expand All @@ -197,9 +198,10 @@ function breakdownPath(path: string, propsToLeave) {
const parts = path.split(/\.Symbol\(([\w.]+)\)|\.(\w+)/).filter(Boolean);
let obj: any = self;
while (parts.length > propsToLeave) {
let next: string | symbol = parts.shift();
let next: string | symbol | undefined = parts.shift();
if (next === undefined) throw new Error('Reached end of parts without finding obj');
if (next === 'window') continue;
if (next.startsWith('Symbol.')) next = Symbol.for(next);
if (next?.startsWith('Symbol.')) next = Symbol.for(next);
obj = obj[next];
if (!obj) {
throw new Error(`Property not found -> ${path} at ${String(next)}`);
Expand Down Expand Up @@ -264,7 +266,9 @@ class PathToInstanceTracker {
}

private static getInstanceForPath(path: string) {
const { parent, property } = getParentAndProperty(path);
const result = getParentAndProperty(path);
if (!result) throw new Error('no parent and property found');
const { parent, property } = result;
return parent[property];
}
}
Expand All @@ -283,11 +287,7 @@ class OtherInvocationsTracker {
{ invocation: any; isAsync: boolean }
>();

static addOtherInvocation(
basePath: string,
otherKey: OtherInvocationKey,
otherInvocation: any,
) {
static addOtherInvocation(basePath: string, otherKey: OtherInvocationKey, otherInvocation: any) {
const [invocationKey, ...otherParts] = otherKey.split('.');
// Remove key/property from path
const otherPath = otherParts.slice(0, -1).join('.');
Expand All @@ -303,7 +303,7 @@ class OtherInvocationsTracker {
static getOtherInvocation(
basePath: string,
otherThis: any,
): { invocation: any; path: string; isAsync: boolean } {
): { invocation: any; path: string; isAsync?: boolean } | undefined {
const otherPath = PathToInstanceTracker.getPath(otherThis);
if (!otherPath) {
return;
Expand Down
Loading

0 comments on commit f15de75

Please sign in to comment.