|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Light\App\Middleware; |
| 6 | + |
| 7 | +use Dot\DependencyInjection\Attribute\Inject; |
| 8 | +use Psr\Http\Message\ResponseInterface; |
| 9 | +use Psr\Http\Message\ServerRequestInterface; |
| 10 | +use Psr\Http\Server\MiddlewareInterface; |
| 11 | +use Psr\Http\Server\RequestHandlerInterface; |
| 12 | + |
| 13 | +use function array_reverse; |
| 14 | +use function ctype_digit; |
| 15 | +use function explode; |
| 16 | +use function filter_var; |
| 17 | +use function inet_pton; |
| 18 | +use function intdiv; |
| 19 | +use function is_string; |
| 20 | +use function ord; |
| 21 | +use function str_contains; |
| 22 | +use function str_starts_with; |
| 23 | +use function strcasecmp; |
| 24 | +use function strlen; |
| 25 | +use function strncmp; |
| 26 | +use function strpos; |
| 27 | +use function strrpos; |
| 28 | +use function substr; |
| 29 | +use function substr_count; |
| 30 | +use function trim; |
| 31 | + |
| 32 | +use const FILTER_VALIDATE_IP; |
| 33 | + |
| 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 | + */ |
| 41 | +class IpAddressMiddleware implements MiddlewareInterface |
| 42 | +{ |
| 43 | + public const string ATTRIBUTE = 'ip_address'; |
| 44 | + |
| 45 | + /** |
| 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 |
| 48 | + */ |
| 49 | + #[Inject( |
| 50 | + 'config.ip_address.trusted_proxies', |
| 51 | + 'config.ip_address.headers', |
| 52 | + )] |
| 53 | + public function __construct( |
| 54 | + private readonly array $trustedProxies = [], |
| 55 | + private readonly array $headers = ['Forwarded', 'X-Forwarded-For', 'X-Real-IP'], |
| 56 | + ) { |
| 57 | + } |
| 58 | + |
| 59 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
| 60 | + { |
| 61 | + return $handler->handle($request->withAttribute(self::ATTRIBUTE, $this->determineIpAddress($request))); |
| 62 | + } |
| 63 | + |
| 64 | + public function determineIpAddress(ServerRequestInterface $request): ?string |
| 65 | + { |
| 66 | + $remoteAddr = $this->normalize($request->getServerParams()['REMOTE_ADDR'] ?? null); |
| 67 | + if ($remoteAddr === null || ! $this->isTrustedProxy($remoteAddr)) { |
| 68 | + return $remoteAddr; |
| 69 | + } |
| 70 | + |
| 71 | + foreach ($this->headers as $header) { |
| 72 | + $ipAddress = $this->fromHeader($request, $header); |
| 73 | + if ($ipAddress !== null) { |
| 74 | + return $ipAddress; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return $remoteAddr; |
| 79 | + } |
| 80 | + |
| 81 | + private function fromHeader(ServerRequestInterface $request, string $header): ?string |
| 82 | + { |
| 83 | + $line = $request->getHeaderLine($header); |
| 84 | + if ($line === '') { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + $hops = []; |
| 89 | + foreach (explode(',', $line) as $hop) { |
| 90 | + $hops[] = $this->normalize(strcasecmp($header, 'Forwarded') === 0 ? $this->forwardedFor($hop) : $hop); |
| 91 | + } |
| 92 | + |
| 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 | + */ |
| 97 | + foreach (array_reverse($hops) as $hop) { |
| 98 | + if ($hop === null) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + if (! $this->isTrustedProxy($hop)) { |
| 102 | + return $hop; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return $hops[0]; |
| 107 | + } |
| 108 | + |
| 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 | + */ |
| 112 | + private function forwardedFor(string $element): string |
| 113 | + { |
| 114 | + foreach (explode(';', $element) as $pair) { |
| 115 | + $parts = explode('=', $pair, 2); |
| 116 | + if (isset($parts[1]) && strcasecmp(trim($parts[0]), 'for') === 0) { |
| 117 | + return $parts[1]; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + return ''; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Strips quotes, IPv6 brackets and ports; returns null for anything that is not an IP address. |
| 126 | + */ |
| 127 | + private function normalize(mixed $value): ?string |
| 128 | + { |
| 129 | + if (! is_string($value)) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + $value = trim(trim($value), '"'); |
| 134 | + if (str_starts_with($value, '[')) { |
| 135 | + $end = strpos($value, ']'); |
| 136 | + $value = $end === false ? '' : substr($value, 1, $end - 1); |
| 137 | + } elseif (substr_count($value, ':') === 1) { |
| 138 | + $value = substr($value, 0, (int) strrpos($value, ':')); |
| 139 | + } |
| 140 | + |
| 141 | + return filter_var($value, FILTER_VALIDATE_IP) === false ? null : $value; |
| 142 | + } |
| 143 | + |
| 144 | + private function isTrustedProxy(string $ipAddress): bool |
| 145 | + { |
| 146 | + foreach ($this->trustedProxies as $proxy) { |
| 147 | + if ($this->matches($ipAddress, $proxy)) { |
| 148 | + return true; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + private function matches(string $ipAddress, string $proxy): bool |
| 156 | + { |
| 157 | + $subnet = $proxy; |
| 158 | + $bits = null; |
| 159 | + if (str_contains($proxy, '/')) { |
| 160 | + [$subnet, $bits] = explode('/', $proxy, 2); |
| 161 | + } |
| 162 | + |
| 163 | + $address = @inet_pton($ipAddress); |
| 164 | + $network = @inet_pton($subnet); |
| 165 | + if ($address === false || $network === false || strlen($address) !== strlen($network)) { |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | + if ($bits === null) { |
| 170 | + return $address === $network; |
| 171 | + } |
| 172 | + |
| 173 | + if (! ctype_digit($bits) || (int) $bits > strlen($address) * 8) { |
| 174 | + return false; |
| 175 | + } |
| 176 | + |
| 177 | + $bytes = intdiv((int) $bits, 8); |
| 178 | + if (strncmp($address, $network, $bytes) !== 0) { |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + $remainder = (int) $bits % 8; |
| 183 | + if ($remainder === 0) { |
| 184 | + return true; |
| 185 | + } |
| 186 | + |
| 187 | + $mask = (0xFF << (8 - $remainder)) & 0xFF; |
| 188 | + $addressBits = ord($address[$bytes]) & $mask; |
| 189 | + $networkBits = ord($network[$bytes]) & $mask; |
| 190 | + |
| 191 | + return $addressBits === $networkBits; |
| 192 | + } |
| 193 | +} |
0 commit comments