瀏覽代碼

[Form] throw an exception if session_id() is empty when a csrf token is generated

Jordi Boggiano 14 年之前
父節點
當前提交
a198bbcf43
共有 2 個文件被更改,包括 10 次插入1 次删除
  1. 5 1
      src/Symfony/Component/Form/Form.php
  2. 5 0
      tests/Symfony/Tests/Component/Form/FormTest.php

+ 5 - 1
src/Symfony/Component/Form/Form.php

@@ -176,7 +176,11 @@ class Form extends FieldGroup
      */
     protected function generateCsrfToken($secret)
     {
-        return md5($secret.session_id().get_class($this));
+        $sessId = session_id();
+        if (!$sessId) {
+            throw new \LogicException('The session must be started in order to generate a proper CSRF Token');
+        }
+        return md5($secret.$sessId.get_class($this));
     }
 
     /**

+ 5 - 0
tests/Symfony/Tests/Component/Form/FormTest.php

@@ -47,6 +47,11 @@ class FormTest extends \PHPUnit_Framework_TestCase
     protected $validator;
     protected $form;
 
+    public static function setUpBeforeClass()
+    {
+        @session_start();
+    }
+
     protected function setUp()
     {
         Form::disableDefaultCsrfProtection();