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

Skip to content

Commit 5824ab8

Browse files
bug #32703 Ensure $request->hasSession() is always checked before calling getSession() (Arman-Hosseini)
This PR was merged into the 4.4 branch. Discussion ---------- Ensure $request->hasSession() is always checked before calling getSession() | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 7b2c326 Ensure $request->hasSession() is always checked before calling getSession()
2 parents b2dadc1 + 7b2c326 commit 5824ab8

File tree

9 files changed

+21
-26
lines changed

9 files changed

+21
-26
lines changed

src/Symfony/Bridge/Twig/AppVariable.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,9 @@ public function getSession()
112112
if (null === $this->requestStack) {
113113
throw new \RuntimeException('The "app.session" variable is not available.');
114114
}
115+
$request = $this->getRequest();
115116

116-
if ($request = $this->getRequest()) {
117-
return $request->getSession();
118-
}
117+
return $request && $request->hasSession() ? $request->getSession() : null;
119118
}
120119

121120
/**
@@ -157,8 +156,7 @@ public function getDebug()
157156
public function getFlashes($types = null)
158157
{
159158
try {
160-
$session = $this->getSession();
161-
if (null === $session) {
159+
if (null === $session = $this->getSession()) {
162160
return [];
163161
}
164162
} catch (\RuntimeException $e) {

src/Symfony/Bridge/Twig/Tests/AppVariableTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function testEnvironment()
5151
public function testGetSession()
5252
{
5353
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
54+
$request->method('hasSession')->willReturn(true);
5455
$request->method('getSession')->willReturn($session = new Session());
5556

5657
$this->setRequestStack($request);
@@ -267,6 +268,7 @@ private function setFlashMessages($sessionHasStarted = true)
267268
$session->method('getFlashBag')->willReturn($flashBag);
268269

269270
$request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
271+
$request->method('hasSession')->willReturn(true);
270272
$request->method('getSession')->willReturn($session);
271273
$this->setRequestStack($request);
272274

src/Symfony/Bundle/FrameworkBundle/Templating/GlobalVariables.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ public function getRequest()
7575
*/
7676
public function getSession()
7777
{
78-
if ($request = $this->getRequest()) {
79-
return $request->getSession();
80-
}
78+
$request = $this->getRequest();
79+
80+
return $request && $request->hasSession() ? $request->getSession() : null;
8181
}
8282

8383
/**

src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function toolbarAction(Request $request, $token)
123123
throw new NotFoundHttpException('The profiler must be enabled.');
124124
}
125125

126-
if ($request->hasSession() && ($session = $request->getSession()) && $session->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
126+
if ($request->hasSession() && ($session = $request->getSession())->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
127127
// keep current flashes for one more request if using AutoExpireFlashBag
128128
$session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
129129
}

src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ public function onKernelResponse(FilterResponseEvent $event)
8888
}
8989

9090
if ($response->headers->has('X-Debug-Token') && $response->isRedirect() && $this->interceptRedirects && 'html' === $request->getRequestFormat()) {
91-
$session = $request->getSession();
92-
if (null !== $session && $session->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
91+
if ($request->hasSession() && ($session = $request->getSession())->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
9392
// keep current flashes for one more request if using AutoExpireFlashBag
9493
$session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
9594
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ public function onKernelRequest(GetResponseEvent $event)
4646
}
4747

4848
// bootstrap the session
49-
$session = $this->getSession();
50-
if (!$session) {
49+
if (!$session = $this->getSession()) {
5150
return;
5251
}
5352

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public function onKernelResponse(FilterResponseEvent $event)
3030
return;
3131
}
3232

33-
$session = $event->getRequest()->getSession();
34-
if ($session && $session->isStarted()) {
33+
$request = $event->getRequest();
34+
if ($request->hasSession() && ($session = $request->getSession())->isStarted()) {
3535
$session->save();
3636
}
3737
}

src/Symfony/Component/Security/Http/Authentication/AuthenticationUtils.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ public function __construct(RequestStack $requestStack)
3838
public function getLastAuthenticationError($clearSession = true)
3939
{
4040
$request = $this->getRequest();
41-
$session = $request->getSession();
4241
$authenticationException = null;
4342

4443
if ($request->attributes->has(Security::AUTHENTICATION_ERROR)) {
4544
$authenticationException = $request->attributes->get(Security::AUTHENTICATION_ERROR);
46-
} elseif (null !== $session && $session->has(Security::AUTHENTICATION_ERROR)) {
45+
} elseif ($request->hasSession() && ($session = $request->getSession())->has(Security::AUTHENTICATION_ERROR)) {
4746
$authenticationException = $session->get(Security::AUTHENTICATION_ERROR);
4847

4948
if ($clearSession) {
@@ -65,9 +64,7 @@ public function getLastUsername()
6564
return $request->attributes->get(Security::LAST_USERNAME, '');
6665
}
6766

68-
$session = $request->getSession();
69-
70-
return null === $session ? '' : $session->get(Security::LAST_USERNAME, '');
67+
return $request->hasSession() ? $request->getSession()->get(Security::LAST_USERNAME, '') : '';
7168
}
7269

7370
/**

src/Symfony/Component/Security/Http/Firewall/ContextListener.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function __invoke(RequestEvent $event)
9090
}
9191

9292
$request = $event->getRequest();
93-
$session = $request->hasPreviousSession() ? $request->getSession() : null;
93+
$session = $request->hasPreviousSession() && $request->hasSession() ? $request->getSession() : null;
9494

9595
if (null === $session || null === $token = $session->get($this->sessionKey)) {
9696
$this->tokenStorage->setToken(null);
@@ -137,14 +137,14 @@ public function onKernelResponse(FilterResponseEvent $event)
137137

138138
$this->dispatcher->removeListener(KernelEvents::RESPONSE, [$this, 'onKernelResponse']);
139139
$this->registered = false;
140-
$session = $request->getSession();
140+
$token = $this->tokenStorage->getToken();
141141

142-
if ((null === $token = $this->tokenStorage->getToken()) || $this->trustResolver->isAnonymous($token)) {
143-
if ($request->hasPreviousSession()) {
144-
$session->remove($this->sessionKey);
142+
if (null === $token || $this->trustResolver->isAnonymous($token)) {
143+
if ($request->hasPreviousSession() && $request->hasSession()) {
144+
$request->getSession()->remove($this->sessionKey);
145145
}
146146
} else {
147-
$session->set($this->sessionKey, serialize($token));
147+
$request->getSession()->set($this->sessionKey, serialize($token));
148148

149149
if (null !== $this->logger) {
150150
$this->logger->debug('Stored the security token in the session.', ['key' => $this->sessionKey]);

0 commit comments

Comments
 (0)