A
A
authoraytee2021-03-22 12:17:43
Ruby on Rails
authoraytee, 2021-03-22 12:17:43

How to solve CSRF issue when connecting Sidekiq, Rails?

I need to add a sidekiq web interface project to Rails, but it says I have a problem with CSRF.

Here is what he writes:

Sidekiq::Web needs a valid Rack session for CSRF protection. If this is a Rails app,
    make sure you mount Sidekiq::Web *inside* your application routes:

    Rails.application.routes.draw do
    mount Sidekiq::Web => "/sidekiq"
    ....
    end

    If this is a bare Rack app, use a session middleware before Sidekiq::Web:
    # first, use IRB to create a shared secret key for sessions and commit it
    require 'securerandom'; File.open(".session.key", "w") {|f| f.write(SecureRandom.hex(32)) }
    # now use the secret with a session cookie middleware
    use Rack::Session::Cookie, secret: File.read(".session.key"), same_site: true, max_age: 86400
    run Sidekiq::Web


And below highlights:



def session(env)
    env["rack.session"] || fail(<<~EOM)
      Sidekiq::Web needs a valid Rack session for CSRF protection. If this is a Rails app,
      make sure you mount Sidekiq::Web *inside* your application routes:


Here is my routes.rb :

require 'sidekiq'
require 'sidekiq/web'
    
Rails.application.routes.draw do
  mount Sidekiq::Web => "/sidekiq"
    
  namespace 'api' do
    namespace 'v1' do
      resources :diaries
      resources :notes
    end
  end
end


It is strange that he tells me to prescribe , despite the fact that I have already written it mount Sidekiq::Web => "/sidekiq"

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question