public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Instead of overriding html_types, base the verification on 
browser_generated_types.

Also Deprecate the old unverifiable types.

[#1145 state:committed]
NZKoz (author)
Thu Nov 13 02:19:53 -0800 2008
commit  f1ad8b48aae3ee26613b3e77bc0056e120096846
tree    6df93a9c456ee4bcb91ca33d57957ae7d838d4f1
parent  00c46b5eeb858629ef1c7ab50f022aecccca42c3
...
19
20
21
22
 
23
24
25
 
 
 
 
 
 
 
26
27
 
 
 
 
28
29
30
...
170
171
172
173
 
174
175
176
177
178
179
 
 
 
 
180
181
182
...
19
20
21
 
22
23
24
 
25
26
27
28
29
30
31
32
 
33
34
35
36
37
38
39
...
179
180
181
 
182
183
184
185
186
187
188
189
190
191
192
193
194
195
0
@@ -19,12 +19,21 @@ module Mime
0
   #     end
0
   #   end
0
   class Type
0
-    @@html_types = Set.new [:html, :url_encoded_form, :multipart_form, :all]
0
+    @@html_types = Set.new [:html, :all]
0
     cattr_reader :html_types
0
 
0
-    # UNUSED, deprecate?
0
+    # These are the content types which browsers can generate without using ajax, flash, etc
0
+    # i.e. following a link, getting an image or posting a form.  CSRF protection
0
+    # only needs to protect against these types.
0
+    @@browser_generated_types = Set.new [:html, :url_encoded_form, :multipart_form]
0
+    cattr_reader :browser_generated_types
0
+
0
+
0
     @@unverifiable_types = Set.new [:text, :json, :csv, :xml, :rss, :atom, :yaml]
0
-    cattr_reader :unverifiable_types
0
+    def self.unverifiable_types
0
+      ActiveSupport::Deprecation.warn("unverifiable_types is deprecated and has no effect", caller)
0
+      @@unverifiable_types
0
+    end
0
 
0
     # A simple helper class used in parsing the accept header
0
     class AcceptItem #:nodoc:
0
@@ -170,13 +179,17 @@ module Mime
0
     # Returns true if Action Pack should check requests using this Mime Type for possible request forgery.  See
0
     # ActionController::RequestForgerProtection.
0
     def verify_request?
0
-      html?
0
+      browser_generated?
0
     end
0
 
0
     def html?
0
       @@html_types.include?(to_sym) || @string =~ /html/
0
     end
0
 
0
+    def browser_generated?
0
+      @@browser_generated_types.include?(to_sym)
0
+    end
0
+
0
     private
0
       def method_missing(method, *args)
0
         if method.to_s =~ /(\w+)\?$/
...
77
78
79
80
81
82
 
 
 
83
84
...
77
78
79
 
 
 
80
81
82
83
84
0
@@ -77,8 +77,8 @@ class MimeTypeTest < Test::Unit::TestCase
0
     all_types.uniq!
0
     # Remove custom Mime::Type instances set in other tests, like Mime::GIF and Mime::IPHONE
0
     all_types.delete_if { |type| !Mime.const_defined?(type.to_s.upcase) }
0
-    verified, unverified = all_types.partition { |type| Mime::Type.html_types.include? type }
0
-    assert verified.each   { |type| assert  Mime.const_get(type.to_s.upcase).verify_request?, "Mime Type is not verified: #{type.inspect}" }
0
-    assert unverified.each { |type| assert !Mime.const_get(type.to_s.upcase).verify_request?, "Mime Type is verified: #{type.inspect}" }
0
+    verified, unverified = all_types.partition { |type| Mime::Type.browser_generated_types.include? type }
0
+    assert verified.each   { |type| assert  Mime.const_get(type.to_s.upcase).verify_request?, "Verifiable Mime Type is not verified: #{type.inspect}" }
0
+    assert unverified.each { |type| assert !Mime.const_get(type.to_s.upcase).verify_request?, "Nonverifiable Mime Type is verified: #{type.inspect}" }
0
   end
0
 end

Comments