Skip to content

Improve sesson write failure message for user error handlers #8186

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 1 commit 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
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ PHP NEWS
. add ZipArchive::getStreamName() method
. add ZipArchive::getStreamIndex() method

- Session:
. Fixed bug GH-7787 (Improve session write failure message for user error
handlers). (ilutov)

<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>
1 change: 1 addition & 0 deletions ext/session/php_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ typedef struct _php_ps_globals {
} mod_user_names;
int mod_user_implemented;
int mod_user_is_open;
zend_string *mod_user_class_name;
const struct ps_serializer_struct *serializer;
zval http_session_vars;
bool auto_start;
Expand Down
25 changes: 24 additions & 1 deletion ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,9 @@ static void php_session_save_current_state(int write) /* {{{ */

if (write) {
IF_SESSION_VARS() {
zend_string *handler_class_name = PS(mod_user_class_name);
const char *handler_function_name;

if (PS(mod_data) || PS(mod_user_implemented)) {
zend_string *val;

Expand All @@ -483,12 +486,15 @@ static void php_session_save_current_state(int write) /* {{{ */
&& zend_string_equals(val, PS(session_vars))
) {
ret = PS(mod)->s_update_timestamp(&PS(mod_data), PS(id), val, PS(gc_maxlifetime));
handler_function_name = handler_class_name != NULL ? "updateTimestamp" : "update_timestamp";
} else {
ret = PS(mod)->s_write(&PS(mod_data), PS(id), val, PS(gc_maxlifetime));
handler_function_name = "write";
}
zend_string_release_ex(val, 0);
} else {
ret = PS(mod)->s_write(&PS(mod_data), PS(id), ZSTR_EMPTY_ALLOC(), PS(gc_maxlifetime));
handler_function_name = "write";
}
}

Expand All @@ -499,9 +505,14 @@ static void php_session_save_current_state(int write) /* {{{ */
"is correct (%s)",
PS(mod)->s_name,
PS(save_path));
} else if (handler_class_name != NULL) {
php_error_docref(NULL, E_WARNING, "Failed to write session data using user "
"defined save handler. (session.save_path: %s, handler: %s::%s)", PS(save_path),
ZSTR_VAL(handler_class_name), handler_function_name);
} else {
php_error_docref(NULL, E_WARNING, "Failed to write session data using user "
"defined save handler. (session.save_path: %s)", PS(save_path));
"defined save handler. (session.save_path: %s, handler: %s)", PS(save_path),
handler_function_name);
}
}
}
Expand Down Expand Up @@ -2042,6 +2053,12 @@ PHP_FUNCTION(session_set_save_handler)
++i;
} ZEND_HASH_FOREACH_END();


if (PS(mod_user_class_name)) {
zend_string_release(PS(mod_user_class_name));
}
PS(mod_user_class_name) = zend_string_copy(Z_OBJCE_P(obj)->name);

if (register_shutdown) {
/* create shutdown function */
php_shutdown_function_entry shutdown_function_entry;
Expand Down Expand Up @@ -2095,6 +2112,11 @@ PHP_FUNCTION(session_set_save_handler)
RETURN_FALSE;
}

if (PS(mod_user_class_name)) {
zend_string_release(PS(mod_user_class_name));
}
PS(mod_user_class_name) = NULL;

/* remove shutdown function */
remove_user_shutdown_function("session_shutdown", sizeof("session_shutdown") - 1);

Expand Down Expand Up @@ -2775,6 +2797,7 @@ static PHP_GINIT_FUNCTION(ps) /* {{{ */
ps_globals->session_status = php_session_none;
ps_globals->default_mod = NULL;
ps_globals->mod_user_implemented = 0;
ps_globals->mod_user_class_name = NULL;
ps_globals->mod_user_is_open = 0;
ps_globals->session_vars = NULL;
ps_globals->set_handler = 0;
Expand Down
90 changes: 90 additions & 0 deletions ext/session/tests/gh7787.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
--TEST--
GH-7787: Provide more SessionHandler failure information
--EXTENSIONS--
session
--SKIPIF--
<?php include('skipif.inc'); ?>
--INI--
session.use_strict_mode=0
session.save_handler=files
--FILE--
<?php
class MySessionHandler extends SessionHandler implements SessionUpdateTimestampHandlerInterface
{
public function open($path, $sessname): bool {
return true;
}

public function close(): bool {
return true;
}

public function read($sessid): string|false {
return 'foo|s:3:"foo";';
}

public function write($sessid, $sessdata): bool {
return false;
}

public function destroy($sessid): bool {
return true;
}

public function gc($maxlifetime): int|false {
return true;
}

public function create_sid(): string {
return sha1(random_bytes(32));
}

public function validateId($sid): bool {
return true;
}

public function updateTimestamp($sessid, $sessdata): bool {
return false;
}
}

ob_start();

$handler = new MySessionHandler();
session_set_save_handler($handler);

session_start();
$_SESSION['foo'] = 'bar';
session_write_close();

session_start();
session_write_close();

session_set_save_handler(
fn() => true,
fn() => true,
fn() => 'foo|s:3:"foo";',
fn() => false,
fn() => true,
fn() => true,
fn() => sha1(random_bytes(32)),
fn() => true,
fn() => false,
);

session_start();
$_SESSION['foo'] = 'bar';
session_write_close();

session_start();
session_write_close();

?>
--EXPECTF--
Warning: session_write_close(): Failed to write session data using user defined save handler. (session.save_path: , handler: MySessionHandler::write) in %s on line %d

Warning: session_write_close(): Failed to write session data using user defined save handler. (session.save_path: , handler: MySessionHandler::updateTimestamp) in %s on line %d

Warning: session_write_close(): Failed to write session data using user defined save handler. (session.save_path: , handler: write) in %s on line %d

Warning: session_write_close(): Failed to write session data using user defined save handler. (session.save_path: , handler: update_timestamp) in %s on line %d