Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading