Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Make HttpCache behaves more like a real reverse proxy #7034

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,18 @@ 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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any reason for only setting it on sub-requests? I find it more consistent to make it for the master request instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My approach is consistent: the master request is handled by the reverse proxy and must keep the information from its client. The sub-request is sent by the reverse proxy and has the local IP address.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant consistency regarding your effort to make HttpCache beahving more like a real reverse proxy, with a real reverse proxy, this header is set on master request, anyway, it's not a big deal though.


$response = $this->forward($subRequest, $catch);

if ($this->isPrivateRequest($request) && !$response->headers->hasCacheControlDirective('public')) {
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1034,4 +1034,36 @@ public function testEsiRecalculateContentLengthHeader()
$this->assertEquals('Hello World!', $this->response->getContent());
$this->assertEquals(12, $this->response->headers->get('Content-Length'));
}

public function testClientIpIsAlwaysLocalhostForForwardedRequests()
{
$this->setNextResponse();
$this->request('GET', '/', array('REMOTE_ADDR' => '10.0.0.1'));

$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'),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class TestHttpKernel extends HttpKernel implements ControllerResolverInterface
protected $called;
protected $customizer;
protected $catch;
protected $backendRequest;

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

public function getBackendRequest()
{
return $this->backendRequest;
}

public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = false)
{
$this->catch = $catch;
$this->backendRequest = $request;

return parent::handle($request, $type, $catch);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class TestMultipleHttpKernel extends HttpKernel implements ControllerResolverInt
protected $headers;
protected $catch;
protected $call;
protected $backendRequest;

public function __construct($responses)
{
Expand All @@ -42,8 +43,15 @@ public function __construct($responses)
parent::__construct(new EventDispatcher(), $this);
}

public function getBackendRequest()
{
return $this->backendRequest;
}

public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = false)
{
$this->backendRequest = $request;

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

Expand Down