Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding CIDR support to proxied ip addresses; Fixes #13 #14

Merged
merged 2 commits into from
Jan 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/ClientIp.php
Original file line number Diff line number Diff line change
@@ -97,7 +97,7 @@ private function getIp(ServerRequestInterface $request)

$localIp = $this->getLocalIp($request);

if ($this->proxyIps && !in_array($localIp, $this->proxyIps)) {
if ($this->proxyIps && !$this->isInProxiedIps($localIp)) {
// Local IP address does not point at a known proxy, do not attempt
// to read proxied IP address.
return $localIp;
@@ -113,6 +113,36 @@ private function getIp(ServerRequestInterface $request)
return $localIp;
}

/**
* checks if the given ip address is in the list of proxied ips provided
*
* @param string $ip
* @return bool
*/
private function isInProxiedIps(string $ip): bool
{
foreach ($this->proxyIps as $proxyIp) {
if ($ip === $proxyIp || self::isInCIDR($ip, $proxyIp)) {
return true;
}
}
return false;
}

private static function isInCIDR(string $ip, string $cidr): bool
{
$tokens = explode('/', $cidr);
if (count($tokens) !== 2 || !self::isValid($ip) || !self::isValid($tokens[0]) || !is_numeric($tokens[1])) {
return false;
}

$cidr_base = ip2long($tokens[0]);
$ip_long = ip2long($ip);
$mask = (0xffffffff << intval($tokens[1])) & 0xffffffff;

return ($cidr_base & $mask) === ($ip_long & $mask);
}

/**
* Returns the IP address from remote service.
*
@@ -180,7 +210,7 @@ private function getForwardedHeaderIp(string $header)

$ip = trim(substr($directive, 4));

if (self::isValid($ip) && !in_array($ip, $this->proxyIps)) {
if (self::isValid($ip) && !$this->isInProxiedIps($ip)) {
return $ip;
}
}
@@ -195,7 +225,7 @@ private function getForwardedHeaderIp(string $header)
private function getHeaderIp(string $header)
{
foreach (array_reverse(array_map('trim', explode(',', $header))) as $ip) {
if (self::isValid($ip) && !in_array($ip, $this->proxyIps)) {
if (self::isValid($ip) && !$this->isInProxiedIps($ip)) {
return $ip;
}
}
18 changes: 18 additions & 0 deletions tests/ClientIpTest.php
Original file line number Diff line number Diff line change
@@ -145,6 +145,24 @@ function ($request) {
], $request);

$this->assertEquals('3.3.3.3', (string) $response->getBody());

$response = Dispatcher::run([
(new ClientIp())->proxy(['1.1.0.0/16', '2.2.2.0/8']),
function ($request) {
echo $request->getAttribute('client-ip');
},
], $request);

$this->assertEquals('3.3.3.3', (string) $response->getBody());

$response = Dispatcher::run([
(new ClientIp())->proxy(['5.5.5.0/12']),
function ($request) {
echo $request->getAttribute('client-ip');
},
], $request);

$this->assertEquals('1.1.1.1', (string) $response->getBody());
}

public function testNoRemoteAddr()