Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e3f6300
Merge branch 'fix/php-session-sid-deprecations' into fix/safe-contain…
mrrobot47 Oct 26, 2026
5abe8df
fix(utils): check container files before opening them and add shared …
mrrobot47 Oct 26, 2026
12fc780
fix(restore): restore the php config in place through checked handles…
mrrobot47 Oct 26, 2026
afce73d
fix(migrations): read and rewrite php.ini through the shared checked …
mrrobot47 Oct 26, 2026
65ba2be
fix(site): stop the php container while update --php replaces its config
mrrobot47 Oct 26, 2026
9fb0691
fix(utils): compare before opening a container file for writing
mrrobot47 Oct 26, 2026
cabd2db
fix(restore): create php config files the site lacks from inside its …
mrrobot47 Oct 26, 2026
45dbd1f
fix(site): pull the new image before stopping php and roll back a fai…
mrrobot47 Oct 26, 2026
7e25409
fix(migrations): drop the php.ini backup when the file was left uncha…
mrrobot47 Oct 26, 2026
018f215
fix(migrations): split CR-only php.ini into lines before dropping the…
mrrobot47 Oct 26, 2026
62d123a
fix(site): check that no other container mounts the php config volume…
mrrobot47 Oct 26, 2026
e06c286
fix(restore): recreate php config subfolders from inside the php cont…
mrrobot47 Oct 29, 2026
db09a28
fix(restore): run in-container restore steps as the parent's owner an…
mrrobot47 Oct 29, 2026
27e6a5f
Merge branch 'fix/php-session-sid-deprecations' into fix/safe-contain…
mrrobot47 Nov 1, 2026
ce4b6b2
fix(restore): keep restored php config directories writable until the…
mrrobot47 Nov 1, 2026
f1e5456
fix(restore): count control-character names against the php config en…
mrrobot47 Nov 1, 2026
0b358b8
fix(update): roll back a PHP switch when saving the site after enabli…
mrrobot47 Nov 1, 2026
86b683c
fix(restore): create restored php config files with O_EXCL inside the…
mrrobot47 Nov 1, 2026
a4a8a0a
Merge branch 'develop' into fix/safe-container-config-writes
mrrobot47 Nov 4, 2026
68cb666
ci: retrigger checks
mrrobot47 Nov 4, 2026
c34a3f8
Merge remote-tracking branch 'upstream/develop' into fix/safe-contain…
mrrobot47 Nov 16, 2026
d04c4b4
fix(site): keep the php restart and the original error when the rollb…
mrrobot47 Nov 16, 2026
1a3b5e8
Merge branch 'fix/php-session-sid-deprecations' into fix/safe-contain…
mrrobot47 Nov 16, 2026
84624bc
Merge branch 'fix/php-session-sid-deprecations' into fix/safe-contain…
mrrobot47 Nov 16, 2026
8c9e20c
Merge remote-tracking branch 'upstream/develop' into fix/safe-contain…
mrrobot47 Nov 16, 2026
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use EE;
use EE\Model\Site;
use function EE\Site\Utils\get_container_file_contents;
use function EE\Site\Utils\put_container_file_contents;
use function EE\Site\Utils\site_php_conf_dir;

