SecurityTest.php 9.4 KB

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