diff --git a/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php b/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php index 1bc3aa492ee4..97e365008b29 100644 --- a/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php +++ b/src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php @@ -413,6 +413,14 @@ protected function fetch(Request $request, $catch = false) $subRequest->headers->remove('if_modified_since'); $subRequest->headers->remove('if_none_match'); + // modify the X-Forwarded-For header if needed + $forwardedFor = $subRequest->headers->get('X-Forwarded-For'); + if ($forwardedFor) { + $subRequest->headers->set('X-Forwarded-For', $forwardedFor.', '.$subRequest->server->get('REMOTE_ADDR')); + } else { + $subRequest->headers->set('X-Forwarded-For', $subRequest->server->get('REMOTE_ADDR')); + } + // fix the client IP address by setting it to 127.0.0.1 as HttpCache // is always called from the same process as the backend. $subRequest->server->set('REMOTE_ADDR', '127.0.0.1'); diff --git a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php index 31dbbd555cf9..89203a8ae2da 100644 --- a/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php @@ -1042,4 +1042,28 @@ public function testClientIpIsAlwaysLocalhostForForwardedRequests() $this->assertEquals('127.0.0.1', $this->kernel->getBackendRequest()->server->get('REMOTE_ADDR')); } + + /** + * @dataProvider getXForwardedForData + */ + public function testXForwarderForHeaderForForwardedRequests($xForwardedFor, $expected) + { + $this->setNextResponse(); + $server = array('REMOTE_ADDR' => '10.0.0.1'); + if (false !== $xForwardedFor) { + $server['HTTP_X_FORWARDED_FOR'] = $xForwardedFor; + } + $this->request('GET', '/', $server); + + $this->assertEquals($expected, $this->kernel->getBackendRequest()->headers->get('X-Forwarded-For')); + } + + public function getXForwardedForData() + { + return array( + array(false, '10.0.0.1'), + array('10.0.0.2', '10.0.0.2, 10.0.0.1'), + array('10.0.0.2, 10.0.0.3', '10.0.0.2, 10.0.0.3, 10.0.0.1'), + ); + } }