Skip to content

Commit 58f9434

Browse files
author
Yasuo Ohgaki
committed
Fixed previous commit may delete unwanted cookies. Sync tests from upper branches.
1 parent a27e51f commit 58f9434

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

ext/session/session.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,49 @@ static int php_session_cache_limiter(TSRMLS_D) /* {{{ */
11831183
#define COOKIE_SECURE "; secure"
11841184
#define COOKIE_HTTPONLY "; HttpOnly"
11851185

1186+
/*
1187+
* Remove already sent session ID cookie.
1188+
* It must be directly removed from SG(sapi_header) because sapi_add_header_ex()
1189+
* removes all of matching cookie. i.e. It deletes all of Set-Cookie headers.
1190+
*/
1191+
static void php_session_remove_cookie() {
1192+
sapi_header_struct *header;
1193+
zend_llist *l = &SG(sapi_headers).headers;
1194+
zend_llist_element *next;
1195+
zend_llist_element *current;
1196+
char *session_cookie, *e_session_name;
1197+
int session_cookie_len, len = sizeof("Set-Cookie")-1;
1198+
1199+
e_session_name = php_url_encode(PS(session_name), strlen(PS(session_name)), NULL);
1200+
spprintf(&session_cookie, 0, "Set-Cookie: %s=", e_session_name);
1201+
efree(e_session_name);
1202+
1203+
session_cookie_len = strlen(session_cookie);
1204+
current = l->head;
1205+
while (current) {
1206+
header = (sapi_header_struct *)(current->data);
1207+
next = current->next;
1208+
if (header->header_len > len && header->header[len] == ':'
1209+
&& !strncmp(header->header, session_cookie, session_cookie_len)) {
1210+
if (current->prev) {
1211+
current->prev->next = next;
1212+
} else {
1213+
l->head = next;
1214+
}
1215+
if (next) {
1216+
next->prev = current->prev;
1217+
} else {
1218+
l->tail = current->prev;
1219+
}
1220+
sapi_free_header(header);
1221+
efree(current);
1222+
--l->count;
1223+
}
1224+
current = next;
1225+
}
1226+
efree(session_cookie);
1227+
}
1228+
11861229
static void php_session_send_cookie(TSRMLS_D) /* {{{ */
11871230
{
11881231
smart_str ncookie = {0};
@@ -1248,7 +1291,8 @@ static void php_session_send_cookie(TSRMLS_D) /* {{{ */
12481291

12491292
smart_str_0(&ncookie);
12501293

1251-
sapi_add_header_ex(ncookie.c, ncookie.len, 0, 1 TSRMLS_CC);
1294+
php_session_remove_cookie(); /* remove already sent session ID cookie */
1295+
sapi_add_header_ex(ncookie.c, ncookie.len, 0, 0 TSRMLS_CC);
12521296
}
12531297
/* }}} */
12541298

ext/session/tests/bug60634.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ echo "um, hi\n";
4242
?>
4343
--EXPECTF--
4444
write: goodbye cruel world
45-
close: goodbye cruel world
45+
close: goodbye cruel world
46+

ext/session/tests/bug60634_error_2.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ write: goodbye cruel world
4747
Fatal error: Uncaught exception 'Exception' in %s
4848
Stack trace:
4949
#0 [internal function]: write('%s', '')
50-
#1 %s/ext/session/tests/bug60634_error_2.php(32): session_write_close()
50+
#1 %s(%d): session_write_close()
5151
#2 {main}
52-
thrown in %s/ext/session/tests/bug60634_error_2.php on line 19
52+
thrown in %s on line %d

ext/session/tests/bug60634_error_4.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ Fatal error: Uncaught exception 'Exception' in %s
4747
Stack trace:
4848
#0 [internal function]: write('%s', '')
4949
#1 {main}
50-
thrown in %s/ext/session/tests/bug60634_error_4.php on line 20
51-
close: goodbye cruel world
50+
thrown in %s on line %d
51+
close: goodbye cruel world
52+

0 commit comments

Comments
 (0)