Skip to content

Commit 00e090a

Browse files
committed
Merge branch 'fix-multiple-ip-types'
Closes #32
2 parents 50ba091 + 724e013 commit 00e090a

File tree

2 files changed

+72
-19
lines changed

2 files changed

+72
-19
lines changed

src/IpAddress.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,15 @@ protected function determineClientIpAddress($request)
172172
}
173173
}
174174

175-
$checkProxyHeaders = $this->checkProxyHeaders;
176-
if ($checkProxyHeaders) {
175+
$checkProxyHeaders = false;
176+
if ($this->checkProxyHeaders) {
177177
// Exact Match
178-
if ($this->trustedProxies && !in_array($ipAddress, $this->trustedProxies)) {
179-
$checkProxyHeaders = false;
178+
if ($this->trustedProxies && in_array($ipAddress, $this->trustedProxies)) {
179+
$checkProxyHeaders = true;
180180
}
181181

182182
// Wildcard Match
183-
if ($checkProxyHeaders && $this->trustedWildcard) {
184-
$checkProxyHeaders = false;
183+
if ($this->checkProxyHeaders && $this->trustedWildcard) {
185184
// IPv4 has 4 parts separated by '.'
186185
// IPv6 has 8 parts separated by ':'
187186
if (strpos($ipAddress, '.') > 0) {
@@ -196,19 +195,22 @@ protected function determineClientIpAddress($request)
196195
if (count($proxy) !== $parts) {
197196
continue; // IP version does not match
198197
}
198+
$match = true;
199199
foreach ($proxy as $i => $part) {
200200
if ($part !== '*' && $part !== $ipAddrParts[$i]) {
201-
break 2;// IP does not match, move to next proxy
201+
$match = false;
202+
break;// IP does not match, move to next proxy
202203
}
203204
}
204-
$checkProxyHeaders = true;
205-
break;
205+
if ($match) {
206+
$checkProxyHeaders = true;
207+
break;
208+
}
206209
}
207210
}
208211

209212
// CIDR Match
210-
if ($checkProxyHeaders && $this->trustedCidr) {
211-
$checkProxyHeaders = false;
213+
if ($this->checkProxyHeaders && $this->trustedCidr) {
212214
// Only IPv4 is supported for CIDR matching
213215
$ipAsLong = ip2long($ipAddress);
214216
if ($ipAsLong) {
@@ -220,15 +222,19 @@ protected function determineClientIpAddress($request)
220222
}
221223
}
222224
}
223-
}
224225

225-
if ($checkProxyHeaders) {
226-
foreach ($this->headersToInspect as $header) {
227-
if ($request->hasHeader($header)) {
228-
$ip = $this->getFirstIpAddressFromHeader($request, $header);
229-
if ($this->isValidIpAddress($ip)) {
230-
$ipAddress = $ip;
231-
break;
226+
if (!$this->trustedProxies && !$this->trustedWildcard && !$this->trustedCidr) {
227+
$checkProxyHeaders = true;
228+
}
229+
230+
if ($checkProxyHeaders) {
231+
foreach ($this->headersToInspect as $header) {
232+
if ($request->hasHeader($header)) {
233+
$ip = $this->getFirstIpAddressFromHeader($request, $header);
234+
if ($this->isValidIpAddress($ip)) {
235+
$ipAddress = $ip;
236+
break;
237+
}
232238
}
233239
}
234240
}

tests/IpAddressTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,53 @@ public function handle(ServerRequestInterface $request): ResponseInterface
316316
$this->assertSame("Hello World", (string) $response->getBody());
317317
}
318318

319+
public function testIpCidrListMatch()
320+
{
321+
$matches = [
322+
'192.16.238.184/24', // negative match
323+
'10.11.0.0/16', // positive match
324+
];
325+
$middleware = new IPAddress(true, $matches);
326+
$env = [
327+
'REMOTE_ADDR' => '10.11.156.95',
328+
'HTTP_X_FORWARDED_FOR' => '123.4.5.6',
329+
];
330+
$ipAddress = $this->simpleRequest($middleware, $env);
331+
$this->assertSame('123.4.5.6', $ipAddress, "Testing CIDR: " . implode(', ', $matches));
332+
}
333+
334+
public function testIp4WildcardsMatch()
335+
{
336+
$matches = [
337+
'192.168.*.*', // negative match
338+
'10.0.238.*', // negative match
339+
'10.11.*.*', // positive match
340+
];
341+
$middleware = new IPAddress(true, $matches);
342+
$env = [
343+
'REMOTE_ADDR' => '10.11.156.95',
344+
'HTTP_X_FORWARDED_FOR' => '123.4.5.6',
345+
];
346+
$ipAddress = $this->simpleRequest($middleware, $env);
347+
$this->assertSame('123.4.5.6', $ipAddress, "Testing wildcard: " . implode(', ', $matches));
348+
}
349+
350+
public function testIp4MultipleTypesMatch()
351+
{
352+
$matches = [
353+
'192.168.0.1', // negative match
354+
'10.0.238.*', // negative match
355+
'10.11.0.0/16', // positive match
356+
];
357+
$middleware = new IPAddress(true, $matches);
358+
$env = [
359+
'REMOTE_ADDR' => '10.11.156.95',
360+
'HTTP_X_FORWARDED_FOR' => '123.4.5.6',
361+
];
362+
$ipAddress = $this->simpleRequest($middleware, $env);
363+
$this->assertSame('123.4.5.6', $ipAddress, "Testing proxies: " . implode(', ', $matches));
364+
}
365+
319366
public function testNotGivingAProxyListShouldThrowException()
320367
{
321368
$this->expectException(\InvalidArgumentException::class);

0 commit comments

Comments
 (0)