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

Skip to content

Commit 187645f

Browse files
committed
Fix REMOTE_ADDR for cached subrequests
Fixes #7091
1 parent 83fc5ed commit 187645f

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

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

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

421-
// modify the X-Forwarded-For header if needed
422-
$forwardedFor = $subRequest->headers->get('X-Forwarded-For');
423-
if ($forwardedFor) {
424-
$subRequest->headers->set('X-Forwarded-For', $forwardedFor.', '.$subRequest->server->get('REMOTE_ADDR'));
425-
} else {
426-
$subRequest->headers->set('X-Forwarded-For', $subRequest->server->get('REMOTE_ADDR'));
427-
}
428-
429-
// fix the client IP address by setting it to 127.0.0.1 as HttpCache
430-
// is always called from the same process as the backend.
431-
$subRequest->server->set('REMOTE_ADDR', '127.0.0.1');
432-
433421
$response = $this->forward($subRequest, $catch);
434422

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

451+
// modify the X-Forwarded-For header if needed
452+
$forwardedFor = $request->headers->get('X-Forwarded-For');
453+
if ($forwardedFor) {
454+
$request->headers->set('X-Forwarded-For', $forwardedFor.', '.$request->server->get('REMOTE_ADDR'));
455+
} else {
456+
$request->headers->set('X-Forwarded-For', $request->server->get('REMOTE_ADDR'));
457+
}
458+
459+
// fix the client IP address by setting it to 127.0.0.1 as HttpCache
460+
// is always called from the same process as the backend.
461+
$request->server->set('REMOTE_ADDR', '127.0.0.1');
462+
463463
// always a "master" request (as the real master request can be in cache)
464464
$response = $this->kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, $catch);
465465
// FIXME: we probably need to also catch exceptions if raw === true

0 commit comments

Comments
 (0)