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

Add CIDR and wildcard matching #21

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
90 changes: 87 additions & 3 deletions src/IpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ class IpAddress implements MiddlewareInterface
*/
protected $trustedProxies;

/**
* List of trusted proxy IP wildcard ranges
*
* @var array
*/
protected $trustedWildcard;

/**
* List of trusted proxy IP CIDR ranges
*
* @var array
*/
protected $trustedCidr;

/**
* Name of the attribute added to the ServerRequest object
*
Expand Down Expand Up @@ -67,7 +81,34 @@ public function __construct(
}

$this->checkProxyHeaders = $checkProxyHeaders;
$this->trustedProxies = $trustedProxies;

if ($trustedProxies) {
foreach ($trustedProxies as $proxy) {
if (strpos($proxy, '*') !== false) {
// Wildcard IP address
// IPv6 is 8 parts separated by ':'
if (strpos($proxy, '.') > 0) {
$delim = '.';
$parts = 4;
} else {
$delim = ':';
$parts = 8;
}
$this->trustedWildcard[] = explode($delim, $proxy, $parts);
} elseif (strpos($proxy, '/') > 6) {
// CIDR notation
list($subnet, $bits) = explode('/', $proxy, 2);
$subnet = ip2long($subnet);
$mask = -1 << (32 - $bits);
$min = $subnet & $mask;
$max = $subnet | ~$mask;
$this->trustedCidr[] = [$min, $max];
} else {
// String-match IP address
$this->trustedProxies[] = $proxy;
}
}
}

if ($attributeName) {
$this->attributeName = $attributeName;
Expand Down Expand Up @@ -132,9 +173,52 @@ protected function determineClientIpAddress($request)
}

$checkProxyHeaders = $this->checkProxyHeaders;
if ($checkProxyHeaders && !empty($this->trustedProxies)) {
if (!in_array($ipAddress, $this->trustedProxies)) {
if ($checkProxyHeaders) {
// Exact Match
if ($this->trustedProxies && !in_array($ipAddress, $this->trustedProxies)) {
$checkProxyHeaders = false;
}

// Wildcard Match
if ($checkProxyHeaders && $this->trustedWildcard) {
$checkProxyHeaders = false;
// IPv4 has 4 parts separated by '.'
// IPv6 has 8 parts separated by ':'
if (strpos($ipAddress, '.') > 0) {
$delim = '.';
$parts = 4;
} else {
$delim = ':';
$parts = 8;
}
$ipAddrParts = explode($delim, $ipAddress, $parts);
foreach ($this->trustedWildcard as $proxy) {
if (count($proxy) !== $parts) {
continue; // IP version does not match
}
foreach ($proxy as $i => $part) {
if ($part !== '*' && $part !== $ipAddrParts[$i]) {
break 2;// IP does not match, move to next proxy
}
}
$checkProxyHeaders = true;
break;
}
}

// CIDR Match
if ($checkProxyHeaders && $this->trustedCidr) {
$checkProxyHeaders = false;
// Only IPv4 is supported for CIDR matching
$ipAsLong = ip2long($ipAddress);
if ($ipAsLong) {
foreach ($this->trustedCidr as $proxy) {
if ($proxy[0] <= $ipAsLong && $ipAsLong <= $proxy[1]) {
$checkProxyHeaders = true;
break;
}
}
}
}
}

Expand Down
Loading