detect proxy in audio worklet

cherrypicked from c119065 (detect proxy, 2024-06-29)
and df1f9bf (detect Proxy properly, 2024-06-30)
This commit is contained in:
SArpnt 2024-08-02 23:43:45 -04:00
parent 5ec0219691
commit 6273ac4bdd
Signed by: SArpnt
SSH key fingerprint: SHA256:iDMeic8KkqqEsN4wODlgsk1d/oW1ojZ/cu/MEWyfLBw

View file

@ -53,6 +53,18 @@ function getErrorMessage(err: any, time: number) {
}
}
// replace Proxy so that they can be detected from the bytebeat code
// this is completely undetectable by the bytebeat code
const proxies = new WeakSet();
Proxy = Object.getPrototypeOf(Proxy).contructor = new Proxy(Proxy, {
construct(target, args) {
// @ts-expect-error
const newProxy = new target(...args);
proxies.add(newProxy);
return newProxy;
},
})
class BytebeatProcessor extends AudioWorkletProcessor {
audioSample = 0; // TODO: is this needed? might be better to use currentTime global
lastFlooredTime = -1;
@ -250,9 +262,7 @@ class BytebeatProcessor extends AudioWorkletProcessor {
funcValue = NaN;
}
if (Array.isArray(funcValue)) {
// replace array for safety, arrays could have modified functions
// TODO can the index operator be overridden? is this safe?
if (Array.isArray(funcValue) && !proxies.has(funcValue)) {
funcValue = [funcValue[0], funcValue[1]];
} else {
funcValue = [funcValue, funcValue];
@ -261,7 +271,11 @@ class BytebeatProcessor extends AudioWorkletProcessor {
let changedSample = false;
for (const c in funcValue) {
try {
funcValue[c] = Number(funcValue[c]);
if (proxies.has(funcValue[c])) {
throw new TypeError("can't convert proxy to number");
} else {
funcValue[c] = +funcValue[c];
}
} catch (err) {
funcValue[c] = NaN;
}