Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ActionController::TestSession#id to return Rack::Session::SessionId instance #38063

Merged
merged 3 commits into from Jan 10, 2020
Merged

Fix ActionController::TestSession#id to return Rack::Session::SessionId instance #38063

merged 3 commits into from Jan 10, 2020

Conversation

abcang
Copy link
Contributor

@abcang abcang commented Dec 21, 2019

When testing with ActionController::TestCase, session.id returns a string value. With the update to rack v2.0.8, session.id now returns an instance of Rack::Session::SessionId. Therefore, fix session.id to return an instance of Rack::Session::SessionId when testing with ActionController::TestCase.

@rails-bot rails-bot bot added the actionpack label Dec 21, 2019
Copy link
Contributor

@bquorning bquorning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On 2a52a38#commitcomment-36521253, I commented exactly that ActionController::TestSession.initialize should be updated to use Rack::Session::SessionId for its @id assignment. Happy to see this PR 👍

@@ -176,12 +176,12 @@ class LiveTestResponse < Live::Response

# Methods #destroy and #load! are overridden to avoid calling methods on the
# @store object, which does not exist for the TestSession class.
class TestSession < Rack::Session::Abstract::SessionHash #:nodoc:
class TestSession < Rack::Session::Abstract::PersistedSecure::SecureSessionHash #:nodoc:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why the superclass has to change. Can you please explain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rack::Session::Abstract::PersistedSecure uses Rack::Session::Abstract::PersistedSecure::SecureSessionHash instead of Rack::Session::Abstract::SessionHash. Therefore, I changed the super class of TestSession to use SecureSessionHash as well.
The difference between SessionHash and SecureSessionHash is the behavior when accessing ["session_id"]. In the case of SecureSessionHash, the public_id of @id is returned.

https://github.com/rack/rack/blob/7fecaee81f59926b6e1913511c90650e76673b38/lib/rack/session/abstract/id.rb#L461-L463

@bquorning
Copy link
Contributor

Not relevant before merging, but: Don’t forget to backport this change to 5-2-stable too.

Co-Authored-By: Benjamin Quorning <22333+bquorning@users.noreply.github.com>
bquorning referenced this pull request Jan 7, 2020
The `ActionDispatch::Session::MemcacheStore` is still vulnerable
given it requires the gem dalli to be updated as well.

CVE-2019-16782

def test_session_id
session = ActionController::TestSession.new
assert_instance_of String, session.id.public_id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test session["session_id"]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added test for session["session_id"]

@rafaelfranca rafaelfranca merged commit b9fac5c into rails:master Jan 10, 2020
rafaelfranca pushed a commit that referenced this pull request Jan 10, 2020
…Id instance (#38063)

* Fix ActionController::TestSession#id to return Rack::Session::SessionId instance

* test SessionId#public_id

* test session["session_id"]

Co-authored-by: Benjamin Quorning <22333+bquorning@users.noreply.github.com>
rafaelfranca pushed a commit that referenced this pull request Jan 10, 2020
…Id instance (#38063)

* Fix ActionController::TestSession#id to return Rack::Session::SessionId instance

* test SessionId#public_id

* test session["session_id"]

Co-authored-by: Benjamin Quorning <22333+bquorning@users.noreply.github.com>
@abcang abcang deleted the test_session_id_to_session_id_instance branch January 11, 2020 04:26
@stanhu
Copy link
Contributor

stanhu commented Feb 12, 2020

Just ran into this in Rails 6.0.2.1. Can this be merged into 6-0-stable and released?

@rafaelfranca
Copy link
Member

It is in 6-0-stable.

DanPoliseno pushed a commit to DanPoliseno/g2 that referenced this pull request Sep 15, 2020
NOTE: The upgrade is backwards compatible with existing sessions, but
the upgrade of redis-rack v2.1.2 changed Redis keys from
`session:gitlab:<random hex value>` to `session:gitlab:2::<hash of hex
value>`. If a session does not have a key in the new schema, it will be
created transparently. The old session key will eventually be expired
automatically.

To upgrade to rack 2.0.9, we need to do the following:

1. Fix ActiveSession to use new Rack::Session::SessionId
2. Add a monkey patch for ActionController::TestSessionPatch

Controller tests were failing without the changes in
rails/rails#38063, which is available on the
Rails `6-0-stable` branch but not in Rails 6.0.2.2.

3. Remove CGI escaping of ActiveSession keys. This was not needed
because CGI escaping was already being done by Rails.

4. Fix deletion of Rack session keys with ActiveSession

redis-rack v2.1.2 changed the session key from one based on the public
ID to the private ID. We need to adapt ActiveSession to delete both
versions of the key to clear out old data and to make it work with the
redis-rack key name changes.
@robotfelix
Copy link
Contributor

@rafaelfranca You merged/backported this into the 5-2-stable branch on 10 Jan 2020, but for some reason it hasn't been included in any of the subsequent releases (5.2.4.2 to 5.2.4.5, with 5.2.4.2 having been released on 19 Mar 2020).

Do you know why this might be? (Apologies if I'm missing something about the parts of 5-2-stable that make it into releases)

For the avoidance of doubt

It is present here:

It is not present on any of:

@jonathanhefner
Copy link
Member

@robotfelix Versions like 5.2.4.x contain security fixes only. The change in question would be included in 5.2.5, if that is ever released. For more information, see the Rails Maintenance Policy.

If you need the change and cannot upgrade to Rails 6 at this time, you might consider pointing your app's rails gem to the 5-2-stable branch.

@robotfelix
Copy link
Contributor

robotfelix commented Feb 22, 2021

@jonathanhefner Thanks for the clarification. I've now seen the same policy being applied to 892eab7 and am happily sitting on 5-2-stable for the time being.

I've opened #41523 to try to help avoid other people running into the same problem in future, and #41524 to try making the Rails Maintenance Policy more explicit about this kind of bug-fix-to-security-patch scenario (I'm a little hesitant to call this a bug fix as that implies that leaving this change out of the security patch wasn't a conscious decision!)

luca-bassoricci pushed a commit to luca-bassoricci/gitlab that referenced this pull request Oct 11, 2022
NOTE: The upgrade is backwards compatible with existing sessions, but
the upgrade of redis-rack v2.1.2 changed Redis keys from
`session:gitlab:<random hex value>` to `session:gitlab:2::<hash of hex
value>`. If a session does not have a key in the new schema, it will be
created transparently. The old session key will eventually be expired
automatically.

To upgrade to rack 2.0.9, we need to do the following:

1. Fix ActiveSession to use new Rack::Session::SessionId
2. Add a monkey patch for ActionController::TestSessionPatch

Controller tests were failing without the changes in
rails/rails#38063, which is available on the
Rails `6-0-stable` branch but not in Rails 6.0.2.2.

3. Remove CGI escaping of ActiveSession keys. This was not needed
because CGI escaping was already being done by Rails.

4. Fix deletion of Rack session keys with ActiveSession

redis-rack v2.1.2 changed the session key from one based on the public
ID to the private ID. We need to adapt ActiveSession to delete both
versions of the key to clear out old data and to make it work with the
redis-rack key name changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants