Verified Commit db6779be authored by Jess's avatar Jess
Browse files

fix: SA-CORE-2026-017

By: larowlan
By: pameeela
By: scott_euser
By: szeidler
By: tylerni7-ant
By: alexpott
By: benjifisher
By: catch
By: cilefen
By:  david strauss
By: longwave
By: mcdruid
By: mingsong
By: vijaycs85
By: xjm
By: greggles
By: poker10
By: prudloff
parent 1f6d11cc
Loading
Loading
Loading
Loading
+52 −16
Changes for core/includes/install.core.inc: 52 added lines, 16 removed lines.
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
 */

use Drupal\user\LoginFinalizer;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Cache\NullBackend;
@@ -213,6 +214,9 @@ function install_state_defaults(): array {
    'config_verified' => FALSE,
    // TRUE when there is a valid database connection.
    'database_verified' => FALSE,
    // TRUE when a database configured in settings.php failed validation at
    // the beginning of this request.
    'configured_database_failed' => FALSE,
    // TRUE when a valid settings.php exists (containing both database
    // connection information and config directory names).
    'settings_verified' => FALSE,
@@ -506,25 +510,32 @@ function install_begin_request($class_loader, &$install_state): void {
  // accessing the database before it is set up yet.)
  drupal_maintenance_theme();

  if (!$install_state['database_verified']) {
  if (!$install_state['settings_verified']) {
    // Do not install over an existing installation. The call to
    // install_verify_database_ready() will throw an AlreadyInstalledException
    // if this is the case.
    try {
      install_verify_database_ready();
    }
    catch (AlreadyInstalledException $e) {
      throw $e;
    }
    catch (InstallerException) {
      $install_state['configured_database_failed'] = TRUE;
    }
  }
  // Verify the last completed task in the database, if there is one.
  if ($install_state['base_system_verified']) {
    // We already queried for install_task before kernel boot. Use the value
    // we found, but still check for the 'done' state which means Drupal is
    // already fully installed.
    // We already queried for install_task before kernel boot.
    $task = $install_state['completed_task'];
    if ($task === 'done') {
      throw new AlreadyInstalledException($container->get('string_translation'));
    }
  }
  else {
    $task = install_verify_completed_task();
  }
  // The install_time state value is set when the installation finishes.
  if ($task === 'done' || (!empty($task) && \Drupal::state()->get('install_time'))) {
    throw new AlreadyInstalledException($container->get('string_translation'));
  }

  // Ensure that the active configuration is empty before installation starts.
  if ($install_state['config_verified'] && empty($task)) {
@@ -1186,6 +1197,8 @@ function install_verify_database_settings($site_path) {
 *
 * @throws \Drupal\Core\Installer\Exception\AlreadyInstalledException
 *   Thrown when the database already has a Drupal key_value table.
 * @throws \Drupal\Core\Installer\Exception\InstallerException
 *   Thrown when the database cannot be checked.
 */
function install_verify_database_ready(): void {
  $existing_install = FALSE;
@@ -1193,9 +1206,8 @@ function install_verify_database_ready(): void {
    try {
      $existing_install = Database::getConnection()->schema()->tableExists('key_value');
    }
    // Do not trigger an error if the database query fails, since the database
    // might not be set up yet.
    catch (\Exception) {
      throw new InstallerException('The configured database could not be verified.', 'Requirements problem');
    }
  }
  if ($existing_install) {
@@ -1576,12 +1588,26 @@ function install_load_profile(&$install_state): void {
/**
 * Performs a full bootstrap of Drupal during installation.
 */
function install_bootstrap_full(): void {
function install_bootstrap_full(&$install_state): void {
  // Store the session on the request object and start it.
  /** @var \Symfony\Component\HttpFoundation\Session\SessionInterface $session */
  $session = \Drupal::service('session');
  \Drupal::request()->setSession($session);
  $session->start();

  // Only the browser session that reached this point first can continue the
  // installation.
  if ($install_state['interactive']) {
    $hash = \Drupal::state()->get('install_session_token');
    if ($hash === NULL) {
      $token = Crypt::randomBytesBase64();
      $session->set('install_session_token', $token);
      \Drupal::state()->set('install_session_token', Crypt::hashBase64($token));
    }
    elseif (!hash_equals($hash, Crypt::hashBase64((string) $session->get('install_session_token')))) {
      throw new InstallerException('The installation can only be continued in the browser session that started it.', 'Access denied');
    }
  }
}

/**
@@ -1898,6 +1924,8 @@ function install_finished(&$install_state): void {

  // Record when this install ran.
  \Drupal::state()->set('install_time', \Drupal::time()->getRequestTime());
  \Drupal::state()->delete('install_session_token');
  \Drupal::request()->getSession()->remove('install_session_token');
}

/**
@@ -2140,8 +2168,8 @@ function install_check_requirements($install_state) {
    if (drupal_verify_install_file($site_path, FILE_EXIST, 'dir')) {
      if (drupal_verify_install_file($file, FILE_EXIST)) {
        // If it does, make sure it is writable.
        $readable = drupal_verify_install_file($file, FILE_READABLE);
        $writable = drupal_verify_install_file($file, FILE_WRITABLE);
        $readable = drupal_verify_install_file($file, FILE_READABLE, auto_fix: FALSE);
        $writable = drupal_verify_install_file($file, FILE_WRITABLE, auto_fix: FALSE);
        $exists = TRUE;
      }
    }
@@ -2174,8 +2202,8 @@ function install_check_requirements($install_state) {
        // keep the file in place. Additionally, this situation also occurs when
        // the test runner is being run be different user than the webserver.
        if (fileowner($default_file) === fileowner($file) || DRUPAL_TEST_IN_CHILD_SITE) {
          $readable = drupal_verify_install_file($file, FILE_READABLE);
          $writable = drupal_verify_install_file($file, FILE_WRITABLE);
          $readable = drupal_verify_install_file($file, FILE_READABLE, auto_fix: FALSE);
          $writable = drupal_verify_install_file($file, FILE_WRITABLE, auto_fix: FALSE);
          $exists = TRUE;
        }
        // If $file and $default_file have different owners, this probably means
@@ -2199,8 +2227,8 @@ function install_check_requirements($install_state) {
          $exists = !$deleted;
          if ($exists) {
            $settings_file_ownership_error = TRUE;
            $readable = drupal_verify_install_file($file, FILE_READABLE);
            $writable = drupal_verify_install_file($file, FILE_WRITABLE);
            $readable = drupal_verify_install_file($file, FILE_READABLE, auto_fix: FALSE);
            $writable = drupal_verify_install_file($file, FILE_WRITABLE, auto_fix: FALSE);
          }
        }
      }
@@ -2292,6 +2320,14 @@ function install_check_requirements($install_state) {
      }
    }
  }
  if ($install_state['configured_database_failed'] && !isset($requirements['database_install_errors'])) {
    $requirements['database_install_errors'] = [
      'title' => t('Database settings'),
      'value' => t('The configured database could not be verified.'),
      'description' => t('Check the database connection and try again.'),
      'severity' => RequirementSeverity::Error,
    ];
  }
  return $requirements;
}