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

cy.setCookie fails when there's a __Host- prefix #8261

Closed
GeoMarkou opened this issue Aug 12, 2020 · 5 comments · Fixed by #8526
Closed

cy.setCookie fails when there's a __Host- prefix #8261

GeoMarkou opened this issue Aug 12, 2020 · 5 comments · Fixed by #8526
Assignees

Comments

@GeoMarkou
Copy link

GeoMarkou commented Aug 12, 2020

Current behavior:

Test fails when there's a __Host- prefix in the cookie name. I've attached the error log.
error.log

cypress_runner.js:198473 Command:   setCookie
cypress_runner.js:198473 Error:     CypressError: `cy.setCookie()` had an unexpected error setting the requested cookie in Chrome.

> Error: Network.setCookie failed to set cookie: {"domain":"localhost","path":"/","secure":false,"httpOnly":false,"expires":2228385617,"name":"__Host-Username","value":"example"}

    at http://localhost/__cypress/runner/cypress_runner.js:154898:17
From previous event:
    at Context.setCookie (http://localhost/__cypress/runner/cypress_runner.js:155074:18)
From Your Spec Code:
    at Context.eval (http://localhost/__cypress/tests?p=cypress/integration/Authentication/Login.ts:10:12)

From Node.js Internals:
  Error: Network.setCookie failed to set cookie: {"domain":"localhost","path":"/","secure":false,"httpOnly":false,"expires":2228385617,"name":"__Host-Username","value":"example"}
      at /home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/lib/browsers/cdp_automation.js:134:31
      at Chrome._handleMessage (/home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/node_modules/chrome-remote-interface/lib/chrome.js:258:17)
      at WebSocket.<anonymous> (/home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/node_modules/chrome-remote-interface/lib/chrome.js:234:22)
      at WebSocket.emit (events.js:210:5)
      at Receiver.receiverOnMessage (/home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/node_modules/ws/lib/websocket.js:797:20)
      at Receiver.emit (events.js:210:5)
      at Receiver.dataMessage (/home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/node_modules/ws/lib/receiver.js:437:14)
      at /home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/node_modules/ws/lib/receiver.js:394:23
      at /home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/node_modules/ws/lib/permessage-deflate.js:306:9
      at /home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/node_modules/ws/lib/permessage-deflate.js:385:7
      at afterWrite (_stream_writable.js:501:3)
      at onwrite (_stream_writable.js:488:7)
      at InflateRaw.afterTransform (_stream_transform.js:96:3)
      at Zlib.processCallback (zlib.js:571:8)
  From previous event:
      at $Cypress.automation (http://localhost/__cypress/runner/cypress_runner.js:164614:14)
      at automate (http://localhost/__cypress/runner/cypress_runner.js:154848:22)
      at automateCookies (http://localhost/__cypress/runner/cypress_runner.js:154862:12)
      at Context.setCookie (http://localhost/__cypress/runner/cypress_runner.js:155070:14)
      at http://localhost/__cypress/runner/cypress_runner.js:167603:15
  From previous event:
      at runCommand (http://localhost/__cypress/runner/cypress_runner.js:167582:8)
      at next (http://localhost/__cypress/runner/cypress_runner.js:167728:14)
      at http://localhost/__cypress/runner/cypress_runner.js:167756:16
  From previous event:
      at next (http://localhost/__cypress/runner/cypress_runner.js:167728:34)
  From previous event:
      at http://localhost/__cypress/runner/cypress_runner.js:167769:37
  From previous event:
      at run (http://localhost/__cypress/runner/cypress_runner.js:167762:19)
      at $Cy.cy.<computed> [as setCookie] (http://localhost/__cypress/runner/cypress_runner.js:168214:11)
      at Context.runnable.fn (http://localhost/__cypress/runner/cypress_runner.js:168439:21)
      at callFn (http://localhost/__cypress/runner/cypress_runner.js:103425:21)
      at Test.../driver/node_modules/mocha/lib/runnable.js.Runnable.run (http://localhost/__cypress/runner/cypress_runner.js:103412:7)
      at http://localhost/__cypress/runner/cypress_runner.js:173679:28
  From previous event:
      at Object.onRunnableRun (http://localhost/__cypress/runner/cypress_runner.js:173667:20)
      at $Cypress.action (http://localhost/__cypress/runner/cypress_runner.js:164430:61)
      at Test.Runnable.run (http://localhost/__cypress/runner/cypress_runner.js:172280:13)
      at Runner.../driver/node_modules/mocha/lib/runner.js.Runner.runTest (http://localhost/__cypress/runner/cypress_runner.js:104084:10)
      at http://localhost/__cypress/runner/cypress_runner.js:104210:12
      at next (http://localhost/__cypress/runner/cypress_runner.js:103993:14)
      at http://localhost/__cypress/runner/cypress_runner.js:104003:7
      at next (http://localhost/__cypress/runner/cypress_runner.js:103905:14)
      at http://localhost/__cypress/runner/cypress_runner.js:103971:5
      at timeslice (http://localhost/__cypress/runner/cypress_runner.js:97897:27)
cypress_runner.js:198473 Snapshot:  The snapshot is missing. Displaying current state of the DOM.
cypress_runner.js:198478 CypressError: `cy.setCookie()` had an unexpected error setting the requested cookie in Chrome.

> Error: Network.setCookie failed to set cookie: {"domain":"localhost","path":"/","secure":false,"httpOnly":false,"expires":2228385617,"name":"__Host-Username","value":"example"}

    at http://localhost/__cypress/runner/cypress_runner.js:154898:17
From previous event:
    at Context.setCookie (http://localhost/__cypress/runner/cypress_runner.js:155074:18)
From Your Spec Code:
    at Context.eval (http://localhost/__cypress/tests?p=cypress/integration/Authentication/Login.ts:10:12)

From Node.js Internals:
  Error: Network.setCookie failed to set cookie: {"domain":"localhost","path":"/","secure":false,"httpOnly":false,"expires":2228385617,"name":"__Host-Username","value":"example"}
      at /home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/lib/browsers/cdp_automation.js:134:31
      at Chrome._handleMessage (/home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/node_modules/chrome-remote-interface/lib/chrome.js:258:17)
      at WebSocket.<anonymous> (/home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/node_modules/chrome-remote-interface/lib/chrome.js:234:22)
      at WebSocket.emit (events.js:210:5)
      at Receiver.receiverOnMessage (/home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/node_modules/ws/lib/websocket.js:797:20)
      at Receiver.emit (events.js:210:5)
      at Receiver.dataMessage (/home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/node_modules/ws/lib/receiver.js:437:14)
      at /home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/node_modules/ws/lib/receiver.js:394:23
      at /home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/node_modules/ws/lib/permessage-deflate.js:306:9
      at /home/george/.cache/Cypress/4.12.1/Cypress/resources/app/packages/server/node_modules/ws/lib/permessage-deflate.js:385:7
      at afterWrite (_stream_writable.js:501:3)
      at onwrite (_stream_writable.js:488:7)
      at InflateRaw.afterTransform (_stream_transform.js:96:3)
      at Zlib.processCallback (zlib.js:571:8)
  From previous event:
      at $Cypress.automation (http://localhost/__cypress/runner/cypress_runner.js:164614:14)
      at automate (http://localhost/__cypress/runner/cypress_runner.js:154848:22)
      at automateCookies (http://localhost/__cypress/runner/cypress_runner.js:154862:12)
      at Context.setCookie (http://localhost/__cypress/runner/cypress_runner.js:155070:14)
      at http://localhost/__cypress/runner/cypress_runner.js:167603:15
  From previous event:
      at runCommand (http://localhost/__cypress/runner/cypress_runner.js:167582:8)
      at next (http://localhost/__cypress/runner/cypress_runner.js:167728:14)
      at http://localhost/__cypress/runner/cypress_runner.js:167756:16
  From previous event:
      at next (http://localhost/__cypress/runner/cypress_runner.js:167728:34)
  From previous event:
      at http://localhost/__cypress/runner/cypress_runner.js:167769:37
  From previous event:
      at run (http://localhost/__cypress/runner/cypress_runner.js:167762:19)
      at $Cy.cy.<computed> [as setCookie] (http://localhost/__cypress/runner/cypress_runner.js:168214:11)
      at Context.runnable.fn (http://localhost/__cypress/runner/cypress_runner.js:168439:21)
      at callFn (http://localhost/__cypress/runner/cypress_runner.js:103425:21)
      at Test.../driver/node_modules/mocha/lib/runnable.js.Runnable.run (http://localhost/__cypress/runner/cypress_runner.js:103412:7)
      at http://localhost/__cypress/runner/cypress_runner.js:173679:28
  From previous event:
      at Object.onRunnableRun (http://localhost/__cypress/runner/cypress_runner.js:173667:20)
      at $Cypress.action (http://localhost/__cypress/runner/cypress_runner.js:164430:61)
      at Test.Runnable.run (http://localhost/__cypress/runner/cypress_runner.js:172280:13)
      at Runner.../driver/node_modules/mocha/lib/runner.js.Runner.runTest (http://localhost/__cypress/runner/cypress_runner.js:104084:10)
      at http://localhost/__cypress/runner/cypress_runner.js:104210:12
      at next (http://localhost/__cypress/runner/cypress_runner.js:103993:14)
      at http://localhost/__cypress/runner/cypress_runner.js:104003:7
      at next (http://localhost/__cypress/runner/cypress_runner.js:103905:14)
      at http://localhost/__cypress/runner/cypress_runner.js:103971:5
      at timeslice (http://localhost/__cypress/runner/cypress_runner.js:97897:27)
logError @ cypress_runner.js:198478
(anonymous) @ cypress_runner.js:198119
emit @ cypress_runner.js:51546
(anonymous) @ cypress_runner.js:182360
emit @ cypress_runner.js:51546
emit @ cypress_runner.js:182403
onPrint @ cypress_runner.js:181224
_onPrintClick @ cypress_runner.js:181229
(anonymous) @ cypress_runner.js:182614
executeAction @ cypress_runner.js:49403
n @ cypress_runner.js:49403
ca @ cypress_runner.js:59069
ja @ cypress_runner.js:59070
ka @ cypress_runner.js:59070
wa @ cypress_runner.js:59072
Aa @ cypress_runner.js:59073
ya @ cypress_runner.js:59073
Da @ cypress_runner.js:59076
Ad @ cypress_runner.js:59139
Gi @ cypress_runner.js:59305
Kb @ cypress_runner.js:59094
Dd @ cypress_runner.js:59141
(anonymous) @ cypress_runner.js:59306
../../node_modules/scheduler/cjs/scheduler.production.min.js.exports.unstable_runWithPriority @ cypress_runner.js:63453
Ii @ cypress_runner.js:59306
Cd @ cypress_runner.js:59140Adding the host prefix is valid and recommended for secure cookies. We use them in our app for features that involve authentication.From MDN:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie> Cookies with names starting with __Host- must be set with the secure flag, must be from a secure page (HTTPS), must not have a domain specified (and therefore aren't sent to subdomains) and the path must be /.### Desired behavior:Cookie gets set and test passes.### Test code to reproduce```typescript

describe('Login', () => {
it('Tries to log in', () => {
// Works
cy.setCookie('Username', 'example');


// Fails
cy.setCookie('__Host-Username', 'example');


});

});

@GeoMarkou
Copy link
Author

For anyone else with this issue, I found a temporary workaround using JavaScript to set the cookie.

// TODO do this properly once they fix __Host prefix bug
document.cookie = `__Host-Username=${Cypress.env('username')};secure;path=/`;

@jennifer-shehane
Copy link
Member

As stated in the MDN docs:

Cookies with names starting with __Host- must be set with the secure flag

You'll need to pass secure: true in the options for cy.setCookie(). See cy.setCookie() options.

it('set cookie', () => {
  // Works
  cy.setCookie('Username', 'example')

  // Fails
  cy.setCookie('__Host-Username', 'example')
})

it('set cookie with secure flag', () => {
  // Works
  cy.setCookie('__Host-Username', 'example', {
    secure: true
  })
})

Screen Shot 2020-08-14 at 2 06 17 PM

@kuceb
Copy link
Contributor

kuceb commented Sep 8, 2020

I've noticed Chrome will error when a cookie starts with __Host- and also has the domain property. The solution is to make sure you set the cookie with the proper url and remove domain.

We should strip the domain property if we're setting a cookie that starts with __Host-

Firefox doesn't have this issue

@cypress-bot cypress-bot bot added the stage: needs investigating Someone from Cypress needs to look at this label Sep 8, 2020
@cypress-bot cypress-bot bot added stage: work in progress and removed stage: needs investigating Someone from Cypress needs to look at this labels Sep 8, 2020
@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: work in progress labels Sep 9, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Sep 28, 2020

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

@cypress-bot cypress-bot bot removed the stage: needs review The PR code is done & tested, needs review label Sep 28, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Sep 29, 2020

Released in 5.3.0.

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

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Sep 29, 2020
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.

3 participants