#2221699: HTTP_HOST header cannot be trusted will enable the Symfony trusted host mechanism as an opt-in protection, configured from settings.php. If the setting is empty, the trusted host mechanism is not enabled.

The initial trusted host settings should be set in the installer, either automatically, or through an additional setting on the CONFIGURE SITE step of the process.

Issue fork drupal-2404259

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mpdonadio’s picture

Assigned: Unassigned » mpdonadio
Status: Postponed » Active

Parent issue was committed, so setting this Active. Initial patch for discussion purposes will be coming later tonight.

mpdonadio’s picture

Assigned: mpdonadio » Unassigned
Status: Active » Needs review
StatusFileSize
new827 bytes

Ok, here is a starting point. This hunk was removed from #2221699: HTTP_HOST header cannot be trusted. It presets the trusted host to the one that the web installer was run as.

dawehner’s picture

Issue tags: +Needs tests

I guess we want to add some test coverage for that bit of code as well.

iantresman’s picture

Version: 8.0.x-dev » 8.0.0-beta15
Priority: Normal » Critical

Being bold setting this to critical, but having just installed beta 15, my status reports shows that the "Trusted Host Settings" is "Not enabled". This is surely critical as it states:

  • This can lead to security vulnerabilities.
  • It is highly recommended that you configure this.

Isn't this trivial to configure in the installer, by setting "trusted_host_patterns" in settings.php to the base URL, and localhost?

platinum1’s picture

I assume a lot of people won't be happy with a new critical so close to the intended RC1 release date, but this is a great usability improvement, especially for those new to Drupal.

tim.plunkett’s picture

Version: 8.0.0-beta15 » 8.0.x-dev
Priority: Critical » Major

This is not a security issue, and calling it a security improvement is borderline. I'd say this is a feature request, but leaving for now.
Also all patches are against the dev branch, we can't change beta15.

mpdonadio’s picture

#6, the discussion of why this wasn't part of the installer starts at #2221699-58: HTTP_HOST header cannot be trusted and picks up again at #2221699-112: HTTP_HOST header cannot be trusted. The main issue has to do with local/remote workflows.

The only other option that I can think of is to add a checkbox w/ message that enables/disables the logic in the patch in #2.

c-logemann’s picture

I'd like to have a possibility for configuring the trusted host at install process. But it should be optional for several reasons I can imagine.
On a second step it would be fine if this setting is changeable. There could be a textfield where the actual host can used as default value.

Another idea is a support for CLI installation. But for this it's easy to create a drush command based on site-alias domain setting.

aaronbauman’s picture

RTBC +1

Also: this really feels like a UX bug.
The current situation is directly opposed with D8's goals for increasing adoption and improving UX.
Immediately upon install, every user will be faced with the menacing error message
"One or more problems were detected with your Drupal installation."
Then, upon clicking through, they're sent to an esoteric explanation of a complex topic, with the actual solution buried 2 screens down.
All this confusion could have been avoided if the installer had simply detected or asked what the user to provide a base url.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dkre’s picture

Yeah I have to agree with #11.

Ideally you shouldn't need to google a problem as soon as you've installed new software.

Yes there is a help message: "The trusted_host_patterns setting is not configured in settings.php." but at first contact (with Drupal) the user won't know where this file is or what it's for. A red error message will make them feel like something is very wrong and will likely assume their installation process, hosting config or something external to the software is possibly creating a conflict or is at fault.

If trusted_hosts being unset isn't a problem or should be optional then surely it makes sense to remove the warning/error and lower the warning level on the status page notification to prevent users thinking their installation is broken or they're doing something wrong.

aaronbauman’s picture

Version: 8.3.x-dev » 8.4.x-dev
Category: Task » Bug report
Issue tags: +D8UX usability

More than a year since Drupal 8 stable dropped, and still the first thing a new admin sees is a bright red error message.

Maybe changing this issue to a UX bug will get it some more attention.

cilefen’s picture

Status: Needs review » Needs work

I agree this doesn't look good on installs but I can't commit it without regression tests.

