Skip to content

Commit

Permalink
Merge branch 'remove-port-from-ipaddress'
Browse files Browse the repository at this point in the history
Closes #17
  • Loading branch information
akrabat committed Jul 3, 2018
2 parents 2677333 + aca0dd3 commit 62a3895
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/IpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ protected function determineClientIpAddress($request)
$ipAddress = null;

$serverParams = $request->getServerParams();
if (isset($serverParams['REMOTE_ADDR']) && $this->isValidIpAddress($serverParams['REMOTE_ADDR'])) {
$ipAddress = $serverParams['REMOTE_ADDR'];
if (isset($serverParams['REMOTE_ADDR'])) {
$remoteAddr = $this->extractIpAddress($serverParams['REMOTE_ADDR']);
if ($this->isValidIpAddress($remoteAddr)) {
$ipAddress = $remoteAddr;
}
}

if ($this->checkProxyHeaders
Expand All @@ -142,6 +145,26 @@ protected function determineClientIpAddress($request)
return $ipAddress;
}

/**
* Remove port from IPV4 address if it exists
*
* Note: leaves IPV6 addresses alone
*
* @param string $ipAddress
* @return string
*/
protected function extractIpAddress($ipAddress)
{
$parts = explode(':', $ipAddress);
if (count($parts) == 2) {
if (filter_var($parts[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
return $parts[0];
}
}

return $ipAddress;
}

/**
* Check that a given string is a valid IP address
*
Expand Down Expand Up @@ -179,6 +202,6 @@ private function getFirstIpAddressFromHeader($request, $header)
}
}

return $headerValue;
return $this->extractIpAddress($headerValue);
}
}
38 changes: 38 additions & 0 deletions tests/IpAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ public function testIpSetByRemoteAddr()
$this->assertSame('192.168.1.1', $ipAddress);
}

public function testIpWithPortSetByRemoteAddr()
{
$middleware = new IPAddress(false, [], 'IP');

$request = ServerRequestFactory::fromGlobals([
'REMOTE_ADDR' => '192.168.1.1:80',
]);
$response = new Response();

$response = $middleware($request, $response, function ($request, $response) use (&$ipAddress) {
// simply store the "ip_address" attribute in to the referenced $ipAddress
$ipAddress = $request->getAttribute('IP');
return $response;
});

$this->assertSame('192.168.1.1', $ipAddress);
}

public function testIpIsNullIfMissing()
{
$middleware = new IPAddress();
Expand Down Expand Up @@ -71,6 +89,26 @@ public function testXForwardedForIp()
$this->assertSame('192.168.1.3', $ipAddress);
}

public function testXForwardedForIpWithPort()
{
$middleware = new IPAddress(true, ['192.168.1.1']);

$request = ServerRequestFactory::fromGlobals([
'REMOTE_ADDR' => '192.168.1.1:81',
'HTTP_X_FORWARDED_FOR' => '192.168.1.3:81, 192.168.1.2:81, 192.168.1.1:81'
]);
$response = new Response();

$ipAddress = '123';
$response = $middleware($request, $response, function ($request, $response) use (&$ipAddress) {
// simply store the "ip_address" attribute in to the referenced $ipAddress
$ipAddress = $request->getAttribute('ip_address');
return $response;
});

$this->assertSame('192.168.1.3', $ipAddress);
}

public function testProxyIpIsIgnored()
{
$middleware = new IPAddress();
Expand Down

0 comments on commit 62a3895

Please sign in to comment.