Skip to content

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

@GeoMarkou

Description

@GeoMarkou

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');


});

});

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions