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

XSRF / CSRF #41

Open
timmillwood opened this issue Oct 17, 2012 · 4 comments
Open

XSRF / CSRF #41

timmillwood opened this issue Oct 17, 2012 · 4 comments
Assignees

Comments

@timmillwood
Copy link

There seems to be little or no protection for XSRF / CSRF.

@timmillwood
Copy link
Author

Simple solution:

before do
  if request.post?
   if session[:csrf] != params[:csrf]
     halt 503
   end
  end

  time = Time.now.to_s
  @key = Digest::SHA1.hexdigest(time)
  session[:csrf] = @key
end

Then on all form views add:

<input type="hidden" name="csrf" value="<%= @key %>" />

@ghost ghost assigned cmhobbs Oct 22, 2012
@cmhobbs
Copy link
Collaborator

cmhobbs commented Oct 22, 2012

I should have some spare time next weekend. I'll take a look at this if @maxjustus doesn't nail it first.

@timmillwood
Copy link
Author

My "simple solution" can be added to any Sinatra app. I think if we're looking to alter the original, a better solution may be available.

@timmillwood
Copy link
Author

A better solution is to use rack/csrf

require 'rack/csrf'

use Rack::Csrf, :raise => true

Then in the views
<%= Rack::Csrf.csrf_tag(env) %>

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