SecurityTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. use InvalidArgumentException;
  21. /**
  22. * SecurityTest class
  23. */
  24. class SecurityTest extends TestCase
  25. {
  26. /**
  27. * Test engine
  28. */
  29. public function testEngineEquivalence(): void
  30. {
  31. $restore = Security::engine();
  32. $newEngine = new OpenSsl();
  33. Security::engine($newEngine);
  34. $this->assertSame($newEngine, Security::engine());
  35. $this->assertNotSame($restore, Security::engine());
  36. }
  37. /**
  38. * testHash method
  39. */
  40. public function testHash(): void
  41. {
  42. $_hashType = Security::$hashType;
  43. $key = 'someKey';
  44. $hash = 'someHash';
  45. $this->assertSame(40, strlen(Security::hash($key, null, false)));
  46. $this->assertSame(40, strlen(Security::hash($key, 'sha1', false)));
  47. $this->assertSame(40, strlen(Security::hash($key, null, true)));
  48. $this->assertSame(40, strlen(Security::hash($key, 'sha1', true)));
  49. $result = Security::hash($key, null, $hash);
  50. $this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  51. $result = Security::hash($key, 'sha1', $hash);
  52. $this->assertSame($result, 'e38fcb877dccb6a94729a81523851c931a46efb1');
  53. $hashType = 'sha1';
  54. Security::setHash($hashType);
  55. $this->assertSame($hashType, Security::$hashType);
  56. $this->assertSame(40, strlen(Security::hash($key, null, true)));
  57. $this->assertSame(40, strlen(Security::hash($key, null, false)));
  58. $this->assertSame(32, strlen(Security::hash($key, 'md5', false)));
  59. $this->assertSame(32, strlen(Security::hash($key, 'md5', true)));
  60. $hashType = 'md5';
  61. Security::setHash($hashType);
  62. $this->assertSame($hashType, Security::$hashType);
  63. $this->assertSame(32, strlen(Security::hash($key, null, false)));
  64. $this->assertSame(32, strlen(Security::hash($key, null, true)));
  65. $this->assertSame(64, strlen(Security::hash($key, 'sha256', false)));
  66. $this->assertSame(64, strlen(Security::hash($key, 'sha256', true)));
  67. Security::setHash($_hashType);
  68. }
  69. /**
  70. * testInvalidHashTypeException
  71. */
  72. public function testInvalidHashTypeException(): void
  73. {
  74. $this->expectException(InvalidArgumentException::class);
  75. $this->expectExceptionMessageMatches('/The hash type `doesnotexist` was not found. Available algorithms are: `\w+/');
  76. Security::hash('test', 'doesnotexist', false);
  77. }
  78. /**
  79. * Test encrypt/decrypt.
  80. */
  81. public function testEncryptDecrypt(): void
  82. {
  83. $txt = 'The quick brown fox';
  84. $key = 'This key is longer than 32 bytes long.';
  85. $result = Security::encrypt($txt, $key);
  86. $this->assertNotEquals($txt, $result, 'Should be encrypted.');
  87. $this->assertNotEquals($result, Security::encrypt($txt, $key), 'Each result is unique.');
  88. $this->assertSame($txt, Security::decrypt($result, $key));
  89. }
  90. /**
  91. * Test that changing the key causes decryption to fail.
  92. */
  93. public function testDecryptKeyFailure(): void
  94. {
  95. $txt = 'The quick brown fox';
  96. $key = 'This key is longer than 32 bytes long.';
  97. $result = Security::encrypt($txt, $key);
  98. $key = 'Not the same key. This one will fail';
  99. $this->assertNull(Security::decrypt($result, $key), 'Modified key will fail.');
  100. }
  101. /**
  102. * Test that decrypt fails when there is an hmac error.
  103. */
  104. public function testDecryptHmacFailure(): void
  105. {
  106. $txt = 'The quick brown fox';
  107. $key = 'This key is quite long and works well.';
  108. $salt = 'this is a delicious salt!';
  109. $result = Security::encrypt($txt, $key, $salt);
  110. // Change one of the bytes in the hmac.
  111. $result[10] = 'x';
  112. $this->assertNull(Security::decrypt($result, $key, $salt), 'Modified hmac causes failure.');
  113. }
  114. /**
  115. * Test that changing the hmac salt will cause failures.
  116. */
  117. public function testDecryptHmacSaltFailure(): void
  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. public function testEncryptInvalidKey(): void
  130. {
  131. $this->expectException(InvalidArgumentException::class);
  132. $this->expectExceptionMessage('Invalid key for encrypt(), key must be at least 256 bits (32 bytes) long.');
  133. $txt = 'The quick brown fox jumped over the lazy dog.';
  134. $key = 'this is too short';
  135. Security::encrypt($txt, $key);
  136. }
  137. /**
  138. * Test encrypting falsey data
  139. */
  140. public function testEncryptDecryptFalseyData(): void
  141. {
  142. $key = 'This is a key that is long enough to be ok.';
  143. $result = Security::encrypt('', $key);
  144. $this->assertSame('', Security::decrypt($result, $key));
  145. $result = Security::encrypt('0', $key);
  146. $this->assertSame('0', Security::decrypt($result, $key));
  147. }
  148. /**
  149. * Test that short keys cause errors
  150. */
  151. public function testDecryptInvalidKey(): void
  152. {
  153. $this->expectException(InvalidArgumentException::class);
  154. $this->expectExceptionMessage('Invalid key for decrypt(), key must be at least 256 bits (32 bytes) long.');
  155. $txt = 'The quick brown fox jumped over the lazy dog.';
  156. $key = 'this is too short';
  157. Security::decrypt($txt, $key);
  158. }
  159. /**
  160. * Test that empty data cause errors
  161. */
  162. public function testDecryptInvalidData(): void
  163. {
  164. $this->expectException(InvalidArgumentException::class);
  165. $this->expectExceptionMessage('The data to decrypt cannot be empty.');
  166. $txt = '';
  167. $key = 'This is a key that is long enough to be ok.';
  168. Security::decrypt($txt, $key);
  169. }
  170. /**
  171. * Tests that the salt can be set and retrieved
  172. */
  173. public function testSalt(): void
  174. {
  175. Security::setSalt('foobarbaz');
  176. $this->assertSame('foobarbaz', Security::getSalt());
  177. }
  178. /**
  179. * Tests that the salt can be set and retrieved
  180. */
  181. public function testGetSetSalt(): void
  182. {
  183. Security::setSalt('foobarbaz');
  184. $this->assertSame('foobarbaz', Security::getSalt());
  185. }
  186. /**
  187. * Test the randomBytes method.
  188. */
  189. public function testRandomBytes(): void
  190. {
  191. $value = Security::randomBytes(16);
  192. $this->assertSame(16, strlen($value));
  193. $value = Security::randomBytes(64);
  194. $this->assertSame(64, strlen($value));
  195. $this->assertMatchesRegularExpression('/[^0-9a-f]/', $value, 'should return a binary string');
  196. }
  197. /**
  198. * Test the randomString method.
  199. */
  200. public function testRandomString(): void
  201. {
  202. $value = Security::randomString(7);
  203. $this->assertSame(7, strlen($value));
  204. $value = Security::randomString();
  205. $this->assertSame(64, strlen($value));
  206. $this->assertMatchesRegularExpression('/^[0-9a-f]+$/', $value, 'should return a ASCII string');
  207. }
  208. /**
  209. * Test the insecureRandomBytes method
  210. */
  211. public function testInsecureRandomBytes(): void
  212. {
  213. $value = Security::insecureRandomBytes(16);
  214. $this->assertSame(16, strlen($value));
  215. $value = Security::insecureRandomBytes(64);
  216. $this->assertSame(64, strlen($value));
  217. $this->assertMatchesRegularExpression('/[^0-9a-f]/', $value, 'should return a binary string');
  218. }
  219. /**
  220. * test constantEquals
  221. */
  222. public function testConstantEquals(): void
  223. {
  224. $this->assertFalse(Security::constantEquals('abcde', null));
  225. $this->assertFalse(Security::constantEquals('abcde', false));
  226. $this->assertFalse(Security::constantEquals('abcde', true));
  227. $this->assertFalse(Security::constantEquals('abcde', 1));
  228. $this->assertFalse(Security::constantEquals(null, 'abcde'));
  229. $this->assertFalse(Security::constantEquals(false, 'abcde'));
  230. $this->assertFalse(Security::constantEquals(1, 'abcde'));
  231. $this->assertFalse(Security::constantEquals(true, 'abcde'));
  232. $this->assertTrue(Security::constantEquals('abcde', 'abcde'));
  233. $this->assertFalse(Security::constantEquals('abcdef', 'abcde'));
  234. $this->assertFalse(Security::constantEquals('abcde', 'abcdef'));
  235. $this->assertFalse(Security::constantEquals('a', 'abcdef'));
  236. $snowman = "\xe2\x98\x83";
  237. $this->assertTrue(Security::constantEquals($snowman, $snowman));
  238. $this->assertFalse(Security::constantEquals(str_repeat($snowman, 3), $snowman));
  239. }
  240. }