Skip to content

Commit afcfd84

Browse files
committed
ip address middleware
1 parent 0644e18 commit afcfd84

1 file changed

Lines changed: 2 additions & 19 deletions

File tree

‎src/App/src/Middleware/IpAddressMiddleware.php‎

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,13 @@
3131

3232
use const FILTER_VALIDATE_IP;
3333

34-
/**
35-
* Determines the client IP address and stores it in the `ip_address` request attribute,
36-
* the same way akrabat/ip-address-middleware does.
37-
*
38-
* Forwarding headers are read only when REMOTE_ADDR is a trusted proxy, because any client can
39-
* send them with any value. With no trusted proxies configured, the attribute is REMOTE_ADDR.
40-
*/
4134
class IpAddressMiddleware implements MiddlewareInterface
4235
{
4336
public const string ATTRIBUTE = 'ip_address';
4437

4538
/**
46-
* @param list<string> $trustedProxies IP addresses or CIDR ranges, e.g. `10.0.0.1` or `173.245.48.0/20`
47-
* @param list<string> $headers checked in order; the first one that gives an address wins
39+
* @param list<string> $trustedProxies
40+
* @param list<string> $headers
4841
*/
4942
#[Inject(
5043
'config.ip_address.trusted_proxies',
@@ -90,10 +83,6 @@ private function fromHeader(ServerRequestInterface $request, string $header): ?s
9083
$hops[] = $this->normalize(strcasecmp($header, 'Forwarded') === 0 ? $this->forwardedFor($hop) : $hop);
9184
}
9285

93-
/**
94-
* Each proxy appends the address it received the request from, so walk from the right: the first
95-
* address that is not one of our own proxies is the client. Anything left of it could be forged.
96-
*/
9786
foreach (array_reverse($hops) as $hop) {
9887
if ($hop === null) {
9988
return null;
@@ -106,9 +95,6 @@ private function fromHeader(ServerRequestInterface $request, string $header): ?s
10695
return $hops[0];
10796
}
10897

109-
/**
110-
* Returns the `for=` value of one RFC 7239 element, e.g. `for=192.0.2.60;proto=https;by=203.0.113.43`.
111-
*/
11298
private function forwardedFor(string $element): string
11399
{
114100
foreach (explode(';', $element) as $pair) {
@@ -121,9 +107,6 @@ private function forwardedFor(string $element): string
121107
return '';
122108
}
123109

124-
/**
125-
* Strips quotes, IPv6 brackets and ports; returns null for anything that is not an IP address.
126-
*/
127110
private function normalize(mixed $value): ?string
128111
{
129112
if (! is_string($value)) {

0 commit comments

Comments
 (0)