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

Set cookie domain prefixed with a dot #1896

Closed
LeungMichael opened this issue Jun 7, 2018 · 11 comments
Closed

Set cookie domain prefixed with a dot #1896

LeungMichael opened this issue Jun 7, 2018 · 11 comments
Labels
stage: wontfix Cypress does not regard this as an issue or will not implement this feature topic: cookies 🍪

Comments

@LeungMichael
Copy link

LeungMichael commented Jun 7, 2018

Current behavior:

The problem I am having is that the website I am testing is hosted on www.something.com. The website is using cookies to avoid showing a popup multiple times. The cookie the website adds has a domain "www.something.com". I want to set this cookie when I start my test run so I don't get the popup.

When I use setCookie and pass it a domain "www.something.com", it actually adds a dot in the domain (.www.something.com). This causes the website to ignore this cookie and adds a duplicate cookie, without the dot before the domain.

Can I somehow force the setCookie to not alter my domain value?

Desired behavior:

The cookie domain to not be prefixed with a dot.

Steps to reproduce:

cy.setCookie('myCookie', 'true', { domain : 'www.something.com' })
cy.getCookie('myCookie').should('have.property', 'domain', 'www.something.com')

the assertion fails with ".www.something.com"

Versions

3.0.1
Windows 10
Chrome

@km333
Copy link

km333 commented Dec 15, 2018

A couple of additional observations:

  1. This also causes js-cookie cookies.remove('my-cookie') to silently not delete the cookie which is causing havoc in my tests (user does not get logged out, etc.)

  2. localhost does not seem to be affected by this problem

@jennifer-shehane
Copy link
Member

jennifer-shehane commented Jul 23, 2019

This is still an issue in Cypress 3.4.0.

Reproducible test

it('test 1', () => {
  cy.visit('https://example.cypress.io')
  cy.setCookie('myCookie', 'true', { domain: 'example.cypress.io' })
  cy.getCookie('myCookie').should('have.property', 'domain', 'example.cypress.io')
})

Screen Shot 2019-07-23 at 9 55 53 PM

I also narrowed down that this is an issue with the setCookie and not the getCookie, since you can see that the actual cookie domain has the . prefix set in it.

Screen Shot 2019-07-26 at 3 02 24 PM

@cypress-bot cypress-bot bot added stage: ready for work The issue is reproducible and in scope and removed stage: needs investigating Someone from Cypress needs to look at this labels Jul 23, 2019
@jennifer-shehane
Copy link
Member

jennifer-shehane commented Jul 26, 2019

After further investigation - this is exactly how cookies work and are implemented within user agents. If you run this within the console - a . is prefixed to the domain that is set.

document.cookie = "test=Hello;domain=example.cypress.io"

Then go to the Application tab of your devtools, you will see the following cookie set, with the domain being .example.cypress.io

Screen Shot 2019-07-26 at 3 59 45 PM copy

Cypress is simply passing along the domain information to the document.cookie as intended. This is not a bug, but exactly what happens within the browser itself.

Notably, as part of the cookies spec, leading . are ignored. https://tools.ietf.org/html/rfc6265#section-4.1.2.3

@jennifer-shehane jennifer-shehane added stage: wontfix Cypress does not regard this as an issue or will not implement this feature and removed stage: ready for work The issue is reproducible and in scope type: bug labels Jul 26, 2019
@chenfan-fchen
Copy link

same here, how do you resolved this?

@emil14
Copy link

emil14 commented Mar 26, 2021

Then go to the Application tab of your devtools, you will see the following cookie set, with the domain being .example.cypress.io

I don't see anything

@xumepadismal
Copy link
Contributor

Hi @jennifer-shehane! Although this is indeed native browsers behavior there IS a possibility to set dot-less cookie by not specifying domain at all:

document.cookie = "test=Hello"

And this is what we can meet in real applications. By using cy.setCookie() we can't mimic this behavior and this can lead to this:

cookie-domain

First cookie was set by PHP itself while second one by Cypress. One of those cookies is definitely outdated and wrong and you never know which one will be used by application.
Furthermore, real applications can have some technical subdomains which can also have their cookies set without leading dot.

Would it be possible to add some extra option to SetCookieOptions? Something like this:

cy.setCookie('test', 'value', {domain: 'example.com', domainExact: true})
cy.setCookie('test', 'value', {domain: 'api.example.com', domainExact: true})
cy.setCookie('test', 'value', {domain: 'static.example.com', domainExact: true})

And last but not least. I noticed that if my baseUrl is e.g. "docs.cypress.io" and I'm trying to cy.setCookie('test', 'value'); (without specifying domain) then cookie will be set to ".cypress.io" which is weird and not match browser behavior

image

@titchimoto
Copy link

Any updates on this issue? Having my tests fail in CI due to this prefixed . and it not setting the exact hostname.

@itsik-avidan
Copy link

Encountered this behavior as well.
We have cookies been set with the expectation to be available on sub-domains and defined using the 'leading-dot' while relying on RFC-6265
It seems that electron is failing to support this while major browsers does

@Songyu-Wang
Copy link
Contributor

@xumepadismal I totally agree with what you said above. However, since this is a closed issue, I don't know if Cypress team will see this and act on it. Could you just open a new issue for this to grab their attention?

I think the main point in this issue is that Cypress set domain with a dot, but the issue we are concerning is that Cypress cannot accommodate dotless application domain which happens in real world

OR

cc @jennifer-shehane and see if she can re-evaluate the issue

@xumepadismal
Copy link
Contributor

@Songyu-Wang There is already an open issue #16856 to track this

@Songyu-Wang
Copy link
Contributor

Ah thank you @xumepadismal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: wontfix Cypress does not regard this as an issue or will not implement this feature topic: cookies 🍪
Projects
None yet
Development

No branches or pull requests

9 participants