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 all commits
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 experimentalModifyObstructiveThirdPartyCode 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)
})
})
})
})
15 changes: 15 additions & 0 deletions packages/server/lib/browsers/electron.js
Expand Up @@ -243,6 +243,13 @@ module.exports = {

if (ua) {
this._setUserAgent(win.webContents, ua)
// @see https://github.com/cypress-io/cypress/issues/22953
} else if (options.experimentalModifyObstructiveThirdPartyCode) {
const userAgent = this._getUserAgent(win.webContents)
// 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, '')

this._setUserAgent(win.webContents, modifiedNonObstructiveUserAgent)
}

const setProxy = () => {
Expand Down Expand Up @@ -415,6 +422,14 @@ module.exports = {
return webContents.session.clearCache()
},

_getUserAgent (webContents) {
const userAgent = webContents.session.getUserAgent()

debug('found user agent: %s', userAgent)

return userAgent
},

_setUserAgent (webContents, userAgent) {
debug('setting user agent to:', userAgent)
// set both because why not
Expand Down
1 change: 1 addition & 0 deletions packages/server/lib/open_project.ts
Expand Up @@ -84,6 +84,7 @@ export class OpenProject {
isTextTerminal: cfg.isTextTerminal,
downloadsFolder: cfg.downloadsFolder,
experimentalSessionAndOrigin: cfg.experimentalSessionAndOrigin,
experimentalModifyObstructiveThirdPartyCode: cfg.experimentalModifyObstructiveThirdPartyCode,
})

// if we don't have the isHeaded property
Expand Down
103 changes: 103 additions & 0 deletions packages/server/test/unit/browsers/electron_spec.js
Expand Up @@ -188,6 +188,7 @@ describe('lib/browsers/electron', () => {
sinon.stub(electron, '_clearCache').resolves()
sinon.stub(electron, '_setProxy').resolves()
sinon.stub(electron, '_setUserAgent')
sinon.stub(electron, '_getUserAgent')
})

it('sets menu.set whether or not its in headless mode', function () {
Expand Down Expand Up @@ -470,6 +471,108 @@ describe('lib/browsers/electron', () => {
})
})
})

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

beforeEach(function () {
userAgent = ''
electron._getUserAgent.callsFake(() => userAgent)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

doing a callsFake over returns since userAgent is assigned dynamically in the test.

})

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
})
})
})

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'

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')
})
})

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')
})
})

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')
})
})

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')
})
})

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')
})
})

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')
})
})

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')
})
})

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')
})
})
})
})
})

context('._render', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/config.ts
Expand Up @@ -30,7 +30,7 @@ export interface FullConfig extends Partial<Cypress.RuntimeConfigOptions & Cypre
// and are required when creating a project.
export type ReceivedCypressOptions =
Pick<Cypress.RuntimeConfigOptions, 'hosts' | 'projectName' | 'clientRoute' | 'devServerPublicPathRoute' | 'namespace' | 'report' | 'socketIoCookie' | 'configFile' | 'isTextTerminal' | 'isNewProject' | 'proxyUrl' | 'browsers' | 'browserUrl' | 'socketIoRoute' | 'arch' | 'platform' | 'spec' | 'specs' | 'browser' | 'version' | 'remote'>
& Pick<Cypress.ResolvedConfigOptions, 'chromeWebSecurity' | 'supportFolder' | 'experimentalSourceRewriting' | 'fixturesFolder' | 'reporter' | 'reporterOptions' | 'screenshotsFolder' | 'supportFile' | 'baseUrl' | 'viewportHeight' | 'viewportWidth' | 'port' | 'experimentalInteractiveRunEvents' | 'userAgent' | 'downloadsFolder' | 'env' | 'excludeSpecPattern' | 'specPattern' | 'experimentalSessionAndOrigin'> // TODO: Figure out how to type this better.
& Pick<Cypress.ResolvedConfigOptions, 'chromeWebSecurity' | 'supportFolder' | 'experimentalSourceRewriting' | 'fixturesFolder' | 'reporter' | 'reporterOptions' | 'screenshotsFolder' | 'supportFile' | 'baseUrl' | 'viewportHeight' | 'viewportWidth' | 'port' | 'experimentalInteractiveRunEvents' | 'userAgent' | 'downloadsFolder' | 'env' | 'excludeSpecPattern' | 'specPattern' | 'experimentalSessionAndOrigin' | 'experimentalModifyObstructiveThirdPartyCode'> // TODO: Figure out how to type this better.

export interface SampleConfigFile{
status: 'changes' | 'valid' | 'skipped' | 'error'
Expand Down