Skip to content

Commit

Permalink
prevent requests from being paused if experimentalSessionAndOrigin fl…
Browse files Browse the repository at this point in the history
…ag is off
  • Loading branch information
chrisbreiding committed Apr 22, 2022
1 parent 6ee45ac commit fde3acf
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 29 deletions.
9 changes: 7 additions & 2 deletions packages/extension/app/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,21 @@ const connect = function (host, path, extraOpts) {
}
})

ws.on('connect', async () => {
ws.on('automation:config', async (config) => {
const isFirefox = await checkIfFirefox()

listenToCookieChanges()
// Non-Firefox browsers use CDP for these instead
if (isFirefox) {
listenToDownloads()
listenToOnBeforeHeaders()

if (config.experimentalSessionAndOrigin) {
listenToOnBeforeHeaders()
}
}
})

ws.on('connect', () => {
ws.emit('automation:client:connected')
})

Expand Down
45 changes: 28 additions & 17 deletions packages/extension/test/integration/background_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,34 +124,32 @@ describe('app/background', () => {

browser.runtime.getBrowserInfo = sinon.stub().resolves({ name: 'Firefox' }),

this.connect = async () => {
this.connect = async (options = {}) => {
const ws = background.connect(`http://localhost:${PORT}`, '/__socket.io')

await ws.on.withArgs('connect').args[0][1]()
// skip 'connect' and 'automation:client:connected' and trigger
// the handler that kicks everything off
await ws.on.withArgs('automation:config').args[0][1](options)

return ws
}

this.httpSrv.listen(PORT, done)
})

afterEach(async function () {
afterEach(function (done) {
this.server.close()

this.httpSrv.close(() => {
done()
})
})

context('.connect', () => {
it('can connect', async function () {
this.server.on('connection', () => {
})

background.connect(`http://localhost:${PORT}`, '/__socket.io')
})

it('emits \'automation:client:connected\'', async function () {
const ws = await this.connect()
const ws = background.connect(`http://localhost:${PORT}`, '/__socket.io')

await ws.on.withArgs('connect').args[0][1]()

expect(ws.emit).to.be.calledWith('automation:client:connected')
})
Expand Down Expand Up @@ -268,14 +266,26 @@ describe('app/background', () => {
})

context('add header to aut iframe requests', () => {
const withExperimentalFlagOn = {
experimentalSessionAndOrigin: true,
}

it('does not listen to `onBeforeSendHeaders` if experimental flag is off', async function () {
sinon.stub(browser.webRequest.onBeforeSendHeaders, 'addListener')

await this.connect()

expect(browser.webRequest.onBeforeSendHeaders.addListener).not.to.be.called
})

it('does not add header if it is the top frame', async function () {
const details = {
parentFrameId: -1,
}

sinon.stub(browser.webRequest.onBeforeSendHeaders, 'addListener')

await this.connect()
await this.connect(withExperimentalFlagOn)

const result = browser.webRequest.onBeforeSendHeaders.addListener.lastCall.args[0](details)

Expand All @@ -289,7 +299,7 @@ describe('app/background', () => {

sinon.stub(browser.webRequest.onBeforeSendHeaders, 'addListener')

await this.connect()
await this.connect(withExperimentalFlagOn)

const result = browser.webRequest.onBeforeSendHeaders.addListener.lastCall.args[0](details)

Expand All @@ -304,7 +314,7 @@ describe('app/background', () => {

sinon.stub(browser.webRequest.onBeforeSendHeaders, 'addListener')

await this.connect()
await this.connect(withExperimentalFlagOn)

const result = browser.webRequest.onBeforeSendHeaders.addListener.lastCall.args[0](details)

Expand All @@ -320,7 +330,7 @@ describe('app/background', () => {

sinon.stub(browser.webRequest.onBeforeSendHeaders, 'addListener')

await this.connect()
await this.connect(withExperimentalFlagOn)
const result = browser.webRequest.onBeforeSendHeaders.addListener.lastCall.args[0](details)

expect(result).to.be.undefined
Expand All @@ -338,7 +348,7 @@ describe('app/background', () => {

sinon.stub(browser.webRequest.onBeforeSendHeaders, 'addListener')

await this.connect()
await this.connect(withExperimentalFlagOn)
const result = browser.webRequest.onBeforeSendHeaders.addListener.lastCall.args[0](details)

expect(result).to.deep.equal({
Expand All @@ -360,7 +370,8 @@ describe('app/background', () => {

const onBeforeSendHeaders = sinon.stub(browser.webRequest.onBeforeSendHeaders, 'addListener')

await this.connect()
await this.connect(withExperimentalFlagOn)

expect(onBeforeSendHeaders).not.to.be.called
})
})
Expand Down
7 changes: 5 additions & 2 deletions packages/server/lib/browsers/chrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,11 @@ export = {
await this._maybeRecordVideo(criClient, options, browser.majorVersion)
await this._navigateUsingCRI(criClient, url)
await this._handleDownloads(criClient, options.downloadsFolder, automation)
await this._handlePausedRequests(criClient)
_listenForFrameTreeChanges(criClient)

if (options.experimentalSessionAndOrigin) {
await this._handlePausedRequests(criClient)
_listenForFrameTreeChanges(criClient)
}

// return the launched browser process
// with additional method to close the remote connection
Expand Down
4 changes: 3 additions & 1 deletion packages/server/lib/browsers/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ module.exports = {
return this._enableDebugger(win.webContents)
})
.then(() => {
this._listenToOnBeforeHeaders(win)
if (options.experimentalSessionAndOrigin) {
this._listenToOnBeforeHeaders(win)
}

return this._handleDownloads(win, options.downloadsFolder, automation)
})
Expand Down
7 changes: 7 additions & 0 deletions packages/server/lib/socket-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,15 @@ const retry = (fn: (res: any) => void) => {
}

export class SocketBase {
protected experimentalSessionAndOrigin: boolean
protected ended: boolean
protected _io?: socketIo.SocketIOServer
protected testsDir: string | null

localBus: EventEmitter

constructor (config: Record<string, any>) {
this.experimentalSessionAndOrigin = config.experimentalSessionAndOrigin
this.ended = false
this.testsDir = null
this.localBus = new EventEmitter()
Expand Down Expand Up @@ -204,6 +206,11 @@ export class SocketBase {

debug('automation:client connected')

// only send the necessary config
automationClient.emit('automation:config', {
experimentalSessionAndOrigin: this.experimentalSessionAndOrigin,
})

// if our automation disconnects then we're
// in trouble and should probably bomb everything
automationClient.on('disconnect', () => {
Expand Down
30 changes: 23 additions & 7 deletions packages/server/test/unit/browsers/chrome_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ describe('lib/browsers/chrome', () => {
return chrome.open('chrome', 'http://', {}, this.automation)
.then(() => {
expect(utils.getPort).to.have.been.calledOnce // to get remote interface port
expect(this.criClient.send.callCount).to.equal(6)

expect(this.criClient.send).to.have.been.calledWith('Page.bringToFront')
expect(this.criClient.send).to.have.been.calledWith('Page.navigate')
expect(this.criClient.send).to.have.been.calledWith('Page.enable')
expect(this.criClient.send).to.have.been.calledWith('Page.setDownloadBehavior')
expect(this.criClient.send).to.have.been.calledWith('Network.enable')
expect(this.criClient.send).to.have.been.calledWith('Fetch.enable')

expect(this.criClient.send.callCount).to.equal(5)
})
})

Expand Down Expand Up @@ -365,6 +365,10 @@ describe('lib/browsers/chrome', () => {
})

describe('adding header to AUT iframe request', function () {
const withExperimentalFlagOn = {
experimentalSessionAndOrigin: true,
}

beforeEach(function () {
const frameTree = {
frameTree: {
Expand All @@ -388,8 +392,20 @@ describe('lib/browsers/chrome', () => {
this.criClient.send.withArgs('Page.getFrameTree').resolves(frameTree)
})

it('does not listen to Fetch.requestPaused if experimental flag is off', async function () {
await chrome.open('chrome', 'http://', { experimentalSessionAndOrigin: false }, this.automation)

expect(this.criClient.on).not.to.be.calledWith('Fetch.requestPaused')
})

it('sends Fetch.enable', async function () {
await chrome.open('chrome', 'http://', withExperimentalFlagOn, this.automation)

expect(this.criClient.send).to.have.been.calledWith('Fetch.enable')
})

it('does not add header when not a document', async function () {
await chrome.open('chrome', 'http://', {}, this.automation)
await chrome.open('chrome', 'http://', withExperimentalFlagOn, this.automation)

this.criClient.on.withArgs('Fetch.requestPaused').yield({
requestId: '1234',
Expand All @@ -402,7 +418,7 @@ describe('lib/browsers/chrome', () => {
})

it('does not add header when it is a spec frame request', async function () {
await chrome.open('chrome', 'http://', {}, this.automation)
await chrome.open('chrome', 'http://', withExperimentalFlagOn, this.automation)

this.criClient.on.withArgs('Page.frameAttached').yield()

Expand All @@ -421,7 +437,7 @@ describe('lib/browsers/chrome', () => {
})

it('appends X-Cypress-Is-AUT-Frame header to AUT iframe request', async function () {
await chrome.open('chrome', 'http://', {}, this.automation)
await chrome.open('chrome', 'http://', withExperimentalFlagOn, this.automation)

this.criClient.on.withArgs('Page.frameAttached').yield()

Expand Down Expand Up @@ -453,15 +469,15 @@ describe('lib/browsers/chrome', () => {
})

it('gets frame tree on Page.frameAttached', async function () {
await chrome.open('chrome', 'http://', {}, this.automation)
await chrome.open('chrome', 'http://', withExperimentalFlagOn, this.automation)

this.criClient.on.withArgs('Page.frameAttached').yield()

expect(this.criClient.send).to.be.calledWith('Page.getFrameTree')
})

it('gets frame tree on Page.frameDetached', async function () {
await chrome.open('chrome', 'http://', {}, this.automation)
await chrome.open('chrome', 'http://', withExperimentalFlagOn, this.automation)

this.criClient.on.withArgs('Page.frameDetached').yield()

Expand Down
14 changes: 14 additions & 0 deletions packages/server/test/unit/browsers/electron_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,21 @@ describe('lib/browsers/electron', () => {
})
})

it('does not listen to `onBeforeSendHeaders` if experimental flag is off', function () {
this.options.experimentalSessionAndOrigin = false
sinon.stub(this.win.webContents.session.webRequest, 'onBeforeSendHeaders')

return electron._launch(this.win, this.url, this.automation, this.options)
.then(() => {
expect(this.win.webContents.session.webRequest.onBeforeSendHeaders).not.to.be.called
})
})

describe('adding header aut iframe requests', function () {
beforeEach(function () {
this.options.experimentalSessionAndOrigin = true
})

it('does not add header if not a sub frame', function () {
sinon.stub(this.win.webContents.session.webRequest, 'onBeforeSendHeaders')

Expand Down

0 comments on commit fde3acf

Please sign in to comment.