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

Sidekiq web messes up user sessions #3377

Closed
heaven opened this issue Mar 9, 2017 · 8 comments
Closed

Sidekiq web messes up user sessions #3377

heaven opened this issue Mar 9, 2017 · 8 comments

Comments

@heaven
Copy link

heaven commented Mar 9, 2017

Ruby version: 2.3.1
Sidekiq / Pro / Enterprise version(s): 4.2.9

Our app is running on a subdomain, like "sub.domain.com". We have sessions configured to be shared across all subdomains:

Carmen::Application.config.session_store :active_record_store,
  key: "_wego#{"_#{Rails.env}" unless Rails.env.production?}_session", :domain => :all

After accessing the app a session is created and stored for ".domain.com". But after accessing "/sidekiq" another session appears in cookies for "sub.domain.com", which breaks the app, log out and a few other things.

@badosu
Copy link
Contributor

badosu commented Mar 9, 2017

Hello @heaven,

When mounting the Sidekiq::Web middleware, could you try setting up the Session options manually?

E.G.

app = Sidekiq::Web.new

app.use Rack::Session::Cookie, options

See this page on how to configure it. Do not forget to pass app instead of Sidekiq::Web to mount.

PS: It seems that it's possible to pass the session options with: Sidekiq::Web.set :sessions, opts, maintaining your mounting code, @mperham might have more info on this.

@heaven
Copy link
Author

heaven commented Mar 9, 2017

I can but it anyway then runs Sidekiq::Web#build_sessions which inserts ::Rack::Session::Cookie to middlewares array.

@badosu
Copy link
Contributor

badosu commented Mar 9, 2017

@heaven This is weird, as build_sessions checks if there's an already mounted middleware or the sessions option as seen here.

Would you mind posting your code that mounts the middleware?

@heaven
Copy link
Author

heaven commented Mar 9, 2017

It does, but it also performs a few other actions that I don't completely understand so I have to copy a decent part of this method. Is there a way to copy application settings inside build_sessions?

@badosu
Copy link
Contributor

badosu commented Mar 9, 2017

@heaven I can't know the context of your question without seeing the changes you are performing.

There should be no reason you'd need to inspect how Sidekiq::Web is mouting it's internal middlewares unless you require a specific use case.

The only two middlewares loaded by default are Rack::Protection and Rack::Session::Cookie. This is what build_sessions does, and only loaded if you did not load them previously with your own settings via Sidekiq::Web.use Middleware, opts.

You could try using the Sidekiq::Web.set :sessions, opts method as well.

Don't forget that these options should be supplied before mounting the Sidekiq::Web middleware or else they won't have any effect.

@heaven
Copy link
Author

heaven commented Mar 9, 2017

@badosu this worked, thanks:

Sidekiq::Web.set :sessions, { domain: ".domain.com" }

Was wondering if this could be detected in Sidekiq, from the :domain option that I pass in session_store.rb

@badosu
Copy link
Contributor

badosu commented Mar 9, 2017

@heaven Glad it worked!

AFAIK Sidekiq Web is totally agnostic of the framework you're using as long as it's a Rack application.

@badosu badosu closed this as completed Mar 9, 2017
@dbalatero
Copy link

dbalatero commented Apr 13, 2018

@heaven I found another solution to this, in case anyone else is Googling:

config/initializers/session_store.rb

require 'sidekiq/web'

Rails.application.config.session_store :active_record_store,
  key: '_my_session_key'

# Turn off Sinatra's sessions, which overwrite the main Rails app's session
# after the first request
Sidekiq::Web.disable(:sessions)

lib/admin_constraint.rb

class AdminConstraint
  def matches?(request)
    user = request.env['warden'].user(:user)
    user && user.admin?
  end
end

config/routes.rb

constraints AdminConstraint.new do
  mount Sidekiq::Web => '/admin/sidekiq'
end

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

No branches or pull requests

3 participants