[php-src] Issue #12504: Corrupted session written when there's a fatal error in autoloader
| From: | ssigwart | Date: | Tue, 24 Oct 2023 04:08:31 +0000 |
| Subject: | [php-src] Issue #12504: Corrupted session written when there's a fatal error in autoloader | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245628@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/12504
Author: ssigwart
### Description
Create two files in the same directory:
index.php
```php
<?php
class TestSessionHandler implements SessionHandlerInterface
{
public function close(): bool
{
return true;
}
public function destroy($id): bool
{
return true;
}
public function gc($max_lifetime)
{
return 0;
}
public function open($path, $name): bool
{
return true;
}
public function read($id)
{
// Return a session object that has 3 variables
return 'before|i:1234;test|O:4:"Test":0:{}after|i:5678;';
}
public function write($id, $data): bool
{
echo 'Write session:' . PHP_EOL;
echo $data . PHP_EOL;
return true;
}
}
session_set_save_handler(new TestSessionHandler());
// Autoload class that's in session
spl_autoload_register(function() {
require_once(__DIR__ . '/Test.class.php');
return true;
});
// If you use this line, the session still fails to load, but the session will not be written back,
which is ideal
// require_once(__DIR__ . '/Test.class.php');
header('Content-type: text/plain');
session_start();
print_r($_SESSION);
```
**Test.class.php**
```php
<?php
class Test
{
// The = null results in a fatal error
public int $var = null;
}
```
When running index.php, it will result in a fatal error. However, it will also write a
session with all variables before the invalid Test class set to zero and no variables
after the $_SESSION['test']. You can see that in the output:
```
<br />
<b>Fatal error</b>: Default value for property of type int may not be null. Use the
nullable type ?int to allow null default value in <b>/path/to/dir/Test.class.php</b> on
line <b>5</b><br />
Write session:
before|i:0;
```
If you uncomment the second require_once so that the autoloader doesn't need to
run, it still results in the fatal error as expected, but it does not write back to the session.
I know that avoiding the fatal error is a workaround, but if this can be fixed, it could prevent
corrupting user sessions if bad code gets deployed to production.
### PHP Version
PHP 8.0.30
### Operating System
_No response_