Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix surrogate not using original request #27309

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fix surrogate not using original request
  • Loading branch information
Toflar authored and nicolas-grekas committed Jun 19, 2018
commit ab86f43d7877a906d22713d7fae793f2076ce3f6
6 changes: 5 additions & 1 deletion 6 src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,11 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
// FIXME: catch exceptions and implement a 500 error page here? -> in Varnish, there is a built-in error page mechanism
if (HttpKernelInterface::MASTER_REQUEST === $type) {
$this->traces = array();
$this->request = $request;
// Keep a clone of the original request for surrogates so they can access it.
// We must clone here to get a separate instance because the application will modify the request during
// the application flow (we know it always does because we do ourselves by setting REMOTE_ADDR to 127.0.0.1
// and adding the X-Forwarded-For header, see HttpCache::forward()).
$this->request = clone $request;
if (null !== $this->surrogate) {
$this->surrogateCacheStrategy = $this->surrogate->createCacheStrategy();
}
Expand Down
39 changes: 39 additions & 0 deletions 39 src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

namespace Symfony\Component\HttpKernel\Tests\HttpCache;

use Symfony\Component\HttpKernel\HttpCache\Esi;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpCache\Store;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
Expand Down Expand Up @@ -1432,6 +1435,42 @@ public function testDoesNotCacheOptionsRequest()
$this->assertHttpKernelIsNotCalled();
$this->assertSame('get', $this->response->getContent());
}

public function testUsesOriginalRequestForSurrogate()
{
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
$store = $this->getMockBuilder(StoreInterface::class)->getMock();

$kernel
->expects($this->exactly(2))
->method('handle')
->willReturnCallback(function (Request $request) {
$this->assertSame('127.0.0.1', $request->server->get('REMOTE_ADDR'));

return new Response();
});

$cache = new HttpCache($kernel,
$store,
new Esi()
);

$request = Request::create('/');
$request->server->set('REMOTE_ADDR', '10.0.0.1');

// Main request
$cache->handle($request, HttpKernelInterface::MASTER_REQUEST);

// Main request was now modified by HttpCache
// The surrogate will ask for the request using $this->cache->getRequest()
// which MUST return the original request so the surrogate
// can actually behave like a reverse proxy like e.g. Varnish would.
$this->assertSame('10.0.0.1', $cache->getRequest()->getClientIp());
$this->assertSame('10.0.0.1', $cache->getRequest()->server->get('REMOTE_ADDR'));

// Surrogate request
$cache->handle($request, HttpKernelInterface::SUB_REQUEST);
}
}

class TestKernel implements HttpKernelInterface
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.