Skip to content

Commit 9c58019

Browse files
hrachdg
authored andcommitted
RequestFactory: correctly ignores not-ip values in HTTP_X_FORWARDED_FOR & REMOTE_ADDR (#122)
1 parent c2a627f commit 9c58019

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

src/Http/RequestFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public function createHttpRequest(): Request
247247
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
248248
$xForwardedForWithoutProxies = array_filter(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']), function ($ip) {
249249
return !array_filter($this->proxies, function ($proxy) use ($ip) {
250-
return Helpers::ipMatch(trim($ip), $proxy);
250+
return filter_var(trim($ip), FILTER_VALIDATE_IP) !== FALSE && Helpers::ipMatch(trim($ip), $proxy);
251251
});
252252
});
253253
$remoteAddr = trim(end($xForwardedForWithoutProxies));

tests/Http/RequestFactory.proxy.x-forwarded.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ test(function () {
3535
$_SERVER = [
3636
'REMOTE_ADDR' => '10.0.0.2', //proxy2
3737
'REMOTE_HOST' => 'proxy2',
38-
'HTTP_X_FORWARDED_FOR' => '123.123.123.123, 172.16.0.1, 10.0.0.1',
39-
'HTTP_X_FORWARDED_HOST' => 'fake, real, proxy1',
38+
'HTTP_X_FORWARDED_FOR' => '123.123.123.123, not-ip.com, 172.16.0.1, 10.0.0.1',
39+
'HTTP_X_FORWARDED_HOST' => 'fake, not-ip.com, real, proxy1',
4040
];
4141

4242
$factory = new RequestFactory;

0 commit comments

Comments
 (0)