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

Skip to content

Commit 0eff68f

Browse files
mweimerskirchfabpot
authored andcommitted
Fix REMOTE_ADDR for cached subrequests
Fixes #7091
1 parent 7d143aa commit 0eff68f

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
@@ -413,18 +413,6 @@ 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-
428416
$response = $this->forward($subRequest, $catch);
429417

430418
if ($this->isPrivateRequest($request) && !$response->headers->hasCacheControlDirective('public')) {
@@ -455,6 +443,18 @@ protected function forward(Request $request, $catch = false, Response $entry = n
455443
$this->esi->addSurrogateEsiCapability($request);
456444
}
457445

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

0 commit comments

Comments
 (0)