Skip to content

Conversation

@Shaeli
Copy link

@Shaeli Shaeli commented Sep 8, 2025

Hello,

I'm working on upgrading an old codebase that relies on the legacy session cookie format (with the --$HMAC suffix).

I noticed that after https://github.com/rack/rack/pull/1177/files (PR is not merged in rack/rack, but the code was merged manually in this repository based on this comment), the verification of old session cookies in this format is still supported when legacy options are set but new session cookies are no longer created with the --$HMAC suffix, even if legacy_generate_hmac and legacy_hmac_secret are provided.

The code comments suggest that backward compatibility should be maintained:

When a :secret option was supplied, the integrity of the
encoded data was protected with HMAC-SHA1. This functionality is still
supported using a set of a legacy options.

However, the code that previously appended the HMAC to new cookies appears to have been removed:

        if @secrets.first
          session_data << "--#{generate_hmac(session_data, @secrets.first)}"
        end

I couldn't find any other place where the --$HMAC suffix is added for new cookies.

Would you consider restoring this behavior when legacy_hmac_secret is set?

I'm happy to update / add tests if this is acceptable, but I wanted to get an opinion first as I'm not familiar with this codebase and could be wrong here :)

Thanks a lot !

I could be wrong, but I think that https://github.com/rack/rack/pull/1177/files is not backward compatible.
It still allows to verify old sessions cookies  with the `--$HMAC` format when using a set of a legacy options, but it doesn't allow to create cookies sessions with the old `--$HMAC` format, even with `legacy_generate_hmac` and `legacy_hmac_secret` are set.

Comment in the code mentions this is backward compatible with the correct options, but still

```
        if @Secrets.first
          session_data << "--#{generate_hmac(session_data, @Secrets.first)}"
        end
```

is removed and I don't see any other place where the `--$HMAC` could be set. This is adding it back.
@rafaelfranca
Copy link

Should we add a test for this case?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants