@@ -40,6 +40,10 @@ class CGI
4040 class Cookie < Array
4141 @@accept_charset = "UTF-8" unless defined? ( @@accept_charset )
4242
43+ TOKEN_RE = %r"\A [[!-~]&&[^()<>@,;:\\ \" /?=\[ \] {}]]+\z "
44+ PATH_VALUE_RE = %r"\A [[ -~]&&[^;]]*\z "
45+ DOMAIN_VALUE_RE = %r"\A (?<label>[A-Za-z][-A-Za-z0-9]*[A-Za-z0-9])(?:\. \g <label>)*\z "
46+
4347 # Create a new CGI::Cookie object.
4448 #
4549 # :call-seq:
@@ -72,8 +76,8 @@ def initialize(name = "", *value)
7276 @domain = nil
7377 @expires = nil
7478 if name . kind_of? ( String )
75- @ name = name
76- @ path = ( %r|\A (.*/)| =~ ENV [ "SCRIPT_NAME" ] ? $1 : "" )
79+ self . name = name
80+ self . path = ( %r|\A (.*/)| =~ ENV [ "SCRIPT_NAME" ] ? $1 : "" )
7781 @secure = false
7882 @httponly = false
7983 return super ( value )
@@ -84,11 +88,11 @@ def initialize(name = "", *value)
8488 raise ArgumentError , "`name' required"
8589 end
8690
87- @ name = options [ "name" ]
91+ self . name = options [ "name" ]
8892 value = Array ( options [ "value" ] )
8993 # simple support for IE
90- @ path = options [ "path" ] || ( %r|\A (.*/)| =~ ENV [ "SCRIPT_NAME" ] ? $1 : "" )
91- @ domain = options [ "domain" ]
94+ self . path = options [ "path" ] || ( %r|\A (.*/)| =~ ENV [ "SCRIPT_NAME" ] ? $1 : "" )
95+ self . domain = options [ "domain" ]
9296 @expires = options [ "expires" ]
9397 @secure = options [ "secure" ] == true
9498 @httponly = options [ "httponly" ] == true
@@ -97,11 +101,35 @@ def initialize(name = "", *value)
97101 end
98102
99103 # Name of this cookie, as a +String+
100- attr_accessor :name
104+ attr_reader :name
105+ # Set name of this cookie
106+ def name = ( str )
107+ if str and !TOKEN_RE . match? ( str )
108+ raise ArgumentError , "invalid name: #{ str . dump } "
109+ end
110+ @name = str
111+ end
112+
101113 # Path for which this cookie applies, as a +String+
102- attr_accessor :path
114+ attr_reader :path
115+ # Set path for which this cookie applies
116+ def path = ( str )
117+ if str and !PATH_VALUE_RE . match? ( str )
118+ raise ArgumentError , "invalid path: #{ str . dump } "
119+ end
120+ @path = str
121+ end
122+
103123 # Domain for which this cookie applies, as a +String+
104- attr_accessor :domain
124+ attr_reader :domain
125+ # Set domain for which this cookie applies
126+ def domain = ( str )
127+ if str and ( ( str = str . b ) . bytesize > 255 or !DOMAIN_VALUE_RE . match? ( str ) )
128+ raise ArgumentError , "invalid domain: #{ str . dump } "
129+ end
130+ @domain = str
131+ end
132+
105133 # Time at which this cookie expires, as a +Time+
106134 attr_accessor :expires
107135 # True if this cookie is secure; false otherwise
0 commit comments