question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HttpOnly cookie is not getting set on the handshake request in Jest

See original GitHub issue

Describe the bug I am using the React socket.io-client package to connect to a socket.io websocket. The authentication of the entire application is based on httpOnly cookies (i.e. these cookies can not be accessed through clientside Javascript).

When running the app in developement, everything works as expected: the socket client sets the httpOnly cookie on the handshake request and the server authenticates this.

But when running the Jest test suite, the httpOnly cookie no longer gets set on the handshake.

Note that when making http requests (using fetch) in Jest, the httpOnly cookie DOES get set as expected. So for whatever reason, the socket.io-client is not setting the httpOnly cookie on the handshake request in Jest…

Any help or suggestions would be greatly appreciated! Thank you

Socket.IO server version: ^3.1.0 Socket.IO client version: ^3.1.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
peeycommented, Apr 2, 2021

I believe this happens because we don’t get access to a response object in the middleware adapter

const wrap = middleware => (socket, next) => middleware(socket.request, {}, next)

The withCredentials only configures CORS to allow cookies to be sent by the client to the server. If these cookies have already been set by some HTTP route then these cookies are sent.

However if the cookies haven’t been set by an HTTP route, then socket.io is invoked without a cookie. The middleware does its job - it starts a new session and sets a cookie. But this is ignored by socket.io and the changes in response headers made by middleware are not propagated to response of the handshake request.

Currently, I see no way of customizing socket.io handshake behaviour (e.g. adding custom headers to be sent in the response), so I see this as a bug. (This used to be possible with some hacks in older versions of socket.io)

1reaction
marnixhohcommented, Apr 30, 2021

To my knowledge, it still hasn’t… I guess most people don’t actually test frontends with a real socket server (/API). However, I actually do prefer to write my client-side tests this way. But for all my socket related things, I had to mock those responses as a way to work around this issue - unfortunately…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Socket.io: HttpOnly cookie is not getting set on the handshake ...
I am using the React socket.io-client package to connect to a socket.io websocket. The authentication of the entire application is based on ...
Read more >
https client and cookie httponly is not being set react axios
But when running the Jest test suite, the httpOnly cookie no longer gets set on the handshake. Note that when making http requests...
Read more >
How to deal with cookies - Socket.IO
When using the cookie option, the server will send a cookie upon handshake (the first HTTP request of the session), with the value...
Read more >
What is an HttpOnly Cookie? - Knowledge Base - CookiePro
An HttpOnly Cookie is a tag added to a browser cookie that prevents client-side scripts from accessing data.
Read more >
The Transport Layer Security (TLS) Protocol Version 1.3
Internet Engineering Task Force (IETF) E. Rescorla Request for Comments: 8446 ... used by the secure channel are produced by the TLS handshake...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found