Repository navigation
fix(restore): drop the deprecated session ID settings from a restored php.ini on PHP 8.4+ #536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -112,6 +112,9 @@ class Site_Backup_Restore { | |
| // metadata.json of the backup being restored. | ||
| private $restore_metadata = []; | ||
|
|
||
| // [ is_warning, message ] of drop_deprecated_session_sid_settings(), printed once the restore is committed. | ||
| private $sid_settings_notice = null; | ||
|
|
||
| private $restore_in_progress = false; | ||
|
|
||
| // WordPress root the running backup excludes core from ('' = core is archived). | ||
|
|
@@ -1947,6 +1950,16 @@ private function apply_restore_transaction( $backup_dir ) { | |
|
|
||
| $this->commit_restore_transaction(); | ||
|
|
||
| if ( $this->sid_settings_notice ) { | ||
| list( $is_warning, $message ) = $this->sid_settings_notice; | ||
| $this->sid_settings_notice = null; | ||
| if ( $is_warning ) { | ||
| EE::warning( $message ); | ||
| } else { | ||
| EE::log( $message ); | ||
| } | ||
| } | ||
|
|
||
| if ( 'wp' === $this->site_data['site_type'] ) { | ||
| $flush = EE::launch( $this->get_ee_shell_command( 'wp cache flush --skip-plugins --skip-themes' ) ); | ||
| if ( $flush->return_code ) { | ||
|
|
@@ -3590,6 +3603,10 @@ private function restore_php_conf_files( $source, array $entries ) { | |
| if ( realpath( $dir ) !== $dir ) { | ||
| throw new \Exception( "config/php/$parent is not a directory, or a directory above it is a symlink" ); | ||
| } | ||
| // Not when it is skipped below. | ||
| if ( 'php/php.ini' === $path && ! $is_dir && ( $current || 'Darwin' !== PHP_OS_FAMILY ) ) { | ||
| $content = $this->drop_deprecated_session_sid_settings( $content, $source, $entries ); | ||
| } | ||
| 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 ) { | ||
|
|
@@ -3628,6 +3645,69 @@ private function restore_php_conf_files( $source, array $entries ) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * A backup taken before v4.14.0 brings back session.sid_length and session.sid_bits_per_character, which PHP 8.4+ | ||
| * reports as deprecated, and the migration that removed them doesn't run again; remove them the same way. | ||
| * | ||
| * @param string $content php.ini from the backup. | ||
| * @param string $source Extracted `php` directory of the backup. | ||
| * @param array $entries Path => lstat() from list_php_conf_backup(). | ||
| * | ||
| * @return string The content to restore. | ||
| */ | ||
| private function drop_deprecated_session_sid_settings( $content, $source, array $entries ) { | ||
| $php_version = (string) ( $this->site_data['php_version'] ?? '' ); | ||
| if ( ! \EE\Site\Utils\php_deprecates_session_sid_settings( $php_version ) ) { | ||
| return $content; | ||
| } | ||
|
|
||
| $custom = 'php/conf.d/custom.ini'; | ||
| try { | ||
| list( $new_content, $removed ) = \EE\Site\Utils\strip_session_sid_settings( $content ); | ||
| // Only the shipped quick reference: nothing to remove. | ||
| if ( ! $removed ) { | ||
| return $content; | ||
| } | ||
| // PHP reads conf.d/custom.ini after php.ini and resolves its ${...} from there. | ||
| if ( isset( $entries[ $custom ] ) && 0100000 === ( $entries[ $custom ]['mode'] & 0170000 ) | ||
| && \EE\Site\Utils\references_session_sid_settings( \EE\Site\Utils\get_container_file_contents( "$source/$custom", realpath( $source ) . '/php/conf.d' ) ) ) { | ||
|
Comment on lines
+3672
to
+3673
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check the If the backup omits
|
||
| throw new \Exception( 'the restored custom.ini refers to them as ${...}, which PHP would then read as empty or as its fallback, so the file was left as it is; remove session.sid_length and session.sid_bits_per_character by hand.' ); | ||
| } | ||
|
|
||
| // Next to the migration's copies: `ee site backup` and `ee site restore` empty EE_BACKUP_DIR/<site>/. | ||
| $root = EE_BACKUP_DIR . '/session-sid-settings-' . date( 'Ymd-His' ); | ||
| $file = '/' . $this->site_data['site_url'] . '/php.ini'; | ||
| if ( file_exists( $root . $file ) ) { | ||
| $root .= '-' . getmypid(); | ||
|
Comment on lines
+3680
to
+3681
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make each saved php.ini copy unreachable by other writers in the same second. The migration and the restore both build their copy paths from Require that both writers reserve their copy path atomically or hold a common lock. That lock must cover the path selection and the write. Use the same exclusion in the migration and the restore. Do not write to a path that already exists.
|
||
| } | ||
| try { | ||
| $this->fs->mkdir( dirname( $root . $file ), 0700 ); | ||
| $this->fs->dumpFile( $root . $file, $content ); | ||
| } catch ( \Exception $e ) { | ||
| // rmdir() refuses a dir that holds other sites' copies. | ||
| @rmdir( dirname( $root . $file ) ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- best effort. | ||
| @rmdir( $root ); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- best effort. | ||
| throw new \Exception( "could not save a copy of it ({$e->getMessage()}), so it was left as it is; remove session.sid_length and session.sid_bits_per_character by hand." ); | ||
| } | ||
| } catch ( \Exception $e ) { | ||
| EE::warning( 'Could not remove the session ID settings deprecated as of PHP 8.4 from the restored php.ini: ' . $e->getMessage() ); | ||
|
|
||
| return $content; | ||
| } | ||
|
|
||
| $lines = array_map( | ||
| function ( $line ) { | ||
| return "`$line`"; | ||
| }, | ||
| array_keys( $removed ) | ||
| ); | ||
| $message = sprintf( 'Removed %s from the restored php.ini: deprecated as of PHP 8.4, so PHP now uses its default. The php.ini from the backup is saved as %s.', implode( ', ', $lines ), $root . $file ); | ||
| // Only a value the user set is worth a warning, as in the migration. Printed after the commit: a rollback puts the previous php.ini back. | ||
| $this->sid_settings_notice = [ in_array( false, $removed, true ), $message ]; | ||
|
|
||
| return $new_content; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $site_dir Site root. | ||
| * @param string $path Path below config/php. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Injection
Reachability: External
Exploitability: Moderate
CWE: CWE-150
View Security blast radius
The restore notice prints the full archived php.ini line, including any trailing comment, to the terminal.
The notice keeps the raw assignment text. Neutralize or drop that text before it reaches
EE::warning()orEE::log(). Show only the directive name, or a form with control characters removed, so archive content cannot change the terminal output.Location: src/helper/Site_Backup_Restore.php, lines 3698-3706 (message construction) and 1953-1961 (emission after commit).
Keep the entries distinct when the same directive appears more than once with different values. Keying the removed entries by directive name alone would merge them.