From 8a666fb37e339627af2f9da492183619bcd8cff3 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 15 Dec 2013 19:26:21 +0530 Subject: [PATCH 1/2] Don't throw exception when trying to encrypt falsey value. --- lib/Cake/Test/Case/Utility/SecurityTest.php | 23 +++++++++++++++------ lib/Cake/Utility/Security.php | 4 +--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/SecurityTest.php b/lib/Cake/Test/Case/Utility/SecurityTest.php index 3fe83b27c17..d0f6a11af13 100644 --- a/lib/Cake/Test/Case/Utility/SecurityTest.php +++ b/lib/Cake/Test/Case/Utility/SecurityTest.php @@ -375,16 +375,27 @@ public function testEncryptInvalidKey() { } /** - * Test that empty data cause errors + * Test encrypting falsey data * - * @expectedException CakeException - * @expectedExceptionMessage The data to encrypt cannot be empty. * @return void */ - public function testEncryptInvalidData() { - $txt = ''; + public function testEncryptDecryptFalseyData() { $key = 'This is a key that is long enough to be ok.'; - Security::encrypt($txt, $key); + + $result = Security::encrypt('', $key); + $this->assertSame('', Security::decrypt($result, $key)); + + $result = Security::encrypt(false, $key); + $this->assertSame('', Security::decrypt($result, $key)); + + $result = Security::encrypt(null, $key); + $this->assertSame('', Security::decrypt($result, $key)); + + $result = Security::encrypt(0, $key); + $this->assertSame('0', Security::decrypt($result, $key)); + + $result = Security::encrypt('0', $key); + $this->assertSame('0', Security::decrypt($result, $key)); } /** diff --git a/lib/Cake/Utility/Security.php b/lib/Cake/Utility/Security.php index 30b5e111ab2..2a1bd2c568b 100644 --- a/lib/Cake/Utility/Security.php +++ b/lib/Cake/Utility/Security.php @@ -303,9 +303,7 @@ protected static function _crypt($password, $salt = false) { */ public static function encrypt($plain, $key, $hmacSalt = null) { self::_checkKey($key, 'encrypt()'); - if (empty($plain)) { - throw new CakeException(__d('cake_dev', 'The data to encrypt cannot be empty.')); - } + if ($hmacSalt === null) { $hmacSalt = Configure::read('Security.salt'); } From 27979286b272c9e5dfd4dd540ca4cb2674caaf31 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 15 Dec 2013 20:29:41 +0530 Subject: [PATCH 2/2] Revert change done in 11f543f1f2. The change is unneeded now as Security::encrypt() no longer throws exception for falsey values. --- lib/Cake/Controller/Component/CookieComponent.php | 2 +- .../Case/Controller/Component/CookieComponentTest.php | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/Cake/Controller/Component/CookieComponent.php b/lib/Cake/Controller/Component/CookieComponent.php index fd7db612422..d1aececd278 100644 --- a/lib/Cake/Controller/Component/CookieComponent.php +++ b/lib/Cake/Controller/Component/CookieComponent.php @@ -472,7 +472,7 @@ protected function _encrypt($value) { if (is_array($value)) { $value = $this->_implode($value); } - if (!$this->_encrypted || !$value) { + if (!$this->_encrypted) { return $value; } $prefix = "Q2FrZQ==."; diff --git a/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php b/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php index ae07a485fa8..f308a267b6b 100644 --- a/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php +++ b/lib/Cake/Test/Case/Controller/Component/CookieComponentTest.php @@ -202,11 +202,11 @@ public function testWriteSimple() { } /** - * test write() Encrypted data with null & empty string & boolean value + * test write() encrypted data with falsey value * * @return void */ - public function testWriteWithNullEmptyString() { + public function testWriteWithFalseyValue() { $this->Cookie->type('aes'); $this->Cookie->key = 'qSI232qs*&sXOw!adre@34SAv!@*(XSL#$%)asGb$@11~_+!@#HKis~#^'; @@ -228,7 +228,11 @@ public function testWriteWithNullEmptyString() { $this->Cookie->write('Testing', '0'); $result = $this->Cookie->read('Testing'); - $this->assertEquals('0', $result); + $this->assertSame('0', $result); + + $this->Cookie->write('Testing', 0); + $result = $this->Cookie->read('Testing'); + $this->assertSame(0, $result); } /**