Skip to content

Commit 75acea0

Browse files
Merge branch '3.2'
* 3.2: [ci] Update travis/appveyor [HttpFoundation] Validate/cast cookie expire time
2 parents a422bc1 + c1bf268 commit 75acea0

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

Cookie.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ public function __construct($name, $value = null, $expire = 0, $path = '/', $dom
112112
} elseif (!is_numeric($expire)) {
113113
$expire = strtotime($expire);
114114

115-
if (false === $expire || -1 === $expire) {
115+
if (false === $expire) {
116116
throw new \InvalidArgumentException('The cookie expiration time is not valid.');
117117
}
118118
}
119119

120120
$this->name = $name;
121121
$this->value = $value;
122122
$this->domain = $domain;
123-
$this->expire = $expire;
123+
$this->expire = 0 < $expire ? (int) $expire : 0;
124124
$this->path = empty($path) ? '/' : $path;
125125
$this->secure = (bool) $secure;
126126
$this->httpOnly = (bool) $httpOnly;
@@ -147,7 +147,7 @@ public function __toString()
147147
} else {
148148
$str .= $this->isRaw() ? $this->getValue() : urlencode($this->getValue());
149149

150-
if ($this->getExpiresTime() !== 0) {
150+
if (0 !== $this->getExpiresTime()) {
151151
$str .= '; expires='.gmdate('D, d-M-Y H:i:s T', $this->getExpiresTime()).'; max-age='.$this->getMaxAge();
152152
}
153153
}

Tests/CookieTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ public function testInstantiationThrowsExceptionIfCookieNameContainsInvalidChara
5252
*/
5353
public function testInvalidExpiration()
5454
{
55-
$cookie = new Cookie('MyCookie', 'foo', 'bar');
55+
new Cookie('MyCookie', 'foo', 'bar');
56+
}
57+
58+
public function testNegativeExpirationIsNotPossible()
59+
{
60+
$cookie = new Cookie('foo', 'bar', -100);
61+
62+
$this->assertSame(0, $cookie->getExpiresTime());
5663
}
5764

5865
public function testGetValue()
@@ -81,6 +88,13 @@ public function testGetExpiresTime()
8188
$this->assertEquals($expire, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date');
8289
}
8390

91+
public function testGetExpiresTimeIsCastToInt()
92+
{
93+
$cookie = new Cookie('foo', 'bar', 3600.9);
94+
95+
$this->assertSame(3600, $cookie->getExpiresTime(), '->getExpiresTime() returns the expire date as an integer');
96+
}
97+
8498
public function testConstructorWithDateTime()
8599
{
86100
$expire = new \DateTime();

0 commit comments

Comments
 (0)