Skip to content

Commit

Permalink
added support for the X-Forwarded-For header (closes #6982, closes #7000
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fabpot committed Feb 11, 2013
1 parent 6a9c510 commit 42d3c4c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Expand Up @@ -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');
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
Expand Up @@ -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'),
);
}
}

0 comments on commit 42d3c4c

Please sign in to comment.