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

Fix TypeError: "fetch" is read-only #169

Closed
wants to merge 9 commits into from

Conversation

PleasantMachine9
Copy link

@PleasantMachine9 PleasantMachine9 commented Jun 25, 2023

It seems a twitch (p.js?) script redefines the window.fetch with Object.defineProperty which makes it read-only (not over-writable with regular assignment).

Only using Object.defineProperty will be able to overwrite it.

@pixeltris
Copy link
Owner

Sounds good. I guess this is something they're A/B testing? I'm currently not seeing this on either Chrome/Firefox.

video-swap-new also uses the same hook so it'd be good to fix there too.

@PleasantMachine9
Copy link
Author

Probably. The load trace has kasada stuff in it

722956/t.prototype.loadKPSDK/b</</this.loadPromise<
https://static.twitchcdn.net/assets/minimal-ebf9adeffef061974e24.js:1:745537
722956/t.prototype.loadKPSDK/b</<
https://static.twitchcdn.net/assets/minimal-ebf9adeffef061974e24.js:1:744877
...
722956/t.prototype.loadKPSDK
https://static.twitchcdn.net/assets/minimal-ebf9adeffef061974e24.js:1:744741

@pixeltris
Copy link
Owner

It looks like you need to pass in a descriptor structure rather than just passing in a function. Example:

var realFetch = window.fetch;
Object.defineProperty(window, "fetch", { value: function(url, init, ...args) { console.log('hi'); realFetch.apply(this, arguments); } });
fetch();

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

Do they do anything with the worker fetch?

@PleasantMachine9
Copy link
Author

It looks like window.fetch is not really used by the twitch vendor.js files after the page is loaded. Overwriting it with anything does nothing.

Based on some traces it looks like p.js or some other obfuscated scriptlet stores the overwritten fetch in some closures and doesn't use the window-global one.

@pixeltris
Copy link
Owner

That could be problematic. I think the simplest solution would be to ensure the scripts run before their code.

I'll take a look when they have pushed this change out.

@PleasantMachine9
Copy link
Author

This PR doesn't fix the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants