diff --git a/src/IpAddress.php b/src/IpAddress.php index 102d2ef..355f8d8 100644 --- a/src/IpAddress.php +++ b/src/IpAddress.php @@ -186,6 +186,11 @@ protected function determineClientIpAddress($request): ?string $ipAddress = $remoteAddr; } } + if ($ipAddress === null) { + // do not continue if there isn't a valid remote address + return $ipAddress; + } + if (!$this->checkProxyHeaders) { // do not check if configured to not check return $ipAddress; diff --git a/tests/IpAddressTest.php b/tests/IpAddressTest.php index 5cc81c6..2404aeb 100644 --- a/tests/IpAddressTest.php +++ b/tests/IpAddressTest.php @@ -22,6 +22,17 @@ private function simpleRequest(IPAddress $middleware, $env, $attrName = 'ip_addr return $attributeValue; } + public function testMissingRemoteAddrSetsTheAttributeToNull() + { + $middleware = new IPAddress(true, []); + $env = [ + 'HTTP_X_FORWARDED_FOR' => '123.4.5.6', + ]; + $ipAddress = $this->simpleRequest($middleware, $env); + + $this->assertSame(null, $ipAddress); + } + public function testIpAddressIsSetByRemoteAddrIfCheckProxyHeadersIsFalse() { $middleware = new IPAddress(false, [], 'IP');