G
YouTube TikTok Games
Skip to content

Set-Cookie header is set in response when valid cookies are sent in headers #52

Open

Description

@hieuk09

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.

I check RFC 6265 and it states that

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?

Activity

  1. ioquatix commented on Aug 7, 2025

    @ioquatix
    Member

    This sounds like a bug, we should probably compare the session value.

  2. hieuk09 commented on Aug 7, 2025

    @hieuk09
    Author

    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.

  3. ioquatix commented on Aug 7, 2025

    @ioquatix
    Member

    It seems okay to me.

  4. hieuk09 commented on Oct 1, 2025

    @hieuk09
    Author

    Hi @ioquatix, sorry for the late response, I've created a fix in #53, could you take a look?

  5. ioquatix commented on Oct 5, 2025

    @ioquatix
    Member

    Okay, I will take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions