Skip to content

Instantly share code, notes, and snippets.

@dhh
Created March 5, 2012 19:22
Show Gist options
  • Save dhh/1980477 to your computer and use it in GitHub Desktop.
Save dhh/1980477 to your computer and use it in GitHub Desktop.
# 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