@@ -76,13 +76,42 @@ PHPAPI int php_header(void)
76
76
}
77
77
}
78
78
79
- PHPAPI int php_setcookie (zend_string * name , zend_string * value , time_t expires , zend_string * path , zend_string * domain , int secure , int httponly , zend_string * samesite , int url_encode )
79
+ #define ILLEGAL_COOKIE_CHARACTER "\",\", \";\", \" \", \"\\t\", \"\\r\", \"\\n\", \"\\013\", and \"\\014\""
80
+ PHPAPI zend_result php_setcookie (zend_string * name , zend_string * value , time_t expires ,
81
+ zend_string * path , zend_string * domain , bool secure , bool httponly ,
82
+ zend_string * samesite , bool url_encode )
80
83
{
81
84
zend_string * dt ;
82
85
sapi_header_line ctr = {0 };
83
- int result ;
86
+ zend_result result ;
84
87
smart_str buf = {0 };
85
88
89
+ if (!ZSTR_LEN (name )) {
90
+ zend_argument_value_error (1 , "cannot be empty" );
91
+ return FAILURE ;
92
+ }
93
+ if (strpbrk (ZSTR_VAL (name ), "=,; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
94
+ zend_argument_value_error (1 , "cannot contain \"=\", " ILLEGAL_COOKIE_CHARACTER );
95
+ return FAILURE ;
96
+ }
97
+ if (!url_encode && value &&
98
+ strpbrk (ZSTR_VAL (value ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
99
+ zend_argument_value_error (2 , "cannot contain " ILLEGAL_COOKIE_CHARACTER );
100
+ return FAILURE ;
101
+ }
102
+
103
+ if (path && strpbrk (ZSTR_VAL (path ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
104
+ zend_value_error ("%s(): \"path\" option cannot contain " ILLEGAL_COOKIE_CHARACTER ,
105
+ get_active_function_name ());
106
+ return FAILURE ;
107
+ }
108
+ if (domain && strpbrk (ZSTR_VAL (domain ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
109
+ zend_value_error ("%s(): \"domain\" option cannot contain " ILLEGAL_COOKIE_CHARACTER ,
110
+ get_active_function_name ());
111
+ return FAILURE ;
112
+ }
113
+ /* Should check value of SameSite? */
114
+
86
115
if (value == NULL || ZSTR_LEN (value ) == 0 ) {
87
116
/*
88
117
* MSIE doesn't delete a cookie when you set it to a null value
@@ -118,7 +147,8 @@ PHPAPI int php_setcookie(zend_string *name, zend_string *value, time_t expires,
118
147
if (!p || * (p + 5 ) != ' ' ) {
119
148
zend_string_free (dt );
120
149
smart_str_free (& buf );
121
- zend_error (E_WARNING , "Expiry date cannot have a year greater than 9999" );
150
+ zend_value_error ("%s(): \"expires\" option cannot have a year greater than 9999" ,
151
+ get_active_function_name ());
122
152
return FAILURE ;
123
153
}
124
154
@@ -201,7 +231,6 @@ static void php_head_parse_cookie_options_array(zval *options, zend_long *expire
201
231
}
202
232
}
203
233
204
- #define ILLEGAL_COOKIE_CHARACTER "\",\", \";\", \" \", \"\\t\", \"\\r\", \"\\n\", \"\\013\", and \"\\014\""
205
234
static void php_setcookie_common (INTERNAL_FUNCTION_PARAMETERS , bool is_raw )
206
235
{
207
236
/* to handle overloaded function array|int */
@@ -228,47 +257,13 @@ static void php_setcookie_common(INTERNAL_FUNCTION_PARAMETERS, bool is_raw)
228
257
"($expires_or_options) is an array" , get_active_function_name ());
229
258
RETURN_THROWS ();
230
259
}
231
- php_head_parse_cookie_options_array (expires_or_options , & expires , & path , & domain , & secure , & httponly , & samesite );
232
- if (path && strpbrk (ZSTR_VAL (path ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
233
- zend_value_error ("%s(): Argument #3 ($expires_or_options[\"path\"]) cannot contain "
234
- ILLEGAL_COOKIE_CHARACTER , get_active_function_name ());
235
- goto cleanup ;
236
- RETURN_THROWS ();
237
- }
238
- if (domain && strpbrk (ZSTR_VAL (domain ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
239
- zend_value_error ("%s(): Argument #3 ($expires_or_options[\"domain\"]) cannot contain "
240
- ILLEGAL_COOKIE_CHARACTER , get_active_function_name ());
241
- goto cleanup ;
242
- RETURN_THROWS ();
243
- }
244
- /* Should check value of SameSite? */
260
+ php_head_parse_cookie_options_array (expires_or_options , & expires , & path ,
261
+ & domain , & secure , & httponly , & samesite );
245
262
} else {
246
263
expires = zval_get_long (expires_or_options );
247
- if (path && strpbrk (ZSTR_VAL (path ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
248
- zend_argument_value_error (4 , "cannot contain " ILLEGAL_COOKIE_CHARACTER );
249
- RETURN_THROWS ();
250
- }
251
- if (domain && strpbrk (ZSTR_VAL (domain ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
252
- zend_argument_value_error (5 , "cannot contain " ILLEGAL_COOKIE_CHARACTER );
253
- RETURN_THROWS ();
254
- }
255
264
}
256
265
}
257
266
258
- if (!ZSTR_LEN (name )) {
259
- zend_argument_value_error (1 , "cannot be empty" );
260
- RETURN_THROWS ();
261
- }
262
- if (strpbrk (ZSTR_VAL (name ), "=,; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
263
- zend_argument_value_error (1 , "cannot contain \"=\", " ILLEGAL_COOKIE_CHARACTER );
264
- RETURN_THROWS ();
265
- }
266
- if (is_raw && value &&
267
- strpbrk (ZSTR_VAL (value ), ",; \t\r\n\013\014" ) != NULL ) { /* man isspace for \013 and \014 */
268
- zend_argument_value_error (2 , "cannot contain " ILLEGAL_COOKIE_CHARACTER );
269
- RETURN_THROWS ();
270
- }
271
-
272
267
if (!EG (exception )) {
273
268
if (php_setcookie (name , value , expires , path , domain , secure , httponly , samesite , !is_raw ) == SUCCESS ) {
274
269
RETVAL_TRUE ;
@@ -278,7 +273,6 @@ static void php_setcookie_common(INTERNAL_FUNCTION_PARAMETERS, bool is_raw)
278
273
}
279
274
280
275
if (expires_or_options && Z_TYPE_P (expires_or_options ) == IS_ARRAY ) {
281
- cleanup :
282
276
if (path ) {
283
277
zend_string_release (path );
284
278
}
0 commit comments