From 5abe8df53379f0412d1d708265336dc39a6679cf Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Mon, 26 Oct 2026 09:40:49 +0530 Subject: [PATCH 01/18] fix(utils): check container files before opening them and add shared read, write and chown helpers open_container_file() now refuses a symlink, FIFO, device or missing file before it opens anything, and checks that the opened file is the inode it saw. New get_container_file_contents(), put_container_file_contents(), check_plain_tree() and chown_container_tree() let every caller that touches a site's php config volume use the same checks. site_php_conf_dir() takes the directory below config/php. --- src/helper/site-utils.php | 151 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 146 insertions(+), 5 deletions(-) diff --git a/src/helper/site-utils.php b/src/helper/site-utils.php index 11175edf..058792cf 100644 --- a/src/helper/site-utils.php +++ b/src/helper/site-utils.php @@ -1729,21 +1729,22 @@ function ini_global_part( string $content ) { } /** - * Real path of a site's conf.d. config/php is EasyEngine's own link to the site's config volume; everything below it belongs to the site's php container. + * Real path of a directory in a site's php config. config/php is EasyEngine's own link to the site's config volume; everything below it belongs to the site's php container. * * @param string $site_fs_path Site root. + * @param string $sub Directory below config/php. * * @return string * @throws \Exception */ -function site_php_conf_dir( string $site_fs_path ) { +function site_php_conf_dir( string $site_fs_path, string $sub = 'php/conf.d' ) { $base = realpath( $site_fs_path . '/config/php' ); if ( false === $base ) { throw new \Exception( 'could not resolve config/php' ); } - return $base . '/php/conf.d'; + return $base . '/' . $sub; } /** @@ -1751,6 +1752,8 @@ function site_php_conf_dir( string $site_fs_path ) { * * Host root must not write through a symlink, a hard link or a swapped directory, so the handle has to be the regular file in $dir. On Linux the kernel's own path of the open file decides; elsewhere the path is checked before and after opening. * + * Left open: a symlink, FIFO or device renamed into place between the lstat() check and the open is opened (non-blocking) and closed unread and unwritten, and opening some devices has side effects. Only an open with O_NOFOLLOW/openat, which PHP lacks, or one inside the container closes that. + * * @param string $file Path. * @param string $mode 'r' or 'r+'. Nothing is created: PHP expands every path it creates or links to an absolute one, so it can't be confined to a directory the container can swap. * @param string $dir Real path of the directory the file must be in. @@ -1770,7 +1773,16 @@ function open_container_file( string $file, string $mode, string $dir ) { throw new \Exception( "a directory above $name is a symlink" ); } - // Non-blocking ('n', O_NONBLOCK), so a FIFO swapped in can't stall the open before it is rejected below. + // PHP can't open without following a symlink (no O_NOFOLLOW, O_PATH or openat), so refuse anything but a regular file before opening it. + $before = @lstat( $file ); + if ( ! $before ) { + throw new \Exception( "$name is missing" ); + } + if ( 0100000 !== ( $before['mode'] & 0170000 ) ) { + throw new \Exception( "$name is not a regular file" ); + } + + // Non-blocking ('n', O_NONBLOCK): a FIFO or device swapped in after the check above is still opened, but can't stall, and is closed below before any read or write. $handle = @fopen( $file, $mode . 'n' ); if ( false === $handle ) { throw new \Exception( "could not open $name" ); @@ -1781,7 +1793,7 @@ function open_container_file( string $file, string $mode, string $dir ) { $linked = @lstat( $file ); $real = opened_file_path( $handle ); $error = null; - if ( ! $opened || ! $linked || $opened['dev'] !== $linked['dev'] || $opened['ino'] !== $linked['ino'] || 0100000 !== ( $linked['mode'] & 0170000 ) ) { + if ( ! $opened || ! $linked || $opened['dev'] !== $before['dev'] || $opened['ino'] !== $before['ino'] || $opened['dev'] !== $linked['dev'] || $opened['ino'] !== $linked['ino'] || 0100000 !== ( $opened['mode'] & 0170000 ) || 0100000 !== ( $linked['mode'] & 0170000 ) ) { $error = "$name is not a regular file"; } elseif ( realpath( dirname( $file ) ) !== $dir || ( null !== $real && "$dir/$name" !== $real ) ) { $error = "a directory above $name is a symlink"; @@ -1866,3 +1878,132 @@ function read_container_file( $handle, int $max = 65536 ) { return $content; } + +/** + * Read a file in a directory the site's php container can change. + * + * @param string $file Path. + * @param string $dir Real path of the directory the file must be in. + * @param int $max Largest size accepted. + * + * @return string + * @throws \Exception When it is missing, not a regular file in $dir, unreadable or larger than $max. + */ +function get_container_file_contents( string $file, string $dir, int $max = 1048576 ) { + + $handle = open_container_file( $file, 'r', $dir ); + try { + return read_container_file( $handle, $max ); + } finally { + fclose( $handle ); + } +} + +/** + * Overwrite an existing file in a directory the site's php container can change, in place, so owner, mode and inode stay. + * + * @param string $file Path. + * @param string $dir Real path of the directory the file must be in. + * @param string $content New content. + * @param string|null $expected Content the file must still hold, e.g. the one the change was made from; null for any. + * @param int $max Largest current size accepted. + * + * @return bool Whether it was written; false when it already held $content. + * @throws \Exception When it isn't a regular file in $dir, holds something else than $expected, or can't be written (the old content is put back). + */ +function put_container_file_contents( string $file, string $dir, string $content, ?string $expected = null, int $max = 1048576 ) { + + $handle = open_container_file( $file, 'r+', $dir ); + try { + $current = read_container_file( $handle, $max ); + if ( null !== $expected && $current !== $expected ) { + throw new \Exception( basename( $file ) . ' changed while it was checked' ); + } + if ( $current === $content ) { + return false; + } + try { + rewrite_container_file( $handle, $content ); + } catch ( \Exception $e ) { + // A short write leaves part of the file; put the old one back. + rewrite_container_file( $handle, $current ); + throw $e; + } + } finally { + fclose( $handle ); + } + + return true; +} + +/** + * Refuse a directory tree that holds anything but directories and regular files. + * + * Only conclusive while nothing can change the tree, e.g. with the site's php container stopped. + * + * @param string $dir Real path of the directory. + * + * @throws \Exception Naming the first other entry. + */ +function check_plain_tree( string $dir ) { + + clearstatcache( true ); + $names = @scandir( $dir ); + if ( false === $names ) { + throw new \Exception( "could not read $dir" ); + } + + foreach ( array_diff( $names, [ '.', '..' ] ) as $name ) { + $path = "$dir/$name"; + $stat = @lstat( $path ); + $type = $stat ? $stat['mode'] & 0170000 : 0; + if ( 0040000 === $type ) { + check_plain_tree( $path ); + } elseif ( 0100000 !== $type ) { + throw new \Exception( "$path is not a regular file or directory" ); + } + } +} + +/** + * Give a user a directory in a site's config volume and every directory and regular file below it, without following symlinks. + * + * Symlinks, FIFOs and devices are left alone, and a directory must still be at its real path before its entries are changed. + * + * @param string $dir Real path of the directory. + * @param string|int $user User name or ID. + * + * @throws \Exception + */ +function chown_container_tree( string $dir, $user ) { + + clearstatcache( true ); + $stat = @lstat( $dir ); + if ( ! $stat || 0040000 !== ( $stat['mode'] & 0170000 ) || realpath( $dir ) !== $dir ) { + throw new \Exception( "$dir is not a directory, or a directory above it is a symlink" ); + } + if ( ! @lchown( $dir, $user ) ) { + throw new \Exception( "could not change the owner of $dir" ); + } + + $names = @scandir( $dir ); + if ( false === $names ) { + throw new \Exception( "could not read $dir" ); + } + foreach ( array_diff( $names, [ '.', '..' ] ) as $name ) { + $path = "$dir/$name"; + clearstatcache( true ); + $entry = @lstat( $path ); + $type = $entry ? $entry['mode'] & 0170000 : 0; + if ( 0040000 === $type ) { + chown_container_tree( $path, $user ); + } elseif ( 0100000 === $type ) { + if ( realpath( $dir ) !== $dir ) { + throw new \Exception( "a directory above $path is a symlink" ); + } + if ( ! @lchown( $path, $user ) ) { + throw new \Exception( "could not change the owner of $path" ); + } + } + } +} From 12fc78048f4398d4fa52742918440f98a92100bf Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Mon, 26 Oct 2026 09:43:38 +0530 Subject: [PATCH 02/18] fix(restore): restore the php config in place through checked handles instead of rsync rsync followed a php-fpm.d, php or conf.d directory that the site's php container had swapped for a symlink, so host root wrote the backed-up files wherever it pointed. Each backed-up file must now be a regular file, and is written over the site's existing regular file through a checked handle. Nothing is created; a file that can't be restored is kept under the backup dir with the manual fix. --- src/helper/Site_Backup_Restore.php | 93 ++++++++++++++++++++---------- 1 file changed, 64 insertions(+), 29 deletions(-) diff --git a/src/helper/Site_Backup_Restore.php b/src/helper/Site_Backup_Restore.php index 639e7ba0..59336232 100644 --- a/src/helper/Site_Backup_Restore.php +++ b/src/helper/Site_Backup_Restore.php @@ -1684,17 +1684,71 @@ private function restore_php_conf( $backup_dir ) { } if ( $this->fs->exists( sprintf( '%s/php', $backup_dir ) ) ) { - $restore_command = sprintf( 'rsync -a %s/php/php-fpm.d/ %s/config/php/php-fpm.d/', $backup_dir, EE_ROOT_DIR . '/sites/' . $this->site_data['site_url'] ); - EE::exec( $restore_command ); + $this->restore_php_conf_files( $backup_dir . '/php' ); + } - $restore_command = sprintf( 'rsync -a %s/php/php/php.ini %s/config/php/php/php.ini', $backup_dir, EE_ROOT_DIR . '/sites/' . $this->site_data['site_url'] ); - EE::exec( $restore_command ); + $this->keep_session_upload_progress_off(); + } - $restore_command = sprintf( 'rsync -a %s/php/php/conf.d/custom.ini %s/config/php/php/conf.d/custom.ini', $backup_dir, EE_ROOT_DIR . '/sites/' . $this->site_data['site_url'] ); - EE::exec( $restore_command ); + /** + * Copy the backed-up php-fpm.d files, php.ini and custom.ini over the site's ones. + * + * The site's php container can swap anything in its config volume for a symlink, so every file is written in place through a checked handle, and none is created. The backup came from that volume too, so its files must be regular. + * + * @param string $source Extracted `php` directory of the backup. + */ + private function restore_php_conf_files( $source ) { + + $site_url = $this->site_data['site_url']; + $site_dir = EE_ROOT_DIR . '/sites/' . $site_url; + // Some 7-Zip versions store config/php itself as a link, which points back at the live volume. + if ( is_link( $source ) ) { + EE::warning( 'Could not restore the php config: the backup holds a symlink instead of it.' ); + + return; } - $this->keep_session_upload_progress_off(); + $files = []; + if ( is_link( $source . '/php-fpm.d' ) ) { + EE::warning( 'Could not restore config/php/php-fpm.d: the backup holds a symlink instead of it.' ); + } else { + foreach ( @scandir( $source . '/php-fpm.d' ) ?: [] as $name ) { + if ( '.' !== $name && '..' !== $name ) { + $files[] = 'php-fpm.d/' . $name; + } + } + } + $files[] = 'php/php.ini'; + $files[] = 'php/conf.d/custom.ini'; + + $kept = EE_BACKUP_DIR . '/' . $site_url . '-php-conf-not-restored-' . date( 'Ymd-His' ); + foreach ( $files as $file ) { + $from = $source . '/' . $file; + if ( ! file_exists( $from ) && ! is_link( $from ) ) { + continue; + } + + $content = null; + try { + $content = \EE\Site\Utils\get_container_file_contents( $from, realpath( $source ) . '/' . dirname( $file ) ); + $dir = \EE\Site\Utils\site_php_conf_dir( $site_dir, dirname( $file ) ); + if ( ! file_exists( "$dir/" . basename( $file ) ) && ! is_link( "$dir/" . basename( $file ) ) ) { + throw new \Exception( 'it is missing in the site' ); + } + \EE\Site\Utils\put_container_file_contents( "$dir/" . basename( $file ), $dir, $content ); + } catch ( \Exception $e ) { + $message = "Could not restore config/php/$file ({$e->getMessage()})."; + if ( null !== $content ) { + try { + $this->fs->dumpFile( "$kept/$file", $content ); + $message .= " The backed-up file is kept as $kept/$file; copy it to config/php/$file and run: ee site reload $site_url"; + } catch ( \Exception $dump_error ) { + $message .= ' Could not keep the backed-up file either: ' . $dump_error->getMessage(); + } + } + EE::warning( $message ); + } + } } /** @@ -1715,34 +1769,15 @@ private function keep_session_upload_progress_off() { // The directory is in the site's config volume, which its php container can change: every read and write goes through a checked handle. try { - $dir = \EE\Site\Utils\site_php_conf_dir( EE_ROOT_DIR . '/sites/' . $site_url ); - $handle = \EE\Site\Utils\open_container_file( $file, 'r', $dir ); - try { - $content = \EE\Site\Utils\read_container_file( $handle ); - } finally { - fclose( $handle ); - } + $dir = \EE\Site\Utils\site_php_conf_dir( EE_ROOT_DIR . '/sites/' . $site_url ); + $content = \EE\Site\Utils\get_container_file_contents( $file, $dir, 65536 ); $new_content = \EE\Site\Utils\add_session_upload_progress_off( $content ); if ( null === $new_content ) { return; } - $handle = \EE\Site\Utils\open_container_file( $file, 'r+', $dir ); - try { - if ( \EE\Site\Utils\read_container_file( $handle ) !== $content ) { - throw new \Exception( 'it changed while it was checked' ); - } - try { - \EE\Site\Utils\rewrite_container_file( $handle, $new_content ); - } catch ( \Exception $e ) { - // A short write leaves part of the file; put the restored one back. - \EE\Site\Utils\rewrite_container_file( $handle, $content ); - throw $e; - } - } finally { - fclose( $handle ); - } + \EE\Site\Utils\put_container_file_contents( $file, $dir, $new_content, $content, 65536 ); } catch ( \Exception $e ) { EE::warning( "Could not turn off session.upload_progress in the restored custom.ini ({$e->getMessage()}). $manual" ); From afce73dfc019e0ca0d78373c841036ba26ade345 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Mon, 26 Oct 2026 09:46:43 +0530 Subject: [PATCH 03/18] fix(migrations): read and rewrite php.ini through the shared checked helpers open_ini() checked only php.ini itself, so a symlinked php/ directory in the config volume redirected the rewrite. Reads are now capped at 1 MB. --- ...d_drop_deprecated_session_sid_settings.php | 100 ++++-------------- 1 file changed, 20 insertions(+), 80 deletions(-) diff --git a/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php b/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php index 06127805..e158a8a8 100644 --- a/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php +++ b/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php @@ -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. @@ -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' ]; @@ -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 ) { @@ -134,42 +132,32 @@ 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; - } - } finally { - fclose( $handle ); - } + put_container_file_contents( $ini, $dir, $new_content, $content, self::MAX_SIZE ); $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." ); @@ -237,54 +225,6 @@ function ( $line ) { return [ implode( '', array_diff_key( $lines, $drop ) ), $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. From 65ba2bee5f43de454d1d2f5d1794ddc8f6a761f1 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Mon, 26 Oct 2026 09:49:50 +0530 Subject: [PATCH 04/18] fix(site): stop the php container while update --php replaces its config update --php removed and re-created everything in the site's config volume while the new php container was running, so the container could redirect those deletes and writes through a symlink. The php container (the only one that mounts the volume) is now stopped first, the volume must hold only directories and regular files, the backup is a fresh copy, and the new config is in place before the site starts with the new PHP version. enable() gives www-data conf.d without following symlinks. --- src/helper/class-ee-site.php | 68 ++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/src/helper/class-ee-site.php b/src/helper/class-ee-site.php index f9dd4ff2..33187c8a 100644 --- a/src/helper/class-ee-site.php +++ b/src/helper/class-ee-site.php @@ -934,18 +934,20 @@ protected function update_php( $args, $assoc_args ) { $array_data = ( array ) $this->site_data; $this->site_data = reset( $array_data ); - EE::log( 'Taking backup of old php config.' ); $site_backup_dir = $this->site_data['site_fs_path'] . '/.backup'; $php_conf_backup_dir = $site_backup_dir . '/config/php-' . $old_php_version; $php_conf_dir = $this->site_data['site_fs_path'] . '/config/php'; $php_confd_dir = $this->site_data['site_fs_path'] . '/config/php/php/conf.d'; + + // The php container can swap anything in its config volume for a symlink, so it stays stopped until the volume is replaced. + $php_stopped = $this->stop_php_for_config_change( $php_conf_dir ); + + EE::log( 'Taking backup of old php config.' ); + // A fresh copy, so nothing is written through a symlink an older backup holds. + $this->fs->remove( $php_conf_backup_dir ); $this->fs->mkdir( $php_conf_backup_dir ); $this->fs->mirror( $php_conf_dir, $php_conf_backup_dir ); - $this->dump_docker_compose_yml( [ 'nohttps' => $no_https ] ); - EE::log( 'Starting site with new PHP version: ' . $php_version . '. This may take sometime.' ); - $this->enable( $args, [ 'force' => true ] ); - EE::log( 'Updating php config.' ); $temp_dir = EE\Utils\get_temp_dir(); $zip_path = $temp_dir . "phpconf-$php_version.zip"; @@ -978,7 +980,15 @@ protected function update_php( $args, $assoc_args ) { EE::exec( "chown -R www-data: $php_conf_dir" ); + $this->dump_docker_compose_yml( [ 'nohttps' => $no_https ] ); + EE::log( 'Starting site with new PHP version: ' . $php_version . '. This may take sometime.' ); + $php_stopped = false; + $this->enable( $args, [ 'force' => true ] ); + } catch ( \Exception $e ) { + if ( ! empty( $php_stopped ) && ( ! chdir( $this->site_data['site_fs_path'] ) || ! EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' start php' ) ) ) { + EE::warning( 'Could not start the php container again. Run: ee site enable ' . $this->site_data['site_url'] . ' --force' ); + } EE::error( $e->getMessage() ); } $site->save(); @@ -987,6 +997,47 @@ protected function update_php( $args, $assoc_args ) { delem_log( 'site php version update end' ); } + /** + * Stop the site's php container, the only one that mounts its config volume, and check the volume holds only directories and regular files. + * + * @param string $php_conf_dir The site's config/php. + * + * @return bool Whether a running container was stopped. + * @throws \Exception When it can't be stopped, another running container mounts the volume, or the volume holds anything else. + */ + private function stop_php_for_config_change( $php_conf_dir ) { + + chdir( $this->site_data['site_fs_path'] ); + $compose = \EE_DOCKER::docker_compose_with_custom(); + $id = trim( EE::launch( "$compose ps -q php" )->stdout ); + $running = '' !== $id && 'running' === \EE_DOCKER::container_status( $id ); + if ( $running && ! EE::exec( "$compose stop php" ) ) { + throw new \Exception( 'Could not stop the php container to update its config.' ); + } + + try { + $volume = realpath( $php_conf_dir ); + if ( false === $volume ) { + throw new \Exception( "Could not resolve $php_conf_dir." ); + } + // On Linux config/php links to the volume's _data; a custom compose file may mount it elsewhere too. + if ( '_data' === basename( $volume ) ) { + $users = EE::launch( 'docker ps -q --filter ' . escapeshellarg( 'volume=' . basename( dirname( $volume ) ) ) ); + if ( $users->return_code || '' !== trim( $users->stdout ) ) { + throw new \Exception( 'The php config volume is still mounted by a running container (' . implode( ', ', preg_split( '/\s+/', trim( $users->stdout ) ) ) . '); stop it and re-run the update.' ); + } + } + \EE\Site\Utils\check_plain_tree( $volume ); + } catch ( \Exception $e ) { + if ( $running ) { + EE::exec( "$compose start php" ); + } + throw new \Exception( 'Could not update the php config: ' . rtrim( $e->getMessage(), '.' ) . '. Nothing was changed.' ); + } + + return $running; + } + protected function sendmail_path_update( $msmtp ) { $custom_ini_path = $this->site_data['site_fs_path'] . '/config/php/php/conf.d/custom.ini'; @@ -1242,7 +1293,12 @@ public function enable( $args, $assoc_args, $exit_on_error = true ) { # Required when newrelic is enabled on site. Newrelic ini is updated via docker-entrypoint. $php_confd_dir = $this->site_data->site_fs_path . '/config/php/php/conf.d'; if ( $this->fs->exists( $php_confd_dir ) ) { - $this->fs->chown( $php_confd_dir, 'www-data', true ); + // The php container may be running and can swap anything in conf.d, so no symlink is followed. + try { + \EE\Site\Utils\chown_container_tree( \EE\Site\Utils\site_php_conf_dir( $this->site_data->site_fs_path ), 'www-data' ); + } catch ( \Exception $e ) { + \EE::warning( sprintf( 'Could not give www-data the php conf.d of %s: %s', $this->site_data->site_url, $e->getMessage() ) ); + } } if ( \EE_DOCKER::docker_compose_up( $this->site_data->site_fs_path, $containers_to_start ) ) { From 9fb06910e5c04893b39de8b8739ef0e90a0c6dad Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Mon, 26 Oct 2026 10:25:17 +0530 Subject: [PATCH 05/18] fix(utils): compare before opening a container file for writing put_container_file_contents() reads first and opens the file for writing only when its content has to change, so an unchanged hard-linked or immutable file is no longer an error. --- src/helper/site-utils.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/helper/site-utils.php b/src/helper/site-utils.php index 058792cf..a05e8987 100644 --- a/src/helper/site-utils.php +++ b/src/helper/site-utils.php @@ -1913,15 +1913,20 @@ function get_container_file_contents( string $file, string $dir, int $max = 1048 */ function put_container_file_contents( string $file, string $dir, string $content, ?string $expected = null, int $max = 1048576 ) { + // Compared read-only first, so a file that needs no change isn't opened for writing (a hard link or an immutable file would refuse that). + $current = get_container_file_contents( $file, $dir, $max ); + if ( null !== $expected && $current !== $expected ) { + throw new \Exception( basename( $file ) . ' changed while it was checked' ); + } + if ( $current === $content ) { + return false; + } + $handle = open_container_file( $file, 'r+', $dir ); try { - $current = read_container_file( $handle, $max ); - if ( null !== $expected && $current !== $expected ) { + if ( read_container_file( $handle, $max ) !== $current ) { throw new \Exception( basename( $file ) . ' changed while it was checked' ); } - if ( $current === $content ) { - return false; - } try { rewrite_container_file( $handle, $content ); } catch ( \Exception $e ) { From cabd2db405153b5810cbe1219c52885ec19706a9 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Mon, 26 Oct 2026 10:28:42 +0530 Subject: [PATCH 06/18] fix(restore): create php config files the site lacks from inside its php container Restore used to create a backed-up file that the site lacked (e.g. a pool file added by hand). It is now written from inside the running php container, where a symlink the container made can't point at the host; existing files are still written in place through a checked handle. On macOS, where config/php isn't mounted, a missing file is skipped as before had no effect. The kept-file warning no longer suggests copying through config/php by hand. --- src/helper/Site_Backup_Restore.php | 49 +++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/src/helper/Site_Backup_Restore.php b/src/helper/Site_Backup_Restore.php index 59336232..37618f49 100644 --- a/src/helper/Site_Backup_Restore.php +++ b/src/helper/Site_Backup_Restore.php @@ -1693,7 +1693,7 @@ private function restore_php_conf( $backup_dir ) { /** * Copy the backed-up php-fpm.d files, php.ini and custom.ini over the site's ones. * - * The site's php container can swap anything in its config volume for a symlink, so every file is written in place through a checked handle, and none is created. The backup came from that volume too, so its files must be regular. + * The site's php container can swap anything in its config volume for a symlink, so an existing file is written in place through a checked handle, and a missing one is created from inside the container. The backup came from that volume too, so its files must be regular. * * @param string $source Extracted `php` directory of the backup. */ @@ -1732,16 +1732,24 @@ private function restore_php_conf_files( $source ) { try { $content = \EE\Site\Utils\get_container_file_contents( $from, realpath( $source ) . '/' . dirname( $file ) ); $dir = \EE\Site\Utils\site_php_conf_dir( $site_dir, dirname( $file ) ); - if ( ! file_exists( "$dir/" . basename( $file ) ) && ! is_link( "$dir/" . basename( $file ) ) ) { - throw new \Exception( 'it is missing in the site' ); + $dest = "$dir/" . basename( $file ); + clearstatcache( true ); + if ( realpath( dirname( $dest ) ) === $dir && ! file_exists( $dest ) && ! is_link( $dest ) ) { + // On macOS config/php isn't mounted (only custom.ini is), so a file missing there has no effect, as before. + if ( 'Darwin' === PHP_OS_FAMILY ) { + EE::debug( "Not restoring config/php/$file: missing in the site." ); + continue; + } + $this->create_in_php_container( $file, $content ); + } else { + \EE\Site\Utils\put_container_file_contents( $dest, $dir, $content ); } - \EE\Site\Utils\put_container_file_contents( "$dir/" . basename( $file ), $dir, $content ); } catch ( \Exception $e ) { $message = "Could not restore config/php/$file ({$e->getMessage()})."; if ( null !== $content ) { try { $this->fs->dumpFile( "$kept/$file", $content ); - $message .= " The backed-up file is kept as $kept/$file; copy it to config/php/$file and run: ee site reload $site_url"; + $message .= " The backed-up file is kept as $kept/$file; put it back once config/php/" . dirname( $file ) . " is a plain directory again, then run: ee site reload $site_url"; } catch ( \Exception $dump_error ) { $message .= ' Could not keep the backed-up file either: ' . $dump_error->getMessage(); } @@ -1751,6 +1759,37 @@ private function restore_php_conf_files( $source ) { } } + /** + * Create a php config file the site lacks from inside its running php container, where a symlink the container made can't point at the host. + * + * @param string $file Path below config/php (/usr/local/etc in the container). + * @param string $content Content. + * + * @throws \Exception + */ + private function create_in_php_container( $file, $content ) { + + $cwd = getcwd(); + chdir( EE_ROOT_DIR . '/sites/' . $this->site_data['site_url'] ); + $id = trim( EE::launch( \EE_DOCKER::docker_compose_with_custom() . ' ps -q php' )->stdout ); + if ( false !== $cwd ) { + chdir( $cwd ); + } + if ( '' === $id || 'running' !== \EE_DOCKER::container_status( $id ) ) { + throw new \Exception( 'it is missing in the site, and its php container is not running to create it' ); + } + + $tmp = tempnam( \EE\Utils\get_temp_dir(), 'ee-php-conf-' ); + if ( false === $tmp || strlen( $content ) !== file_put_contents( $tmp, $content ) ) { + throw new \Exception( 'could not write a temporary file' ); + } + $result = EE::launch( sprintf( 'docker exec -i -u root %s sh -c %s sh %s < %s', escapeshellarg( $id ), escapeshellarg( 'test ! -e "$1" && test ! -L "$1" && cat > "$1"' ), escapeshellarg( '/usr/local/etc/' . $file ), escapeshellarg( $tmp ) ) ); + unlink( $tmp ); + if ( $result->return_code ) { + throw new \Exception( 'it is missing in the site and could not be created in its php container' ); + } + } + /** * A backup taken before v4.13.3 brings back a custom.ini without `session.upload_progress.enabled = Off`, so add it * again unless the restored file sets the directive itself. The site reload at the end of the restore applies it. From 45dbd1f02f8d2b8eb7be2ed131959c33bb766f47 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Mon, 26 Oct 2026 10:30:49 +0530 Subject: [PATCH 07/18] fix(site): pull the new image before stopping php and roll back a failed update --php The new php image is pulled before php stops, so the site is down only while its config is switched, and a failed pull changes nothing. A failure after that puts the old config back (with php stopped again), the old compose file and a running php container; enable() now throws instead of exiting so this also covers it. An older config backup of the same version is kept aside instead of removed. A failed restart after a refused update says so. --- src/helper/class-ee-site.php | 81 +++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 11 deletions(-) diff --git a/src/helper/class-ee-site.php b/src/helper/class-ee-site.php index 33187c8a..09a487d3 100644 --- a/src/helper/class-ee-site.php +++ b/src/helper/class-ee-site.php @@ -939,16 +939,33 @@ protected function update_php( $args, $assoc_args ) { $php_conf_dir = $this->site_data['site_fs_path'] . '/config/php'; $php_confd_dir = $this->site_data['site_fs_path'] . '/config/php/php/conf.d'; + $rollback = [ + 'site' => $this->site_data, + 'backup' => $php_conf_backup_dir, + 'stopped' => false, + 'switched' => false, + ]; + + // Pull the new image before php stops, so the site is down only while its config is switched. + $this->dump_docker_compose_yml( [ 'nohttps' => $no_https ] ); + chdir( $this->site_data['site_fs_path'] ); + if ( ! EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' pull php' ) ) { + throw new \Exception( "Could not pull the image of PHP $php_version." ); + } + // The php container can swap anything in its config volume for a symlink, so it stays stopped until the volume is replaced. - $php_stopped = $this->stop_php_for_config_change( $php_conf_dir ); + $rollback['stopped'] = $this->stop_php_for_config_change( $php_conf_dir ); EE::log( 'Taking backup of old php config.' ); - // A fresh copy, so nothing is written through a symlink an older backup holds. - $this->fs->remove( $php_conf_backup_dir ); + // A fresh copy, so nothing is written through a symlink an older backup holds; that one is kept aside. + if ( $this->fs->exists( $php_conf_backup_dir ) || is_link( $php_conf_backup_dir ) ) { + $this->fs->rename( $php_conf_backup_dir, $php_conf_backup_dir . '.' . date( 'YmdHis' ) ); + } $this->fs->mkdir( $php_conf_backup_dir ); $this->fs->mirror( $php_conf_dir, $php_conf_backup_dir ); EE::log( 'Updating php config.' ); + $rollback['switched'] = true; $temp_dir = EE\Utils\get_temp_dir(); $zip_path = $temp_dir . "phpconf-$php_version.zip"; $unzip_folder = $temp_dir . "php-$php_version"; @@ -980,14 +997,13 @@ protected function update_php( $args, $assoc_args ) { EE::exec( "chown -R www-data: $php_conf_dir" ); - $this->dump_docker_compose_yml( [ 'nohttps' => $no_https ] ); EE::log( 'Starting site with new PHP version: ' . $php_version . '. This may take sometime.' ); - $php_stopped = false; - $this->enable( $args, [ 'force' => true ] ); + $rollback['stopped'] = true; + $this->enable( $args, [ 'force' => true ], false ); } catch ( \Exception $e ) { - if ( ! empty( $php_stopped ) && ( ! chdir( $this->site_data['site_fs_path'] ) || ! EE::exec( \EE_DOCKER::docker_compose_with_custom() . ' start php' ) ) ) { - EE::warning( 'Could not start the php container again. Run: ee site enable ' . $this->site_data['site_url'] . ' --force' ); + if ( ! empty( $rollback ) ) { + $this->roll_back_php_update( $rollback, $old_php_version, $no_https ); } EE::error( $e->getMessage() ); } @@ -997,6 +1013,48 @@ protected function update_php( $args, $assoc_args ) { delem_log( 'site php version update end' ); } + /** + * Put a failed `update --php` back: the old config (with php stopped again), the old compose file, and a running php container. + * + * @param array $rollback Site data as an array, the config backup dir, whether php was stopped and whether the config was switched. + * @param string $old_version PHP version to go back to. + * @param bool $no_https Whether the site has no SSL. + */ + private function roll_back_php_update( $rollback, $old_version, $no_https ) { + + $site_fs_path = $rollback['site']['site_fs_path']; + $manual = 'Run: ee site enable ' . $rollback['site']['site_url'] . ' --force'; + chdir( $site_fs_path ); + $compose = \EE_DOCKER::docker_compose_with_custom(); + + if ( $rollback['switched'] ) { + try { + // enable() may have started the new container already; the volume is only safe to touch with it stopped. + if ( ! EE::exec( "$compose stop php" ) ) { + throw new \Exception( 'could not stop the php container' ); + } + $volume = realpath( $site_fs_path . '/config/php' ); + \EE\Site\Utils\check_plain_tree( $volume ); + $this->fs->remove( array_map( function ( $name ) use ( $volume ) { + return "$volume/$name"; + }, array_diff( scandir( $volume ), [ '.', '..' ] ) ) ); + $this->fs->mirror( $rollback['backup'], $volume ); + \EE\Site\Utils\chown_container_tree( "$volume/php/conf.d", 'www-data' ); + EE::log( 'Restored the previous php config.' ); + } catch ( \Exception $e ) { + EE::warning( "Could not restore the previous php config from {$rollback['backup']} ({$e->getMessage()}). $manual" ); + } + } + + $this->site_data = $rollback['site']; + $this->site_data['php_version'] = $old_version; + $this->dump_docker_compose_yml( [ 'nohttps' => $no_https ] ); + + if ( $rollback['stopped'] && ! \EE_DOCKER::docker_compose_up( $site_fs_path, [ 'php' ] ) ) { + EE::warning( "Could not start the php container again. $manual" ); + } + } + /** * Stop the site's php container, the only one that mounts its config volume, and check the volume holds only directories and regular files. * @@ -1029,10 +1087,11 @@ private function stop_php_for_config_change( $php_conf_dir ) { } \EE\Site\Utils\check_plain_tree( $volume ); } catch ( \Exception $e ) { - if ( $running ) { - EE::exec( "$compose start php" ); + $message = 'Could not update the php config: ' . rtrim( $e->getMessage(), '.' ) . '. Nothing was changed.'; + if ( $running && ! EE::exec( "$compose start php" ) ) { + $message .= ' Could not start the php container again. Run: ee site enable ' . $this->site_data['site_url'] . ' --force'; } - throw new \Exception( 'Could not update the php config: ' . rtrim( $e->getMessage(), '.' ) . '. Nothing was changed.' ); + throw new \Exception( $message ); } return $running; From 7e254093025d6fa30d98574f7de52d51c0c96d36 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Mon, 26 Oct 2026 10:33:27 +0530 Subject: [PATCH 08/18] fix(migrations): drop the php.ini backup when the file was left unchanged A refused write (php.ini changed meanwhile or hard-linked) left an unused backup behind; it is removed, and a failed write reports where the backup is. --- ...e-command_drop_deprecated_session_sid_settings.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php b/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php index e158a8a8..47516f9d 100644 --- a/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php +++ b/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php @@ -152,7 +152,16 @@ private function migrate_site( $site ) { $backup = EE_BACKUP_DIR . '/' . $site->site_url . '/php.ini.with-session-sid-settings'; $this->fs->dumpFile( $backup, $content ); - put_container_file_contents( $ini, $dir, $new_content, $content, self::MAX_SIZE ); + 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" ); + } + $this->fs->remove( $backup ); + throw $e; + } $this->changed[] = [ 'site' => $site, From 018f215a3802f90b17a5a3c0267650bc86744735 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Mon, 26 Oct 2026 10:54:19 +0530 Subject: [PATCH 09/18] fix(migrations): split CR-only php.ini into lines before dropping the session ID settings A CR-only php.ini was one line, so a match removed the whole file. Lines now end after every LF and after a CR not followed by LF. --- ...93000_site-command_drop_deprecated_session_sid_settings.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php b/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php index 47516f9d..2f673f6f 100644 --- a/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php +++ b/migrations/container/20261023193000_site-command_drop_deprecated_session_sid_settings.php @@ -190,7 +190,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" ); From 62d123a991bd007e93ad844b34553e9d33d27c01 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Mon, 26 Oct 2026 10:57:17 +0530 Subject: [PATCH 10/18] fix(site): check that no other container mounts the php config volume before a rollback enable() can start a custom compose service that also mounts config_php before it fails; the rollback now refuses to touch the volume while one runs, as the update itself does. --- src/helper/class-ee-site.php | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/helper/class-ee-site.php b/src/helper/class-ee-site.php index 09a487d3..b9e4b3e2 100644 --- a/src/helper/class-ee-site.php +++ b/src/helper/class-ee-site.php @@ -1034,6 +1034,7 @@ private function roll_back_php_update( $rollback, $old_version, $no_https ) { throw new \Exception( 'could not stop the php container' ); } $volume = realpath( $site_fs_path . '/config/php' ); + self::check_config_volume_unused( $volume ); \EE\Site\Utils\check_plain_tree( $volume ); $this->fs->remove( array_map( function ( $name ) use ( $volume ) { return "$volume/$name"; @@ -1078,13 +1079,7 @@ private function stop_php_for_config_change( $php_conf_dir ) { if ( false === $volume ) { throw new \Exception( "Could not resolve $php_conf_dir." ); } - // On Linux config/php links to the volume's _data; a custom compose file may mount it elsewhere too. - if ( '_data' === basename( $volume ) ) { - $users = EE::launch( 'docker ps -q --filter ' . escapeshellarg( 'volume=' . basename( dirname( $volume ) ) ) ); - if ( $users->return_code || '' !== trim( $users->stdout ) ) { - throw new \Exception( 'The php config volume is still mounted by a running container (' . implode( ', ', preg_split( '/\s+/', trim( $users->stdout ) ) ) . '); stop it and re-run the update.' ); - } - } + self::check_config_volume_unused( $volume ); \EE\Site\Utils\check_plain_tree( $volume ); } catch ( \Exception $e ) { $message = 'Could not update the php config: ' . rtrim( $e->getMessage(), '.' ) . '. Nothing was changed.'; @@ -1097,6 +1092,24 @@ private function stop_php_for_config_change( $php_conf_dir ) { return $running; } + /** + * On Linux config/php links to the volume's _data; a custom compose file may mount it in another service too. + * + * @param string $volume Real path of config/php. + * + * @throws \Exception When a running container still mounts it. + */ + private static function check_config_volume_unused( $volume ) { + + if ( '_data' !== basename( $volume ) ) { + return; + } + $users = EE::launch( 'docker ps -q --filter ' . escapeshellarg( 'volume=' . basename( dirname( $volume ) ) ) ); + if ( $users->return_code || '' !== trim( $users->stdout ) ) { + throw new \Exception( 'The php config volume is still mounted by a running container (' . implode( ', ', preg_split( '/\s+/', trim( $users->stdout ) ) ) . '); stop it and re-run the update.' ); + } + } + protected function sendmail_path_update( $msmtp ) { $custom_ini_path = $this->site_data['site_fs_path'] . '/config/php/php/conf.d/custom.ini'; From e06c286a7127294fb7051e6c07ca57173502adda Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Thu, 29 Oct 2026 07:00:58 +0530 Subject: [PATCH 11/18] fix(restore): recreate php config subfolders from inside the php container The rsync-based restore copied the whole php-fpm.d tree; the checked restore only handled its top-level files. Walk the backup's php-fpm.d tree again (directories and regular files only, symlinks, FIFOs, devices and control-character names refused), create missing directories and files from inside the running php container one level at a time, relative to a parent that must be a real directory there, and apply the backed-up permission bits there too. Created entries take the owner of their parent directory, since the backup holds no owner; existing files are still overwritten in place through the checked helpers and keep their owner. Entries below a directory that can't be restored are kept with the other unrestorable files. --- src/helper/Site_Backup_Restore.php | 214 ++++++++++++++++++++++------- 1 file changed, 166 insertions(+), 48 deletions(-) diff --git a/src/helper/Site_Backup_Restore.php b/src/helper/Site_Backup_Restore.php index 37618f49..057391c1 100644 --- a/src/helper/Site_Backup_Restore.php +++ b/src/helper/Site_Backup_Restore.php @@ -53,6 +53,9 @@ class Site_Backup_Restore { // Global backup lock handle for serializing backups private $global_backup_lock_handle = null; + // ID of the site's php container while its config is restored; '' when it isn't running. + private $php_container_id; + public function __construct() { $this->fs = new Filesystem(); } @@ -1691,9 +1694,11 @@ private function restore_php_conf( $backup_dir ) { } /** - * Copy the backed-up php-fpm.d files, php.ini and custom.ini over the site's ones. + * Copy the backed-up php-fpm.d tree, php.ini and custom.ini over the site's ones. + * + * The site's php container can swap anything in its config volume for a symlink, so an existing file is written in place through a checked handle, and a missing directory or file is created from inside the running container, where a symlink can't reach the host. The backup came from that volume too, so it may hold only directories and regular files. * - * The site's php container can swap anything in its config volume for a symlink, so an existing file is written in place through a checked handle, and a missing one is created from inside the container. The backup came from that volume too, so its files must be regular. + * Modes (permission bits) come from the backup, applied inside the container. Owners don't: the backup has none (unzip extracts as root), so a created entry takes the owner of the directory it is created in and an existing one keeps its own. Timestamps aren't restored. * * @param string $source Extracted `php` directory of the backup. */ @@ -1701,6 +1706,7 @@ private function restore_php_conf_files( $source ) { $site_url = $this->site_data['site_url']; $site_dir = EE_ROOT_DIR . '/sites/' . $site_url; + clearstatcache( true ); // Some 7-Zip versions store config/php itself as a link, which points back at the live volume. if ( is_link( $source ) ) { EE::warning( 'Could not restore the php config: the backup holds a symlink instead of it.' ); @@ -1708,48 +1714,79 @@ private function restore_php_conf_files( $source ) { return; } - $files = []; - if ( is_link( $source . '/php-fpm.d' ) ) { - EE::warning( 'Could not restore config/php/php-fpm.d: the backup holds a symlink instead of it.' ); - } else { - foreach ( @scandir( $source . '/php-fpm.d' ) ?: [] as $name ) { - if ( '.' !== $name && '..' !== $name ) { - $files[] = 'php-fpm.d/' . $name; - } - } + $entries = []; + $refused = []; + $this->list_php_conf_backup( $source, 'php-fpm.d', true, $entries, $refused ); + foreach ( [ 'php', 'php/php.ini', 'php/conf.d', 'php/conf.d/custom.ini' ] as $path ) { + $this->list_php_conf_backup( $source, $path, false, $entries, $refused ); + } + foreach ( $refused as $path => $reason ) { + EE::warning( "Could not restore config/php/$path: $reason." ); } - $files[] = 'php/php.ini'; - $files[] = 'php/conf.d/custom.ini'; - - $kept = EE_BACKUP_DIR . '/' . $site_url . '-php-conf-not-restored-' . date( 'Ymd-His' ); - foreach ( $files as $file ) { - $from = $source . '/' . $file; - if ( ! file_exists( $from ) && ! is_link( $from ) ) { - continue; - } + $this->php_container_id = null; + $kept = EE_BACKUP_DIR . '/' . $site_url . '-php-conf-not-restored-' . date( 'Ymd-His' ); + $not_restored = []; + $skipped = []; + $mode_not_set = []; + foreach ( $entries as $path => $stat ) { + $is_dir = 0040000 === ( $stat['mode'] & 0170000 ); + $mode = sprintf( '%04o', $stat['mode'] & 0777 ); + $parent = dirname( $path ); $content = null; try { - $content = \EE\Site\Utils\get_container_file_contents( $from, realpath( $source ) . '/' . dirname( $file ) ); - $dir = \EE\Site\Utils\site_php_conf_dir( $site_dir, dirname( $file ) ); - $dest = "$dir/" . basename( $file ); + if ( ! $is_dir ) { + $content = \EE\Site\Utils\get_container_file_contents( "$source/$path", realpath( $source ) . ( '.' === $parent ? '' : "/$parent" ) ); + } + if ( isset( $skipped[ $parent ] ) ) { + $skipped[ $path ] = true; + continue; + } + if ( isset( $not_restored[ $parent ] ) ) { + $not_restored[ $path ] = $not_restored[ $parent ]; + if ( $is_dir ) { + continue; + } + throw new \Exception( "config/php/{$not_restored[ $parent ]} was not restored" ); + } + $dir = \EE\Site\Utils\site_php_conf_dir( $site_dir, '.' === $parent ? '' : $parent ); + $dir = rtrim( $dir, '/' ); + $dest = "$dir/" . basename( $path ); clearstatcache( true ); - if ( realpath( dirname( $dest ) ) === $dir && ! file_exists( $dest ) && ! is_link( $dest ) ) { - // On macOS config/php isn't mounted (only custom.ini is), so a file missing there has no effect, as before. + $current = @lstat( $dest ); + if ( realpath( $dir ) !== $dir ) { + throw new \Exception( "config/php/$parent is not a directory, or a directory above it is a symlink" ); + } + if ( ! $current ) { + // On macOS config/php isn't mounted (only custom.ini is), so an entry missing there has no effect, as before. if ( 'Darwin' === PHP_OS_FAMILY ) { - EE::debug( "Not restoring config/php/$file: missing in the site." ); + EE::debug( "Not restoring config/php/$path: missing in the site." ); + $skipped[ $path ] = true; continue; } - $this->create_in_php_container( $file, $content ); + $this->php_container_op( $is_dir ? 'dir' : 'file', $path, $mode, $content ); + } elseif ( $is_dir ) { + if ( 0040000 !== ( $current['mode'] & 0170000 ) || realpath( $dest ) !== $dest ) { + throw new \Exception( 'it is not a directory in the site' ); + } } else { \EE\Site\Utils\put_container_file_contents( $dest, $dir, $content ); } + // Not on macOS, where the container doesn't see config/php. + if ( $current && 'Darwin' !== PHP_OS_FAMILY && $mode !== sprintf( '%04o', $current['mode'] & 07777 ) ) { + try { + $this->php_container_op( 'mode', $path, $mode ); + } catch ( \Exception $e ) { + $mode_not_set[] = "config/php/$path ({$e->getMessage()})"; + } + } } catch ( \Exception $e ) { - $message = "Could not restore config/php/$file ({$e->getMessage()})."; + $not_restored[ $path ] = $not_restored[ $path ] ?? $path; + $message = "Could not restore config/php/$path ({$e->getMessage()})."; if ( null !== $content ) { try { - $this->fs->dumpFile( "$kept/$file", $content ); - $message .= " The backed-up file is kept as $kept/$file; put it back once config/php/" . dirname( $file ) . " is a plain directory again, then run: ee site reload $site_url"; + $this->fs->dumpFile( "$kept/$path", $content ); + $message .= " The backed-up file is kept as $kept/$path; put it back once config/php/" . ( $not_restored[ $parent ] ?? $parent ) . " is a plain directory again, then run: ee site reload $site_url"; } catch ( \Exception $dump_error ) { $message .= ' Could not keep the backed-up file either: ' . $dump_error->getMessage(); } @@ -1757,36 +1794,117 @@ private function restore_php_conf_files( $source ) { EE::warning( $message ); } } + if ( $mode_not_set ) { + EE::warning( 'Restored, but could not set the backed-up mode of: ' . implode( ', ', $mode_not_set ) . '.' ); + } } /** - * Create a php config file the site lacks from inside its running php container, where a symlink the container made can't point at the host. + * Collect a backed-up entry below the extracted `php` directory, and with $recurse everything below it, parents first. * - * @param string $file Path below config/php (/usr/local/etc in the container). - * @param string $content Content. + * @param string $source Extracted `php` directory of the backup. + * @param string $path Path below it. + * @param bool $recurse Whether to collect a directory's entries. + * @param array $entries Path => lstat() of each directory and regular file. + * @param array $refused Path => reason for each entry that is skipped. + * @param int $depth Directory depth. + */ + private function list_php_conf_backup( $source, $path, $recurse, array &$entries, array &$refused, $depth = 0 ) { + + $parent = dirname( $path ); + if ( '.' !== $parent && ! isset( $entries[ $parent ] ) ) { + return; + } + $stat = @lstat( "$source/$path" ); + if ( ! $stat ) { + return; + } + $type = $stat['mode'] & 0170000; + if ( 0040000 !== $type && 0100000 !== $type ) { + $refused[ $path ] = 'the backup holds a symlink or special file there'; + + return; + } + if ( count( $entries ) >= 1000 || $depth > 16 ) { + $refused[ $path ] = 'the backup holds too many files or directories'; + + return; + } + $entries[ $path ] = $stat; + if ( ! $recurse || 0040000 !== $type ) { + return; + } + foreach ( @scandir( "$source/$path" ) ?: [] as $name ) { + if ( '.' === $name || '..' === $name ) { + continue; + } + if ( preg_match( '/[\x00-\x1f\x7f]/', $name ) ) { + $refused[ $path . '/' . addcslashes( $name, "\0..\37\177" ) ] = 'its name holds control characters'; + continue; + } + $this->list_php_conf_backup( $source, "$path/$name", true, $entries, $refused, $depth + 1 ); + } + } + + /** + * Create a directory or file the site's php config lacks, or set the mode of one, from inside its running php container, where a symlink the container made can't point at the host. + * + * The parent must be a real directory (no symlink in its path) inside the container, and a created entry takes the parent's owner. + * + * @param string $op 'dir', 'file' or 'mode'. + * @param string $path Path below config/php (/usr/local/etc in the container). + * @param string $mode Octal permission bits. + * @param string|null $content Content of a created file. * * @throws \Exception */ - private function create_in_php_container( $file, $content ) { + private function php_container_op( $op, $path, $mode, $content = null ) { - $cwd = getcwd(); - chdir( EE_ROOT_DIR . '/sites/' . $this->site_data['site_url'] ); - $id = trim( EE::launch( \EE_DOCKER::docker_compose_with_custom() . ' ps -q php' )->stdout ); - if ( false !== $cwd ) { - chdir( $cwd ); - } - if ( '' === $id || 'running' !== \EE_DOCKER::container_status( $id ) ) { - throw new \Exception( 'it is missing in the site, and its php container is not running to create it' ); + if ( ! preg_match( '#^(php-fpm\.d|php)(/[^/]+)*$#', $path ) || preg_match( '#(^|/)\.\.?(/|$)|[\x00-\x1f\x7f]#', $path ) || ! preg_match( '/^0[0-7]{3}$/', $mode ) ) { + throw new \Exception( 'unexpected path or mode' ); } - $tmp = tempnam( \EE\Utils\get_temp_dir(), 'ee-php-conf-' ); - if ( false === $tmp || strlen( $content ) !== file_put_contents( $tmp, $content ) ) { - throw new \Exception( 'could not write a temporary file' ); + if ( null === $this->php_container_id ) { + $cwd = getcwd(); + chdir( EE_ROOT_DIR . '/sites/' . $this->site_data['site_url'] ); + $id = trim( EE::launch( \EE_DOCKER::docker_compose_with_custom() . ' ps -q php' )->stdout ); + if ( false !== $cwd ) { + chdir( $cwd ); + } + $this->php_container_id = ( '' !== $id && 'running' === \EE_DOCKER::container_status( $id ) ) ? $id : ''; + } + if ( '' === $this->php_container_id ) { + throw new \Exception( 'its php container is not running' ); + } + + // Run relative to the parent the shell changed into, so a directory swapped above it afterwards can't redirect the change. + $script = 'set -e; op=$2; m=$3; p=$1/$4; d=${p%/*}; n=./${p##*/} +cd -P -- "$d" 2>/dev/null && [ "$(pwd -P)" = "$d" ] || exit 3 +if [ "$op" = mode ]; then [ ! -L "$n" ] || exit 4; chmod -- "$m" "$n"; exit 0; fi +[ ! -e "$n" ] && [ ! -L "$n" ] || exit 4 +o=$(stat -c %u:%g .) +if [ "$op" = dir ]; then mkdir -m 700 -- "$n"; else (umask 077; set -C; cat > "$n"); fi +chown -h -- "$o" "$n"; chmod -- "$m" "$n"'; + + $tmp = null; + if ( 'file' === $op ) { + $tmp = tempnam( \EE\Utils\get_temp_dir(), 'ee-php-conf-' ); + if ( false === $tmp || strlen( $content ) !== file_put_contents( $tmp, $content ) ) { + throw new \Exception( 'could not write a temporary file' ); + } + } + $result = EE::launch( sprintf( 'docker exec %s-u root %s sh -c %s sh /usr/local/etc %s %s %s%s', $tmp ? '-i ' : '', escapeshellarg( $this->php_container_id ), escapeshellarg( $script ), escapeshellarg( $op ), escapeshellarg( $mode ), escapeshellarg( $path ), $tmp ? ' < ' . escapeshellarg( $tmp ) : '' ) ); + if ( $tmp ) { + unlink( $tmp ); + } + if ( 3 === $result->return_code ) { + throw new \Exception( 'a directory above it is not a real directory in its php container' ); + } + if ( 4 === $result->return_code ) { + throw new \Exception( 'mode' === $op ? 'it is a symlink in its php container' : 'it appeared in its php container while it was restored' ); } - $result = EE::launch( sprintf( 'docker exec -i -u root %s sh -c %s sh %s < %s', escapeshellarg( $id ), escapeshellarg( 'test ! -e "$1" && test ! -L "$1" && cat > "$1"' ), escapeshellarg( '/usr/local/etc/' . $file ), escapeshellarg( $tmp ) ) ); - unlink( $tmp ); if ( $result->return_code ) { - throw new \Exception( 'it is missing in the site and could not be created in its php container' ); + throw new \Exception( 'mode' === $op ? 'could not change it in its php container' : 'it is missing in the site and could not be created in its php container' ); } } From db09a28c496e1f5f33f2d1dee63ba1b5cf94613d Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Thu, 29 Oct 2026 07:08:35 +0530 Subject: [PATCH 12/18] fix(restore): run in-container restore steps as the parent's owner and bound the backup walk Each in-container create or chmod now runs as the owner of the parent directory (looked up first, behind the same real-directory check), so a symlink planted by a user who can write that directory can't make root change files that user couldn't; created entries belong to that owner without a chown, and directories get their mode from mkdir directly. A failed chmod after a successful create is reported as a mode that wasn't set instead of a failed create. php.ini and custom.ini are collected before php-fpm.d, the walk stops at 1000 entries (refused ones included) with one warning, modes are compared on the permission bits only, a macOS skip no longer reads the backed-up file first, and the kept-file hint names a directory only when one was the cause. --- src/helper/Site_Backup_Restore.php | 109 +++++++++++++++++++---------- 1 file changed, 71 insertions(+), 38 deletions(-) diff --git a/src/helper/Site_Backup_Restore.php b/src/helper/Site_Backup_Restore.php index 057391c1..7583930c 100644 --- a/src/helper/Site_Backup_Restore.php +++ b/src/helper/Site_Backup_Restore.php @@ -1696,9 +1696,8 @@ private function restore_php_conf( $backup_dir ) { /** * Copy the backed-up php-fpm.d tree, php.ini and custom.ini over the site's ones. * - * The site's php container can swap anything in its config volume for a symlink, so an existing file is written in place through a checked handle, and a missing directory or file is created from inside the running container, where a symlink can't reach the host. The backup came from that volume too, so it may hold only directories and regular files. - * - * Modes (permission bits) come from the backup, applied inside the container. Owners don't: the backup has none (unzip extracts as root), so a created entry takes the owner of the directory it is created in and an existing one keeps its own. Timestamps aren't restored. + * The container can swap anything in its config volume, so existing files are written in place through checked handles and missing entries are created inside the container; the backup came from that volume too, so it may hold only directories and regular files. + * Permission bits come from the backup; owners don't (the backup has none), so a created entry belongs to its parent's owner and an existing one keeps its own. * * @param string $source Extracted `php` directory of the backup. */ @@ -1716,10 +1715,10 @@ private function restore_php_conf_files( $source ) { $entries = []; $refused = []; - $this->list_php_conf_backup( $source, 'php-fpm.d', true, $entries, $refused ); foreach ( [ 'php', 'php/php.ini', 'php/conf.d', 'php/conf.d/custom.ini' ] as $path ) { $this->list_php_conf_backup( $source, $path, false, $entries, $refused ); } + $this->list_php_conf_backup( $source, 'php-fpm.d', true, $entries, $refused ); foreach ( $refused as $path => $reason ) { EE::warning( "Could not restore config/php/$path: $reason." ); } @@ -1734,14 +1733,14 @@ private function restore_php_conf_files( $source ) { $mode = sprintf( '%04o', $stat['mode'] & 0777 ); $parent = dirname( $path ); $content = null; + if ( isset( $skipped[ $parent ] ) ) { + $skipped[ $path ] = true; + continue; + } try { if ( ! $is_dir ) { $content = \EE\Site\Utils\get_container_file_contents( "$source/$path", realpath( $source ) . ( '.' === $parent ? '' : "/$parent" ) ); } - if ( isset( $skipped[ $parent ] ) ) { - $skipped[ $path ] = true; - continue; - } if ( isset( $not_restored[ $parent ] ) ) { $not_restored[ $path ] = $not_restored[ $parent ]; if ( $is_dir ) { @@ -1749,8 +1748,7 @@ private function restore_php_conf_files( $source ) { } throw new \Exception( "config/php/{$not_restored[ $parent ]} was not restored" ); } - $dir = \EE\Site\Utils\site_php_conf_dir( $site_dir, '.' === $parent ? '' : $parent ); - $dir = rtrim( $dir, '/' ); + $dir = rtrim( \EE\Site\Utils\site_php_conf_dir( $site_dir, '.' === $parent ? '' : $parent ), '/' ); $dest = "$dir/" . basename( $path ); clearstatcache( true ); $current = @lstat( $dest ); @@ -1764,7 +1762,9 @@ private function restore_php_conf_files( $source ) { $skipped[ $path ] = true; continue; } - $this->php_container_op( $is_dir ? 'dir' : 'file', $path, $mode, $content ); + if ( ! $this->php_container_op( $is_dir ? 'dir' : 'file', $path, $mode, $content ) ) { + $mode_not_set[] = "config/php/$path (could not change it in its php container)"; + } } elseif ( $is_dir ) { if ( 0040000 !== ( $current['mode'] & 0170000 ) || realpath( $dest ) !== $dest ) { throw new \Exception( 'it is not a directory in the site' ); @@ -1773,9 +1773,11 @@ private function restore_php_conf_files( $source ) { \EE\Site\Utils\put_container_file_contents( $dest, $dir, $content ); } // Not on macOS, where the container doesn't see config/php. - if ( $current && 'Darwin' !== PHP_OS_FAMILY && $mode !== sprintf( '%04o', $current['mode'] & 07777 ) ) { + if ( $current && 'Darwin' !== PHP_OS_FAMILY && $mode !== sprintf( '%04o', $current['mode'] & 0777 ) ) { try { - $this->php_container_op( 'mode', $path, $mode ); + if ( ! $this->php_container_op( 'mode', $path, $mode ) ) { + throw new \Exception( 'could not change it in its php container' ); + } } catch ( \Exception $e ) { $mode_not_set[] = "config/php/$path ({$e->getMessage()})"; } @@ -1786,7 +1788,8 @@ private function restore_php_conf_files( $source ) { if ( null !== $content ) { try { $this->fs->dumpFile( "$kept/$path", $content ); - $message .= " The backed-up file is kept as $kept/$path; put it back once config/php/" . ( $not_restored[ $parent ] ?? $parent ) . " is a plain directory again, then run: ee site reload $site_url"; + $fix = isset( $not_restored[ $parent ] ) ? "once config/php/{$not_restored[ $parent ]} is a plain directory again" : 'once the cause is fixed'; + $message .= " The backed-up file is kept as $kept/$path; put it back $fix, then run: ee site reload $site_url"; } catch ( \Exception $dump_error ) { $message .= ' Could not keep the backed-up file either: ' . $dump_error->getMessage(); } @@ -1808,33 +1811,46 @@ private function restore_php_conf_files( $source ) { * @param array $entries Path => lstat() of each directory and regular file. * @param array $refused Path => reason for each entry that is skipped. * @param int $depth Directory depth. + * + * @return bool False once the entry limit is reached. */ private function list_php_conf_backup( $source, $path, $recurse, array &$entries, array &$refused, $depth = 0 ) { $parent = dirname( $path ); if ( '.' !== $parent && ! isset( $entries[ $parent ] ) ) { - return; + return true; + } + // Refused entries count too, so a crafted tree can't produce unbounded warnings. + if ( count( $entries ) + count( $refused ) >= 1000 ) { + $refused[ $path ] = 'the backup holds more than 1000 entries, so this and the rest were not restored'; + + return false; } $stat = @lstat( "$source/$path" ); if ( ! $stat ) { - return; + return true; } $type = $stat['mode'] & 0170000; if ( 0040000 !== $type && 0100000 !== $type ) { $refused[ $path ] = 'the backup holds a symlink or special file there'; - return; + return true; } - if ( count( $entries ) >= 1000 || $depth > 16 ) { - $refused[ $path ] = 'the backup holds too many files or directories'; + if ( $depth > 16 ) { + $refused[ $path ] = 'it is nested too deep'; - return; + return true; } $entries[ $path ] = $stat; if ( ! $recurse || 0040000 !== $type ) { - return; + return true; } - foreach ( @scandir( "$source/$path" ) ?: [] as $name ) { + $handle = @opendir( "$source/$path" ); + if ( false === $handle ) { + return true; + } + $more = true; + while ( $more && false !== ( $name = readdir( $handle ) ) ) { if ( '.' === $name || '..' === $name ) { continue; } @@ -1842,20 +1858,24 @@ private function list_php_conf_backup( $source, $path, $recurse, array &$entries $refused[ $path . '/' . addcslashes( $name, "\0..\37\177" ) ] = 'its name holds control characters'; continue; } - $this->list_php_conf_backup( $source, "$path/$name", true, $entries, $refused, $depth + 1 ); + $more = $this->list_php_conf_backup( $source, "$path/$name", true, $entries, $refused, $depth + 1 ); } + closedir( $handle ); + + return $more; } /** * Create a directory or file the site's php config lacks, or set the mode of one, from inside its running php container, where a symlink the container made can't point at the host. * - * The parent must be a real directory (no symlink in its path) inside the container, and a created entry takes the parent's owner. + * The step runs as the owner of the parent directory, which must be a real directory (no symlink in its path), so it can't change what that owner couldn't, and a created entry belongs to that owner. * * @param string $op 'dir', 'file' or 'mode'. * @param string $path Path below config/php (/usr/local/etc in the container). * @param string $mode Octal permission bits. * @param string|null $content Content of a created file. * + * @return bool Whether the mode was set; false when only that failed. * @throws \Exception */ private function php_container_op( $op, $path, $mode, $content = null ) { @@ -1877,14 +1897,24 @@ private function php_container_op( $op, $path, $mode, $content = null ) { throw new \Exception( 'its php container is not running' ); } - // Run relative to the parent the shell changed into, so a directory swapped above it afterwards can't redirect the change. + // Changes into the parent and acts relative to it, so a directory swapped above it afterwards can't redirect the step. $script = 'set -e; op=$2; m=$3; p=$1/$4; d=${p%/*}; n=./${p##*/} cd -P -- "$d" 2>/dev/null && [ "$(pwd -P)" = "$d" ] || exit 3 -if [ "$op" = mode ]; then [ ! -L "$n" ] || exit 4; chmod -- "$m" "$n"; exit 0; fi +if [ "$op" = owner ]; then stat -c %u:%g .; exit 0; fi +if [ "$op" = mode ]; then [ ! -L "$n" ] || exit 4; chmod -- "$m" "$n" || exit 5; exit 0; fi [ ! -e "$n" ] && [ ! -L "$n" ] || exit 4 -o=$(stat -c %u:%g .) -if [ "$op" = dir ]; then mkdir -m 700 -- "$n"; else (umask 077; set -C; cat > "$n"); fi -chown -h -- "$o" "$n"; chmod -- "$m" "$n"'; +if [ "$op" = dir ]; then mkdir -m "$m" -- "$n"; exit 0; fi +(umask 077; set -C; cat > "$n") +chmod -- "$m" "$n" || exit 5'; + $run = function ( $user, $op, $stdin = null ) use ( $path, $mode, $script ) { + return EE::launch( sprintf( 'docker exec %s-u %s %s sh -c %s sh /usr/local/etc %s %s %s%s', $stdin ? '-i ' : '', escapeshellarg( $user ), escapeshellarg( $this->php_container_id ), escapeshellarg( $script ), escapeshellarg( $op ), escapeshellarg( $mode ), escapeshellarg( $path ), $stdin ? ' < ' . escapeshellarg( $stdin ) : '' ) ); + }; + + $owner = $run( 'root', 'owner' ); + $user = trim( $owner->stdout ); + if ( 3 === $owner->return_code || ! preg_match( '/^\d+:\d+$/', $user ) ) { + throw new \Exception( 'a directory above it is not a real directory in its php container' ); + } $tmp = null; if ( 'file' === $op ) { @@ -1893,18 +1923,21 @@ private function php_container_op( $op, $path, $mode, $content = null ) { throw new \Exception( 'could not write a temporary file' ); } } - $result = EE::launch( sprintf( 'docker exec %s-u root %s sh -c %s sh /usr/local/etc %s %s %s%s', $tmp ? '-i ' : '', escapeshellarg( $this->php_container_id ), escapeshellarg( $script ), escapeshellarg( $op ), escapeshellarg( $mode ), escapeshellarg( $path ), $tmp ? ' < ' . escapeshellarg( $tmp ) : '' ) ); + $result = $run( $user, $op, $tmp ); if ( $tmp ) { unlink( $tmp ); } - if ( 3 === $result->return_code ) { - throw new \Exception( 'a directory above it is not a real directory in its php container' ); - } - if ( 4 === $result->return_code ) { - throw new \Exception( 'mode' === $op ? 'it is a symlink in its php container' : 'it appeared in its php container while it was restored' ); - } - if ( $result->return_code ) { - throw new \Exception( 'mode' === $op ? 'could not change it in its php container' : 'it is missing in the site and could not be created in its php container' ); + switch ( $result->return_code ) { + case 0: + return true; + case 5: + return false; + case 3: + throw new \Exception( 'a directory above it is not a real directory in its php container' ); + case 4: + throw new \Exception( 'mode' === $op ? 'it is a symlink in its php container' : 'it appeared in its php container while it was restored' ); + default: + throw new \Exception( 'mode' === $op ? 'could not change it in its php container' : 'it is missing in the site and could not be created in its php container' ); } } From ce4b6b25a03554b1d807776913d00688f4415f99 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Sun, 1 Nov 2026 06:11:19 +0530 Subject: [PATCH 13/18] fix(restore): keep restored php config directories writable until their contents are back A backed-up directory without owner write or search bits (e.g. 0555) was created, or an existing one tightened, before its missing children were created inside it as its owner, so they failed. Directories now get owner rwx while the walk runs and their backed-up modes are applied afterwards, deepest first. --- src/helper/Site_Backup_Restore.php | 34 +++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/helper/Site_Backup_Restore.php b/src/helper/Site_Backup_Restore.php index 7583930c..f56aa39a 100644 --- a/src/helper/Site_Backup_Restore.php +++ b/src/helper/Site_Backup_Restore.php @@ -1728,9 +1728,25 @@ private function restore_php_conf_files( $source ) { $not_restored = []; $skipped = []; $mode_not_set = []; + $dir_modes = []; + $set_mode = function ( $path, $mode ) use ( &$mode_not_set ) { + try { + if ( ! $this->php_container_op( 'mode', $path, $mode ) ) { + throw new \Exception( 'could not change it in its php container' ); + } + + return true; + } catch ( \Exception $e ) { + $mode_not_set[] = "config/php/$path ({$e->getMessage()})"; + + return false; + } + }; foreach ( $entries as $path => $stat ) { $is_dir = 0040000 === ( $stat['mode'] & 0170000 ); $mode = sprintf( '%04o', $stat['mode'] & 0777 ); + // A directory stays writable and searchable by its owner until everything below it is restored. + $mode_now = $is_dir ? sprintf( '%04o', $stat['mode'] & 0777 | 0700 ) : $mode; $parent = dirname( $path ); $content = null; if ( isset( $skipped[ $parent ] ) ) { @@ -1762,8 +1778,10 @@ private function restore_php_conf_files( $source ) { $skipped[ $path ] = true; continue; } - if ( ! $this->php_container_op( $is_dir ? 'dir' : 'file', $path, $mode, $content ) ) { + if ( ! $this->php_container_op( $is_dir ? 'dir' : 'file', $path, $mode_now, $content ) ) { $mode_not_set[] = "config/php/$path (could not change it in its php container)"; + } elseif ( $mode_now !== $mode ) { + $dir_modes[ $path ] = $mode; } } elseif ( $is_dir ) { if ( 0040000 !== ( $current['mode'] & 0170000 ) || realpath( $dest ) !== $dest ) { @@ -1773,13 +1791,9 @@ private function restore_php_conf_files( $source ) { \EE\Site\Utils\put_container_file_contents( $dest, $dir, $content ); } // Not on macOS, where the container doesn't see config/php. - if ( $current && 'Darwin' !== PHP_OS_FAMILY && $mode !== sprintf( '%04o', $current['mode'] & 0777 ) ) { - try { - if ( ! $this->php_container_op( 'mode', $path, $mode ) ) { - throw new \Exception( 'could not change it in its php container' ); - } - } catch ( \Exception $e ) { - $mode_not_set[] = "config/php/$path ({$e->getMessage()})"; + if ( $current && 'Darwin' !== PHP_OS_FAMILY ) { + if ( ( sprintf( '%04o', $current['mode'] & 0777 ) === $mode_now || $set_mode( $path, $mode_now ) ) && $mode_now !== $mode ) { + $dir_modes[ $path ] = $mode; } } } catch ( \Exception $e ) { @@ -1797,6 +1811,10 @@ private function restore_php_conf_files( $source ) { EE::warning( $message ); } } + // Deepest first, so a directory is tightened only after everything below it. + foreach ( array_reverse( $dir_modes, true ) as $path => $mode ) { + $set_mode( $path, $mode ); + } if ( $mode_not_set ) { EE::warning( 'Restored, but could not set the backed-up mode of: ' . implode( ', ', $mode_not_set ) . '.' ); } From f1e54568cee444bc683159735c5a1f272265d1a2 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Sun, 1 Nov 2026 06:13:58 +0530 Subject: [PATCH 14/18] fix(restore): count control-character names against the php config entry limit Names with control characters were refused before the 1000-entry check, so a directory full of them produced unbounded refusals and warnings. The check now runs after the limit, and refused paths are shown escaped. --- src/helper/Site_Backup_Restore.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/helper/Site_Backup_Restore.php b/src/helper/Site_Backup_Restore.php index f56aa39a..ff1958fe 100644 --- a/src/helper/Site_Backup_Restore.php +++ b/src/helper/Site_Backup_Restore.php @@ -1838,12 +1838,18 @@ private function list_php_conf_backup( $source, $path, $recurse, array &$entries if ( '.' !== $parent && ! isset( $entries[ $parent ] ) ) { return true; } + $shown = addcslashes( $path, "\0..\37\177" ); // Refused entries count too, so a crafted tree can't produce unbounded warnings. if ( count( $entries ) + count( $refused ) >= 1000 ) { - $refused[ $path ] = 'the backup holds more than 1000 entries, so this and the rest were not restored'; + $refused[ $shown ] = 'the backup holds more than 1000 entries, so this and the rest were not restored'; return false; } + if ( $shown !== $path ) { + $refused[ $shown ] = 'its name holds control characters'; + + return true; + } $stat = @lstat( "$source/$path" ); if ( ! $stat ) { return true; @@ -1872,10 +1878,6 @@ private function list_php_conf_backup( $source, $path, $recurse, array &$entries if ( '.' === $name || '..' === $name ) { continue; } - if ( preg_match( '/[\x00-\x1f\x7f]/', $name ) ) { - $refused[ $path . '/' . addcslashes( $name, "\0..\37\177" ) ] = 'its name holds control characters'; - continue; - } $more = $this->list_php_conf_backup( $source, "$path/$name", true, $entries, $refused, $depth + 1 ); } closedir( $handle ); From 0b358b8aa2efcae0e55d52483d95eaf3a2685e82 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Sun, 1 Nov 2026 06:16:57 +0530 Subject: [PATCH 15/18] fix(update): roll back a PHP switch when saving the site after enabling it fails enable() exited on a failed database update even when called with exit_on_error false, so update --php ended before its rollback and before saving the PHP version. It now rethrows to the caller in that mode. --- src/helper/class-ee-site.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/helper/class-ee-site.php b/src/helper/class-ee-site.php index b9e4b3e2..1dd35a1d 100644 --- a/src/helper/class-ee-site.php +++ b/src/helper/class-ee-site.php @@ -1408,6 +1408,10 @@ public function enable( $args, $assoc_args, $exit_on_error = true ) { try { update_site_db_entry( $this->site_data['site_url'], $this->site_data ); } catch ( \Exception $e ) { + // A caller that rolls back (update --php) handles it. + if ( ! $exit_on_error ) { + throw $e; + } EE::error( $e->getMessage() ); } From 86b683c0aa2afb2cb1b321ef08379d76412b78cf Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Sun, 1 Nov 2026 06:31:53 +0530 Subject: [PATCH 16/18] fix(restore): create restored php config files with O_EXCL inside the container noclobber only refuses an existing regular file, so a FIFO the container put at the name after the existence check was opened by `cat >` and blocked the docker exec, hanging the restore. dd's conv=excl creates with O_CREAT|O_EXCL and refuses any existing inode without opening it. --- src/helper/Site_Backup_Restore.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/helper/Site_Backup_Restore.php b/src/helper/Site_Backup_Restore.php index ff1958fe..d2c83d59 100644 --- a/src/helper/Site_Backup_Restore.php +++ b/src/helper/Site_Backup_Restore.php @@ -1918,13 +1918,14 @@ private function php_container_op( $op, $path, $mode, $content = null ) { } // Changes into the parent and acts relative to it, so a directory swapped above it afterwards can't redirect the step. + // dd's conv=excl creates with O_EXCL, so anything put there after the check (a FIFO too) is refused without being opened. $script = 'set -e; op=$2; m=$3; p=$1/$4; d=${p%/*}; n=./${p##*/} cd -P -- "$d" 2>/dev/null && [ "$(pwd -P)" = "$d" ] || exit 3 if [ "$op" = owner ]; then stat -c %u:%g .; exit 0; fi if [ "$op" = mode ]; then [ ! -L "$n" ] || exit 4; chmod -- "$m" "$n" || exit 5; exit 0; fi [ ! -e "$n" ] && [ ! -L "$n" ] || exit 4 if [ "$op" = dir ]; then mkdir -m "$m" -- "$n"; exit 0; fi -(umask 077; set -C; cat > "$n") +(umask 077; dd of="$n" conv=excl status=none 2>/dev/null) || { if [ -e "$n" ] || [ -L "$n" ]; then exit 4; fi; exit 1; } chmod -- "$m" "$n" || exit 5'; $run = function ( $user, $op, $stdin = null ) use ( $path, $mode, $script ) { return EE::launch( sprintf( 'docker exec %s-u %s %s sh -c %s sh /usr/local/etc %s %s %s%s', $stdin ? '-i ' : '', escapeshellarg( $user ), escapeshellarg( $this->php_container_id ), escapeshellarg( $script ), escapeshellarg( $op ), escapeshellarg( $mode ), escapeshellarg( $path ), $stdin ? ' < ' . escapeshellarg( $stdin ) : '' ) ); From 68cb66695f7e94ca31cc930fbf297c3006d875fd Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Wed, 4 Nov 2026 16:49:48 +0530 Subject: [PATCH 17/18] ci: retrigger checks From d04c4b4683899dc5c56a7a60115570758781b7e6 Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Mon, 16 Nov 2026 15:40:17 +0530 Subject: [PATCH 18/18] fix(site): keep the php restart and the original error when the rollback can't rewrite docker-compose.yml A failed compose write in roll_back_php_update() is now a warning pointing to `ee site enable --refresh`, so the php container is still started and update_php() still reports the error that triggered the rollback. --- src/helper/class-ee-site.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/helper/class-ee-site.php b/src/helper/class-ee-site.php index 1dd35a1d..df063437 100644 --- a/src/helper/class-ee-site.php +++ b/src/helper/class-ee-site.php @@ -1049,7 +1049,13 @@ private function roll_back_php_update( $rollback, $old_version, $no_https ) { $this->site_data = $rollback['site']; $this->site_data['php_version'] = $old_version; - $this->dump_docker_compose_yml( [ 'nohttps' => $no_https ] ); + try { + $this->dump_docker_compose_yml( [ 'nohttps' => $no_https ] ); + } catch ( \Exception $e ) { + // Keep going so php restarts and the caller reports its error; compose still names the new image, so --refresh, not --force. + $manual = 'Run: ee site enable ' . $rollback['site']['site_url'] . ' --refresh'; + EE::warning( "Could not restore docker-compose.yml to PHP $old_version ({$e->getMessage()}). $manual" ); + } if ( $rollback['stopped'] && ! \EE_DOCKER::docker_compose_up( $site_fs_path, [ 'php' ] ) ) { EE::warning( "Could not start the php container again. $manual" );