Skip to content

Commit 0c07c55

Browse files
committed
Also amend session_regenerate_id()
1 parent 1f81a3b commit 0c07c55

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

ext/session/session.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,18 +2256,24 @@ PHP_FUNCTION(session_regenerate_id)
22562256
}
22572257
RETURN_THROWS();
22582258
}
2259-
if (PS(use_strict_mode) && PS(mod)->s_validate_sid &&
2260-
PS(mod)->s_validate_sid(&PS(mod_data), PS(id)) == SUCCESS) {
2261-
zend_string_release_ex(PS(id), 0);
2262-
PS(id) = PS(mod)->s_create_sid(&PS(mod_data));
2263-
if (!PS(id)) {
2264-
PS(mod)->s_close(&PS(mod_data));
2265-
PS(session_status) = php_session_none;
2266-
if (!EG(exception)) {
2267-
zend_throw_error(NULL, "Failed to create session ID by collision: %s (path: %s)", PS(mod)->s_name, PS(save_path));
2259+
if (PS(use_strict_mode)) {
2260+
if ((!PS(mod_user_implemented) && PS(mod)->s_validate_sid) || !Z_ISUNDEF(PS(mod_user_names).name.ps_validate_sid)) {
2261+
int limit = 3;
2262+
/* Try to generate non-existing ID */
2263+
while (limit-- && PS(mod)->s_validate_sid(&PS(mod_data), PS(id)) == SUCCESS) {
2264+
zend_string_release_ex(PS(id), 0);
2265+
PS(id) = PS(mod)->s_create_sid(&PS(mod_data));
2266+
if (!PS(id)) {
2267+
PS(mod)->s_close(&PS(mod_data));
2268+
PS(session_status) = php_session_none;
2269+
if (!EG(exception)) {
2270+
zend_throw_error(NULL, "Failed to create session ID by collision: %s (path: %s)", PS(mod)->s_name, PS(save_path));
2271+
}
2272+
RETURN_THROWS();
2273+
}
22682274
}
2269-
RETURN_THROWS();
22702275
}
2276+
// TODO warn that ID cannot be verified? else { }
22712277
}
22722278
/* Read is required to make new session data at this point. */
22732279
if (PS(mod)->s_read(&PS(mod_data), PS(id), &data, PS(gc_maxlifetime)) == FAILURE) {
@@ -2294,7 +2300,6 @@ PHP_FUNCTION(session_regenerate_id)
22942300
/* }}} */
22952301

22962302
/* {{{ Generate new session ID. Intended for user save handlers. */
2297-
/* This is not used yet */
22982303
PHP_FUNCTION(session_create_id)
22992304
{
23002305
zend_string *prefix = NULL, *new_id;

0 commit comments

Comments
 (0)