Created
March 5, 2012 19:22
-
-
Save dhh/1980477 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/views/posts/new.html.erb | |
<%= form_for(Post.new) do |form| %> | |
Title: <%= form.text_field :title %> | |
Body: <%= form.text_field :body %> | |
<% end %> | |
...would produce a signature field that can be used to automatically untaint: | |
<input type="hidden" name="signature" value="...."> | |
# controller | |
class PostsController | |
def create | |
# This will blow up if it contains anything but title + body | |
# because it'll then be marked as tainted | |
Post.create(params[:post]) | |
rescue ActiveRecord::TaintedAttributes | |
# This would happen if you tried to submit post[published]=1 as | |
# part of the request | |
end | |
def update | |
# This will work because #permit would untaint the hash | |
Post.find(params[:id]).update_attributes( | |
params[:post].permit(:title, :body) | |
) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment