Skip to content

[RFC] SameSite cookie option #3398

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

Closed
wants to merge 2 commits into from
Closed
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/session/php_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ typedef struct _php_ps_globals {
char *cookie_domain;
zend_bool cookie_secure;
zend_bool cookie_httponly;
char *cookie_samesite;
const ps_module *mod;
const ps_module *default_mod;
void *mod_data;
Expand Down
130 changes: 103 additions & 27 deletions ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@ PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("session.cookie_domain", "", PHP_INI_ALL, OnUpdateSessionString, cookie_domain, php_ps_globals, ps_globals)
STD_PHP_INI_ENTRY("session.cookie_secure", "0", PHP_INI_ALL, OnUpdateSessionBool, cookie_secure, php_ps_globals, ps_globals)
STD_PHP_INI_ENTRY("session.cookie_httponly", "0", PHP_INI_ALL, OnUpdateSessionBool, cookie_httponly, php_ps_globals, ps_globals)
STD_PHP_INI_ENTRY("session.cookie_samesite", "", PHP_INI_ALL, OnUpdateString, cookie_samesite, php_ps_globals, ps_globals)
STD_PHP_INI_ENTRY("session.use_cookies", "1", PHP_INI_ALL, OnUpdateSessionBool, use_cookies, php_ps_globals, ps_globals)
STD_PHP_INI_ENTRY("session.use_only_cookies", "1", PHP_INI_ALL, OnUpdateSessionBool, use_only_cookies, php_ps_globals, ps_globals)
STD_PHP_INI_ENTRY("session.use_strict_mode", "0", PHP_INI_ALL, OnUpdateSessionBool, use_strict_mode, php_ps_globals, ps_globals)
Expand Down Expand Up @@ -1362,6 +1363,11 @@ static int php_session_send_cookie(void) /* {{{ */
smart_str_appends(&ncookie, COOKIE_HTTPONLY);
}

if (PS(cookie_samesite)[0]) {
smart_str_appends(&ncookie, COOKIE_SAMESITE);
smart_str_appends(&ncookie, PS(cookie_samesite));
}

smart_str_0(&ncookie);

