Skip to content

Commit 60ad382

Browse files
committed
[HttpFoundation] fixed error when an IP in the X-Forwarded-For HTTP header contains a port
1 parent f68b7c7 commit 60ad382

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,11 @@ public function getClientIps()
793793

794794
// Eliminate all IPs from the forwarded IP chain which are trusted proxies
795795
foreach ($clientIps as $key => $clientIp) {
796+
// Remove port on IPv4 address (unfortunately, it does happen)
797+
if (preg_match('{((?:\d+\.){3}\d+)\:\d+}', $clientIp, $match)) {
798+
$clientIps[$key] = $clientIp = $match[1];
799+
}
800+
796801
if (IpUtils::checkIp($clientIp, self::$trustedProxies)) {
797802
unset($clientIps[$key]);
798803
}

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,9 @@ public function testGetClientIpsProvider()
884884
array(array('3620:0:1cfe:face:b00c::3'), '1620:0:1cfe:face:b00c::3', '3620:0:1cfe:face:b00c::3,2620:0:1cfe:face:b00c::3', array('1620:0:1cfe:face:b00c::3', '2620:0:1cfe:face:b00c::3')),
885885
// multiple forwarded for with remote IPv4 addr and some reverse proxies trusted but in the middle
886886
array(array('2620:0:1cfe:face:b00c::3', '4620:0:1cfe:face:b00c::3'), '1620:0:1cfe:face:b00c::3', '4620:0:1cfe:face:b00c::3,3620:0:1cfe:face:b00c::3,2620:0:1cfe:face:b00c::3', array('1620:0:1cfe:face:b00c::3', '3620:0:1cfe:face:b00c::3')),
887+
888+
// client IP with port
889+
array(array('88.88.88.88'), '127.0.0.1', '88.88.88.88:12345, 127.0.0.1', array('127.0.0.1')),
887890
);
888891
}
889892

0 commit comments

Comments
 (0)