alexpott’s picture

We also need to bear in my mind that a fair few hosters don't allow settings.php to be written during an install. See parts of #2156401: Write install_profile value to configuration and only to settings.php if it is writeable for a long and big discussion of this.

cilefen’s picture

Component: base system » install system
Category: Bug report » Task
Issue tags: +Triaged core major

Thank you, everyone, for working on this issue. @alexpott, @catch, @cottser, @xjm and I considered this issue and agreed this should be a major priority task in the install system.

jacobsanford’s picture

The patch in #1 no longer applied to 8.4.x. A reroll with no further modifications is attached.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

donquixote’s picture

I agree with the issue.

But what we describe and agree on the intended behavior, and add it to the issue summary, before posting patches?

I think it is important that a user sees what will be registered as trusted host pattern.
I don't think the installer should do this silently.

Easy solution

Provide a checkbox in the installer: "Only allow requests from detected.hostname.com. This setting can later be changed in the settings.php under $settings['trusted_host_patterns']."
The checkbox could be enabled by default.

Advanced solution

Let the installer show a form element, where a user can define which hosts should be trusted.
This should be pre-filled with the host from the current request.
It could be a textarea, where each line is a host.
Initially, this will not use regular expressions, but the plain host as-is. So it would say "example.com", not "^example\.com$".

There could be a checkbox or something to switch this to regular expression mode.
But maybe this is not even needed. People who need those could do it in settings.php directly.

Add a description / help text, explaining that this will be converted into a regular expression in the settings.php, allowing to add additional trusted hosts later.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

purencool’s picture

The attached patch serves as a proof of concept to extend the functionality of the installer's SiteConfigureForm.php class. It enables the site installer to easily add domains to the trusted_host_patterns found in the settings.php file. The idea came from comment #22 which is mentioned below.

Advanced solution
Let the installer show a form element, where a user can define which hosts should be trusted.
This should be pre-filled with the host from the current request.
It could be a textarea, where each line is a host.
Initially, this will not use regular expressions, but the plain host as-is. So it would say "example.com", not "^example\.com$".

There could be a checkbox or something to switch this to regular expression mode.
But maybe this is not even needed. People who need those could do it in settings.php directly.

Add a description / help text, explaining that this will be converted into a regular expression in the settings.php, allowing to add additional trusted hosts later.

Initial Criteria

In order to ensure a developer-friendly experience while maintaining usability for first-time Drupal installation, I have compiled the following criteria for consideration:

  1. The installation input text area is not required.
  2. Multiple trusted host patterns could be added on install.
  3. Some initial testing to see if the domain is relevant.
  4. Allow domains like localhost could be added.
  5. Noncomplex patterns can be added see these examples.
  6. Automate characters like ^,$,/.

Current solution

The installation screen

Results in the settings.php

$settings['trusted_host_patterns'] = array (
  0 => '^localhost$',
  1 => '^testing\\.domain\\.com$',
  2 => '^domain\\.com$',
);

Know Issues

I'm currently experiencing difficulty in finding a solution to prevent two "\\" from being written into the settings.php in the trusted_host_patterns array. It appears that the SettingsEditor::rewrite applies a regular expression and adds an another forward slash. However, it is not clear to me how this happens.
https://git.drupalcode.org/project/drupal/-/blob/11.x/core/lib/Drupal/Co...

SettingsEditor::rewrite($settings_path, $settings);

The second main issue relates to the permission settings of the settings.php file. During the installation process, the code requires write permissions to be granted. However, it is necessary to overwrite the existing permissions using PHP and then reset them again so that the trusted_host_patterns array can write to the file. I can't seem to find another solution to resolve this without changing the permissions and don't know if it's good practice.

        $settings_path = $this->sitePath . '/settings.php';
        chmod($settings_path, 0744);
        SettingsEditor::rewrite($settings_path, $settings);
        chmod($settings_path, 0444);
purencool’s picture

StatusFileSize
new5.43 KB

The bot didn't recognize "recognise" in the translation strings.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

prudloff made their first commit to this issue’s fork.