Don't emit errors when operating on corrupted cookie data.

When deleting from corrupted cookie data, there shouldn't be any errors.

Refs #9779
This commit is contained in:
mark_story 2016-12-03 14:10:47 -05:00
parent 410df003e6
commit 27f951fb41
2 changed files with 25 additions and 3 deletions

View file

@ -283,8 +283,11 @@ class CookieComponent extends Component {
return null;
}
if (!empty($names[1]) && is_array($this->_values[$this->name][$key])) {
return Hash::get($this->_values[$this->name][$key], $names[1]);
if (!empty($names[1])) {
if (is_array($this->_values[$this->name][$key])) {
return Hash::get($this->_values[$this->name][$key], $names[1]);
}
return null;
}
return $this->_values[$this->name][$key];
}
@ -336,7 +339,7 @@ class CookieComponent extends Component {
return;
}
$names = explode('.', $key, 2);
if (isset($this->_values[$this->name][$names[0]])) {
if (isset($this->_values[$this->name][$names[0]]) && is_array($this->_values[$this->name][$names[0]])) {
$this->_values[$this->name][$names[0]] = Hash::remove($this->_values[$this->name][$names[0]], $names[1]);
}
$this->_delete('[' . implode('][', $names) . ']');

View file

@ -451,6 +451,25 @@ class CookieComponentTest extends CakeTestCase {
$this->assertNull($data);
}
/**
* test delete() on corrupted/truncated cookie data.
*
* @return void
*/
public function testDeleteCorruptedCookieData() {
$this->Cookie->type('aes');
$this->Cookie->key = sha1('some bad key');
$data = $this->_implode(array('name' => 'jill', 'age' => 24));
// Corrupt the cookie data by slicing some bytes off.
$_COOKIE['CakeTestCookie'] = array(
'BadData' => substr(Security::encrypt($data, $this->Cookie->key), 0, -5)
);
$this->assertNull($this->Cookie->delete('BadData.name'));
$this->assertNull($this->Cookie->read('BadData.name'));
}
/**
* testReadingCookieArray
*