Skip to content

Commit

Permalink
Merge pull request #88 from HonzaCZ/master
Browse files Browse the repository at this point in the history
RequestFactory: Fixed possible remoteAddr spoofing (issue #87)
  • Loading branch information
milo committed Mar 29, 2016
2 parents dca62fe + cae5d68 commit c3a9a20
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
15 changes: 12 additions & 3 deletions src/Http/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,20 @@ public function createHttpRequest()
}

if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$remoteAddr = trim(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0]);
$xForwardedForWithoutProxies = array_filter(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']), function ($ip) {
return !array_filter($this->proxies, function ($proxy) use ($ip) {
return Helpers::ipMatch(trim($ip), $proxy);
});
});
$remoteAddr = trim(end($xForwardedForWithoutProxies));
$xForwardedForRealIpKey = key($xForwardedForWithoutProxies);
}

if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$remoteHost = trim(explode(',', $_SERVER['HTTP_X_FORWARDED_HOST'])[0]);
if (isset($xForwardedForRealIpKey) && !empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
$xForwardedHost = explode(',', $_SERVER['HTTP_X_FORWARDED_HOST']);
if (isset($xForwardedHost[$xForwardedForRealIpKey])) {
$remoteHost = trim($xForwardedHost[$xForwardedForRealIpKey]);
}
}
}

Expand Down
24 changes: 21 additions & 3 deletions tests/Http/RequestFactory.proxy.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ test(function () {
$_SERVER = [
'REMOTE_ADDR' => '127.0.0.3',
'REMOTE_HOST' => 'localhost',
'HTTP_X_FORWARDED_FOR' => '23.75.345.200, 10.0.0.1',
'HTTP_X_FORWARDED_HOST' => 'otherhost, anotherhost',
'HTTP_X_FORWARDED_FOR' => '23.75.45.200',
'HTTP_X_FORWARDED_HOST' => 'otherhost',
];

$factory = new RequestFactory;
Expand All @@ -25,6 +25,24 @@ test(function () {
Assert::same('localhost', $factory->createHttpRequest()->getRemoteHost());

$factory->setProxy('127.0.0.1/8');
Assert::same('23.75.345.200', $factory->createHttpRequest()->getRemoteAddress());
Assert::same('23.75.45.200', $factory->createHttpRequest()->getRemoteAddress());
Assert::same('otherhost', $factory->createHttpRequest()->getRemoteHost());
});

test(function () {
$_SERVER = [
'REMOTE_ADDR' => '10.0.0.2', //proxy2
'REMOTE_HOST' => 'proxy2',
'HTTP_X_FORWARDED_FOR' => '123.123.123.123, 172.16.0.1, 10.0.0.1',
'HTTP_X_FORWARDED_HOST' => 'fake, real, proxy1',
];

$factory = new RequestFactory;
$factory->setProxy('10.0.0.0/24');
Assert::same('172.16.0.1', $factory->createHttpRequest()->getRemoteAddress());
Assert::same('real', $factory->createHttpRequest()->getRemoteHost());

$factory->setProxy(['10.0.0.1', '10.0.0.2']);
Assert::same('172.16.0.1', $factory->createHttpRequest()->getRemoteAddress());
Assert::same('real', $factory->createHttpRequest()->getRemoteHost());
});

0 comments on commit c3a9a20

Please sign in to comment.