Skip to content

Commit

Permalink
fix: Make cross-origin document.cookie work (#22594)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding committed Jun 30, 2022
1 parent 2b3ab9a commit 5573fe5
Show file tree
Hide file tree
Showing 13 changed files with 329 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ context('cy.origin aliasing', () => {

it('fails for dom elements outside origin', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.equal('`cy.get()` could not find a registered alias for: `@welcome_button`.\nYou have not aliased anything yet.')
expect(err.message).to.equal('`cy.get()` could not find a registered alias for: `@link`.\nYou have not aliased anything yet.')
done()
})

cy.get('[data-cy="welcome"]').as('welcome_button')
cy.get('[data-cy="cross-origin-secondary-link"]').as('link')

cy.origin('http://foobar.com:3500', () => {
cy.get('@welcome_button').click()
cy.get('@link').click()
})
})
})
Expand Down
189 changes: 172 additions & 17 deletions packages/driver/cypress/e2e/e2e/origin/cookie_login.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ describe('cy.origin - cookie login', () => {
cy.get('h1').invoke('text').should('equal', 'No user found')
}

beforeEach(() => {
// makes it nice and readable even on a small screen with devtools open :)
cy.viewport(300, 400)
})

/****************************************************************************
Cookie Login Flow
- localhost/fixtures/primary-origin.html:
Expand Down Expand Up @@ -107,17 +112,6 @@ describe('cy.origin - cookie login', () => {
verifyLoggedIn(username)
})

it('makes cross-origin cookies readable via document.cookie', () => {
cy.visit('/fixtures/primary-origin.html')
cy.get('[data-cy="cookie-login"]').click()
cy.origin('http://foobar.com:3500', { args: { username } }, ({ username }) => {
cy.get('[data-cy="username"]').type(username)
cy.get('[data-cy="login"]').click()
})

cy.document().its('cookie').should('include', `user=${username}`)
})

it('handles browser-sent cookies being overridden by server-kept cookies', () => {
cy.visit('https://localhost:3502/fixtures/primary-origin.html')
cy.get('[data-cy="cookie-login-override"]').click()
Expand Down Expand Up @@ -421,10 +415,10 @@ describe('cy.origin - cookie login', () => {
username = getUsername()

cy.visit('/fixtures/primary-origin.html')
cy.get('[data-cy="cookie-login"]').click()
})

it('expired -> not logged in', () => {
cy.get('[data-cy="cookie-login"]').click()
cy.origin('http://foobar.com:3500', { args: { username } }, ({ username }) => {
const expires = (new Date()).toUTCString()

Expand All @@ -437,6 +431,7 @@ describe('cy.origin - cookie login', () => {
})

it('expired -> not accessible via cy.getCookie()', () => {
cy.get('[data-cy="cookie-login"]').click()
cy.origin('http://foobar.com:3500', { args: { username } }, ({ username }) => {
const expires = (new Date()).toUTCString()

Expand All @@ -449,6 +444,7 @@ describe('cy.origin - cookie login', () => {
})

it('expired -> not accessible via document.cookie', () => {
cy.get('[data-cy="cookie-login-land-on-idp"]').click()
cy.origin('http://foobar.com:3500', { args: { username } }, ({ username }) => {
const expires = (new Date()).toUTCString()

Expand All @@ -457,7 +453,10 @@ describe('cy.origin - cookie login', () => {
cy.get('[data-cy="login"]').click()
})

cy.document().its('cookie').should('not.include', 'user=')
cy.origin('http://idp.com:3500', () => {
cy.clearCookie('user')
cy.document().its('cookie').should('not.include', 'user=')
})
})
})

Expand All @@ -468,10 +467,10 @@ describe('cy.origin - cookie login', () => {
username = getUsername()

cy.visit('/fixtures/primary-origin.html')
cy.get('[data-cy="cookie-login"]').click()
})

it('past max-age -> not logged in', () => {
cy.get('[data-cy="cookie-login"]').click()
cy.origin('http://foobar.com:3500', { args: { username } }, ({ username }) => {
cy.get('[data-cy="username"]').type(username)
cy.get('[data-cy="localhostCookieProps"]').type('Max-Age=1')
Expand All @@ -487,6 +486,7 @@ describe('cy.origin - cookie login', () => {
// in Firefox. this issue doesn't seem to be specific to cross-origin tests,
// as it happens even using cy.setCookie()
it('past max-age -> not accessible via cy.getCookie()', { browser: '!firefox' }, () => {
cy.get('[data-cy="cookie-login"]').click()
cy.origin('http://foobar.com:3500', { args: { username } }, ({ username }) => {
cy.get('[data-cy="username"]').type(username)
cy.get('[data-cy="localhostCookieProps"]').type('Max-Age=1')
Expand All @@ -499,18 +499,25 @@ describe('cy.origin - cookie login', () => {
})

it('past max-age -> not accessible via document.cookie', () => {
cy.get('[data-cy="cookie-login-land-on-idp"]').click()
cy.origin('http://foobar.com:3500', { args: { username } }, ({ username }) => {
cy.get('[data-cy="username"]').type(username)
cy.get('[data-cy="localhostCookieProps"]').type('Max-Age=1')
cy.get('[data-cy="login"]').click()
})

cy.wait(1000) // give cookie time to expire
cy.reload()
cy.document().its('cookie').should('not.include', 'user=')
cy.origin('http://idp.com:3500', () => {
cy.wait(1000) // give cookie time to expire
cy.reload()
cy.document().its('cookie').should('not.include', 'user=')
})
})

describe('preference over Expires', () => {
beforeEach(() => {
cy.get('[data-cy="cookie-login"]').click()
})

it('past Max-Age, before Expires -> not logged in', () => {
const expires = dayjs().add(1, 'day').toDate().toUTCString()

Expand Down Expand Up @@ -642,4 +649,152 @@ describe('cy.origin - cookie login', () => {
verifyIdpNotLoggedIn({ isHttps: true, cookieKey: '__Secure-user' })
})
})

describe('document.cookie', () => {
let username

beforeEach(() => {
username = getUsername()

cy.visit('/fixtures/primary-origin.html')
})

it('gets cookie set by http request', () => {
cy.get('[data-cy="cookie-login-land-on-idp"]').click()
cy.origin('http://foobar.com:3500', { args: { username } }, ({ username }) => {
cy.get('[data-cy="username"]').type(username)
cy.get('[data-cy="login"]').click()
})

cy.origin('http://idp.com:3500', { args: { username } }, ({ username }) => {
cy.document().its('cookie').should('include', `user=${username}`)
})
})

it('works when setting cookie', () => {
cy.get('[data-cy="cross-origin-secondary-link"]').click()
cy.origin('http://foobar.com:3500', () => {
cy.document().then((doc) => {
doc.cookie = 'key=value'
})

cy.document().its('cookie').should('equal', 'key=value')
})
})

it('works when setting cookie with extra, benign parts', () => {
cy.get('[data-cy="cross-origin-secondary-link"]').click()
cy.origin('http://foobar.com:3500', () => {
cy.document().then((doc) => {
doc.cookie = 'key=value; wont=beset'
})

cy.document().its('cookie').should('equal', 'key=value')
})
})

it('cookie properties are preserved when set via automation', () => {
cy.get('[data-cy="cross-origin-secondary-link"]').click()
cy.origin('http://foobar.com:3500', () => {
cy.document().then((doc) => {
doc.cookie = 'key=value; SameSite=Strict; Path=/foo'
})

cy.getCookie('key').then((cookie) => {
expect(Cypress._.omit(cookie, 'expiry')).to.deep.equal({
domain: '.foobar.com',
httpOnly: false,
name: 'key',
path: '/foo',
sameSite: 'strict',
secure: false,
value: 'value',
})
})
})
})

it('does not set cookie when invalid', () => {
cy.get('[data-cy="cross-origin-secondary-link"]').click()
cy.origin('http://foobar.com:3500', () => {
cy.document().then((doc) => {
doc.cookie = '=value'
})

cy.document().its('cookie').should('equal', '')
})
})

it('works when setting subsequent cookies', () => {
cy.get('[data-cy="cross-origin-secondary-link"]').click()
cy.origin('http://foobar.com:3500', () => {
cy.document().then((doc) => {
doc.cookie = 'key1=value1'
})

cy.document().its('cookie').should('equal', 'key1=value1')
cy.document().then((doc) => {
doc.cookie = 'key2=value2'
})

cy.document().its('cookie').should('equal', 'key2=value2; key1=value1')
})
})

it('makes cookie available to cy.getCookie()', () => {
cy.get('[data-cy="cross-origin-secondary-link"]').click()
cy.origin('http://foobar.com:3500', () => {
cy.document().then((doc) => {
doc.cookie = 'key=value'
})

cy.getCookie('key').its('value').should('equal', 'value')
})
})

it('no longer returns cookie after cy.clearCookie()', () => {
cy.get('[data-cy="cookie-login-land-on-idp"]').click()
cy.origin('http://foobar.com:3500', { args: { username } }, ({ username }) => {
cy.get('[data-cy="username"]').type(username)
cy.get('[data-cy="login"]').click()
})

cy.origin('http://idp.com:3500', () => {
cy.clearCookie('user')
cy.document().its('cookie').should('equal', '')
})
})

it('no longer returns cookie after cy.clearCookies()', () => {
cy.get('[data-cy="cookie-login-land-on-idp"]').click()
cy.origin('http://foobar.com:3500', { args: { username } }, ({ username }) => {
cy.get('[data-cy="username"]').type(username)
cy.get('[data-cy="login"]').click()
})

cy.origin('http://idp.com:3500', () => {
cy.clearCookies()
cy.document().its('cookie').should('equal', '')
})
})

it('works when setting cookie in addition to cookie that already exists from http request', () => {
cy.get('[data-cy="cookie-login-land-on-idp"]').click()
cy.origin('http://foobar.com:3500', { args: { username } }, ({ username }) => {
cy.get('[data-cy="username"]').type(username)
cy.get('[data-cy="login"]').click()
})

cy.origin('http://idp.com:3500', { args: { username } }, ({ username }) => {
cy.document().then((doc) => {
doc.cookie = 'key=value'
})

// order of the cookies differs depending on browser, so just
// ensure that each one is there
cy.document().its('cookie').should('include', 'key=value')
cy.document().its('cookie').should('include', `user=${username}`)
})
})
})
})
26 changes: 20 additions & 6 deletions packages/driver/cypress/fixtures/primary-origin.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,28 @@
<li><a data-cy="cookie-login-subdomain">Login with Social (subdomain)</a></li>
<li><a data-cy="cookie-login-alias">Login with Social (aliased localhost)</a></li>
<li><a data-cy="cookie-login-override">Login with Social (cookie override)</a></li>
<li><a data-cy="welcome" href="http://localhost:3500/welcome">Go to Welcome</a></li>
<li><a data-cy="cookie-login-land-on-idp">Login with Social (lands on idp)</a></li>
</ul>
<script>
document.querySelector('[data-cy=cookie-login]').href = `http://localhost:3500/prelogin?redirect=${encodeURIComponent('http://www.foobar.com:3500/fixtures/auth/cookie-login.html?redirect=http://localhost:3500/login')}`
document.querySelector('[data-cy=cookie-login-https]').href = `https://localhost:3502/prelogin?redirect=${encodeURIComponent('https://www.foobar.com:3502/fixtures/auth/cookie-login.html?redirect=https://localhost:3502/login')}`
document.querySelector('[data-cy=cookie-login-subdomain]').href = `http://localhost:3500/prelogin?redirect=${encodeURIComponent('http://www.foobar.com:3500/fixtures/auth/cookie-login.html?redirect=http://localhost:3500/login&subdomain=true')}`
document.querySelector('[data-cy=cookie-login-alias]').href = `http://localhost:3500/prelogin?redirect=${encodeURIComponent('http://www.foobar.com:3500/fixtures/auth/cookie-login.html?redirect=http://localhost:3500/login&alias=true')}`
document.querySelector('[data-cy=cookie-login-override]').href = `https://localhost:3502/prelogin?override=true&redirect=${encodeURIComponent('https://www.foobar.com:3502/fixtures/auth/cookie-login.html?redirect=https://localhost:3502/login')}`
function setHref (dataCy, redirect2, primaryQueryAddition = '') {
const isHttps = redirect2.startsWith('https')
const url = isHttps ? 'https://localhost:3502' : 'http://localhost:3500'
const redirect1Path = '/fixtures/auth/cookie-login.html'
const redirect = encodeURIComponent(isHttps
? `https://www.foobar.com:3502${redirect1Path}?redirect=${redirect2}`
: `http://www.foobar.com:3500${redirect1Path}?redirect=${redirect2}`)

document.querySelector(`[data-cy="${dataCy}"]`).href = (
`${url}/prelogin?redirect=${redirect}${primaryQueryAddition}`
)
}

setHref('cookie-login', 'http://localhost:3500/login')
setHref('cookie-login-https', 'https://localhost:3502/login')
setHref('cookie-login-subdomain', 'http://localhost:3500/login&subdomain=true')
setHref('cookie-login-alias', 'http://localhost:3500/login&alias=true')
setHref('cookie-login-override', 'https://localhost:3502/login', '&override=true')
setHref('cookie-login-land-on-idp', 'http://www.idp.com:3500/welcome')
</script>
</body>
</html>
6 changes: 6 additions & 0 deletions packages/driver/src/cypress/cookies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash'
import Cookies from 'js-cookie'
import { CookieJar, toughCookieToAutomationCookie } from '@packages/server/lib/util/cookies'

import $errUtils from './error_utils'

Expand Down Expand Up @@ -149,6 +150,11 @@ export const $Cookies = (namespace, domain) => {
return _.extend(defaults, obj)
},

parse (cookieString: string) {
return CookieJar.parse(cookieString)
},

toughCookieToAutomationCookie,
}

return API
Expand Down
2 changes: 1 addition & 1 deletion packages/proxy/lib/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import RequestMiddleware from './request-middleware'
import ResponseMiddleware from './response-middleware'
import { DeferredSourceMapCache } from '@packages/rewriter'
import type { RemoteStates } from '@packages/server/lib/remote_states'
import type { CookieJar } from '@packages/server/lib/cookie-jar'
import type { CookieJar } from '@packages/server/lib/util/cookies'
import type { Automation } from '@packages/server/lib/automation/automation'

function getRandomColorFn () {
Expand Down

4 comments on commit 5573fe5

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 5573fe5 Jun 30, 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.3.1/linux-x64/develop-5573fe50b09164ebb49039c0525ec535a36e9ee7/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 5573fe5 Jun 30, 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.3.1/linux-arm64/develop-5573fe50b09164ebb49039c0525ec535a36e9ee7/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 5573fe5 Jun 30, 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.3.1/darwin-arm64/develop-5573fe50b09164ebb49039c0525ec535a36e9ee7/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 5573fe5 Jun 30, 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 win32 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.3.1/win32-x64/develop-5573fe50b09164ebb49039c0525ec535a36e9ee7/cypress.tgz

Please sign in to comment.