Skip to content

Commit 0d232ba

Browse files
Samuel Vogelfabpot
authored andcommitted
Improve documentation of X-Forwarded-For header handling
1 parent 9ed5608 commit 0d232ba

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/Symfony/Component/HttpFoundation/IpUtils.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ class IpUtils
2424
private function __construct() {}
2525

2626
/**
27-
* Validates an IPv4 or IPv6 address.
27+
* Checks if an IPv4 or IPv6 address is contained in the list of given IPs or subnets
2828
*
29-
* @param string $requestIp
30-
* @param string|array $ips
29+
* @param string $requestIp IP to check
30+
* @param string|array $ips List of IPs or subnets (can be a string if only a single one)
3131
*
3232
* @return boolean Whether the IP is valid
3333
*/
@@ -49,10 +49,11 @@ public static function checkIp($requestIp, $ips)
4949
}
5050

5151
/**
52-
* Validates an IPv4 address.
52+
* Compares two IPv4 addresses.
53+
* In case a subnet is given, it checks if it contains the request IP.
5354
*
54-
* @param string $requestIp
55-
* @param string $ip
55+
* @param string $requestIp IPv4 address to check
56+
* @param string $ip IPv4 address or subnet in CIDR notation
5657
*
5758
* @return boolean Whether the IP is valid
5859
*/
@@ -73,13 +74,14 @@ public static function checkIp4($requestIp, $ip)
7374
}
7475

7576
/**
76-
* Validates an IPv6 address.
77+
* Compares two IPv6 addresses.
78+
* In case a subnet is given, it checks if it contains the request IP.
7779
*
7880
* @author David Soria Parra <dsp at php dot net>
7981
* @see https://github.com/dsp/v6tools
8082
*
81-
* @param string $requestIp
82-
* @param string $ip
83+
* @param string $requestIp IPv6 address to check
84+
* @param string $ip IPv6 address or subnet in CIDR notation
8385
*
8486
* @return boolean Whether the IP is valid
8587
*

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -732,9 +732,9 @@ public function setSession(SessionInterface $session)
732732
/**
733733
* Returns the client IP addresses.
734734
*
735-
* The least trusted IP address is first, and the most trusted one last.
736-
* The "real" client IP address is the first one, but this is also the
737-
* least trusted one.
735+
* In the returned array the most trusted IP address is first, and the
736+
* least trusted one last. The "real" client IP address is the last one,
737+
* but this is also the least trusted one. Trusted proxies are stripped.
738738
*
739739
* Use this method carefully; you should use getClientIp() instead.
740740
*
@@ -755,17 +755,19 @@ public function getClientIps()
755755
}
756756

757757
$clientIps = array_map('trim', explode(',', $this->headers->get(self::$trustedHeaders[self::HEADER_CLIENT_IP])));
758-
$clientIps[] = $ip;
758+
$clientIps[] = $ip; // Complete the IP chain with the IP the request actually came from
759759

760760
$trustedProxies = !self::$trustedProxies ? array($ip) : self::$trustedProxies;
761-
$ip = $clientIps[0];
761+
$ip = $clientIps[0]; // Fallback to this when the client IP falls into the range of trusted proxies
762762

763+
// Eliminate all IPs from the forwarded IP chain which are trusted proxies
763764
foreach ($clientIps as $key => $clientIp) {
764765
if (IpUtils::checkIp($clientIp, $trustedProxies)) {
765766
unset($clientIps[$key]);
766767
}
767768
}
768769

770+
// Now the IP chain contains only untrusted proxies and the client IP
769771
return $clientIps ? array_reverse($clientIps) : array($ip);
770772
}
771773

0 commit comments

Comments
 (0)