Skip to content

Commit b33e1fb

Browse files
committed
Trust only private network ranges for the PRIVATE_SUBNETS proxy keyword
1 parent 34e447d commit b33e1fb

5 files changed

Lines changed: 15 additions & 5 deletions

File tree

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"react/event-loop": "^1.5",
1717
"react/socket": "^1.16",
1818
"symfony/deprecation-contracts": "^2.5 || ^3.0",
19-
"symfony/http-foundation": "^7.4 || ^8.1",
19+
"symfony/http-foundation": "^7.4.23 || ^8.1.11",
2020
"symfony/routing": "^7.4 || ^8.1"
2121
},
2222
"require-dev": {

‎docs/middleware/resolve-forwarded-client-address.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ $middleware = new ResolveForwardedClientAddress(
2525

2626
The list of trusted proxies supports single IP addresses and subnet ranges, as well as two special values:
2727

28-
- `PRIVATE_SUBNETS` - trusts all private network ranges (see `Symfony\Component\HttpFoundation\IpUtils::PRIVATE_SUBNETS`)
28+
- `PRIVATE_SUBNETS` - trusts all private network ranges (see `Symfony\Component\HttpFoundation\IpUtils::PRIVATE_RANGES`)
2929
- `REMOTE_ADDR` - trusts every connection; only use this when the server can only be reached through the proxy
3030

3131
Only the `Request::HEADER_X_FORWARDED_FOR` (the `X-Forwarded-For` header) and `Request::HEADER_FORWARDED` (the [RFC 7239](https://datatracker.ietf.org/doc/html/rfc7239) `Forwarded` header) values are used by this middleware, any other headers in the bit field are ignored.

‎src/Application.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public function withRequestTimeout(?float $timeout): self
271271
* instead of the proxy's address. This follows the same conventions as Symfony's {@see Request::setTrustedProxies()}.
272272
*
273273
* @param list<string> $proxies The addresses or subnets of the trusted proxies; the string "REMOTE_ADDR" trusts the
274-
* address of every connection, and "PRIVATE_SUBNETS" trusts all private network ranges
274+
* address of every connection, and "PRIVATE_SUBNETS" trusts all private network ranges ({@see \Symfony\Component\HttpFoundation\IpUtils::PRIVATE_RANGES})
275275
* @param int-mask-of<Request::HEADER_*> $trustedHeaderSet A bit field of the headers to trust from the proxies; only the
276276
* {@see Request::HEADER_FORWARDED} and {@see Request::HEADER_X_FORWARDED_FOR} headers are used
277277
*/

‎src/Http/Middleware/ResolveForwardedClientAddress.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
/**
3636
* @param list<string> $trustedProxies The addresses or subnets of the trusted proxies; the string "REMOTE_ADDR" trusts
37-
* the address of every connection, and "PRIVATE_SUBNETS" is replaced by {@see IpUtils::PRIVATE_SUBNETS}
37+
* the address of every connection, and "PRIVATE_SUBNETS" (or "private_ranges") is replaced by {@see IpUtils::PRIVATE_RANGES}
3838
* @param int-mask-of<Request::HEADER_*> $trustedHeaderSet A bit field of the headers to trust from the proxies; only the
3939
* {@see Request::HEADER_FORWARDED} and {@see Request::HEADER_X_FORWARDED_FOR} headers are used
4040
*/
@@ -49,7 +49,8 @@ public function __construct(
4949

5050
foreach ($trustedProxies as $trustedProxy) {
5151
if ('PRIVATE_SUBNETS' === $trustedProxy || 'private_ranges' === $trustedProxy) {
52-
array_push($proxies, ...IpUtils::PRIVATE_SUBNETS);
52+
// Unlike IpUtils::PRIVATE_SUBNETS, this list has no range a remote client can connect from, such as the 6to4, Teredo, or NAT64 ranges
53+
array_push($proxies, ...IpUtils::PRIVATE_RANGES);
5354
} elseif ('REMOTE_ADDR' !== $trustedProxy) {
5455
$proxies[] = $trustedProxy;
5556
}

‎tests/Http/Middleware/ResolveForwardedClientAddressTest.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ public static function dataClientAddress(): \Generator
9090

9191
yield 'Private subnets keyword' => [['PRIVATE_SUBNETS'], $xff, '192.168.1.10', ['X-Forwarded-For' => '203.0.113.5'], '203.0.113.5', '192.168.1.10', true];
9292

93+
yield 'Private ranges keyword' => [['private_ranges'], $xff, '10.0.0.1', ['X-Forwarded-For' => '203.0.113.5'], '203.0.113.5', '10.0.0.1', true];
94+
95+
// These ranges embed an IPv4 address in a public IPv6 address a client can connect from, so they must not be trusted as private networks
96+
yield 'Private subnets keyword does not trust a 6to4 address' => [['PRIVATE_SUBNETS'], $xff, '2002:cb00:7105::1', ['X-Forwarded-For' => '198.51.100.1'], '2002:cb00:7105::1', null, true];
97+
98+
yield 'Private subnets keyword does not trust a Teredo address' => [['PRIVATE_SUBNETS'], $xff, '2001:0:4136:e378:8000:63bf:3fff:fdd2', ['X-Forwarded-For' => '198.51.100.1'], '2001:0:4136:e378:8000:63bf:3fff:fdd2', null, true];
99+
100+
yield 'Private subnets keyword does not trust a NAT64 address' => [['PRIVATE_SUBNETS'], $xff, '64:ff9b::cb00:7105', ['X-Forwarded-For' => '198.51.100.1'], '64:ff9b::cb00:7105', null, true];
101+
93102
yield 'Remote address keyword trusts any connection' => [['REMOTE_ADDR'], $xff, '203.0.113.99', ['X-Forwarded-For' => '198.51.100.1'], '198.51.100.1', '203.0.113.99', true];
94103
}
95104

0 commit comments

Comments
 (0)