Skip to content

standard: Add CHIPS support for setcookie/setrawcookie #19054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions ext/standard/head.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ PHPAPI bool php_header(void)
#define ILLEGAL_COOKIE_CHARACTER "\",\", \";\", \" \", \"\\t\", \"\\r\", \"\\n\", \"\\013\", or \"\\014\""
PHPAPI zend_result php_setcookie(zend_string *name, zend_string *value, time_t expires,
zend_string *path, zend_string *domain, bool secure, bool httponly,
zend_string *samesite, bool url_encode)
zend_string *samesite, bool partitioned, bool url_encode)
{
zend_string *dt;
sapi_header_line ctr = {0};
Expand Down Expand Up @@ -182,6 +182,9 @@ PHPAPI zend_result php_setcookie(zend_string *name, zend_string *value, time_t e
smart_str_appends(&buf, COOKIE_SAMESITE);
smart_str_append(&buf, samesite);
}
if (partitioned) {
smart_str_appends(&buf, COOKIE_PARTITIONED);
}

ctr.line = ZSTR_VAL(buf.s);
ctr.line_len = (uint32_t) ZSTR_LEN(buf.s);
Expand All @@ -192,7 +195,7 @@ PHPAPI zend_result php_setcookie(zend_string *name, zend_string *value, time_t e
}

static zend_result php_head_parse_cookie_options_array(HashTable *options, zend_long *expires, zend_string **path,
zend_string **domain, bool *secure, bool *httponly, zend_string **samesite)
zend_string **domain, bool *secure, bool *httponly, zend_string **samesite, bool *partitioned)
{
zend_string *key;
zval *value;
Expand All @@ -212,6 +215,8 @@ static zend_result php_head_parse_cookie_options_array(HashTable *options, zend_
*secure = zval_is_true(value);
} else if (zend_string_equals_literal_ci(key, "httponly")) {
*httponly = zval_is_true(value);
} else if (zend_string_equals_literal_ci(key, "partitioned")) {
*partitioned = zval_is_true(value);
} else if (zend_string_equals_literal_ci(key, "samesite")) {
*samesite = zval_get_string(value);
} else {
Expand All @@ -227,9 +232,9 @@ static void php_setcookie_common(INTERNAL_FUNCTION_PARAMETERS, bool is_raw)
HashTable *options = NULL;
zend_long expires = 0;
zend_string *name, *value = NULL, *path = NULL, *domain = NULL, *samesite = NULL;
bool secure = 0, httponly = 0;
bool secure = 0, httponly = 0, partitioned = 0;

ZEND_PARSE_PARAMETERS_START(1, 7)
ZEND_PARSE_PARAMETERS_START(1, 8)
Z_PARAM_STR(name)
Z_PARAM_OPTIONAL
Z_PARAM_STR(value)
Expand All @@ -238,6 +243,7 @@ static void php_setcookie_common(INTERNAL_FUNCTION_PARAMETERS, bool is_raw)
Z_PARAM_STR(domain)
Z_PARAM_BOOL(secure)
Z_PARAM_BOOL(httponly)
Z_PARAM_BOOL(partitioned)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not add new parameters. SameSite is only supported within the $options array as well.

see also: #10317.

ZEND_PARSE_PARAMETERS_END();

if (options) {
Expand All @@ -248,13 +254,13 @@ static void php_setcookie_common(INTERNAL_FUNCTION_PARAMETERS, bool is_raw)
}

if (FAILURE == php_head_parse_cookie_options_array(options, &expires, &path,
&domain, &secure, &httponly, &samesite)
&domain, &secure, &httponly, &samesite, &partitioned)
) {
goto cleanup;
}
}

if (php_setcookie(name, value, expires, path, domain, secure, httponly, samesite, !is_raw) == SUCCESS) {
if (php_setcookie(name, value, expires, path, domain, secure, httponly, samesite, partitioned, !is_raw) == SUCCESS) {
RETVAL_TRUE;
} else {
RETVAL_FALSE;
Expand Down
3 changes: 2 additions & 1 deletion ext/standard/head.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@
#define COOKIE_SECURE "; secure"
#define COOKIE_HTTPONLY "; HttpOnly"
#define COOKIE_SAMESITE "; SameSite="
#define COOKIE_PARTITIONED "; Partitioned"

extern PHP_RINIT_FUNCTION(head);

PHPAPI bool php_header(void);
PHPAPI zend_result php_setcookie(zend_string *name, zend_string *value, time_t expires,
zend_string *path, zend_string *domain, bool secure, bool httponly,
zend_string *samesite, bool url_encode);
zend_string *samesite, bool url_encode, bool partitioned);

#endif
7 changes: 6 additions & 1 deletion ext/standard/tests/network/setcookie.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ setcookie('name', 'value', 0, '/path/');
setcookie('name', 'value', 0, '', 'domain.tld');
setcookie('name', 'value', 0, '', '', TRUE);
setcookie('name', 'value', 0, '', '', FALSE, TRUE);
setcookie('name', 'value', 0, '', '', FALSE, FALSE, TRUE);

setcookie('name', 'value', ['expires' => $tsp]);
setcookie('name', 'value', ['expires' => $tsn, 'path' => '/path/', 'domain' => 'domain.tld', 'secure' => true, 'httponly' => true, 'samesite' => 'Strict']);

setcookie('name', 'value', ['partitioned' => 1]);

$expected = array(
'Set-Cookie: name=deleted; expires='.date('D, d M Y H:i:s', 1).' GMT; Max-Age=0',
'Set-Cookie: name=deleted; expires='.date('D, d M Y H:i:s', 1).' GMT; Max-Age=0',
Expand All @@ -33,8 +36,10 @@ $expected = array(
'Set-Cookie: name=value; domain=domain.tld',
'Set-Cookie: name=value; secure',
'Set-Cookie: name=value; HttpOnly',
'Set-Cookie: name=value; Partitioned',
'Set-Cookie: name=value; expires='.date('D, d M Y H:i:s', $tsp).' GMT; Max-Age=5',
'Set-Cookie: name=value; expires='.date('D, d M Y H:i:s', $tsn).' GMT; Max-Age=0; path=/path/; domain=domain.tld; secure; HttpOnly; SameSite=Strict'
'Set-Cookie: name=value; expires='.date('D, d M Y H:i:s', $tsn).' GMT; Max-Age=0; path=/path/; domain=domain.tld; secure; HttpOnly; SameSite=Strict',
'Set-Cookie: name=value; Partitioned',
);

$headers = headers_list();
Expand Down
Loading