Skip to content

Commit 643c9ff

Browse files
committed
Replace REMOTE_ADDR in trusted proxies with the current REMOTE_ADDR
1 parent 41a450b commit 643c9ff

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)
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