Skip to content

Commit

Permalink
Merge pull request #55 from akrabat/52-no-remote-addr
Browse files Browse the repository at this point in the history
Set to null if REMOTE_ADDR is empty or invalid
  • Loading branch information
akrabat authored Jan 10, 2025
2 parents 17943ec + 9fa449f commit 20ecdc3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/IpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions tests/IpAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 20ecdc3

Please sign in to comment.