Changeset 7525
- Timestamp:
- 09/21/07 15:05:49 (1 year ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/cgi_ext/cookie.rb (modified) (5 diffs)
- trunk/actionpack/lib/action_controller/cookies.rb (modified) (1 diff)
- trunk/actionpack/test/controller/cookie_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r7523 r7525 1 1 *SVN* 2 3 * Added support for HTTP Only cookies (works in IE6+ and FF 2.0.5+) as an improvement for XSS attacks #8895 [lifo/Spakman] 2 4 3 5 * Don't warn when a path segment precedes a required segment. Closes #9615. [Nicholas Seckar] trunk/actionpack/lib/action_controller/cgi_ext/cookie.rb
r6733 r7525 4 4 class CGI #:nodoc: 5 5 class Cookie < DelegateClass(Array) 6 attr_accessor :name, :value, :path, :domain, :expires 7 attr_reader :secure, :http_only 8 6 9 # Create a new CGI::Cookie object. 7 10 # … … 20 23 # false). Secure cookies are only transmitted to HTTPS 21 24 # servers. 22 # 25 # http_only:: whether this cookie can be accessed by client side scripts (e.g. document.cookie) or only over HTTP 26 # More details: http://msdn2.microsoft.com/en-us/library/system.web.httpcookie.httponly.aspx 27 # Defaults to false. 23 28 # These keywords correspond to attributes of the cookie object. 24 29 def initialize(name = '', *value) … … 29 34 @expires = nil 30 35 @secure = false 36 @http_only = false 31 37 @path = nil 32 38 else … … 36 42 @expires = name['expires'] 37 43 @secure = name['secure'] || false 44 @http_only = name['http_only'] || false 38 45 @path = name['path'] 39 46 end 40 47 41 unless @name 42 raise ArgumentError, "`name' required" 43 end 48 raise ArgumentError, "`name' required" unless @name 44 49 45 50 # simple support for IE … … 56 61 end 57 62 58 attr_accessor("name", "value", "path", "domain", "expires") 59 attr_reader("secure") 63 # Set whether the Cookie is a secure cookie or not. 64 def secure=(val) 65 @secure = val == true 66 end 60 67 61 # Set whether the Cookie is a secure cookie or not. 62 # 63 # +val+ must be a boolean. 64 def secure=(val) 65 @secure = val if val == true or val == false 66 @secure 68 # Set whether the Cookie is an HTTP only cookie or not. 69 def http_only=(val) 70 @http_only = val == true 67 71 end 68 72 69 73 # Convert the Cookie to its string representation. 70 74 def to_s 71 buf = ""75 buf = '' 72 76 buf << @name << '=' 73 74 if @value.kind_of?(String) 75 buf << CGI::escape(@value) 76 else 77 buf << @value.collect{|v| CGI::escape(v) }.join("&") 78 end 79 80 if @domain 81 buf << '; domain=' << @domain 82 end 83 84 if @path 85 buf << '; path=' << @path 86 end 87 88 if @expires 89 buf << '; expires=' << CGI::rfc1123_date(@expires) 90 end 91 92 if @secure == true 93 buf << '; secure' 94 end 95 96 buf 77 buf << (@value.kind_of?(String) ? CGI::escape(@value) : @value.collect{|v| CGI::escape(v) }.join("&")) 78 buf << '; domain=' << @domain if @domain 79 buf << '; path=' << @path if @path 80 buf << '; expires=' << CGI::rfc1123_date(@expires) if @expires 81 buf << '; secure' if @secure 82 buf << '; HttpOnly' if @http_only 97 83 end 98 84 trunk/actionpack/lib/action_controller/cookies.rb
r7438 r7525 24 24 # * <tt>expires</tt> - the time at which this cookie expires, as a +Time+ object. 25 25 # * <tt>secure</tt> - whether this cookie is a secure cookie or not (default to false). 26 # Secure cookies are only transmitted to HTTPS servers. 26 # Secure cookies are only transmitted to HTTPS servers. 27 # * <tt>http_only</tt> - whether this cookie is accessible via scripting or only HTTP (defaults to false). 28 27 29 module Cookies 28 30 protected trunk/actionpack/test/controller/cookie_test.rb
r7435 r7525 33 33 end 34 34 35 def authenticate_with_http_only 36 cookies["user_name"] = { :value => "david", :http_only => true } 37 end 38 35 39 def rescue_action(e) 36 40 raise unless ActionController::MissingTemplate # No templates here, and we don't care about the output … … 59 63 get :authenticate_for_fourten_days 60 64 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david", "expires" => Time.local(2005, 10, 10)) ], @response.headers["cookie"] 65 end 66 67 def test_setting_cookie_with_http_only 68 get :authenticate_with_http_only 69 assert_equal [ CGI::Cookie::new("name" => "user_name", "value" => "david", "http_only" => true) ], @response.headers["cookie"] 70 assert_equal CGI::Cookie::new("name" => "user_name", "value" => "david", "path" => "/", "http_only" => true).to_s, @response.headers["cookie"].to_s 61 71 end 62 72