Skip to content

Commit 7b5328f

Browse files
author
Neil Ferreira
committed
getClientIp() will now only return valid IP addresses, rather than assuming the X_FORWARDED_FOR is the first comma seperated value.
1 parent 78747e6 commit 7b5328f

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,16 @@ public function getClientIp()
546546
if ($this->server->has('HTTP_CLIENT_IP')) {
547547
return $this->server->get('HTTP_CLIENT_IP');
548548
} elseif ($this->server->has('HTTP_X_FORWARDED_FOR')) {
549-
$clientIp = explode(',', $this->server->get('HTTP_X_FORWARDED_FOR'), 2);
549+
$clientIp = explode(',', $this->server->get('HTTP_X_FORWARDED_FOR'));
550550

551-
return isset($clientIp[0]) ? trim($clientIp[0]) : '';
551+
foreach ($clientIp as $ipAddress) {
552+
$cleanIpAddress = trim($ipAddress);
553+
554+
if (false !== filter_var($cleanIpAddress, FILTER_VALIDATE_IP)) {
555+
return $cleanIpAddress;
556+
}
557+
}
558+
return '';
552559
}
553560
}
554561

0 commit comments

Comments
 (0)