Skip to content

Commit

Permalink
fix: also set electron user agent through CDP. see #23597 (#23721)
Browse files Browse the repository at this point in the history
  • Loading branch information
AtofStryker committed Sep 9, 2022
1 parent 23d52af commit afb8e67
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 60 deletions.
5 changes: 5 additions & 0 deletions packages/server/lib/browsers/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,11 @@ export = {
// set both because why not
webContents.userAgent = userAgent

// In addition to the session, also set the user-agent optimistically through CDP. @see https://github.com/cypress-io/cypress/issues/23597
webContents.debugger.sendCommand('Network.setUserAgentOverride', {
userAgent,
})

return webContents.session.setUserAgent(userAgent)
},

Expand Down
167 changes: 107 additions & 60 deletions packages/server/test/unit/browsers/electron_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ describe('lib/browsers/electron', () => {
webRequest: {
onBeforeSendHeaders () {},
},
setUserAgent: sinon.stub(),
getUserAgent: sinon.stub(),
clearCache: sinon.stub(),
},
getOSProcessId: sinon.stub().returns(ELECTRON_PID),
'debugger': {
Expand Down Expand Up @@ -473,104 +476,148 @@ describe('lib/browsers/electron', () => {
})
})
})
})

describe('setUserAgent with experimentalModifyObstructiveThirdPartyCode', () => {
let userAgent
describe('setUserAgent with experimentalModifyObstructiveThirdPartyCode', () => {
let userAgent
let originalSendCommandSpy

beforeEach(function () {
userAgent = ''
electron._getUserAgent.callsFake(() => userAgent)
})
beforeEach(function () {
userAgent = ''
this.win.webContents.session.getUserAgent.callsFake(() => userAgent)
// set a reference to the original sendCommand as it is decorated in electron.ts. This way, we can assert on the spy
originalSendCommandSpy = this.win.webContents.debugger.sendCommand
})

describe('disabled', function () {
it('does not attempt to replace the user agent', function () {
this.options.experimentalModifyObstructiveThirdPartyCode = false
describe('disabled', function () {
it('does not attempt to replace the user agent', function () {
this.options.experimentalModifyObstructiveThirdPartyCode = false

return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
expect(electron._setUserAgent).not.to.be.called
return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
expect(this.win.webContents.session.setUserAgent).not.to.be.called
expect(originalSendCommandSpy).not.to.be.calledWith('Network.setUserAgentOverride', {
userAgent,
})
})
})
})

describe('enabled and attempts to replace obstructive user agent string containing:', function () {
beforeEach(function () {
this.options.experimentalModifyObstructiveThirdPartyCode = true
})
describe('enabled and attempts to replace obstructive user agent string containing:', function () {
beforeEach(function () {
this.options.experimentalModifyObstructiveThirdPartyCode = true
})

it('does not attempt to replace the user agent if the user passes in an explicit user agent', function () {
userAgent = 'barbaz'
this.options.experimentalModifyObstructiveThirdPartyCode = false
this.options.userAgent = 'foobar'
it('does not attempt to replace the user agent if the user passes in an explicit user agent', function () {
userAgent = 'barbaz'
this.options.experimentalModifyObstructiveThirdPartyCode = false
this.options.userAgent = 'foobar'

return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
expect(electron._setUserAgent).to.be.calledWith(this.win.webContents, 'foobar')
expect(electron._setUserAgent).not.to.be.calledWith(this.win.webContents, 'barbaz')
return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
expect(this.win.webContents.session.setUserAgent).to.be.calledWith('foobar')
expect(this.win.webContents.session.setUserAgent).not.to.be.calledWith('barbaz')
expect(originalSendCommandSpy).to.be.calledWith('Network.setUserAgentOverride', {
userAgent: 'foobar',
})
})
})

it('versioned cypress', function () {
userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/10.0.3 Chrome/100.0.4896.75 Electron/18.0.4 Safari/537.36'
it('versioned cypress', function () {
userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/10.0.3 Chrome/100.0.4896.75 Electron/18.0.4 Safari/537.36'

return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
expect(electron._setUserAgent).to.have.been.calledWith(this.win.webContents, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36')
return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
const expectedUA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36'

expect(this.win.webContents.session.setUserAgent).to.have.been.calledWith(expectedUA)
expect(originalSendCommandSpy).to.be.calledWith('Network.setUserAgentOverride', {
userAgent: expectedUA,
})
})
})

it('development cypress', function () {
userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/0.0.0-development Chrome/100.0.4896.75 Electron/18.0.4 Safari/537.36'
it('development cypress', function () {
userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/0.0.0-development Chrome/100.0.4896.75 Electron/18.0.4 Safari/537.36'

return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
expect(electron._setUserAgent).to.have.been.calledWith(this.win.webContents, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36')
return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
const expectedUA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36'

expect(this.win.webContents.session.setUserAgent).to.have.been.calledWith(expectedUA)
expect(originalSendCommandSpy).to.be.calledWith('Network.setUserAgentOverride', {
userAgent: expectedUA,
})
})
})

it('older Windows user agent', function () {
userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) electron/1.0.0 Chrome/53.0.2785.113 Electron/1.4.3 Safari/537.36'
it('older Windows user agent', function () {
userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) electron/1.0.0 Chrome/53.0.2785.113 Electron/1.4.3 Safari/537.36'

return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
expect(electron._setUserAgent).to.have.been.calledWith(this.win.webContents, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 Safari/537.36')
return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
const expectedUA = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 Safari/537.36'

expect(this.win.webContents.session.setUserAgent).to.have.been.calledWith(expectedUA)
expect(originalSendCommandSpy).to.be.calledWith('Network.setUserAgentOverride', {
userAgent: expectedUA,
})
})
})

it('newer Windows user agent', function () {
userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Teams/1.5.00.4689 Chrome/85.0.4183.121 Electron/10.4.7 Safari/537.36'
it('newer Windows user agent', function () {
userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Teams/1.5.00.4689 Chrome/85.0.4183.121 Electron/10.4.7 Safari/537.36'

return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
expect(electron._setUserAgent).to.have.been.calledWith(this.win.webContents, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Teams/1.5.00.4689 Chrome/85.0.4183.121 Safari/537.36')
return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
const expectedUA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Teams/1.5.00.4689 Chrome/85.0.4183.121 Safari/537.36'

expect(this.win.webContents.session.setUserAgent).to.have.been.calledWith(expectedUA)
expect(originalSendCommandSpy).to.be.calledWith('Network.setUserAgentOverride', {
userAgent: expectedUA,
})
})
})

it('Linux user agent', function () {
userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Typora/0.9.93 Chrome/83.0.4103.119 Electron/9.0.5 Safari/E7FBAF'
it('Linux user agent', function () {
userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Typora/0.9.93 Chrome/83.0.4103.119 Electron/9.0.5 Safari/E7FBAF'

return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
expect(electron._setUserAgent).to.have.been.calledWith(this.win.webContents, 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Typora/0.9.93 Chrome/83.0.4103.119 Safari/E7FBAF')
return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
const expectedUA = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Typora/0.9.93 Chrome/83.0.4103.119 Safari/E7FBAF'

expect(this.win.webContents.session.setUserAgent).to.have.been.calledWith(expectedUA)
expect(originalSendCommandSpy).to.be.calledWith('Network.setUserAgentOverride', {
userAgent: expectedUA,
})
})
})

it('older MacOS user agent', function () {
// this user agent containing Cypress was actually a common UA found on a website for Electron purposes...
userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/8.3.0 Chrome/91.0.4472.124 Electron/13.1.7 Safari/537.36'
it('older MacOS user agent', function () {
// this user agent containing Cypress was actually a common UA found on a website for Electron purposes...
userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/8.3.0 Chrome/91.0.4472.124 Electron/13.1.7 Safari/537.36'

return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
expect(electron._setUserAgent).to.have.been.calledWith(this.win.webContents, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36')
return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
const expectedUA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'

expect(this.win.webContents.session.setUserAgent).to.have.been.calledWith(expectedUA)
expect(originalSendCommandSpy).to.be.calledWith('Network.setUserAgentOverride', {
userAgent: expectedUA,
})
})
})

it('newer MacOS user agent', function () {
userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36'
it('newer MacOS user agent', function () {
userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36'

return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
expect(electron._setUserAgent).to.have.been.calledWith(this.win.webContents, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36')
return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
const expectedUA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36'

expect(this.win.webContents.session.setUserAgent).to.have.been.calledWith(expectedUA)
expect(originalSendCommandSpy).to.be.calledWith('Network.setUserAgentOverride', {
userAgent: expectedUA,
})
})
})
Expand Down

4 comments on commit afb8e67

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on afb8e67 Sep 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.8.0/linux-x64/develop-afb8e67a0b63b28161efb3ee4d5e736a7a4cb2ba/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on afb8e67 Sep 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.8.0/linux-arm64/develop-afb8e67a0b63b28161efb3ee4d5e736a7a4cb2ba/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on afb8e67 Sep 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.8.0/darwin-x64/develop-afb8e67a0b63b28161efb3ee4d5e736a7a4cb2ba/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on afb8e67 Sep 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.8.0/darwin-arm64/develop-afb8e67a0b63b28161efb3ee4d5e736a7a4cb2ba/cypress.tgz

Please sign in to comment.