Skip to content

Commit a95a619

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: ext/session: check the created ID before validating it
2 parents 7db6f5b + 25b7dd0 commit a95a619

2 files changed

Lines changed: 55 additions & 0 deletions

File tree

ext/session/session.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,6 +2484,9 @@ PHP_FUNCTION(session_create_id)
24842484
int limit = 3;
24852485
while (limit--) {
24862486
new_id = PS(mod)->s_create_sid(&PS(mod_data));
2487+
if (!new_id) {
2488+
break;
2489+
}
24872490
if (!PS(mod)->s_validate_sid || (PS(mod_user_implemented) && Z_ISUNDEF(PS(mod_user_names).ps_validate_sid))) {
24882491
break;
24892492
} else {
@@ -2505,6 +2508,9 @@ PHP_FUNCTION(session_create_id)
25052508
zend_string_release_ex(new_id, false);
25062509
} else {
25072510
smart_str_free(&id);
2511+
if (EG(exception)) {
2512+
RETURN_THROWS();
2513+
}
25082514
php_error_docref(NULL, E_WARNING, "Failed to create new ID");
25092515
RETURN_FALSE;
25102516
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
session_create_id() when the create_sid handler throws
3+
--INI--
4+
session.save_handler=files
5+
session.name=PHPSESSID
6+
session.gc_probability=0
7+
--EXTENSIONS--
8+
session
9+
--FILE--
10+
<?php
11+
12+
ob_start();
13+
14+
class MySessionHandler extends SessionHandler
15+
{
16+
public int $calls = 0;
17+
18+
public function create_sid(): string
19+
{
20+
if ($this->calls++ > 0) {
21+
throw new Exception('create_sid failed');
22+
}
23+
return parent::create_sid();
24+
}
25+
26+
public function validateId(string $id): bool
27+
{
28+
return false;
29+
}
30+
}
31+
32+
session_set_save_handler(new MySessionHandler(), true);
33+
session_start();
34+
35+
try {
36+
session_create_id();
37+
} catch (Throwable $e) {
38+
echo $e::class, ": ", $e->getMessage(), PHP_EOL;
39+
$previous = $e->getPrevious();
40+
echo $previous::class, ": ", $previous->getMessage(), PHP_EOL;
41+
}
42+
43+
var_dump(session_status() === PHP_SESSION_ACTIVE);
44+
45+
?>
46+
--EXPECT--
47+
Error: Session id must be a string
48+
Exception: create_sid failed
49+
bool(true)

0 commit comments

Comments
 (0)