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

How to expire the csrf token? #12

Open
funkyboy opened this issue Feb 3, 2016 · 5 comments
Open

How to expire the csrf token? #12

funkyboy opened this issue Feb 3, 2016 · 5 comments

Comments

@funkyboy
Copy link

funkyboy commented Feb 3, 2016

I was wondering, is there a way to tweak the expiration of the csrf token?
Or even better, generate a new random one for each new request?

@funkyboy
Copy link
Author

funkyboy commented Feb 3, 2016

I am trying

before do
  env['rack.session']['csrf.token'] = SecureRandom.urlsafe_base64(32)
end

It works but I am not sure it's the best way and how it impacts on the performance.

@baldowl
Copy link
Owner

baldowl commented Feb 3, 2016

I think it's better if in an after filter you remove the key from session; something like (untested):

after do
  env['rack.session'].delete(Rack::Csrf.key)
end

@funkyboy
Copy link
Author

funkyboy commented Feb 3, 2016

Ok, but if I remove it, will a new random one be generated and injected?

@baldowl
Copy link
Owner

baldowl commented Feb 3, 2016

Nvm, scrap it: it would remove the token before Rack::Csrf had a chance to look at it.

@funkyboy
Copy link
Author

funkyboy commented Feb 3, 2016

So far the "before approach" seems to work. It's tricky in the case of Ajax calls that use themetatag so I went with:

  before do
    unless request.xhr?
      env['rack.session']['csrf.token'] = SecureRandom.urlsafe_base64(32)
    end 
  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

2 participants