Changeset 7596
- Timestamp:
- 09/23/07 18:14:44 (2 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/base.rb (modified) (1 diff)
- trunk/actionpack/lib/action_controller/request_forgery_protection.rb (modified) (6 diffs)
- trunk/actionpack/lib/action_view/helpers/form_tag_helper.rb (modified) (1 diff)
- trunk/actionpack/lib/action_view/helpers/prototype_helper.rb (modified) (1 diff)
- trunk/actionpack/lib/action_view/helpers/url_helper.rb (modified) (1 diff)
- trunk/actionpack/test/controller/request_forgery_protection_test.rb (modified) (8 diffs)
- trunk/actionpack/test/template/text_helper_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r7592 r7596 1 1 *SVN* 2 3 * Rename some RequestForgeryProtection methods. The class method is now #protect_from_forgery, and the default parameter is now 'authenticity_token'. [Rick] 2 4 3 5 * Merge csrf_killer plugin into rails. Adds RequestForgeryProtection model that verifies session-specific _tokens for non-GET requests. [Rick] trunk/actionpack/lib/action_controller/base.rb
r7592 r7596 328 328 cattr_accessor :resource_action_separator 329 329 330 # Sets the token parameter name for RequestForgery. Calling # verify_token sets it to :_token by default330 # Sets the token parameter name for RequestForgery. Calling #protect_from_forgery sets it to :authenticity_token by default 331 331 @@request_forgery_protection_token = nil 332 332 cattr_accessor :request_forgery_protection_token trunk/actionpack/lib/action_controller/request_forgery_protection.rb
r7592 r7596 2 2 class InvalidToken < ActionControllerError; end 3 3 4 # Protect a controller's actions with the # verify_tokenmethod. Failure to validate will result in a ActionController::InvalidToken4 # Protect a controller's actions with the #protect_from_forgery method. Failure to validate will result in a ActionController::InvalidToken 5 5 # exception. Customize the error message through the use of rescue_templates and rescue_action_in_public. 6 6 # 7 7 # class FooController < ApplicationController 8 8 # # uses the cookie session store 9 # verify_token:except => :index9 # protect_from_forgery :except => :index 10 10 # 11 11 # # uses one of the other session stores that uses a session_id value. 12 # verify_token:secret => 'my-little-pony', :except => :index12 # protect_from_forgery :secret => 'my-little-pony', :except => :index 13 13 # end 14 14 # … … 16 16 # 17 17 # * <tt>:only/:except</tt> - passed to the before_filter call. Set which actions are verified. 18 # * <tt>:secret</tt> - Custom salt used to generate the form_ token. Leave this off if you are using the cookie session store.18 # * <tt>:secret</tt> - Custom salt used to generate the form_authenticity_token. Leave this off if you are using the cookie session store. 19 19 # * <tt>:digest</tt> - Message digest used for hashing. Defaults to 'SHA1' 20 20 module RequestForgeryProtection 21 21 def self.included(base) 22 22 base.class_eval do 23 class_inheritable_accessor : verify_token_options24 self. verify_token_options = {}25 helper_method :form_ token23 class_inheritable_accessor :request_forgery_protection_options 24 self.request_forgery_protection_options = {} 25 helper_method :form_authenticity_token 26 26 end 27 27 base.extend(ClassMethods) … … 29 29 30 30 module ClassMethods 31 def verify_token(options = {})32 self.request_forgery_protection_token ||= : _token33 before_filter :verify_ request_token, :only => options.delete(:only), :except => options.delete(:except)34 verify_token_options.update(options)31 def protect_from_forgery(options = {}) 32 self.request_forgery_protection_token ||= :authenticity_token 33 before_filter :verify_authenticity_token, :only => options.delete(:only), :except => options.delete(:except) 34 request_forgery_protection_options.update(options) 35 35 end 36 36 end … … 38 38 protected 39 39 # The actual before_filter that is used. Modify this to change how you handle unverified requests. 40 def verify_ request_token40 def verify_authenticity_token 41 41 verified_request? || raise(ActionController::InvalidToken) 42 42 end … … 46 46 # * is the format restricted? By default, only HTML and AJAX requests are checked. 47 47 # * is it a GET request? Gets should be safe and idempotent 48 # * Does the form_ token match the given _token value from the params?48 # * Does the form_authenticity_token match the given _token value from the params? 49 49 def verified_request? 50 request_forgery_protection_token.nil? || request.method == :get || !verifiable_request_format? || form_token == params[request_forgery_protection_token] 50 request_forgery_protection_token.nil? || 51 request.method == :get || 52 !verifiable_request_format? || 53 form_authenticity_token == params[request_forgery_protection_token] 51 54 end 52 55 … … 56 59 57 60 # Sets the token value for the current session. Pass a :secret option in #verify_token to add a custom salt to the hash. 58 def form_token 59 @form_token ||= verify_token_options[:secret] ? token_from_session_id : token_from_cookie_session 61 def form_authenticity_token 62 @form_authenticity_token ||= if request_forgery_protection_options[:secret] 63 authenticity_token_from_session_id 64 else 65 authenticity_token_from_cookie_session 66 end 60 67 end 61 68 62 69 # Generates a unique digest using the session_id and the CSRF secret. 63 def token_from_session_id 64 key = verify_token_options[:secret].respond_to?(:call) ? verify_token_options[:secret].call(@session) : verify_token_options[:secret] 65 digest = verify_token_options[:digest] || 'SHA1' 70 def authenticity_token_from_session_id 71 key = if request_forgery_protection_options[:secret].respond_to?(:call) 72 request_forgery_protection_options[:secret].call(@session) 73 else 74 request_forgery_protection_options[:secret] 75 end 76 digest = request_forgery_protection_options[:digest] ||= 'SHA1' 66 77 OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new(digest), key.to_s, session.session_id.to_s) 67 78 end 68 79 69 80 # No secret was given, so assume this is a cookie session store. 70 def token_from_cookie_session81 def authenticity_token_from_cookie_session 71 82 session[:csrf_id] ||= CGI::Session.generate_unique_id 72 83 session.dbman.generate_digest(session[:csrf_id]) trunk/actionpack/lib/action_view/helpers/form_tag_helper.rb
r7592 r7596 425 425 '' 426 426 else 427 tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_ token)427 tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token) 428 428 end 429 429 end trunk/actionpack/lib/action_view/helpers/prototype_helper.rb
r7592 r7596 746 746 js_options['parameters'] = "'" 747 747 end 748 js_options['parameters'] << " _token=' + encodeURIComponent('#{escape_javascript form_token}')"748 js_options['parameters'] << "#{request_forgery_protection_token}=' + encodeURIComponent('#{escape_javascript form_authenticity_token}')" 749 749 end 750 750 trunk/actionpack/lib/action_view/helpers/url_helper.rb
r7592 r7596 475 475 if request_forgery_protection_token 476 476 submit_function << "var s = document.createElement('input'); s.setAttribute('type', 'hidden'); " 477 submit_function << "s.setAttribute('name', ' _token'); s.setAttribute('value', '#{escape_javascript form_token}'); f.appendChild(s);"477 submit_function << "s.setAttribute('name', '#{request_forgery_protection_token}'); s.setAttribute('value', '#{escape_javascript form_authenticity_token}'); f.appendChild(s);" 478 478 end 479 479 submit_function << "f.submit();" trunk/actionpack/test/controller/request_forgery_protection_test.rb
r7592 r7596 6 6 7 7 class RequestForgeryProtectionController < ActionController::Base 8 verify_token:only => :index, :secret => 'abc'8 protect_from_forgery :only => :index, :secret => 'abc' 9 9 10 10 def index … … 28 28 end 29 29 @token = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('SHA1'), 'abc', '123') 30 ActionController::Base.request_forgery_protection_token = : _token30 ActionController::Base.request_forgery_protection_token = :authenticity_token 31 31 end 32 32 … … 37 37 def test_should_render_form_with_token_tag 38 38 get :index 39 assert_select 'form>div>input[name=?][value=?]', ' _token', @token39 assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token 40 40 end 41 41 … … 76 76 77 77 def test_should_allow_post_with_token 78 post :index, : _token => @token78 post :index, :authenticity_token => @token 79 79 assert_response :success 80 80 end 81 81 82 82 def test_should_allow_put_with_token 83 put :index, : _token => @token83 put :index, :authenticity_token => @token 84 84 assert_response :success 85 85 end 86 86 87 87 def test_should_allow_delete_with_token 88 delete :index, : _token => @token88 delete :index, :authenticity_token => @token 89 89 assert_response :success 90 90 end … … 108 108 # no token is given, assume the cookie store is used 109 109 class CsrfCookieMonsterController < ActionController::Base 110 verify_token:only => :index110 protect_from_forgery :only => :index 111 111 112 112 def index … … 138 138 end 139 139 @token = Digest::SHA1.hexdigest("secure") 140 ActionController::Base.request_forgery_protection_token = : _token140 ActionController::Base.request_forgery_protection_token = :authenticity_token 141 141 end 142 142 … … 147 147 def test_should_render_form_with_token_tag 148 148 get :index 149 assert_select 'form>div>input[name=?][value=?]', ' _token', @token149 assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token 150 150 end 151 151 … … 186 186 187 187 def test_should_allow_post_with_token 188 post :index, : _token => @token188 post :index, :authenticity_token => @token 189 189 assert_response :success 190 190 end 191 191 192 192 def test_should_allow_put_with_token 193 put :index, : _token => @token193 put :index, :authenticity_token => @token 194 194 assert_response :success 195 195 end 196 196 197 197 def test_should_allow_delete_with_token 198 delete :index, : _token => @token198 delete :index, :authenticity_token => @token 199 199 assert_response :success 200 200 end trunk/actionpack/test/template/text_helper_test.rb
r7589 r7596 290 290 end 291 291 292 ActionView:: Base.sanitized_allowed_tags.each do |tag_name|292 ActionView::Helpers::TextHelper.sanitized_allowed_tags.each do |tag_name| 293 293 define_method "test_should_allow_#{tag_name}_tag" do 294 294 assert_sanitized "start <#{tag_name} title=\"1\" onclick=\"foo\">foo <bad>bar</bad> baz</#{tag_name}> end", %(start <#{tag_name} title="1">foo bar baz</#{tag_name}> end) … … 552 552 assert_equal((expected || text), sanitize(text)) 553 553 end 554 555 # pull in configuration values from ActionView::Base556 [:sanitized_protocol_separator, :sanitized_protocol_attributes, :sanitized_bad_tags, :sanitized_allowed_tags, :sanitized_allowed_attributes, :sanitized_allowed_protocols, :sanitized_allowed_css_properties, :sanitized_allowed_css_keywords, :sanitized_shorthand_css_properties, :sanitized_uri_attributes].each do |attr|557 define_method attr do558 ActionView::Base.send(attr)559 end560 end561 554 end