How to set User-Agent header with Puppeteer JS and not fail

Don’t just blindly change your User-Agent header.

Navigator object

A website can check our navigator object. Navigator properties are so fucked up, but here they are:

  • productSub:
    · Safari, Chrome, Opera, Edge … gives 20030107
    · Firefox gives 20100101
  • vendor:
    · Chrome, Opera … gives Google Inc.
    · Safari gives Apple Computer, Inc.
    · Firefox, Internet Explorer, Edge … gives empty string
  • oscpu: only Firefox
  • cpuClass: only Internet Explorer
Fake Firefox / Windows
Fake Internet Explorer / Windows

Window.open

A website can open a new window and get navigator.userAgent from that new window.

window.open.toString = () => 'function open() { [native code] }'

Service Worker

One thing we can’t fake is communication with a service worker. After service worker registration, main thread can communicate with a worker via postMessage.

main.js
service-worker.js

Conclusion

User-Agent sniffing is one small piece of browser fingerprinting puzzle. Sometimes you have to make an extra effort and don’t just blindly fake your User-Agent header. Make sure that website is not double checking you.

Coffee Driven Software Developer @SBGenomics