php_session_remove_cookie(); /* remove already sent session ID cookie */
Expand Down Expand Up @@ -1661,20 +1667,30 @@ PHPAPI void session_adapt_url(const char *url, size_t urllen, char **new, size_t
******************************** */

/* {{{ proto bool session_set_cookie_params(int lifetime [, string path [, string domain [, bool secure[, bool httponly]]]])
session_set_cookie_params(array options)
Set session cookie parameters */
static PHP_FUNCTION(session_set_cookie_params)
{
zval *lifetime;
zend_string *path = NULL, *domain = NULL;
int argc = ZEND_NUM_ARGS();
zend_bool secure = 0, httponly = 0;
zval *lifetime_or_options = NULL;
zend_string *lifetime = NULL, *path = NULL, *domain = NULL, *samesite = NULL;
zend_bool secure = 0, secure_null = 1;
zend_bool httponly = 0, httponly_null = 1;
zend_string *ini_name;
int result;
int found = 0;

if (!PS(use_cookies) ||
zend_parse_parameters(argc, "z|SSbb", &lifetime, &path, &domain, &secure, &httponly) == FAILURE) {
if (!PS(use_cookies)) {
return;
}

ZEND_PARSE_PARAMETERS_START(1, 5)
Z_PARAM_ZVAL(lifetime_or_options)
Z_PARAM_OPTIONAL
Z_PARAM_STR(path)
Z_PARAM_STR(domain)
Z_PARAM_BOOL_EX(secure, secure_null, 1, 0)
Z_PARAM_BOOL_EX(httponly, httponly_null, 1, 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't we want to add SameSite here too?

Copy link
Member

Choose a reason for hiding this comment

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

Nope, samesite will only be supported in the new array forms of these functions, not the (now legacy) multi-argument variant.

ZEND_PARSE_PARAMETERS_END();

if (PS(session_status) == php_session_active) {
php_error_docref(NULL, E_WARNING, "Cannot change session cookie parameters when session is active");
Expand All @@ -1686,47 +1702,106 @@ static PHP_FUNCTION(session_set_cookie_params)
RETURN_FALSE;
}

convert_to_string_ex(lifetime);
if (Z_TYPE_P(lifetime_or_options) == IS_ARRAY) {
zend_string *key;
zval *value;

ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(lifetime_or_options), key, value) {
if (key) {
ZVAL_DEREF(value);
if(!strcasecmp("lifetime", ZSTR_VAL(key))) {
lifetime = zval_get_string(value);
found++;
} else if(!strcasecmp("path", ZSTR_VAL(key))) {
path = zval_get_string(value);
found++;
} else if(!strcasecmp("domain", ZSTR_VAL(key))) {
domain = zval_get_string(value);
found++;
} else if(!strcasecmp("secure", ZSTR_VAL(key))) {
secure = zval_is_true(value);
secure_null = 0;
found++;
} else if(!strcasecmp("httponly", ZSTR_VAL(key))) {
httponly = zval_is_true(value);
httponly_null = 0;
found++;
} else if(!strcasecmp("samesite", ZSTR_VAL(key))) {
samesite = zval_get_string(value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we want to check that value is either Strict or Lax?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could, but that falls a bit out of the scope here. No other values are sanitized at this moment. It would probably be a good proposal to validate all values (ie. check if the domain is actually a domain) but I'd keep that a separate discussion.

found++;
} else {
php_error_docref(NULL, E_WARNING, "Unrecognized key '%s' found in the options array", ZSTR_VAL(key));
}
} else {
php_error_docref(NULL, E_WARNING, "Numeric key found in the options array");
}
} ZEND_HASH_FOREACH_END();

ini_name = zend_string_init("session.cookie_lifetime", sizeof("session.cookie_lifetime") - 1, 0);
if (zend_alter_ini_entry(ini_name, Z_STR_P(lifetime), PHP_INI_USER, PHP_INI_STAGE_RUNTIME) == FAILURE) {
zend_string_release_ex(ini_name, 0);
RETURN_FALSE;
if (found == 0) {
php_error_docref(NULL, E_WARNING, "No valid keys were found in the options array");
RETURN_FALSE;
}
} else {
lifetime = zval_get_string(lifetime_or_options);
}
zend_string_release_ex(ini_name, 0);

if (lifetime) {
ini_name = zend_string_init("session.cookie_lifetime", sizeof("session.cookie_lifetime") - 1, 0);
result = zend_alter_ini_entry(ini_name, lifetime, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
zend_string_release(lifetime);
zend_string_release_ex(ini_name, 0);
if (result == FAILURE) {
RETURN_FALSE;
}
}
if (path) {
ini_name = zend_string_init("session.cookie_path", sizeof("session.cookie_path") - 1, 0);
if (zend_alter_ini_entry(ini_name, path, PHP_INI_USER, PHP_INI_STAGE_RUNTIME) == FAILURE) {
zend_string_release_ex(ini_name, 0);
RETURN_FALSE;
result = zend_alter_ini_entry(ini_name, path, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
if (found > 0) {
zend_string_release(path);
}
zend_string_release_ex(ini_name, 0);
if (result == FAILURE) {
RETURN_FALSE;
}
}
if (domain) {
ini_name = zend_string_init("session.cookie_domain", sizeof("session.cookie_domain") - 1, 0);
if (zend_alter_ini_entry(ini_name, domain, PHP_INI_USER, PHP_INI_STAGE_RUNTIME) == FAILURE) {
zend_string_release_ex(ini_name, 0);
RETURN_FALSE;
result = zend_alter_ini_entry(ini_name, domain, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
if (found > 0) {
zend_string_release(domain);
}
zend_string_release_ex(ini_name, 0);
if (result == FAILURE) {
RETURN_FALSE;
}
}

if (argc > 3) {
if (!secure_null) {
ini_name = zend_string_init("session.cookie_secure", sizeof("session.cookie_secure") - 1, 0);
if (zend_alter_ini_entry_chars(ini_name, secure ? "1" : "0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME) == FAILURE) {
zend_string_release_ex(ini_name, 0);
result = zend_alter_ini_entry_chars(ini_name, secure ? "1" : "0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
zend_string_release_ex(ini_name, 0);
if (result == FAILURE) {
RETURN_FALSE;
}
zend_string_release_ex(ini_name, 0);
}
if (argc > 4) {
if (!httponly_null) {
ini_name = zend_string_init("session.cookie_httponly", sizeof("session.cookie_httponly") - 1, 0);
if (zend_alter_ini_entry_chars(ini_name, httponly ? "1" : "0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME) == FAILURE) {
zend_string_release_ex(ini_name, 0);
result = zend_alter_ini_entry_chars(ini_name, httponly ? "1" : "0", 1, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
zend_string_release_ex(ini_name, 0);
if (result == FAILURE) {
RETURN_FALSE;
}
}
if (samesite) {
ini_name = zend_string_init("session.cookie_samesite", sizeof("session.cookie_samesite") - 1, 0);
result = zend_alter_ini_entry(ini_name, samesite, PHP_INI_USER, PHP_INI_STAGE_RUNTIME);
if (found > 0) {
zend_string_release(samesite);
}
zend_string_release_ex(ini_name, 0);
if (result == FAILURE) {
RETURN_FALSE;
}
}

RETURN_TRUE;
Expand All @@ -1748,6 +1823,7 @@ static PHP_FUNCTION(session_get_cookie_params)
add_assoc_string(return_value, "domain", PS(cookie_domain));
add_assoc_bool(return_value, "secure", PS(cookie_secure));
add_assoc_bool(return_value, "httponly", PS(cookie_httponly));
add_assoc_string(return_value, "samesite", PS(cookie_samesite));
}
/* }}} */

Expand Down Expand Up @@ -2627,7 +2703,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_session_cache_expire, 0, 0, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO_EX(arginfo_session_set_cookie_params, 0, 0, 1)
ZEND_ARG_INFO(0, lifetime)
ZEND_ARG_INFO(0, lifetime_or_options)
ZEND_ARG_INFO(0, path)
ZEND_ARG_INFO(0, domain)
ZEND_ARG_INFO(0, secure)
Expand Down
36 changes: 33 additions & 3 deletions ext/session/tests/session_get_cookie_params_basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ session.cookie_path="/"
session.cookie_domain=""
session.cookie_secure=0
session.cookie_httponly=0
session.cookie_samesite=""
--FILE--
<?php

Expand All @@ -26,13 +27,21 @@ var_dump(session_set_cookie_params(3600, "/path", "blah", FALSE, FALSE));
var_dump(session_get_cookie_params());
var_dump(session_set_cookie_params(1234567890, "/guff", "foo", TRUE, TRUE));
var_dump(session_get_cookie_params());
var_dump(session_set_cookie_params([
"lifetime" => 123,
"path" => "/bar",
"domain" => "baz",
"secure" => FALSE,
"httponly" => FALSE,
"samesite" => "please"]));
var_dump(session_get_cookie_params());

echo "Done";
ob_end_flush();
?>
--EXPECT--
*** Testing session_get_cookie_params() : basic functionality ***
array(5) {
array(6) {
["lifetime"]=>
int(0)
["path"]=>
Expand All @@ -43,9 +52,11 @@ array(5) {
bool(false)
["httponly"]=>
bool(false)
["samesite"]=>
string(0) ""
}
bool(true)
array(5) {
array(6) {
["lifetime"]=>
int(3600)
["path"]=>
Expand All @@ -56,9 +67,11 @@ array(5) {
bool(false)
["httponly"]=>
bool(false)
["samesite"]=>
string(0) ""
}
bool(true)
array(5) {
array(6) {
["lifetime"]=>
int(1234567890)
["path"]=>
Expand All @@ -69,5 +82,22 @@ array(5) {
bool(true)
["httponly"]=>
bool(true)
["samesite"]=>
string(0) ""
}
bool(true)
array(6) {
["lifetime"]=>
int(123)
["path"]=>
string(4) "/bar"
["domain"]=>
string(3) "baz"
["secure"]=>
bool(false)
["httponly"]=>
bool(false)
["samesite"]=>
string(6) "please"
}
Done
41 changes: 35 additions & 6 deletions ext/session/tests/session_get_cookie_params_variation1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ session.cookie_path="/"
session.cookie_domain=""
session.cookie_secure=0
session.cookie_httponly=0
session.cookie_samesite=""
--FILE--
<?php

Expand All @@ -32,13 +33,15 @@ ini_set("session.cookie_secure", TRUE);
var_dump(session_get_cookie_params());
ini_set("session.cookie_httponly", TRUE);
var_dump(session_get_cookie_params());
ini_set("session.cookie_samesite", "foo");
var_dump(session_get_cookie_params());

echo "Done";
ob_end_flush();
?>
--EXPECT--
*** Testing session_get_cookie_params() : variation ***
array(5) {
array(6) {
["lifetime"]=>
int(0)
["path"]=>
Expand All @@ -49,8 +52,10 @@ array(5) {
bool(false)
["httponly"]=>
bool(false)
["samesite"]=>
string(0) ""
}
array(5) {
array(6) {
["lifetime"]=>
int(3600)
["path"]=>
Expand All @@ -61,8 +66,10 @@ array(5) {
bool(false)
["httponly"]=>
bool(false)
["samesite"]=>
string(0) ""
}
array(5) {
array(6) {
["lifetime"]=>
int(3600)
["path"]=>
Expand All @@ -73,8 +80,10 @@ array(5) {
bool(false)
["httponly"]=>
bool(false)
["samesite"]=>
string(0) ""
}
array(5) {
array(6) {
["lifetime"]=>
int(3600)
["path"]=>
Expand All @@ -85,8 +94,10 @@ array(5) {
bool(false)
["httponly"]=>
bool(false)
["samesite"]=>
string(0) ""
}
array(5) {
array(6) {
["lifetime"]=>
int(3600)
["path"]=>
Expand All @@ -97,8 +108,10 @@ array(5) {
bool(true)
["httponly"]=>
bool(false)
["samesite"]=>
string(0) ""
}
array(5) {
array(6) {
["lifetime"]=>
int(3600)
["path"]=>
Expand All @@ -109,6 +122,22 @@ array(5) {
bool(true)
["httponly"]=>
bool(true)
["samesite"]=>
string(0) ""
}
array(6) {
["lifetime"]=>
int(3600)
["path"]=>
string(5) "/path"
["domain"]=>
string(3) "foo"
["secure"]=>
bool(true)
["httponly"]=>
bool(true)
["samesite"]=>
string(3) "foo"
}
Done

Loading