Fix session recovery after deletion#1040
Conversation
| end | ||
|
|
||
| [create_sid_with_empty_session(dc), {}] | ||
| update_session_persisted_data(req, { id: sid }) |
There was a problem hiding this comment.
For now we forward through the env only persisted session id, but if/when that approach will be accepted I tend to come back and extend session_persisted_data just a bit:
- calculate the checksum of persisted session once fetched from the store, and check it on saving. This way when session is not changed (which is the case in 99%) we can just refresh the session TTL (
dc.touch) but not update the store, effectively dodging the update race condition. cascan be fetched for persisted session and checked on update (dc.set_cas), introducing the optimistic lock strategy over the session store and effectively dodging any kind of race conditions.
|
@danmayer any thoughts on it? ) |
|
yeah the approach seems OK. I do not actively use dalli sessions so I have limited exposure to all the use cases / edge cases. Do you actively use this in a project? Mostly curious as we are looking to deprecate some of the less used features of dalli and possibly remove some things like the binary protocol in favor of the meta protocol, so gathering active feedback from the community is helpful. also, take a look at the failing tests |
|
ahh I think all the failures are due to rubocop issues |
Yeah, memcaced is our session store for a long time. And due to specifics of our project the given race condition is not theoretical but very practical for us ) For now we fixed it by a sort of monkey patch, then reported the advisory into rack upstream. My hope was to think up a way to fix that on rack level considering that any other
Will check shorty, np ) |
8da32d6 to
f3c4286
Compare
|
@danmayer pushed ) |
There was a problem hiding this comment.
Pull Request Overview
This PR fixes the session recovery issue after deletion, ensuring that a previously valid session id cannot be reused after logout.
- Introduces a new test in test/test_rack_session.rb to verify the session id change.
- Updates session handling in lib/rack/session/dalli.rb to raise an error when a stale session is updated.
- Updates the CHANGELOG.md accordingly.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/test_rack_session.rb | Adds new tests to validate that session ids are not reused post‑logout. |
| lib/rack/session/dalli.rb | Adjusts session lookup and write logic to properly handle missing sessions and raises errors as needed. |
| CHANGELOG.md | Documents the session recovery fix. |
Comments suppressed due to low confidence (1)
test/test_rack_session.rb:389
- The variable 'session_match' is referenced without a definition, which may lead to a runtime error during test execution. Consider defining 'session_match' or explicitly extracting the session id from the cookie string.
refute_equal login_cookie[session_match], logout_cookie[session_match]
|
@danmayer should I do something with head ruby failing builds? ) Seems like the fails are not relevant ) |
|
These head failures aren't related. I will try to get ruby head to reproduce locally... I believe there was some changes in Ruby on how it loads and sets up CGI as I have seen similar fixes going into yjit / Ruby so it might get sorted or we might need to bump rack for Ruby head. Anyways, this looks good to me. You good with me merging @petergoldstein |
petergoldstein
left a comment
There was a problem hiding this comment.
This looks good. Feel free to merge
It fixes the session recovery after deletion, the same security advisories were published recently within
rackrepos:Rack session middleware prepares the session at the beginning of request, then saves is back to the store with possible changes applied by host rack application. This way the session becomes to be a subject of race conditions in general sense over concurrent rack requests.
In particular the following scenario is possible:
Steps to reproduce: