-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathTrustProxies.php
More file actions
58 lines (49 loc) · 1.76 KB
/
Copy pathTrustProxies.php
File metadata and controls
58 lines (49 loc) · 1.76 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Middleware\TrustProxies as TrustedProxyMiddleware;
use Illuminate\Http\Request;
class TrustProxies extends TrustedProxyMiddleware
{
/**
* The trusted proxy headers computed from config('trustedproxy.headers').
*
* Overrides the parent's $headers property/headers() fallback so that an
* empty or entirely invalid APP_TRUSTED_HEADERS results in trusting *no*
* headers, rather than silently falling back to the framework's default
* header set. See headers().
*
* @var int
*/
protected int $headerBitmask = 0;
public function __construct()
{
$header_bitmask = 0;
foreach (explode(",", config('trustedproxy.headers')) as $header) {
$header = trim($header);
if (!$header) {
continue;
}
try {
$header_bitmask |= constant(Request::class . "::$header"); // once we get to PHP 8.3, this can become Request::{$header}
} catch (\Throwable $e) {
\Log::error("Error parsing APP_TRUSTED_HEADERS: " . $header . " is not a valid setting, ignoring");
}
}
\Log::debug("Final header bitmask: $header_bitmask");
$this->headerBitmask = $header_bitmask;
// note, we do *not* need to also set the Proxies themselves since the middleware does that itself.
}
/**
* Get the trusted headers.
*
* Deliberately does not fall back to the parent's hardcoded default
* header set: an empty/unconfigured APP_TRUSTED_HEADERS must mean *no*
* headers are trusted, not "trust everything".
*
* @return int
*/
protected function headers()
{
return $this->headerBitmask;
}
}