forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrustHosts.php
More file actions
39 lines (32 loc) · 1.16 KB
/
TrustHosts.php
File metadata and controls
39 lines (32 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
namespace ProcessMaker\Http\Middleware;
use Illuminate\Http\Middleware\TrustHosts as Middleware;
use Closure;
use Illuminate\Http\Request;
class TrustHosts extends Middleware
{
public function hosts(): array
{
$trustedHost = $this->allSubdomainsOfApplicationUrl();
return [$trustedHost];
}
public function handle(Request $request, $next)
{
if ($request->hasHeader('X-Forwarded-Host')) {
$forwardedHost = $request->header('X-Forwarded-Host');
$trustedPattern = $this->allSubdomainsOfApplicationUrl();
if (!$this->hostIsValid($forwardedHost, $trustedPattern)) {
\Log::warning('Rejected request with untrusted X-Forwarded-Host', [
'forwarded_host' => $forwardedHost,
'trusted_pattern' => $trustedPattern
]);
abort(400, 'Invalid Host Header');
}
}
return parent::handle($request, $next);
}
protected function hostIsValid(string $host, string $pattern): bool
{
return preg_match('/' . str_replace('/', '\/', $pattern) . '/', $host) === 1;
}
}