You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Set-Cookie header is set in response when valid cookies are sent in headers #52
Currently, when setting up a rack app using Cookie session, I encounter the following behavior:
Step 1: I send the first request without any cookies. I receive a response with Set-Cookie header.
Step 2: I send the second request with Cookie header equal to the above Set-Cookie header
In Rack 2.x: Set-Cookie is not set in the response In Rack 3.x: Set-Cookie header is set
I checked and saw that in lib/rack/session/abstract/id.rb, set_cookie method compares new cookie value with existing one to decide whether to set Set-Cookie header. However, in lib/rack/session/cookie.rb, when encode_session_data, the encryptor injects a random IV, so cookie value is always different, even though the session value is the same.
If a server sends multiple responses containing Set-Cookie headers concurrently to the user agent (e.g., when communicating with the user agent over multiple sockets), these responses create a "race condition" that can lead to unpredictable behavior.
Can I confirm whether it's expected that Rack 3.x changes the behavior here?
I see, thanks for the info. In this case, I think one solution could be setting session_id to env['rack.session_id'] when fetching it for the first time (set it to nil if cannot find anything), after that, compares it with the session_id from request.session.id to decide.
How does that sound to you? If that's ok, I can create a PR to fix.
Activity
ioquatix commentedon Aug 7, 2025
This sounds like a bug, we should probably compare the session value.
hieuk09 commentedon Aug 7, 2025
I see, thanks for the info. In this case, I think one solution could be setting
session_idtoenv['rack.session_id']when fetching it for the first time (set it tonilif cannot find anything), after that, compares it with thesession_idfromrequest.session.idto decide.How does that sound to you? If that's ok, I can create a PR to fix.
ioquatix commentedon Aug 7, 2025
It seems okay to me.
hieuk09 commentedon Oct 1, 2025
Hi @ioquatix, sorry for the late response, I've created a fix in #53, could you take a look?
ioquatix commentedon Oct 5, 2025
Okay, I will take a look.