8000 feature #33574 [Http][DI] Replace REMOTE_ADDR in trusted proxies with… · symfony/symfony@66f8f68 · GitHub
[go: up one dir, main page]

Skip to content

Commit 66f8f68

Browse files
committed
feature #33574 [Http][DI] Replace REMOTE_ADDR in trusted proxies with the current REMOTE_ADDR (mcfedr)
This PR was merged into the 4.4 branch. Discussion ---------- [Http][DI] Replace REMOTE_ADDR in trusted proxies with the current REMOTE_ADDR | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | License | MIT | Doc PR | Currently handling trusted ips when deploying behind some CDNs/Load balancers such as ELB is difficult because they dont have a constant IP address, its possible to overcome this as is suggested by the docs - https://symfony.com/doc/current/deployment/proxies.html#but-what-if-the-ip-of-my-reverse-proxy-changes-constantly - by settings trusted proxies to `$request->server->get('REMOTE_ADDR')` - but this has to be done in code, and so becomes dangerous if you code is deployed in different environments. This change would allow the developer to stick to providing the envvar `TRUSTED_PROXIES`, and in the environment behind a ELB set the value to the literal string `REMOTE_ADDR`, and have it replaced at run time. This way in environments that are not using ELB his app is kept safe. I think doing this replacement in `Request:: setTrustedProxies` is the best place because it means this feature isn't exposed to other parts of the code that might call `Request::getTrustedProxies`. Commits ------- 643c9ff Replace REMOTE_ADDR in trusted proxies with the current REMOTE_ADDR
2 parents 7a3bfac + 643c9ff commit 66f8f68

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,14 +567,22 @@ public function overrideGlobals()
567567
*
568568
* You should only list the reverse proxies that you manage directly.
569569
*
570-
* @param array $proxies A list of trusted proxies
570+
* @param array $proxies A list of trusted proxies, the string 'REMOTE_ADDR' will be replaced with $_SERVER['REMOTE_ADDR']
571571
* @param int $trustedHeaderSet A bit field of Request::HEADER_*, to set which headers to trust from your proxies
572572
*
573573
* @throws \InvalidArgumentException When $trustedHeaderSet is invalid
574574
*/
575575
public static function setTrustedProxies(array $proxies, int $trustedHeaderSet)
576576
{
577-
self::$trustedProxies = $proxies;
577+
self::$trustedProxies = array_reduce($proxies, function ($proxies, $proxy) {
578+
if ('REMOTE_ADDR' !== $proxy) {
579+
$proxies[] = $proxy;
580+
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
581+
$proxies[] = $_SERVER['REMOTE_ADDR'];
582+
}
583+
584+
return $proxies;
585+
}, []);
578586
self::$trustedHeaderSet = $trustedHeaderSet;
579587
}
580588

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2324,6 +2324,26 @@ public function testTrustedPortDoesNotDefaultToZero()
23242324

23252325
$this->assertSame(80, $request->getPort());
23262326
}
2327+
2328+
/**
2329+
* @dataProvider trustedProxiesRemoteAddr
2330+
*/
2331+
public function testTrustedProxiesRemoteAddr($serverRemoteAddr, $trustedProxies, $result)
2332+
{
2333+
$_SERVER['REMOTE_ADDR'] = $serverRemoteAddr;
2334+
Request::setTrustedProxies($trustedProxies, Request::HEADER_X_FORWARDED_ALL);
2335+
$this->assertSame($result, Request::getTrustedProxies());
2336+
}
2337+
2338+
public function trustedProxiesRemoteAddr()
2339+
{
2340+
return [
2341+
['1.1.1.1', ['REMOTE_ADDR'], ['1.1.1.1']],
2342+
['1.1.1.1', ['REMOTE_ADDR', '2.2.2.2'], ['1.1.1.1', '2.2.2.2']],
2343+
[null, ['REMOTE_ADDR'], []],
2344+
[null, ['REMOTE_ADDR', '2.2.2.2'], ['2.2.2.2']],
2345+
];
2346+
}
23272347
}
23282348

23292349
class RequestContentProxy extends Request

0 commit comments

Comments
 (0)
0