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

Check for a valid encoding before using a public session id #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/action_dispatch/session/active_record_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def get_session_model(request, id)
end

def get_session_with_fallback(sid)
if sid && !self.class.private_session_id?(sid.public_id)
if sid && sid.public_id.valid_encoding? && !self.class.private_session_id?(sid.public_id)
if (secure_session = session_class.find_by_session_id(sid.private_id))
secure_session
elsif (insecure_session = session_class.find_by_session_id(sid.public_id))
Expand Down
16 changes: 16 additions & 0 deletions test/action_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,22 @@ def test_incoming_invalid_session_id_via_cookie_should_be_ignored
end
end

def test_incorrectly_encoded_session_id_via_cookie_should_be_ignored
with_test_route_set do
open_session do |sess|
incorrectly_encoded_id = "\xAA\xAA".force_encoding('UTF-8')
sess.cookies['_session_id'] = incorrectly_encoded_id
sess.get '/set_session_value'
new_session_id = sess.cookies['_session_id']
assert_not_equal incorrectly_encoded_id, new_session_id

sess.get '/get_session_value'
new_session_id_2 = sess.cookies['_session_id']
assert_equal new_session_id, new_session_id_2
end
end
end

def test_incoming_invalid_session_id_via_parameter_should_be_ignored
with_test_route_set(:cookie_only => false) do
open_session do |sess|
Expand Down
Loading