@@ -40,6 +40,10 @@ class CGI
40
40
class Cookie < Array
41
41
@@accept_charset = "UTF-8" unless defined? ( @@accept_charset )
42
42
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
+
43
47
# Create a new CGI::Cookie object.
44
48
#
45
49
# :call-seq:
@@ -72,8 +76,8 @@ def initialize(name = "", *value)
72
76
@domain = nil
73
77
@expires = nil
74
78
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 : "" )
77
81
@secure = false
78
82
@httponly = false
79
83
return super ( value )
@@ -84,11 +88,11 @@ def initialize(name = "", *value)
84
88
raise ArgumentError , "`name' required"
85
89
end
86
90
87
- @ name = options [ "name" ]
91
+ self . name = options [ "name" ]
88
92
value = Array ( options [ "value" ] )
89
93
# 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" ]
92
96
@expires = options [ "expires" ]
93
97
@secure = options [ "secure" ] == true
94
98
@httponly = options [ "httponly" ] == true
@@ -97,11 +101,35 @@ def initialize(name = "", *value)
97
101
end
98
102
99
103
# 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
+
101
113
# 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
+
103
123
# 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
+
105
133
# Time at which this cookie expires, as a +Time+
106
134
attr_accessor :expires
107
135
# True if this cookie is secure; false otherwise
0 commit comments