Преглед изворни кода

[HttpFoundation] more sophisticated checks for valid expiration

Robert Schönthal пре 14 година
родитељ
комит
87e1359ebd

+ 6 - 1
src/Symfony/Component/HttpFoundation/Cookie.php

@@ -40,11 +40,16 @@ class Cookie
         if (empty($name)) {
             throw new \InvalidArgumentException('The cookie name cannot be empty');
         }
+        
+        //check if the expiration is valid
+        if(!$expire instanceof \DateTime && !is_numeric($expire) && (strtotime($expire) === false || strtotime($expire) === -1)){
+            throw new \InvalidArgumentException('The cookie expiration is not valid');
+        }
 
         $this->name = $name;
         $this->value = $value;
         $this->domain = $domain;
-        $this->expire = (integer) $expire;
+        $this->expire = $expire;
         $this->path = $path;
         $this->secure = (Boolean) $secure;
         $this->httponly = (Boolean) $httponly;

+ 8 - 0
tests/Symfony/Tests/Component/HttpFoundation/CookieTest.php

@@ -68,6 +68,14 @@ class CookieTest extends \PHPUnit_Framework_TestCase
     {
         new Cookie('MyCookie', $value);
     }
+    
+    /**
+     * @expectedException InvalidArgumentException
+     */
+    public function testInvalidExpiration()
+    {
+        $cookie = new Cookie('MyCookie', 'foo','bar');        
+    }
 
     /**
      * @covers Symfony\Component\HttpFoundation\Cookie::getValue