Skip to content

Commit f638d3f

Browse files
Ben ThomsonLuke Towers
authored andcommitted
Add app.trustedHosts config and force host checks on password reset (#5423)
Add app.trustedHosts config and force host checks on backend password reset. Related: octobercms/library@f29865a (cherry picked from commit f555ab6)
1 parent ea67b61 commit f638d3f

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

config/app.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,36 @@
4343

4444
'url' => 'http://localhost',
4545

46+
/*
47+
|--------------------------------------------------------------------------
48+
| Trusted hosts
49+
|--------------------------------------------------------------------------
50+
|
51+
| You may specify valid hosts for your application as an array or boolean
52+
| below. This helps prevent host header poisoning attacks.
53+
|
54+
| Possible values:
55+
| - `true`: Trust the host specified in app.url, as well as the "www"
56+
| subdomain, if applicable.
57+
| - `false`: Disable the trusted hosts feature.
58+
| - array: Defines the domains to be trusted hosts. Each item should be
59+
| a string defining a domain, IP address, or a regex pattern.
60+
|
61+
| Example of array values:
62+
|
63+
| 'trustedHosts' => [
64+
| 'example.com', // Matches just example.com
65+
| 'www.example.com', // Matches just www.example.com
66+
| '^(.+\.)?example\.com$', // Matches example.com and all subdomains
67+
| 'https://example.com', // Matches just example.com
68+
| ],
69+
|
70+
| NOTE: Even when set to `false`, this functionality is explicitly enabled
71+
| on the Backend password reset flow for security reasons.
72+
*/
73+
74+
'trustedHosts' => true,
75+
4676
/*
4777
|--------------------------------------------------------------------------
4878
| Application Timezone

modules/backend/controllers/Auth.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use ValidationException;
1414
use Exception;
1515
use Config;
16+
use October\Rain\Foundation\Http\Middleware\CheckForTrustedHost;
1617

1718
/**
1819
* Authentication controller
@@ -147,6 +148,20 @@ public function restore()
147148
*/
148149
public function restore_onSubmit()
149150
{
151+
// Force Trusted Host verification on password reset link generation
152+
// regardless of config to protect against host header poisoning
153+
$trustedHosts = Config::get('app.trustedHosts', false);
154+
if ($trustedHosts === false) {
155+
$hosts = CheckForTrustedHost::processTrustedHosts(true);
156+
157+
if (count($hosts)) {
158+
Request::setTrustedHosts($hosts);
159+
160+
// Trigger the host validation logic
161+
Request::getHost();
162+
}
163+
}
164+
150165
$rules = [
151166
'login' => 'required|between:2,255'
152167
];

0 commit comments

Comments
 (0)