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

feat: add modify obstructive user agent code for electron to strip el… #22958

Merged
merged 14 commits into from Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 50 additions & 0 deletions packages/driver/cypress/e2e/e2e/origin/user_agent_override.cy.ts
@@ -0,0 +1,50 @@
// @see https://github.com/cypress-io/cypress/issues/22953
// We modify the user agent to strip cypress and electron out of the user agent string to appear more chrome-like
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to strip out cypress? I thought it wasn't causing any issues and only electron was? Or are we just stripping out all non-standard parts of the referrer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just stripping out non standard parts of the user agent. Seems weird to keep it in there but currently there aren't ill adverse side effects with keeping it as far as we know

// this only happens in electron and when the experimentalSessionAndOrigin flag is set to true
describe('user agent override', {
browser: 'electron',
}, () => {
it('persists modified user agent after cy.visit', () => {
cy.wrap(window.navigator.userAgent).as('userAgentBefore')
cy.visit('/fixtures/primary-origin.html')
cy.wrap(window.navigator.userAgent).then((userAgentAfter) => {
cy.get('@userAgentBefore').then((userAgentBefore) => {
expect(userAgentBefore).to.equal(userAgentAfter)
})
})
})

it('persists modified user agent after cy.reload', () => {
cy.wrap(window.navigator.userAgent).as('userAgentBefore')
cy.visit('/fixtures/primary-origin.html')
cy.reload()
cy.wrap(window.navigator.userAgent).then((userAgentAfter) => {
cy.get('@userAgentBefore').then((userAgentBefore) => {
expect(userAgentBefore).to.equal(userAgentAfter)
})
})
})

it('persists modified user agent after cy.go', () => {
cy.visit('/fixtures/primary-origin.html')
cy.get('a[data-cy="cross-origin-secondary-link"]').click()

cy.origin('http://foobar.com:3500', {
args: {
userAgentBefore: window.navigator.userAgent,
},
}, ({ userAgentBefore }) => {
cy.visit('http://www.foobar.com:3500/fixtures/dom.html')

cy.go('back')
cy.wrap(window.navigator.userAgent).then((userAgentAfter) => {
expect(userAgentBefore).to.equal(userAgentAfter)
})

cy.go('forward')
cy.wrap(window.navigator.userAgent).then((userAgentAfter) => {
expect(userAgentBefore).to.equal(userAgentAfter)
})
})
})
})
14 changes: 14 additions & 0 deletions packages/server/lib/browsers/electron.js
Expand Up @@ -58,6 +58,20 @@ const _getAutomation = async function (win, options, parent) {

const automation = await CdpAutomation.create(sendCommand, on, sendClose, parent, options.experimentalSessionAndOrigin)

// Only pertains to e2e code as experimentalSessionAndOrigin is enforced only as an e2e config option
// @see https://github.com/cypress-io/cypress/issues/22953
if (options.experimentalSessionAndOrigin) {
const { userAgent } = await sendCommand('Browser.getVersion')
// replace any obstructive electron user agents that contain electron or cypress references to appear more chrome-like
const modifiedNonObstructiveUserAgent = userAgent.replace(/Cypress.*?\s|[Ee]lectron.*?\s/g, '')

sendCommand('Network.setUserAgentOverride', {
userAgent: modifiedNonObstructiveUserAgent,
}).catch((e) => {
debug('failed to set user agent override in electron', e.message)
})
}

automation.onRequest = _.wrap(automation.onRequest, async (fn, message, data) => {
switch (message) {
case 'take:screenshot': {
Expand Down
108 changes: 108 additions & 0 deletions packages/server/test/unit/browsers/electron_spec.js
Expand Up @@ -470,6 +470,114 @@ describe('lib/browsers/electron', () => {
})
})
})

describe('setUserAgentOverride with experimentalSessionAndOrigin', () => {
let userAgent

beforeEach(function () {
userAgent = ''
this.win.webContents.debugger.sendCommand = sinon.stub().withArgs('Browser.getVersion').callsFake(() => {
return {
userAgent,
}
})
})

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

return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
expect(this.win.webContents.debugger.sendCommand).to.not.be.calledWithMatch('Network.setUserAgentOverride')
})
})
})

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

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(this.win.webContents.debugger.sendCommand).to.be.calledWithMatch('Network.setUserAgentOverride', {
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('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(this.win.webContents.debugger.sendCommand).to.be.calledWithMatch('Network.setUserAgentOverride', {
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('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(this.win.webContents.debugger.sendCommand).to.be.calledWithMatch('Network.setUserAgentOverride', {
userAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.113 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(this.win.webContents.debugger.sendCommand).to.be.calledWithMatch('Network.setUserAgentOverride', {
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 Safari/537.36',
})
})
})

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(this.win.webContents.debugger.sendCommand).to.be.calledWithMatch('Network.setUserAgentOverride', {
userAgent: 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Typora/0.9.93 Chrome/83.0.4103.119 Safari/E7FBAF',
})
})
})

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(this.win.webContents.debugger.sendCommand).to.be.calledWithMatch('Network.setUserAgentOverride', {
userAgent: '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',
})
})
})

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(this.win.webContents.debugger.sendCommand).to.be.calledWithMatch('Network.setUserAgentOverride', {
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',
})
})
})
})
})
})

context('._render', () => {
Expand Down