ray-lothian / UserAgent-Switcher

A User-Agent spoofer browser extension that is highly configurable
https://webextension.org/listing/useragent-switcher.html
Mozilla Public License 2.0
888 stars 143 forks source link

userAgentData is incomplete #163

Closed billy4479 closed 1 year ago

billy4479 commented 1 year ago

The navigator.userAgentData object lacks the platform key. According to MDN and the spec, it should be a string containing the underlying operating system, for example "Windows", "Linux" or "Android".

This "confuses" many websites that think can rely on userAgentData, because it's not undefined, but because it's incomplete it causes problems when the website tries to access userAgentData.platform. A website that shows this behavior (at the time writing) is web.snapchat.com.

baptx commented 1 year ago

I was going to report this issue with Snapchat web version. We have to change the user agent but it fails with this addon unlike another addon that I have been using: https://addons.mozilla.org/en-US/firefox/addon/user-agent-switcher-revived. I even created a polyfill with Greasemonkey using this JavaScript code to try if it works: window.eval("window.navigator.userAgentData = new function (){this.platform = 'Linux';};"); However the addon otherwrites navigator.userAgentData unlike the other addon. In fact I noticed that by default navigator.userAgentData is undefined when using the other addon but it still works. However I had to update the version of the user agent on the other addon, I used this value from Linux Chromium: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36

billy4479 commented 1 year ago

I did some further investigation and Snapchat web does provide some kind of polyfill for userAgentData but only if it's undefined before. If it is not undefined it assumes it is compliant to the specification, which, in this case, it is not, causing the website to crash. The issue with Snapchat is kind of unrelated to this extension: the website relies on NavigatorUAData which is an experimental API that Firefox does not support (yet). The options here would be to write some kind of polyfill for the full interface or just ignore it and let the website provide its own, the current solution is something in between that breaks websites that assume that it's present.

Another example of this is Whatsapp web, that, at the time of writing, if loaded from Firefox with the UA set to Chrome, throws an error complaining that getHighEntropyValues does not exist. This problem is also related to the "polyfill" provided by this extension being incomplete, because Whatsapp web works natively fine on Firefox too.

baptx commented 1 year ago

@billy4479 Why not leave navigator.userAgentData undefined like the other working addon I mentioned is doing? It works on Snapchat Web. Anyway navigator.userAgentData is experimental technology and cannot be trusted in production like mentioned on https://developer.mozilla.org/en-US/docs/Web/API/Navigator/userAgentData. In fact the addon breaks production websites like Snapchat because it uses this experimental technology (even if it can also be seen as an issue with the website).

Update: do you have popular websites examples that break if navigator.userAgentData is undefined? I never had this issue.

baptx commented 1 year ago

Adding an option to keep navigator.userAgentData undefined could be useful in case there are other problems like this in the future.

billy4479 commented 1 year ago

Thanks for the fix :)

navigator.userAgentData's interface defines also two member functions, namely NavigatorUAData.toJSON() and NavigatorUAData.getHighEntropyValues(). It would be desirable, if possible, to have those functions too as some websites (web.whatsapp.com for example) rely on those

ray-lothian commented 1 year ago

Adding an option to keep navigator.userAgentData undefined could be useful

Done.

navigator.userAgentData's interface defines also two member functions...

The new implementation contains these two methods. Give it a try, and let me know how it works.

billy4479 commented 1 year ago

Thank you for the quick fix, it works like a charm! Thank you so much