-
-
Notifications
You must be signed in to change notification settings - Fork 998
Expand file tree
/
Copy pathTrustProxies.php
More file actions
67 lines (56 loc) · 1.89 KB
/
Copy pathTrustProxies.php
File metadata and controls
67 lines (56 loc) · 1.89 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
59
60
61
62
63
64
65
66
67
<?php
namespace Leantime\Core\Middleware;
use Closure;
use Leantime\Core\Configuration\Environment;
use Leantime\Core\Http\IncomingRequest;
use Symfony\Component\HttpFoundation\Response;
/**
* Class TrustProxies
*
* The TrustProxies class is responsible for handling incoming requests and checking if they are from trusted proxies.
*/
class TrustProxies
{
/**
* The trusted proxies for this application.
*
* @var array
*/
protected $proxies = [];
/**
* The headers that should be used to detect proxies.
*
* @var int
*/
protected $headers = IncomingRequest::HEADER_X_FORWARDED_FOR |
IncomingRequest::HEADER_X_FORWARDED_HOST |
IncomingRequest::HEADER_X_FORWARDED_PORT |
IncomingRequest::HEADER_X_FORWARDED_PROTO |
IncomingRequest::HEADER_X_FORWARDED_AWS_ELB;
/**
* Constructor for the class.
*
* @param Environment $config An instance of the Environment class.
*/
public function __construct(Environment $config)
{
$this->proxies = explode(',', ($config->trustedProxies ?? '127.0.0.1,REMOTE_ADDR'));
}
/**
* Handle the incoming request and pass it to the next middleware.
* If the request is not from a trusted proxy, it returns a response with an error message.
*
* @param IncomingRequest $request The incoming request.
* @param Closure $next The next middleware closure.
* @return Response The response returned by the next middleware.
*/
public function handle(IncomingRequest $request, Closure $next): Response
{
// Trusted proxies config is set in LoadConfig
// $request::setTrustedProxies($this->proxies, $this->headers);
if (! $request->isFromTrustedProxy()) {
return new Response(json_encode(['error' => 'Not a trusted proxy']), 403);
}
return $next($request);
}
}