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

fix: Improve cross-origin cookie handling #22320

Merged
merged 35 commits into from
Jun 23, 2022

Conversation

chrisbreiding
Copy link
Contributor

User facing changelog

  • Fixed various issues with cookies when testing across multiple origins

Additional details

Previously, we worked around handling cross-origin cookie limitations by forcing all cookies to be SameSite=None. This introduces a new approach where all cookies are captured in a server-side cookie jar and, where needed, cookies are attached to cross-origin requests to simulate what the browser would do if the AUT were in the top frame.

PR Tasks

  • Have tests been added/updated?
  • Has the original issue (or this PR, if no issue exists) been tagged with a release in ZenHub? (user-facing changes only)
  • N/A Has a PR for user-facing changes been opened in cypress-documentation?
  • N/A Have API changes been updated in the type definitions?

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jun 14, 2022

Thanks for taking the time to open a PR!

packages/driver/src/cy/commands/sessions/manager.ts Outdated Show resolved Hide resolved
@@ -4,6 +4,7 @@ import { Cookies } from './cookies'
import { Screenshot } from './screenshot'
import type { BrowserPreRequest } from '@packages/proxy'
import type { AutomationMiddleware, OnRequestEvent } from '@packages/types'
import { removeAllCookies, removeCookie } from '../cookie-jar'
Copy link
Member

Choose a reason for hiding this comment

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

without lookin into this deeply, why not merge the cookie jar and the cookies class? the names make it sound like one or the other could leverage each other and/or they could be merged?

Seems like the Cookies class might be able to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They serve different purposes.

lib/cookie-jar is a stateful wrapper around tough-cookie's cookie implementation and stores cookies in memory for the sake of cross-origin testing.

lib/automation/cookies is a stateless wrapper around automating cookies. It's more about taking actions with cookies than storing them.

I don't really see anything that can be shared between them and I think it makes sense to keep their concerns separate.

Copy link
Member

Choose a reason for hiding this comment

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

hmm okay. Since the names are so similar, thoughts on updating both files to add a quick blur that describes their intent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Added in ab6e987 (#22320)

packages/proxy/lib/http/response-middleware.ts Outdated Show resolved Hide resolved
}

