Rails ActiveRecord store and new session

Bardt

I am new to Rails and experience a strange issue I don't understand. I use ActiveRecord as a session store and need to add session id as a property of JSON responses for all the requests. I use Devise as well if it have some impact on the situation. The problem is that if a request is made by a user without cookies (or at least without session id in the cookie) the session.id is empty or - attention, please - not the same value that is set in the response cookie.

For debugging, I add this code as an after_filter to ApplicationController:

puts session.id
puts request.session_options[:id]

Both values are the same. They match the value in the cookie if it is present. Otherwise, if session id is not present in the cookie, the cookie set after that request has different value.

My opinion is that session_id gets new value after it is actually saved to the database, where it have to be unique. DB migration:

def change
  create_table :sessions do |t|
    t.string :session_id, :null => false
    t.text :data
    t.timestamps
  end

  add_index :sessions, :session_id, :unique => true
  add_index :sessions, :updated_at
end

My question: How can I get the actual session.id value of a new session before the first response is rendered?

UPD:

I just created a new Rails app that uses ActiveRecord session store without Devise, and I can get session.id that is going to be set in cookie just before response with this code id application controller:

class ApplicationController < ActionController::Base
  after_filter :show_session

  def show_session
    puts session.id
  end
end

But in my existing app with Devise I get a value that really looks like a session id, but that doesn't match the value set in the cookie via Set-Cookie response header and the value actually saved to sessions table in database. Looks like Devise have a conflict with ActiveRecord session store in some way. Need to go deeper to figure it out.

UPD 2

Looks like I found the problem roots. As I said, I use Devise for authorization with Omniauth. According to the documentation, sign_in method resets session id for security reasons. But after that reset session.id returns the old value, that had been automatically set. I use this code as an Omniauth callback:

def facebook_access_token
  sign_in @user
  puts session.id
end

And in console I get session id different from the one set in the Set-Cookie response header. If I comment "sign_in" line, these values match. New question: how can I get the new session id value after it is been reset inside of sign_in method? Is it an internal Warden/Devise implementation or something?

Bardt

The problem I experienced was caused by default Warden configuration. It renewed session id, but somehow the new id was not accessible via session.id.

The only way I found to stop this behavior was putting this code into config/initializers/devise.rb:

Warden::Manager.after_set_user do |user,auth,opts|
  auth.env["rack.session.options"][:renew] = false
end

Probably this method is not really good for security reasons, but I have no other ideas in a week of searching and reading sources.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Rails 5 API Omniauth use ActiveRecord Session Store

How to find a session record in the database using the cookie value in an Rails app using activerecord activerecord-session_store

updating to rails 6, but "activerecord-session_store (~> 2.0) was resolved to 2.0.0, which depends on actionpack (>= 5.2.4.1)"

Rails: activerecord update to new association

Rails session data - Store in a hash

Rails: How to store data in session?

Store two child records for a resource - ActiveRecord | Rails

Copy an object to a new database in Rails ActiveRecord

how to set new variable to activerecord object in rails

Why is session store initializer removed in Rails 5.1.1

Rails session store stopped working correctly

Rails app with different session store for each model

Rails: How to access session parameter / ActiveRecord::StatementInvalid in Orders#create

Rails ActiveAdmin link_to new model inside different - ActiveRecord::AssociationTypeMismatch

Rails and Devise Gem: Avoiding store session for certain controller and action

Error when use session_store in Rails 4

Rails session store to persist search params between requests

How to hit 'create' instead of 'new' for session in Rails auth pipeline?

How do you open and close a new database connection with ActiveRecord Rails 5

Rails: How do I filter a ActiveRecord collection result without making new db queries?

Determine if ActiveRecord Object is New

Rails Error: ActiveRecord::RecordNotFound

Rails ActiveRecord summary statement

Rails virtual attribute with ActiveRecord

Group Query in Rails ActiveRecord

Rails: ActiveRecord::AssociationTypeMismatch error

Rails Activerecord not returning values

ActiveRecord query with Rails 4

Rails ActiveRecord Join Confusion