8000 merged branch fabpot/ip-cache (PR #7034) · symfony/symfony@22e997a · GitHub
[go: up one dir, main page]

Skip to content

Commit 22e997a

Browse files
committed
merged branch fabpot/ip-cache (PR #7034)
This PR was submitted for the 2.2 branch but it was merged into the 2.1 branch instead (closes #7034). Commits ------- 1fdded5 [HttpKernel] added support for the X-Forwarded-For header (closes #6982, closes #7000) be65d7c [HttpKernel] fixed the IP address in HttpCache when calling the backend Discussion ---------- Make HttpCache behaves more like a real reverse proxy | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #6982, #7000 | License | MIT | Doc PR | n/a --------------------------------------------------------------------------- by bendavies at 2013-02-10T00:55:29Z Awesome, thanks Fabien. should this not target 2.0/2.1?
2 parents e0637fa + 42d3c4c commit 22e997a

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,18 @@ protected function fetch(Request $request, $catch = false)
413413
$subRequest->headers->remove('if_modified_since');
414414
$subRequest->headers->remove('if_none_match');
415415

416+
// modify the X-Forwarded-For header if needed
417+
$forwardedFor = $subRequest->headers->get('X-Forwarded-For');
418+
if ($forwardedFor) {
419+
$subRequest->headers->set('X-Forwarded-For', $forwardedFor.', '.$subRequest->server->get('REMOTE_ADDR'));
420+
} else {
421+
$subRequest->headers->set('X-Forwarded-For', $subRequest->server->get('REMOTE_ADDR'));
422+
}
423+
424+
// fix the client IP address by setting it to 127.0.0.1 as HttpCache
425+
// is always called from the same process as the backend.
426+
$subRequest->server->set('REMOTE_ADDR', '127.0.0.1');
427+
416428
$response = $this->forward($subRequest, $catch);
417429

418430
if ($this->isPrivateRequest($request) && !$response->headers->hasCacheControlDirective('public')) {

src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,4 +1034,36 @@ public function testEsiRecalculateContentLengthHeader()
10341034
$this->assertEquals('Hello World!', $this->response->getContent());
10351035
$this->assertEquals(12, $this->response->headers->get('Content-Length'));
10361036
}
1037+
1038+
public function testClientIpIsAlwaysLocalhostForForwardedRequests()
1039+
{
1040+
$this->setNextResponse();
1041+
$this->request('GET', '/', array('REMOTE_ADDR' => '10.0.0.1'));
1042+
1043+
$this->assertEquals('127.0.0.1', $this->kernel->getBackendRequest()->server->get('REMOTE_ADDR'));
1044+
}
1045+
1046+
/**
1047+
* @dataProvider getXForwardedForData
1048+
*/
1049+
public function testXForwarderForHeaderForForwardedRequests($xForwardedFor, $expected)
1050+
{
1051+
$this->setNextResponse();
1052+
$server = array('REMOTE_ADDR' => '10.0.0.1');
1053+
if (false !== $xForwardedFor) {
1054+
$server['HTTP_X_FORWARDED_FOR'] = $xForwardedFor;
1055+
}
1056+
$this->request('GET', '/', $server);
1057+
1058+
$this->assertEquals($expected, $this->kernel->getBackendRequest()->headers->get('X-Forwarded-For'));
1059+
}
1060+
1061+
public function getXForwardedForData()
1062+
{
1063+
return array(
1064+
array(false, '10.0.0.1'),
1065+
array('10.0.0.2', '10.0.0.2, 10.0.0.1'),
1066+
array('10.0.0.2, 10.0.0.3', '10.0.0.2, 10.0.0.3, 10.0.0.1'),
1067+
);
1068+
}
10371069
}

src/Symfony/Component/HttpKernel/Tests/HttpCache/TestHttpKernel.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class TestHttpKernel extends HttpKernel implements ControllerResolverInterface
2626
protected $called;
2727
protected $customizer;
2828
protected $catch;
29+
protected $backendRequest;
2930

3031
public function __construct($body, $status, $headers, \Closure $customizer = null)
3132
{
@@ -39,9 +40,15 @@ public function __construct($body, $status, $headers, \Closure $customizer = nul
3940
parent::__construct(new EventDispatcher(), $this);
4041
}
4142

43+
public function getBackendRequest()
44+
{
45+
return $this->backendRequest;
46+
}
47+
4248
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = false)
4349
{
4450
$this->catch = $catch;
51+
$this->backendRequest = $request;
4552

4653
return parent::handle($request, $type, $catch);
4754
}

src/Symfony/Component/HttpKernel/Tests/HttpCache/TestMultipleHttpKernel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TestMultipleHttpKernel extends HttpKernel implements ControllerResolverInt
2525
protected $headers;
2626
protected $catch;
2727
protected $call;
28+
protected $backendRequest;
2829

2930
public function __construct($responses)
3031
{
@@ -42,8 +43,15 @@ public function __construct($responses)
4243
parent::__construct(new EventDispatcher(), $this);
4344
}
4445

46+
public function getBackendRequest()
47+
{
48+
return $this->backendRequest;
49+
}
50+
4551
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = false)
4652
{
53+
$this->backendRequest = $request;
54+
4755
return parent::handle($request, $type, $catch);
4856
}
4957

0 commit comments

Comments
 (0)
0