Skip to content

Commit

Permalink
Fix cookie expiry time calculation on 32bit systems.
Browse files Browse the repository at this point in the history
strtotime() misbehaves on 32bit systems when the resulting timestamp
would overflow an integer. Use a DateTime to workaround this issue.

Fixes #3868
  • Loading branch information
markstory committed Jun 4, 2013
1 parent 027cfe9 commit 3aa189e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/Cake/Controller/Component/CookieComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,20 +387,20 @@ public function type($type = 'cipher') {
* @return integer Unix timestamp
*/
protected function _expire($expires = null) {
$now = time();
if (is_null($expires)) {
return $this->_expires;
}
$this->_reset = $this->_expires;

if (!$expires) {
return $this->_expires = 0;
}
$now = new DateTime();

if (is_int($expires) || is_numeric($expires)) {
return $this->_expires = $now + intval($expires);
return $this->_expires = $now->format('U') + intval($expires);
}
return $this->_expires = strtotime($expires, $now);
$now->modify($expires);
return $this->_expires = $now->format('U');
}

/**
Expand Down
25 changes: 25 additions & 0 deletions lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,31 @@ public function testWriteSimple() {
$this->assertEquals('value', $result);
}

/**
* test write with distant future cookies
*
* @return void
*/
public function testWriteFarFuture() {
$this->Cookie->write('Testing', 'value', false, '+90 years');
$future = new DateTime('now');
$future->modify('+90 years');

$expected = array(
'name' => $this->Cookie->name . '[Testing]',
'value' => 'value',
'path' => '/',
'domain' => '',
'secure' => false,
'httpOnly' => false);
$result = $this->Controller->response->cookie($this->Cookie->name . '[Testing]');

$this->assertEquals($future->format('U'), $result['expire'], '', 3);
unset($result['expire']);

$this->assertEquals($expected, $result);
}

/**
* test write with httpOnly cookies
*
Expand Down

0 comments on commit 3aa189e

Please sign in to comment.