Skip to content

Commit

Permalink
Merge pull request #2 from unleashedtech/hotfix/trusted-host-patterns…
Browse files Browse the repository at this point in the history
…-domains-sites

Fixing getSettings method to support domains & sites in the trusted_host_patterns array. Updating change log.
  • Loading branch information
dl-unleashed-technologies authored Mar 30, 2022
2 parents 3d79c90 + 5ea19da commit b4eeead
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi

## [Unreleased][unreleased]

## [0.2.1] - 2022-03-30

### Fixed

- Fixed construction of `trusted_host_patterns` array to support sites defined in the `SITES` constant.

## [0.2.0] - 2022-03-14

### Changed
Expand Down Expand Up @@ -135,7 +141,8 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi

**Initial release!**

[unreleased]: https://github.com/unleashedtech/dotenv-drupal/compare/0.2.0...main
[unreleased]: https://github.com/unleashedtech/dotenv-drupal/compare/0.2.1...main
[0.2.1]: https://github.com/unleashedtech/dotenv-drupal/compare/0.2.0...0.2.1
[0.2.0]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.17...0.2.0
[0.1.17]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.16...0.1.17
[0.1.16]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.15...0.1.16
Expand Down
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,31 @@ so the shorter DSN can be used.
Streamlined environment-dependent configuration of _one_ Solr core is supported at this time.

#### TRUSTED_HOST_PATTERNS
Optional. A CSV list of patterns specifying [trusted hosts](https://www.drupal.org/docs/installing-drupal/trusted-host-settings#s-protecting-in-drupal-8). If this
variable is not set, this package will populate Drupal's `trusted_host_patterns` array
based on the value of [DOMAINS](#domains).
Optional. A CSV list of patterns specifying [trusted hosts](https://www.drupal.org/docs/installing-drupal/trusted-host-settings#s-protecting-in-drupal-8).

Start (`^`) & end (`$`) characters are added to every pattern, by default.

##### Fallback Functionality
If the `TRUSTED_HOST_PATTERNS` variable is _not_ set, this package will populate
Drupal's `trusted_host_patterns` array based on the values of [DOMAINS](#domains)
& [SITES](#sites) variables.

###### Examples
If you have `DOMAINS` set to `foo.example,bar.example`, the Drupal `trusted_host_patterns`
settings array would have the following values:

* `^foo\.example$`
* `^www\.foo\.example$`
* `^bar\.example$`
* `^www\.bar\.example$`

If you have `DOMAINS` set to `foo.example` & `SITES` set to `bar,baz,qux`, the
Drupal `trusted_host_patterns` setting array would have the following values:

* `^bar\.foo\.example$`
* `^baz\.foo\.example$`
* `^qux\.foo\.example$`

#### Configuration Conclusion
With these few Environment Variables, you will be able to configure Drupal in a streamlined
fashion similar to the way Symfony is configured. Support for many more common Drupal features
Expand Down
25 changes: 24 additions & 1 deletion src/Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,11 @@ public function setDatabaseName(string $database): void
$this->databaseName = $database;
}

public function isMultiSite(): bool
{
return count($this->getSites()) > 1;
}

public function isMultiSiteDefaultSiteAllowed(): bool
{
return $this->isMultiSiteDefaultSiteAllowed;
Expand Down Expand Up @@ -287,14 +292,32 @@ public function getSettings(): array
if (isset($_SERVER['HASH_SALT'])) {
$settings['hash_salt'] = $_SERVER['HASH_SALT'];
}

// Configure trusted host patterns.
// @see https://github.com/unleashedtech/dotenv-drupal/blob/main/README.md#trusted_host_patterns
$settings['trusted_host_patterns'] = [];
if (isset($_SERVER['TRUSTED_HOST_PATTERNS'])) {
foreach (explode(',', $_SERVER['TRUSTED_HOST_PATTERNS']) as $pattern) {
$settings['trusted_host_patterns'][] = '^' . $pattern . '$';
}
}
else {
foreach ($this->getDomains() as $domain) {
$settings['trusted_host_patterns'][] = '^' . str_replace('.', '\.', $domain) . '$';
if (! $this->isMultiSite() || $this->isMultiSiteDefaultSiteAllowed()) {
$settings['trusted_host_patterns'][] = '^' . \str_replace('.', '\.', $domain) . '$';
$settings['trusted_host_patterns'][] = '^www\.' . \str_replace('.', '\.', $domain) . '$';
}

foreach ($this->getSites() as $site) {
if ($site === 'default') {
continue;
}

$settings['trusted_host_patterns'][] = \vsprintf('^%s\.%s$', [
$site,
\str_replace('.', '\.', $domain),
]);
}
}
}

Expand Down

0 comments on commit b4eeead

Please sign in to comment.