SecurityTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 1.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Utility;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\Utility\Crypto\OpenSsl;
  19. use Cake\Utility\Security;
  20. /**
  21. * SecurityTest class
  22. */
  23. class SecurityTest extends TestCase
  24. {
  25. /**
  26. * testHash method
  27. *
  28. * @return void
  29. */
  30. public function testHash()
  31. {
  32. $_hashType = Security::$hashType;
  33. $key = 'someKey';
  34. $hash = 'someHash';
  35. $this->assertSame(40, strlen(Security::hash($key, null, false)));
  36. $this->assertSame(40, strlen(Security::hash($key, 'sha1', false)));
  37. $this->assertSame(40, strlen(Security::hash($key, null, true)));
  38. $this->assertSame(40, strlen(Security::hash($key, 'sha1', true)));
  39. $result = Security::hash($key, null, $hash);
  40. $this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  41. $result = Security::hash($key, 'sha1', $hash);
  42. $this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  43. $hashType = 'sha1';
  44. Security::setHash($hashType);
  45. $this->assertSame($hashType, Security::$hashType);
  46. $this->assertSame(40, strlen(Security::hash($key, null, true)));
  47. $this->assertSame(40, strlen(Security::hash($key, null, false)));
  48. $this->assertSame(32, strlen(Security::hash($key, 'md5', false)));
  49. $this->assertSame(32, strlen(Security::hash($key, 'md5', true)));
  50. $hashType = 'md5';
  51. Security::setHash($hashType);
  52. $this->assertSame($hashType, Security::$hashType);
  53. $this->assertSame(32, strlen(Security::hash($key, null, false)));
  54. $this->assertSame(32, strlen(Security::hash($key, null, true)));
  55. $this->assertSame(64, strlen(Security::hash($key, 'sha256', false)));
  56. $this->assertSame(64, strlen(Security::hash($key, 'sha256', true)));
  57. Security::setHash($_hashType);
  58. }
  59. /**
  60. * testInvalidHashTypeException
  61. *
  62. * @expectedException \RuntimeException
  63. * @expectedExceptionMessageRegExp /The hash type `doesnotexist` was not found. Available algorithms are: \w+/
  64. * @return void
  65. */
  66. public function testInvalidHashTypeException()
  67. {
  68. Security::hash('test', 'doesnotexist', false);
  69. }
  70. /**
  71. * Test encrypt/decrypt.
  72. *
  73. * @return void
  74. */
  75. public function testEncryptDecrypt()
  76. {
  77. $txt = 'The quick brown fox';
  78. $key = 'This key is longer than 32 bytes long.';
  79. $result = Security::encrypt($txt, $key);
  80. $this->assertNotEquals($txt, $result, 'Should be encrypted.');
  81. $this->assertNotEquals($result, Security::encrypt($txt, $key), 'Each result is unique.');
  82. $this->assertEquals($txt, Security::decrypt($result, $key));
  83. }
  84. /**
  85. * Test that changing the key causes decryption to fail.
  86. *
  87. * @return void
  88. */
  89. public function testDecryptKeyFailure()
  90. {
  91. $txt = 'The quick brown fox';
  92. $key = 'This key is longer than 32 bytes long.';
  93. $result = Security::encrypt($txt, $key);
  94. $key = 'Not the same key. This one will fail';
  95. $this->assertNull(Security::decrypt($txt, $key), 'Modified key will fail.');
  96. }
  97. /**
  98. * Test that decrypt fails when there is an hmac error.
  99. *
  100. * @return void
  101. */
  102. public function testDecryptHmacFailure()
  103. {
  104. $txt = 'The quick brown fox';
  105. $key = 'This key is quite long and works well.';
  106. $salt = 'this is a delicious salt!';
  107. $result = Security::encrypt($txt, $key, $salt);
  108. // Change one of the bytes in the hmac.
  109. $result[10] = 'x';
  110. $this->assertNull(Security::decrypt($result, $key, $salt), 'Modified hmac causes failure.');
  111. }
  112. /**
  113. * Test that changing the hmac salt will cause failures.
  114. *
  115. * @return void
  116. */
  117. public function testDecryptHmacSaltFailure()
  118. {
  119. $txt = 'The quick brown fox';
  120. $key = 'This key is quite long and works well.';
  121. $salt = 'this is a delicious salt!';
  122. $result = Security::encrypt($txt, $key, $salt);
  123. $salt = 'humpty dumpty had a great fall.';
  124. $this->assertNull(Security::decrypt($result, $key, $salt), 'Modified salt causes failure.');
  125. }
  126. /**
  127. * Test that short keys cause errors
  128. *
  129. * @return void
  130. */
  131. public function testEncryptInvalidKey()
  132. {
  133. $this->expectException(\InvalidArgumentException::class);
  134. $this->expectExceptionMessage('Invalid key for encrypt(), key must be at least 256 bits (32 bytes) long.');
  135. $txt = 'The quick brown fox jumped over the lazy dog.';
  136. $key = 'this is too short';
  137. Security::encrypt($txt, $key);
  138. }
  139. /**
  140. * Test encrypting falsey data
  141. *
  142. * @return void
  143. */
  144. public function testEncryptDecryptFalseyData()
  145. {
  146. $key = 'This is a key that is long enough to be ok.';
  147. $result = Security::encrypt('', $key);
  148. $this->assertSame('', Security::decrypt($result, $key));
  149. $result = Security::encrypt('0', $key);
  150. $this->assertSame('0', Security::decrypt($result, $key));
  151. }
  152. /**
  153. * Test that short keys cause errors
  154. *
  155. * @return void
  156. */
  157. public function testDecryptInvalidKey()
  158. {
  159. $this->expectException(\InvalidArgumentException::class);
  160. $this->expectExceptionMessage('Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long.');
  161. $txt = 'The quick brown fox jumped over the lazy dog.';
  162. $key = 'this is too short';
  163. Security::decrypt($txt, $key);
  164. }
  165. /**
  166. * Test that empty data cause errors
  167. *
  168. * @return void
  169. */
  170. public function testDecryptInvalidData()
  171. {
  172. $this->expectException(\InvalidArgumentException::class);
  173. $this->expectExceptionMessage('The data to decrypt cannot be empty.');
  174. $txt = '';
  175. $key = 'This is a key that is long enough to be ok.';
  176. Security::decrypt($txt, $key);
  177. }
  178. /**
  179. * Test engine
  180. *
  181. * @return void
  182. */
  183. public function testEngineEquivalence()
  184. {
  185. $restore = Security::engine();
  186. $newEngine = new OpenSsl();
  187. Security::engine($newEngine);
  188. $this->assertSame($newEngine, Security::engine());
  189. $this->assertNotSame($restore, Security::engine());
  190. }
  191. /**
  192. * Tests that the salt can be set and retrieved
  193. *
  194. * @return void
  195. */
  196. public function testSalt()
  197. {
  198. Security::setSalt('foobarbaz');
  199. $this->assertEquals('foobarbaz', Security::getSalt());
  200. }
  201. /**
  202. * Tests that the salt can be set and retrieved
  203. *
  204. * @return void
  205. */
  206. public function testGetSetSalt()
  207. {
  208. Security::setSalt('foobarbaz');
  209. $this->assertEquals('foobarbaz', Security::getSalt());
  210. }
  211. /**
  212. * Test the randomBytes method.
  213. *
  214. * @return void
  215. */
  216. public function testRandomBytes()
  217. {
  218. $value = Security::randomBytes(16);
  219. $this->assertSame(16, strlen($value));
  220. $value = Security::randomBytes(64);
  221. $this->assertSame(64, strlen($value));
  222. $this->assertRegExp('/[^0-9a-f]/', $value, 'should return a binary string');
  223. }
  224. /**
  225. * Test the randomString method.
  226. *
  227. * @return void
  228. */
  229. public function testRandomString()
  230. {
  231. $value = Security::randomString(7);
  232. $this->assertSame(7, strlen($value));
  233. $value = Security::randomString();
  234. $this->assertSame(64, strlen($value));
  235. $this->assertRegExp('/^[0-9a-f]+$/', $value, 'should return a ASCII string');
  236. }
  237. /**
  238. * Test the insecureRandomBytes method
  239. *
  240. * @return void
  241. */
  242. public function testInsecureRandomBytes()
  243. {
  244. $value = Security::insecureRandomBytes(16);
  245. $this->assertSame(16, strlen($value));
  246. $value = Security::insecureRandomBytes(64);
  247. $this->assertSame(64, strlen($value));
  248. $this->assertRegExp('/[^0-9a-f]/', $value, 'should return a binary string');
  249. }
  250. /**
  251. * test constantEquals
  252. *
  253. * @return void
  254. */
  255. public function testConstantEquals()
  256. {
  257. $this->assertFalse(Security::constantEquals('abcde', null));
  258. $this->assertFalse(Security::constantEquals('abcde', false));
  259. $this->assertFalse(Security::constantEquals('abcde', true));
  260. $this->assertFalse(Security::constantEquals('abcde', 1));
  261. $this->assertFalse(Security::constantEquals(null, 'abcde'));
  262. $this->assertFalse(Security::constantEquals(false, 'abcde'));
  263. $this->assertFalse(Security::constantEquals(1, 'abcde'));
  264. $this->assertFalse(Security::constantEquals(true, 'abcde'));
  265. $this->assertTrue(Security::constantEquals('abcde', 'abcde'));
  266. $this->assertFalse(Security::constantEquals('abcdef', 'abcde'));
  267. $this->assertFalse(Security::constantEquals('abcde', 'abcdef'));
  268. $this->assertFalse(Security::constantEquals('a', 'abcdef'));
  269. $snowman = "\xe2\x98\x83";
  270. $this->assertTrue(Security::constantEquals($snowman, $snowman));
  271. $this->assertFalse(Security::constantEquals(str_repeat($snowman, 3), $snowman));
  272. }
  273. }