From 16ee611fad37b6b271088eda4cdbe3d6be088af1 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 14 Mar 2012 19:03:39 -0400 Subject: Do not include the authenticity token in forms where remote: true as ajax forms use the meta-tag value --- actionpack/CHANGELOG.md | 2 ++ actionpack/lib/action_view/helpers/form_tag_helper.rb | 11 +++++++++-- actionpack/test/controller/request_forgery_protection_test.rb | 11 +++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 2a98556b71..465d19e50d 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,7 @@ ## Rails 3.2.3 (unreleased) ## +* Do not include the authenticity token in forms where remote: true as ajax forms use the meta-tag value *DHH* + * Turn off verbose mode of rack-cache, we still have X-Rack-Cache to check that info. Closes #5245. *Santiago Pastorino* diff --git a/actionpack/lib/action_view/helpers/form_tag_helper.rb b/actionpack/lib/action_view/helpers/form_tag_helper.rb index e3ad96ec1b..74b6034c8d 100644 --- a/actionpack/lib/action_view/helpers/form_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/form_tag_helper.rb @@ -609,8 +609,15 @@ module ActionView # responsibility of the caller to escape all the values. html_options["action"] = url_for(url_for_options) html_options["accept-charset"] = "UTF-8" - html_options["data-remote"] = true if html_options.delete("remote") - html_options["authenticity_token"] = html_options.delete("authenticity_token") if html_options.has_key?("authenticity_token") + + if html_options.delete("remote") + html_options["data-remote"] = true + + # The authenticity token is taken from the meta tag in this case + html_options["authenticity_token"] = false + else + html_options["authenticity_token"] = html_options.delete("authenticity_token") if html_options.has_key?("authenticity_token") + end end end diff --git a/actionpack/test/controller/request_forgery_protection_test.rb b/actionpack/test/controller/request_forgery_protection_test.rb index fd5a41a0bb..f4e7336834 100644 --- a/actionpack/test/controller/request_forgery_protection_test.rb +++ b/actionpack/test/controller/request_forgery_protection_test.rb @@ -37,6 +37,10 @@ module RequestForgeryProtectionActions render :inline => "<%= form_for(:some_resource, :authenticity_token => false ) {} %>" end + def form_for_remote + render :inline => "<%= form_for(:some_resource, :remote => true ) {} %>" + end + def rescue_action(e) raise e end end @@ -103,6 +107,13 @@ module RequestForgeryProtectionTests assert_select 'form>div>input[name=?][value=?]', 'custom_authenticity_token', @token end + def test_should_render_form_without_token_tag_if_remote + assert_not_blocked do + get :form_for_remote + end + assert_no_match /authenticity_token/, response.body + end + def test_should_allow_get assert_not_blocked { get :index } end -- cgit v1.2.3