Skip to content

BUG-10807: session_regenerate_id fails to check results of validateId… #10813

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion ext/session/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -2295,7 +2295,7 @@ PHP_FUNCTION(session_regenerate_id)
if ((!PS(mod_user_implemented) && PS(mod)->s_validate_sid) || !Z_ISUNDEF(PS(mod_user_names).ps_validate_sid)) {
int limit = 3;
/* Try to generate non-existing ID */
while (limit-- && PS(mod)->s_validate_sid(&PS(mod_data), PS(id)) == SUCCESS) {
while (limit-- && PS(mod)->s_validate_sid(&PS(mod_data), PS(id)) == FAILURE) {
Copy link
Member

Choose a reason for hiding this comment

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

This change is definitely incorrect.

I know the method name is terrible, but validateId() MUST only return true if a session with this ID already exists.

Symfony seems to have hit that mistake too and fixed it: symfony/symfony#47843

zend_string_release_ex(PS(id), 0);
PS(id) = PS(mod)->s_create_sid(&PS(mod_data));
if (!PS(id)) {
Expand Down
69 changes: 69 additions & 0 deletions ext/session/tests/bug10807.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
--TEST--
Bug #10807 (session_regenerate_id with custom handler and use_strict_mode generates three new session ids)
Copy link
Member

Choose a reason for hiding this comment

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

I agree this is a bit confusing at first... But "Bug #XXXXX" is for the old bugs.php.net bug IDs. For GitHub we use "GH-XXXXX". So in this case you should change Bug #10807 to GH-10807.
Similarly, you should name your file gh10807 instead of bug10807.

Copy link
Author

Choose a reason for hiding this comment

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

Changes made. Might I suggest that the docs at https://qa.php.net/write-test.php are updated to handle this.

Re rebasing to the 8.1 branch, my fork doesn't have this branch and this has exceeded my github knowledge. Are you able to take this over for now?

--EXTENSIONS--
session
--INI--
html_errors=0
session.save_handler=files
session.use_strict_mode=1
--FILE--
<?php

/**
* This class will return a number for the session id, incrementing each time a new
* session is generated
*/
class Bug10807SessionHandler implements SessionHandlerInterface, SessionIdInterface {

private bool $fail_validation;
private int $sid_counter;

public function __construct() {
$this->fail_validation = FALSE;
$this->sid_counter = 1;

session_set_save_handler($this, FALSE);
}

public function setFailValidation() {
$this->fail_validation = TRUE;
}

public function create_sid() : string {
return strval($this->sid_counter++);
}

public function validateId(string $id) : bool {

if ($this->fail_validation && intval($id)< 4) {
return FALSE;
}

return TRUE;

}

public function close(): bool { return TRUE; }
public function destroy(string $id): bool { return TRUE; }
public function gc(int $max_lifetime): int|false { return TRUE; }
public function open(string $path, string $name): bool { return TRUE; }
public function read(string $id): string|false { return ''; }
public function write(string $id, string $data): bool { return TRUE; }

}

ob_start();

$save_handler = new Bug10807SessionHandler();
session_start(); //session id = 1
session_regenerate_id(); //session id = 2
var_dump(session_id());

$save_handler->setFailValidation();
session_regenerate_id(); //should invoke create_sid twice as session id 3 will be invalid
var_dump(session_id());

?>
--EXPECT--
string(1) "2"
string(1) "4"