/**
* Removes session.sid_length and session.sid_bits_per_character, deprecated as of PHP 8.4, from the php.ini of PHP 8.4 and 8.5 sites.
Expand All @@ -12,6 +15,9 @@
*/
class DropDeprecatedSessionSidSettings extends Base {

/** Largest php.ini read; the shipped one is about 70 KB. */
const MAX_SIZE = 1048576;

/** `latest` sites run the php8.4 image. */
const PHP_VERSIONS = [ '8.4', '8.5', 'latest' ];

Expand Down Expand Up @@ -106,15 +112,7 @@ public function down() {
if ( false === $old ) {
throw new \Exception( 'Could not read ' . $change['backup'] );
}
$handle = self::open_ini( $change['ini'] );
if ( ! $handle ) {
throw new \Exception( $change['ini'] . ' is gone.' );
}
try {
self::rewrite( $handle, $old, $change['ini'] );
} finally {
fclose( $handle );
}
put_container_file_contents( $change['ini'], $change['dir'], $old, null, self::MAX_SIZE );
$this->fs->remove( $change['backup'] );
self::reload_fpm( $change['site'] );
} catch ( \Throwable $e ) {
Expand All @@ -134,42 +132,41 @@ private function migrate_site( $site ) {
$ini = $site->site_fs_path . '/config/php/php/php.ini';

// Missing on macOS, where php.ini stays in the container.
$handle = self::open_ini( $ini );
if ( ! $handle ) {
if ( ! file_exists( $ini ) && ! is_link( $ini ) ) {
EE::debug( "Skipping $site->site_url: no php.ini at $ini." );

return;
}

try {
$content = stream_get_contents( $handle );
if ( false === $content ) {
throw new \Exception( "Could not read $ini." );
}
// The site's php container can swap anything in its config volume, so php.ini is read and rewritten in place through a checked handle.
$dir = site_php_conf_dir( $site->site_fs_path, 'php' );
$content = get_container_file_contents( $ini, $dir, self::MAX_SIZE );

list( $new_content, $removed ) = self::strip_settings( $content );
if ( $new_content === $content ) {
EE::debug( "$ini has no deprecated session ID settings." );
list( $new_content, $removed ) = self::strip_settings( $content );
if ( $new_content === $content ) {
EE::debug( "$ini has no deprecated session ID settings." );

return;
}
return;
}

$backup = EE_BACKUP_DIR . '/' . $site->site_url . '/php.ini.with-session-sid-settings';
$this->fs->dumpFile( $backup, $content );
$backup = EE_BACKUP_DIR . '/' . $site->site_url . '/php.ini.with-session-sid-settings';
$this->fs->dumpFile( $backup, $content );

try {
self::rewrite( $handle, $new_content, $ini );
} catch ( \Exception $e ) {
self::rewrite( $handle, $content, $ini );
throw $e;
try {
put_container_file_contents( $ini, $dir, $new_content, $content, self::MAX_SIZE );
} catch ( \Exception $e ) {
// A failed write may have left php.ini partly written; any other failure left it as it was.
if ( 0 === strpos( $e->getMessage(), 'could not write' ) ) {
throw new \Exception( $e->getMessage() . "; the previous file is $backup" );
}
} finally {
fclose( $handle );
$this->fs->remove( $backup );
throw $e;
}

$this->changed[] = [
'site' => $site,
'ini' => $ini,
'dir' => $dir,
'backup' => $backup,
];
EE::debug( "Removed the deprecated session ID settings from $ini; the previous file is $backup." );
Expand All @@ -195,7 +192,8 @@ private function migrate_site( $site ) {
*/
public static function strip_settings( $content ) {

$lines = preg_split( '/(?<=\n)/', $content, -1, PREG_SPLIT_NO_EMPTY );
// After every LF, and after a CR not followed by LF, so a CR-only file isn't read as one line.
$lines = preg_split( '/(?<=\n)|(?<=\r)(?!\n)/', $content, -1, PREG_SPLIT_NO_EMPTY );
$norm = array_map(
function ( $line ) {
return rtrim( $line, " \t\r\n" );
Expand Down Expand Up @@ -264,54 +262,6 @@ function ( $line ) {
return [ $new_content, $removed ];
}

/**
* Opens php.ini for rewriting in place, so no other path is created, followed or re-resolved.
* The directory is mounted into the site's php container, so nothing in it is trusted.
*
* @param string $file php.ini path.
*
* @return resource|null Null when there is no regular file.
* @throws \Exception
*/
private static function open_ini( $file ) {

if ( is_link( $file ) || ! is_file( $file ) ) {
return null;
}

$handle = fopen( $file, 'r+' );
if ( false === $handle ) {
throw new \Exception( "Could not open $file." );
}

// Swapped for a symlink after the check, or a hard link to another file.
$opened = fstat( $handle );
clearstatcache( true, $file );
$linked = lstat( $file );
if ( ! $opened || ! $linked || $opened['dev'] !== $linked['dev'] || $opened['ino'] !== $linked['ino'] || 0100000 !== ( $linked['mode'] & 0170000 ) || 1 !== $opened['nlink'] ) {
fclose( $handle );
throw new \Exception( "$file is not a regular file." );
}

return $handle;
}

/**
* The removal only shortens the file, so writing over it needs no new space; mode and owner stay.
*
* @param resource $handle Handle from open_ini().
* @param string $content New content.
* @param string $file php.ini path, for the error.
*
* @throws \Exception
*/
private static function rewrite( $handle, $content, $file ) {

if ( ! rewind( $handle ) || strlen( $content ) !== fwrite( $handle, $content ) || ! fflush( $handle ) || ! ftruncate( $handle, strlen( $content ) ) ) {
throw new \Exception( "Could not write $file." );
}
}

/**
* PHP-FPM reads php.ini only when it starts; a graceful reload (as `ee site reload --php` does) applies it.
* Stopped containers read it when they start.
Expand Down
Loading