Skip to content

Commit 59d030c

Browse files
committed
Merge branch 'PHP-8.0'
* PHP-8.0: Fix #80774: session_name() problem with backslash
2 parents 8e8e001 + 6dcd640 commit 59d030c

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

ext/session/session.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,13 +1264,11 @@ static void php_session_remove_cookie(void) {
12641264
zend_llist_element *next;
12651265
zend_llist_element *current;
12661266
char *session_cookie;
1267-
zend_string *e_session_name;
12681267
size_t session_cookie_len;
12691268
size_t len = sizeof("Set-Cookie")-1;
12701269

1271-
e_session_name = php_url_encode(PS(session_name), strlen(PS(session_name)));
1272-
spprintf(&session_cookie, 0, "Set-Cookie: %s=", ZSTR_VAL(e_session_name));
1273-
zend_string_free(e_session_name);
1270+
ZEND_ASSERT(strpbrk(PS(session_name), "=,; \t\r\n\013\014") == NULL);
1271+
spprintf(&session_cookie, 0, "Set-Cookie: %s=", PS(session_name));
12741272

12751273
session_cookie_len = strlen(session_cookie);
12761274
current = l->head;
@@ -1302,7 +1300,7 @@ static int php_session_send_cookie(void) /* {{{ */
13021300
{
13031301
smart_str ncookie = {0};
13041302
zend_string *date_fmt = NULL;
1305-
zend_string *e_session_name, *e_id;
1303+
zend_string *e_id;
13061304

13071305
if (SG(headers_sent)) {
13081306
const char *output_start_filename = php_output_get_start_filename();
@@ -1316,16 +1314,20 @@ static int php_session_send_cookie(void) /* {{{ */
13161314
return FAILURE;
13171315
}
13181316

1319-
/* URL encode session_name and id because they might be user supplied */
1320-
e_session_name = php_url_encode(PS(session_name), strlen(PS(session_name)));
1317+
/* Prevent broken Set-Cookie header, because the session_name might be user supplied */
1318+
if (strpbrk(PS(session_name), "=,; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
1319+
php_error_docref(NULL, E_WARNING, "session.name cannot contain any of the following '=,; \\t\\r\\n\\013\\014'");
1320+
return FAILURE;
1321+
}
1322+
1323+
/* URL encode id because it might be user supplied */
13211324
e_id = php_url_encode(ZSTR_VAL(PS(id)), ZSTR_LEN(PS(id)));
13221325

13231326
smart_str_appendl(&ncookie, "Set-Cookie: ", sizeof("Set-Cookie: ")-1);
1324-
smart_str_appendl(&ncookie, ZSTR_VAL(e_session_name), ZSTR_LEN(e_session_name));
1327+
smart_str_appendl(&ncookie, PS(session_name), strlen(PS(session_name)));
13251328
smart_str_appendc(&ncookie, '=');
13261329
smart_str_appendl(&ncookie, ZSTR_VAL(e_id), ZSTR_LEN(e_id));
13271330

1328-
zend_string_release_ex(e_session_name, 0);
13291331
zend_string_release_ex(e_id, 0);
13301332

13311333
if (PS(cookie_lifetime) > 0) {

ext/session/tests/bug80774.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Bug #80774 (session_name() problem with backslash)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('session')) die("skip session extension not available");
6+
?>
7+
--FILE--
8+
<?php
9+
session_name("foo\\bar");
10+
session_id('12345');
11+
session_start();
12+
?>
13+
--EXPECTHEADERS--
14+
Set-Cookie: foo\bar=12345; path=/
15+
--EXPECT--

ext/session/tests/session_name_variation1.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@ string(9) "PHPSESSID"
4242
bool(true)
4343
string(9) "PHPSESSID"
4444
string(9) "PHPSESSID"
45+
46+
Warning: session_start(): session.name cannot contain any of the following '=,; \t\r\n\013\014' in %s on line %d
4547
bool(true)
4648
string(1) " "
4749
bool(true)
4850
string(1) " "
4951

5052
Warning: session_name(): session.name "" cannot be numeric or empty in %s on line %d
5153
string(1) " "
54+
55+
Warning: session_start(): session.name cannot contain any of the following '=,; \t\r\n\013\014' in %s on line %d
5256
bool(true)
5357
string(1) " "
5458
bool(true)

0 commit comments

Comments
 (0)