Skip to content

Commit 6b033ef

Browse files
committed
Replace REMOTE_ADDR in trusted proxies with the current REMOTE_ADDR
1 parent 5d94ace commit 6b033ef

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,14 +567,23 @@ 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+
if (isset($_SERVER['REMOTE_ADDR'])) {
580+
$proxies[] = $_SERVER['REMOTE_ADDR'];
581+
}
582+
} else {
583+
$proxies[] = $proxy;
584+
}
585+
return $proxies;
586+
}, []);
578587
self::$trustedHeaderSet = $trustedHeaderSet;
579588
}
580589

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy