Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8a27bd7

Browse files
[HttpFoundation] Add PRIVATE_SUBNETS as a shortcut for private IP address ranges to Request::setTrustedProxies()
1 parent 1958d21 commit 8a27bd7

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ public function getConfigTreeBuilder(): TreeBuilder
113113
->end()
114114
->scalarNode('trusted_proxies')
115115
->beforeNormalization()
116-
->ifTrue(fn ($v) => 'private_ranges' === $v)
117-
->then(fn ($v) => implode(',', IpUtils::PRIVATE_SUBNETS))
116+
->ifTrue(fn ($v) => 'private_ranges' === $v || 'PRIVATE_SUBNETS' === $v)
117+
->then(fn () => IpUtils::PRIVATE_SUBNETS)
118118
->end()
119119
->end()
120120
->arrayNode('trusted_headers')

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add optional `$requests` parameter to `RequestStack::__construct()`
88
* Add optional `$v4Bytes` and `$v6Bytes` parameters to `IpUtils::anonymize()`
9+
* Add `PRIVATE_SUBNETS` as a shortcut for private IP address ranges to `Request::setTrustedProxies()`
910

1011
7.1
1112
---

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -520,20 +520,26 @@ public function overrideGlobals(): void
520520
*
521521
* You should only list the reverse proxies that you manage directly.
522522
*
523-
* @param array $proxies A list of trusted proxies, the string 'REMOTE_ADDR' will be replaced with $_SERVER['REMOTE_ADDR']
524-
* @param int $trustedHeaderSet A bit field of Request::HEADER_*, to set which headers to trust from your proxies
523+
* @param array $proxies A list of trusted proxies, the string 'REMOTE_ADDR' will be replaced with $_SERVER['REMOTE_ADDR']
524+
* @param int-mask-of<Request::HEADER_*> $trustedHeaderSet A bit field to set which headers to trust from your proxies
525525
*/
526526
public static function setTrustedProxies(array $proxies, int $trustedHeaderSet): void
527527
{
528-
self::$trustedProxies = array_reduce($proxies, function ($proxies, $proxy) {
529-
if ('REMOTE_ADDR' !== $proxy) {
530-
$proxies[] = $proxy;
531-
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
532-
$proxies[] = $_SERVER['REMOTE_ADDR'];
528+
if (false !== $i = array_search('REMOTE_ADDR', $proxies, true)) {
529+
if (isset($_SERVER['REMOTE_ADDR'])) {
530+
$proxies[$i] = $_SERVER['REMOTE_ADDR'];
531+
} else {
532+
unset($proxies[$i]);
533+
$proxies = array_values($proxies);
533534
}
535+
}
536+
537+
if (false !== $i = array_search('PRIVATE_SUBNETS', $proxies, true)) {
538+
unset($proxies[$i]);
539+
$proxies = array_merge($proxies, IpUtils::PRIVATE_SUBNETS);
540+
}
534541

535-
return $proxies;
536-
}, []);
542+
self::$trustedProxies = $proxies;
537543
self::$trustedHeaderSet = $trustedHeaderSet;
538544
}
539545

0 commit comments

Comments
 (0)