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

Skip to content

[HttpKernel/HttpCache] Fix "REMOTE_ADDR" for cached subrequests #7092

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 1 commit into from
Closed
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
24 changes: 12 additions & 12 deletions src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,18 +418,6 @@ 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');

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

if ($this->isPrivateRequest($request) && !$response->headers->hasCacheControlDirective('public')) {
Expand Down Expand Up @@ -460,6 +448,18 @@ protected function forward(Request $request, $catch = false, Response $entry = n
$this->esi->addSurrogateEsiCapability($request);
}

// modify the X-Forwarded-For header if needed
$forwardedFor = $request->headers->get('X-Forwarded-For');
if ($forwardedFor) {
$request->headers->set('X-Forwarded-For', $forwardedFor.', '.$request->server->get('REMOTE_ADDR'));
} else {
$request->headers->set('X-Forwarded-For', $request->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.
$request->server->set('REMOTE_ADDR', '127.0.0.1');

// always a "master" request (as the real master request can be in cache)
$response = $this->kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch);
// FIXME: we probably need to also catch exceptions if raw === true
Expand Down