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

Skip to content

Commit 5990514

Browse files
bug #46697 [HttpKernel] Disable session tracking while collecting profiler data (nicolas-grekas)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpKernel] Disable session tracking while collecting profiler data | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - While playing with the Symfony book, I noticed that HttpCache wouldn't cache a public response because I was logged in as admin. After inspecting the reason why, I noticed that some data collectors access the session and thus make the request private. In this PR, the session tracking logic is disabled by using the same code that we use in the firewall, where we also ignore session tracking until authentication is actually needed by the app. Commits ------- 2ad3f30 [HttpKernel] Disable session tracking while collecting profiler data
2 parents e28d378 + 2ad3f30 commit 5990514

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/Symfony/Component/HttpKernel/EventListener/ProfilerListener.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
namespace Symfony\Component\HttpKernel\EventListener;
1313

1414
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15+
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
1617
use Symfony\Component\HttpFoundation\RequestStack;
18+
use Symfony\Component\HttpFoundation\Session\Session;
1719
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
1820
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1921
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
@@ -87,8 +89,21 @@ public function onKernelResponse(FilterResponseEvent $event)
8789
return;
8890
}
8991

90-
if (!$profile = $this->profiler->collect($request, $event->getResponse(), $exception)) {
91-
return;
92+
$session = method_exists(Request::class, 'getPreferredFormat') && $request->hasPreviousSession() && $request->hasSession() ? $request->getSession() : null;
93+
94+
if ($session instanceof Session) {
95+
$usageIndexValue = $usageIndexReference = &$session->getUsageIndex();
96+
$usageIndexReference = \PHP_INT_MIN;
97+
}
98+
99+
try {
100+
if (!$profile = $this->profiler->collect($request, $event->getResponse(), $exception)) {
101+
return;
102+
}
103+
} finally {
104+
if ($session instanceof Session) {
105+
$usageIndexReference = $usageIndexValue;
106+
}
92107
}
93108

94109
$this->profiles[$request] = $profile;

0 commit comments

Comments
 (0)