Skip to content

Commit 62a3895

Browse files
committed
Merge branch 'remove-port-from-ipaddress'
Closes #17
2 parents 2677333 + aca0dd3 commit 62a3895

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

src/IpAddress.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,11 @@ protected function determineClientIpAddress($request)
120120
$ipAddress = null;
121121

122122
$serverParams = $request->getServerParams();
123-
if (isset($serverParams['REMOTE_ADDR']) && $this->isValidIpAddress($serverParams['REMOTE_ADDR'])) {
124-
$ipAddress = $serverParams['REMOTE_ADDR'];
123+
if (isset($serverParams['REMOTE_ADDR'])) {
124+
$remoteAddr = $this->extractIpAddress($serverParams['REMOTE_ADDR']);
125+
if ($this->isValidIpAddress($remoteAddr)) {
126+
$ipAddress = $remoteAddr;
127+
}
125128
}
126129

127130
if ($this->checkProxyHeaders
@@ -142,6 +145,26 @@ protected function determineClientIpAddress($request)
142145
return $ipAddress;
143146
}
144147

148+
/**
149+
* Remove port from IPV4 address if it exists
150+
*
151+
* Note: leaves IPV6 addresses alone
152+
*
153+
* @param string $ipAddress
154+
* @return string
155+
*/
156+
protected function extractIpAddress($ipAddress)
157+
{
158+
$parts = explode(':', $ipAddress);
159+
if (count($parts) == 2) {
160+
if (filter_var($parts[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
161+
return $parts[0];
162+
}
163+
}
164+
165+
return $ipAddress;
166+
}
167+
145168
/**
146169
* Check that a given string is a valid IP address
147170
*
@@ -179,6 +202,6 @@ private function getFirstIpAddressFromHeader($request, $header)
179202
}
180203
}
181204

182-
return $headerValue;
205+
return $this->extractIpAddress($headerValue);
183206
}
184207
}

tests/IpAddressTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ public function testIpSetByRemoteAddr()
3434
$this->assertSame('192.168.1.1', $ipAddress);
3535
}
3636

37+
public function testIpWithPortSetByRemoteAddr()
38+
{
39+
$middleware = new IPAddress(false, [], 'IP');
40+
41+
$request = ServerRequestFactory::fromGlobals([
42+
'REMOTE_ADDR' => '192.168.1.1:80',
43+
]);
44+
$response = new Response();
45+
46+
$response = $middleware($request, $response, function ($request, $response) use (&$ipAddress) {
47+
// simply store the "ip_address" attribute in to the referenced $ipAddress
48+
$ipAddress = $request->getAttribute('IP');
49+
return $response;
50+
});
51+
52+
$this->assertSame('192.168.1.1', $ipAddress);
53+
}
54+
3755
public function testIpIsNullIfMissing()
3856
{
3957
$middleware = new IPAddress();
@@ -71,6 +89,26 @@ public function testXForwardedForIp()
7189
$this->assertSame('192.168.1.3', $ipAddress);
7290
}
7391

92+
public function testXForwardedForIpWithPort()
93+
{
94+
$middleware = new IPAddress(true, ['192.168.1.1']);
95+
96+
$request = ServerRequestFactory::fromGlobals([
97+
'REMOTE_ADDR' => '192.168.1.1:81',
98+
'HTTP_X_FORWARDED_FOR' => '192.168.1.3:81, 192.168.1.2:81, 192.168.1.1:81'
99+
]);
100+
$response = new Response();
101+
102+
$ipAddress = '123';
103+
$response = $middleware($request, $response, function ($request, $response) use (&$ipAddress) {
104+
// simply store the "ip_address" attribute in to the referenced $ipAddress
105+
$ipAddress = $request->getAttribute('ip_address');
106+
return $response;
107+
});
108+
109+
$this->assertSame('192.168.1.3', $ipAddress);
110+
}
111+
74112
public function testProxyIpIsIgnored()
75113
{
76114
$middleware = new IPAddress();

0 commit comments

Comments
 (0)