1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
From: Ryan Weaver <ryan@thatsquality.com>
Date: Sat, 10 Feb 2018 17:35:59 -0500
Subject: Adding session strategy to ALL listeners to avoid *any* possible
fixation
[CVE-2018-11385] https://symfony.com/blog/cve-2018-11385-session-fixation-issue-for-guard-authentication
Origin: backport, https://github.com/symfony/symfony/commit/fa5bf4b17d45ee32f41bd1a9abc3fb6c134ec89b
---
.../Http/Firewall/AbstractPreAuthenticatedListener.php | 15 +++++++++++++++
.../Http/Firewall/BasicAuthenticationListener.php | 16 ++++++++++++++++
.../Http/Firewall/DigestAuthenticationListener.php | 14 ++++++++++++++
.../Http/Firewall/SimplePreAuthenticationListener.php | 16 ++++++++++++++++
.../Http/Session/SessionAuthenticationStrategy.php | 7 +++++--
.../Session/SessionAuthenticationStrategyInterface.php | 7 ++-----
6 files changed, 68 insertions(+), 7 deletions(-)
diff --git a/src/Symfony/Component/Security/Http/Firewall/AbstractPreAuthenticatedListener.php b/src/Symfony/Component/Security/Http/Firewall/AbstractPreAuthenticatedListener.php
index b793310..ee10e2a 100644
--- a/src/Symfony/Component/Security/Http/Firewall/AbstractPreAuthenticatedListener.php
+++ b/src/Symfony/Component/Security/Http/Firewall/AbstractPreAuthenticatedListener.php
@@ -84,6 +84,9 @@ abstract class AbstractPreAuthenticatedListener implements ListenerInterface
if (null !== $this->logger) {
$this->logger->info('Pre-authentication successful.', array('token' => (string) $token));
}
+
+ $this->migrateSession($request);
+
$this->tokenStorage->setToken($token);
if (null !== $this->dispatcher) {
@@ -120,4 +123,16 @@ abstract class AbstractPreAuthenticatedListener implements ListenerInterface
* @return array An array composed of the user and the credentials
*/
abstract protected function getPreAuthenticatedData(Request $request);
+
+ private function migrateSession(Request $request)
+ {
+ if (!$request->hasSession() || !$request->hasPreviousSession()) {
+ return;
+ }
+
+ // Destroying the old session is broken in php 5.4.0 - 5.4.10
+ // See https://bugs.php.net/63379
+ $destroy = \PHP_VERSION_ID < 50400 || \PHP_VERSION_ID >= 50411;
+ $request->getSession()->migrate($destroy);
+ }
}
diff --git a/src/Symfony/Component/Security/Http/Firewall/BasicAuthenticationListener.php b/src/Symfony/Component/Security/Http/Firewall/BasicAuthenticationListener.php
index ebe96ea..9a204cd 100644
--- a/src/Symfony/Component/Security/Http/Firewall/BasicAuthenticationListener.php
+++ b/src/Symfony/Component/Security/Http/Firewall/BasicAuthenticationListener.php
@@ -11,6 +11,7 @@
namespace Symfony\Component\Security\Http\Firewall;
+use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
@@ -72,6 +73,9 @@ class BasicAuthenticationListener implements ListenerInterface
try {
$token = $this->authenticationManager->authenticate(new UsernamePasswordToken($username, $request->headers->get('PHP_AUTH_PW'), $this->providerKey));
+
+ $this->migrateSession($request);
+
$this->tokenStorage->setToken($token);
} catch (AuthenticationException $e) {
$token = $this->tokenStorage->getToken();
@@ -90,4 +94,16 @@ class BasicAuthenticationListener implements ListenerInterface
$event->setResponse($this->authenticationEntryPoint->start($request, $e));
}
}
+
+ private function migrateSession(Request $request)
+ {
+ if (!$request->hasSession() || !$request->hasPreviousSession()) {
+ return;
+ }
+
+ // Destroying the old session is broken in php 5.4.0 - 5.4.10
+ // See https://bugs.php.net/63379
+ $destroy = \PHP_VERSION_ID < 50400 || \PHP_VERSION_ID >= 50411;
+ $request->getSession()->migrate($destroy);
+ }
}
diff --git a/src/Symfony/Component/Security/Http/Firewall/DigestAuthenticationListener.php b/src/Symfony/Component/Security/Http/Firewall/DigestAuthenticationListener.php
index d8d71fb..a123125 100644
--- a/src/Symfony/Component/Security/Http/Firewall/DigestAuthenticationListener.php
+++ b/src/Symfony/Component/Security/Http/Firewall/DigestAuthenticationListener.php
@@ -119,6 +119,8 @@ class DigestAuthenticationListener implements ListenerInterface
$this->logger->info('Digest authentication successful.', array('username' => $digestAuth->getUsername(), 'received' => $digestAuth->getResponse()));
}
+ $this->migrateSession($request);
+
$this->tokenStorage->setToken(new UsernamePasswordToken($user, $user->getPassword(), $this->providerKey));
}
@@ -135,6 +137,18 @@ class DigestAuthenticationListener implements ListenerInterface
$event->setResponse($this->authenticationEntryPoint->start($request, $authException));
}
+
+ private function migrateSession(Request $request)
+ {
+ if (!$request->hasSession() || !$request->hasPreviousSession()) {
+ return;
+ }
+
+ // Destroying the old session is broken in php 5.4.0 - 5.4.10
+ // See https://bugs.php.net/63379
+ $destroy = \PHP_VERSION_ID < 50400 || \PHP_VERSION_ID >= 50411;
+ $request->getSession()->migrate($destroy);
+ }
}
class DigestData
diff --git a/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php b/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php
index 8f1f6fd..7e936cd 100644
--- a/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php
+++ b/src/Symfony/Component/Security/Http/Firewall/SimplePreAuthenticationListener.php
@@ -11,6 +11,7 @@
namespace Symfony\Component\Security\Http\Firewall;
+use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
@@ -89,6 +90,9 @@ class SimplePreAuthenticationListener implements ListenerInterface
}
$token = $this->authenticationManager->authenticate($token);
+
+ $this->migrateSession($request);
+
$this->tokenStorage->setToken($token);
if (null !== $this->dispatcher) {
@@ -123,4 +127,16 @@ class SimplePreAuthenticationListener implements ListenerInterface
}
}
}
+
+ private function migrateSession(Request $request)
+ {
+ if (!$request->hasSession() || !$request->hasPreviousSession()) {
+ return;
+ }
+
+ // Destroying the old session is broken in php 5.4.0 - 5.4.10
+ // See https://bugs.php.net/63379
+ $destroy = \PHP_VERSION_ID < 50400 || \PHP_VERSION_ID >= 50411;
+ $request->getSession()->migrate($destroy);
+ }
}
diff --git a/src/Symfony/Component/Security/Http/Session/SessionAuthenticationStrategy.php b/src/Symfony/Component/Security/Http/Session/SessionAuthenticationStrategy.php
index ccfa6ba..15e9b24 100644
--- a/src/Symfony/Component/Security/Http/Session/SessionAuthenticationStrategy.php
+++ b/src/Symfony/Component/Security/Http/Session/SessionAuthenticationStrategy.php
@@ -47,9 +47,12 @@ class SessionAuthenticationStrategy implements SessionAuthenticationStrategyInte
return;
case self::MIGRATE:
+ // Note: this logic is duplicated in several authentication listeners
+ // until Symfony 5.0 due to a security fix with BC compat
+
// Destroying the old session is broken in php 5.4.0 - 5.4.10
- // See php bug #63379
- $destroy = PHP_VERSION_ID < 50400 || PHP_VERSION_ID >= 50411;
+ // See https://bugs.php.net/63379
+ $destroy = \PHP_VERSION_ID < 50400 || \PHP_VERSION_ID >= 50411;
$request->getSession()->migrate($destroy);
return;
diff --git a/src/Symfony/Component/Security/Http/Session/SessionAuthenticationStrategyInterface.php b/src/Symfony/Component/Security/Http/Session/SessionAuthenticationStrategyInterface.php
index dd0c381..8de89b1 100644
--- a/src/Symfony/Component/Security/Http/Session/SessionAuthenticationStrategyInterface.php
+++ b/src/Symfony/Component/Security/Http/Session/SessionAuthenticationStrategyInterface.php
@@ -27,11 +27,8 @@ interface SessionAuthenticationStrategyInterface
/**
* This performs any necessary changes to the session.
*
- * This method is called before the TokenStorage is populated with a
- * Token, and only by classes inheriting from AbstractAuthenticationListener.
- *
- * @param Request $request
- * @param TokenInterface $token
+ * This method should be called before the TokenStorage is populated with a
+ * Token. It should be used by authentication listeners when a session is used.
*/
public function onAuthentication(Request $request, TokenInterface $token);
}
|