3

Following code changes property printed (when accessed through javaScript) by browser console and not the actual Use-Agent string sent with the header.

Object.defineProperty(navigator, 'userAgent', {
    get: function () { return 'Mozilla/5.0 (Windows NT 6.2; WOW64; 
    rv:28.0) Gecko/20100101 Firefox/28.0)'; }
});
user435421
  • 859
  • 2
  • 13
  • 31
  • 1
    _A Navigator object can be retrieved using the **read-only** window.navigator property._ https://developer.mozilla.org/en-US/docs/Web/API/Navigator – mplungjan Sep 19 '19 at 07:44
  • However https://stackoverflow.com/questions/2166540/how-can-i-fool-a-site-that-looks-at-the-javascript-object-navigator-to-see-tha – mplungjan Sep 19 '19 at 07:46
  • Firefox and Safari just let you use `XHR.setRequestHeader('User-Agent', 'whatever')`. Chrome on the other hand throws an error... – Kaiido Sep 19 '19 at 12:19
  • This is now allowed, see my answer – Gertjan Brouwer Dec 27 '20 at 19:54

2 Answers2

5

This is now possible to do, see: https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name You can use ‘ XMLHttpRequest.setRequestHeader(User-Agent, value)’

Gertjan Brouwer
  • 996
  • 1
  • 12
  • 35
  • Although it should be possible, it does seems like Chromium is behaving like old IE - they make there own rules. I tested both Chrome and Edge - User-Agent is being ignored and controlled by said browsers. – gimlichael Jun 21 '22 at 06:40
  • Chromium responds with this in the console: Refused to set unsafe header "User-Agent". – gimlichael Jun 21 '22 at 06:51
3

This isn't possible from the page itself. The user agent string (as sent in the HTTP request headers) can only be changed via browser extension or browser configuration.

Brad
  • 159,648
  • 54
  • 349
  • 530