-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
AtofStryker
merged 14 commits into
develop
from
normalize-obstructive-user-agent-electron
Jul 30, 2022
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a219f90
feat: add modify obstructive user agent code for electron to strip el…
AtofStryker c1d1d00
Merge branch 'develop' into normalize-obstructive-user-agent-electron
AtofStryker a9cfb26
fix user agent destructuring error within unit tests
AtofStryker dbe001e
Merge branch 'normalize-obstructive-user-agent-electron' of github.co…
AtofStryker f768a0c
Merge branch 'develop' of github.com:cypress-io/cypress into normaliz…
AtofStryker 7673014
remove explicit CDP command and opt for _setUserAgent. add test to no…
AtofStryker e2f1298
add get user agent method to electron and update tests
AtofStryker 00a49e0
move user agent override in electron under experimentalModifyObstruct…
AtofStryker 6c56bac
move position of setting user agent to browser launch, hopefully fixi…
AtofStryker 7c1e479
Merge branch 'develop' of github.com:cypress-io/cypress into normaliz…
AtofStryker b71f7ed
update stale documentation and remove no longer necessary stub of get…
AtofStryker 366e5ff
add user agent to get user agent debug log
AtofStryker a644135
Merge branch 'develop' into normalize-obstructive-user-agent-electron
AtofStryker 7084d4c
Merge branch 'develop' into normalize-obstructive-user-agent-electron
mschile File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
packages/driver/cypress/e2e/e2e/origin/user_agent_override.cy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
// 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) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 () { | ||
|
@@ -470,6 +471,108 @@ describe('lib/browsers/electron', () => { | |
}) | ||
}) | ||
}) | ||
|
||
describe('setUserAgent with experimentalModifyObstructiveThirdPartyCode', () => { | ||
let userAgent | ||
|
||
beforeEach(function () { | ||
userAgent = '' | ||
electron._getUserAgent.callsFake(() => userAgent) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doing a |
||
}) | ||
|
||
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', () => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 onlyelectron
was? Or are we just stripping out all non-standard parts of the referrer?There was a problem hiding this comment.
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