removeAllCookies () {
this._cookieJar.removeAllCookiesSync()
Copy link
Member

Choose a reason for hiding this comment

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

Does this need to be sync?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't need to, but there's no reason for it not to be sync. The cookie store is kept in memory, so all this ends up doing is basically cookieJar.cookies = {}. There's no sync file system access to worry about or anything.

@cypress
Copy link

cypress bot commented Jun 14, 2022



Test summary

37681 0 456 0Flakiness 4


Run details

Project cypress
Status Passed
Commit bae7061
Started Jun 23, 2022 2:08 PM
Ended Jun 23, 2022 2:26 PM
Duration 17:40 💡
OS Linux Debian - 10.11
Browser Multiple

View run in Cypress Dashboard ➡️


Flakiness

commands/actions/click.cy.js Flakiness
1 ... > scroll-behavior > can scroll to and click elements in html with scroll-behavior: smooth
cypress/proxy-logging.cy.ts Flakiness
1 Proxy Logging > request logging > xhr log has response body/status code when xhr response is logged second
2 Proxy Logging > request logging > xhr log has response body/status code when xhr response is logged second
create-react-app.cy.ts Flakiness
1 Working with cra-ejected > should live-reload on src changes

This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

packages/proxy/lib/http/util/cookies.ts Show resolved Hide resolved
async capturePreviousCookies () {
// this plays a part in adding cross-origin cookies to the browser via
// automation. if the request doesn't need cross-origin handling, this
// is a nooop
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// is a nooop
// is a noop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just wanted to emphasize that this does nooothing in that situation 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 52b6874 (#22320).

async getAddedCookies () {
// this plays a part in adding cross-origin cookies to the browser via
// automation. if the request doesn't need cross-origin handling, this
// is a nooop
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// is a nooop
// is a noop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 52b6874 (#22320).

@@ -474,6 +476,8 @@ export class SocketBase {
return this.localBus.emit('cross:origin:release:html')
case 'cross:origin:finished':
return this.localBus.emit('cross:origin:finished', args[0])
case 'url:changed':
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this related to cookies or something that was missed previously?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, this is vestigial from a previous implementation. I just missed removing it. Addressed in 3c558b5 (#22320).

Copy link
Contributor

@AtofStryker AtofStryker left a comment

Choose a reason for hiding this comment

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

Obligatory Cookie Monster GIF

packages/proxy/lib/http/request-middleware.ts Show resolved Hide resolved
@@ -393,99 +393,101 @@ const MaybePreventCaching: ResponseMiddleware = function () {
this.next()
}

const determineIfNeedsCrossOriginHandling = (ctx: HttpMiddlewareThis<ResponseMiddlewareProps>) => {
const previousAUTRequestUrl = ctx.getPreviousAUTRequestUrl()
const checkNeedsCrossOriginHandling = (ctx: HttpMiddlewareThis<ResponseMiddlewareProps>) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const checkNeedsCrossOriginHandling = (ctx: HttpMiddlewareThis<ResponseMiddlewareProps>) => {
const checkIfNeedsCrossOriginHandling = (ctx: HttpMiddlewareThis<ResponseMiddlewareProps>) => {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 95124a3 (#22320)

packages/proxy/package.json Outdated Show resolved Hide resolved
packages/driver/cypress.config.ts Outdated Show resolved Hide resolved
packages/driver/cypress/e2e/e2e/origin/cookie_login.cy.ts Outdated Show resolved Hide resolved
packages/server/lib/automation/cookies.ts Outdated Show resolved Hide resolved
packages/server/lib/automation/cookies.ts Outdated Show resolved Hide resolved
packages/server/lib/browsers/cdp_automation.ts Outdated Show resolved Hide resolved
packages/server/lib/server-base.ts Outdated Show resolved Hide resolved
packages/proxy/lib/http/util/cookies.ts Show resolved Hide resolved
@@ -300,6 +300,11 @@ export class CdpAutomation {
return this.getCookie(data)
})

case 'add:cookies':
setCookie = data.map((cookie) => normalizeSetCookieProps(cookie)) as Protocol.Network.SetCookieRequest[]
Copy link
Contributor

Choose a reason for hiding this comment

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

Wonder if there is additional benefit to specifying the setCookie type to show it might be a single SetCookieRequest or an array of them on line 278, and then the consuming code just casts it, though I don't think this has a lot of benefit from a typings perspective.

let setCookie: Protocol.Network.SetCookieRequest | Protocol.Network.SetCookieRequest[]

@chrisbreiding chrisbreiding merged commit a21c942 into develop Jun 23, 2022
@chrisbreiding chrisbreiding deleted the issue-20685-improve-cookie-handling branch June 23, 2022 15:00
tgriesser added a commit that referenced this pull request Jun 24, 2022
…esser/CLOUD-577-spec-list-display-latest-runs-batching

* muaz/CLOUD-577-spec-list-display-latest-runs:
  fix: Update "Request Access" button state after requesting access (ACI) (#22499)
  feat: Support "Queued" latest run status (#22497)
  fix: remove ctx.cloud.reset in tests, handle infinite loop in stale results (#22483)
  chore: add reporter webpack to gulp watch script (#22386)
  fix: Increase timeout for npm-webpack-dev-server tests (#22489)
  fix: Time out unmatched prerequests in proxy to avoid leaking memory (#22462)
  fix: Sort results in findCrossOriginLogs test helper to deterministic (#22481)
  fix: memory leak caused by storing base64 encoded files recieved by CDP `Network.requestWillBeSent` (#22460)
  fix: Improve cross-origin cookie handling (#22320)
  feat: Add button to clear value from search fields (#22202)
  chore: Add test to verify settings panels are collapsed by default (#22382)
  fix: process_profiler follow up work for v10 (#22363)
  chore: Update Chrome (stable) to 103.0.5060.53 (#22441)
  refactor: use design system windicss config (#21503)
  chore: update readme logo (#22433)
  chore: Update Chrome (beta) to 103.0.5060.53 (#22351)
  chore: updating version (#22432)
  Trigger Build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants