Skip to content

cy.getCookies() Does Not Return Cookies Whose Domain Does Not Correspond with baseUrl #8956

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

Closed
todd-m-kemp opened this issue Oct 23, 2020 · 6 comments · Fixed by #25012
Closed
Assignees

Comments

@todd-m-kemp
Copy link

todd-m-kemp commented Oct 23, 2020

Current behavior

cy.getCookies() returns only the cookies whose domain matches that of baseUrl.

Desired behavior

cy.getCookies() should return all cookies, regardless of their domain.

Test code to reproduce

With the baseUrl set to "https://cypress.io", run the following test:

context('Cookies with Different Domains', function () {
  it('Test', function () {
    cy.setCookie('cookie1', 'value 1', { domain: 'domain1.cypress.io' });
    cy.setCookie('cookie2', 'value 2', { domain: 'domain2.cypress.io' });
    cy.setCookie('cookie3', 'value 3', { domain: 'domain3.different.io' });
    cy.getCookies()
      .should('have.length', 3); // Fails; length is 2.
  });
});

cy.getCookies() returns only the cookies whose domain ends in cypress.io. The cookie ending in different.io is missing.

If baseUrl is https://different.io instead, then only cookie3 is returned.

Versions

Cypress version 5.4.0, Chrome 86, macOS 10.15.6.

@cypress-bot cypress-bot bot added the stage: needs investigating Someone from Cypress needs to look at this label Oct 26, 2020
@johot
Copy link

johot commented Feb 22, 2021

I found the same problem for cy.getCookie(name) this occured for me because I was visiting my frontend on locahost but the backend was running in a local-test-env.com location.

Seems like a security issue almost. What if I want to test my code on localhost but with a backend running somewhere else? I can then easily get these kinds of problems.

@OmarSlame
Copy link

OmarSlame commented Aug 31, 2021

is there any workaround to get all existing cookies in all domains?

@aryzing
Copy link

aryzing commented Jan 1, 2022

Sorry for the bump, it's been a while. Any chance this bug can be addressed? Checking for cookies form other domains is critical to several areas, including security, analytics, and authentication. What's stopping this issue moving forward?

@rubencodes
Copy link

For everyone here looking for a work-around, it looks like this trick from the clearCookies API also works with getCookies.

Cypress.Commands.add("getAllCookies", () => {
	// Undocumented `domain` parameter, which seems to be the only way to
	// reliably get cross-origin cookies.
	// See: https://github.com/cypress-io/cypress/issues/781#issuecomment-783412186
	// @ts-ignore
	return cy.getCookies({ domain: null });
});

Would love a more "official" API/solution, but this worked for me.

@cypress-bot cypress-bot bot added stage: backlog and removed stage: needs investigating Someone from Cypress needs to look at this labels Apr 29, 2022
@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review stage: new issues and removed stage: backlog stage: needs review The PR code is done & tested, needs review labels Dec 6, 2022
@chrisbreiding chrisbreiding self-assigned this Dec 6, 2022
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 8, 2022

The code for this is done in cypress-io/cypress#25012, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 13, 2022

Released in 12.1.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v12.1.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Dec 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants