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 document.cookie patch #23643

Merged
merged 44 commits into from
Oct 18, 2022

Conversation

chrisbreiding
Copy link
Contributor

@chrisbreiding chrisbreiding commented Aug 31, 2022

User facing changelog

  • Fixed an issue where, in certain circumstances, document.cookie would not reflect the correct value in cross-origin tests

Additional details

Previously, the document.cookie patch would have to wait ~250ms to be up-to-date with cookies stored in the browser because it polls automation for them. This could result in document.cookie not being accurate if a page read its value on page load.

This fixes the issue by updating the document.cookie patch's value before the page loads by sending the cookies down from the proxy when the response is intercepted. This makes the correct value available on page load.

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 Aug 31, 2022

Thanks for taking the time to open a PR!

@cypress
Copy link

cypress bot commented Sep 7, 2022



Test summary

4123 0 350 0Flakiness 0


Run details

Project cypress
Status Passed
Commit bb2e37e
Started Oct 18, 2022 3:54 PM
Ended Oct 18, 2022 4:06 PM
Duration 11:55 💡
OS Linux Debian - 11.4
Browser Electron 106

View run in Cypress Dashboard ➡️


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

@chrisbreiding chrisbreiding marked this pull request as ready for review September 7, 2022 18:44
@chrisbreiding chrisbreiding changed the title fix: Improve document.cookie patch (wip) fix: Improve document.cookie patch Sep 7, 2022
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.

any idea if this fixes the issues inside the cookie_behavior tests with trying to get/set cookies inside cy.origin using the cypress api?

I think we might need to change the patch a little bit to work with cookies of the same key, but different domains/paths (which are apparently more common than I would have thought), but I think there is something we can try that I suggested below!

packages/driver/src/cross-origin/patches/cookies.js Outdated Show resolved Hide resolved
packages/driver/src/cross-origin/patches/cookies.js Outdated Show resolved Hide resolved
packages/driver/src/cross-origin/patches/cookies.js Outdated Show resolved Hide resolved
packages/driver/src/cross-origin/patches/cookies.js Outdated Show resolved Hide resolved
})

Cypress.on('clear:cookies', () => {
reset()
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we still need to do this if we store the reference to get:cookies, or just clear the reference out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is to optimistically clear out the cookies when a user calls cy.clearCookies() so there's no delay/race condition waiting to sync with the automation cookies. There's actually no longer any polling to sync with automation cookies. It just caused issues and now all cases of cookies changing in automation are taken care of in a more deterministic fashion

packages/proxy/lib/http/response-middleware.ts Outdated Show resolved Hide resolved
@chrisbreiding
Copy link
Contributor Author

Going to move this back into draft until after #23297 is merged, since it's going to need to be heavily refactored after that.

@chrisbreiding chrisbreiding marked this pull request as draft September 9, 2022 13:54
cookieJar.removeAllCookies()
}

const bindCypressListeners = (Cypress: Cypress.Cypress) => {
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to listen for cookies changes even before a cypress instance attaches?

for example, would the following test work? (I haven't run this so it probably doesn't work without modifications)

it('might need to listen without a cypress instance', () => {
  visit('localhost')
  visit('http://www.foobar.com:3500')
  cy.setCookie('foo', 'bar', {domain: foobar.com})
  cy.origin('foobar.com:3500', () => {
    cy.window((win) => {
      expect(win.document.cookies).to.eq('foo=bar')
    }
  })
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently that fails with The command was expected to run against origin http://localhost:3500 but the application is at origin http://www.foobar.com:3500. I think it makes sense that the user should put it inside the origin callback in that case and then the listeners will be set up as well.

Copy link
Member

Choose a reason for hiding this comment

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

I think we may want to revisit throwing that error:

The command was expected to run against origin http://localhost:3500 but the application is at origin http://www.foobar.com:3500

I added the limitation that cy.cookies could only be run when the aut was same-site as top, I don't think thats correct anymore since we will be allowing cy.cookies to be set on any domain (i think it can be done now?) and we should probably think about changing this behaviour in #24264

Comment on lines +44 to +47
// We monkey-patch document.cookie when in a cross-origin injection, because
// document.cookie runs into cross-origin restrictions when the AUT is on
// a different origin than top. The goal is to make it act like it would
// if the user's app was run in top.
Copy link
Member

Choose a reason for hiding this comment

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

In the case outside of cy.origin where we are same-site but cross-origin, would we want to apply this patch?
We may need to check on this for #24275

@AtofStryker

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 to keep in mind. I'll defer to Bill on that one.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe so, at least for hostname specific cookies

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.

Do we know if this fixes some of cookie assertions in cookie_behavior.cy.ts? I think most of those are fixed with #24264?

httpOnly: false,
maxAge: null,
path: null,
sameSite: 'lax',
Copy link
Contributor

Choose a reason for hiding this comment

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

I think so. Does this get updated to the known values once the automation cookies sync in?

@chrisbreiding
Copy link
Contributor Author

Do we know if this fixes some of cookie assertions in cookie_behavior.cy.ts? I think most of those are fixed with #24264?

I don't think so because this only fixes timing issues with document.cookie, not with cookies being available via automation. To fix those issues, we probably need to move Firefox to use CDP for cookies and ensure they're set before an cookie-setting requests are allowed to proceed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix issue with the timing of setting cookies in the browser
3 participants