浏览代码

[Security] LogoutListener should not invoke handlers' logout() method if token is empty

If a user was not authenticated and visited the logout path, a null value was passed to the handler's logout() method, resulting in a catchable fatal error.
Jeremy Mikola 14 年之前
父节点
当前提交
46b1b5bd60
共有 1 个文件被更改,包括 13 次插入13 次删除
  1. 13 13
      src/Symfony/Component/HttpKernel/Security/Firewall/LogoutListener.php

+ 13 - 13
src/Symfony/Component/HttpKernel/Security/Firewall/LogoutListener.php

@@ -27,7 +27,7 @@ class LogoutListener implements ListenerInterface
     protected $securityContext;
     protected $logoutPath;
     protected $targetUrl;
-    protected $handlers;    
+    protected $handlers;
 
     /**
      * Constructor
@@ -43,17 +43,17 @@ class LogoutListener implements ListenerInterface
         $this->targetUrl = $targetUrl;
         $this->handlers = array();
     }
-    
+
     /**
      * Adds a logout handler
-     * 
+     *
      * @param LogoutHandlerInterface $handler
      * @return void
      */
     public function addHandler(LogoutHandlerInterface $handler)
     {
         $this->handlers[] = $handler;
-    }    
+    }
 
     /**
      * Registers a core.security listener.
@@ -65,14 +65,14 @@ class LogoutListener implements ListenerInterface
     {
         $dispatcher->connect('core.security', array($this, 'handle'), 0);
     }
-    
+
     /**
      * {@inheritDoc}
      */
     public function unregister(EventDispatcher $dispatcher)
     {
     }
-    
+
     /**
      * Performs the logout if requested
      *
@@ -85,16 +85,16 @@ class LogoutListener implements ListenerInterface
         if ($this->logoutPath !== $request->getPathInfo()) {
             return;
         }
-        
+
         $response = new Response();
         $response->setRedirect(0 !== strpos($this->targetUrl, 'http') ? $request->getUriForPath($this->targetUrl) : $this->targetUrl, 302);
-        
-        $token = $this->securityContext->getToken();
-        
-        foreach ($this->handlers as $handler) {
-            $handler->logout($request, $response, $token);
+
+        if ($token = $this->securityContext->getToken()) {
+            foreach ($this->handlers as $handler) {
+                $handler->logout($request, $response, $token);
+            }
         }
-        
+
         $this->securityContext->setToken(null);
         $event->setReturnValue($response);