Skip to content

Commit c6599d2

Browse files
author
olysenko
committed
Merge remote-tracking branch 'origin/MAGETWO-96983' into chaika_february
2 parents b2f1417 + 726e0c6 commit c6599d2

File tree

4 files changed

+85
-56
lines changed

4 files changed

+85
-56
lines changed

lib/internal/Magento/Framework/Encryption/Adapter/SodiumChachaIetf.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function __construct(
3333
*
3434
* @param string $data
3535
* @return string string
36+
* @throws \SodiumException
3637
*/
3738
public function encrypt(string $data): string
3839
{
@@ -58,13 +59,17 @@ public function decrypt(string $data): string
5859
$nonce = mb_substr($data, 0, SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES, '8bit');
5960
$payload = mb_substr($data, SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES, null, '8bit');
6061

61-
$plainText = sodium_crypto_aead_chacha20poly1305_ietf_decrypt(
62-
$payload,
63-
$nonce,
64-
$nonce,
65-
$this->key
66-
);
62+
try {
63+
$plainText = sodium_crypto_aead_chacha20poly1305_ietf_decrypt(
64+
$payload,
65+
$nonce,
66+
$nonce,
67+
$this->key
68+
);
69+
} catch (\SodiumException $e) {
70+
$plainText = '';
71+
}
6772

68-
return $plainText;
73+
return $plainText !== false ? $plainText : '';
6974
}
7075
}

lib/internal/Magento/Framework/Encryption/Test/Unit/Adapter/SodiumChachaIetfTest.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
*/
1212
namespace Magento\Framework\Encryption\Test\Unit\Adapter;
1313

14-
class SodiumChachaIetfTest extends \PHPUnit\Framework\TestCase
14+
use Magento\Framework\Encryption\Adapter\SodiumChachaIetf;
15+
use PHPUnit\Framework\TestCase;
16+
17+
class SodiumChachaIetfTest extends TestCase
1518
{
19+
/**
20+
* @return array
21+
*/
1622
public function getCryptData(): array
1723
{
18-
$fixturesFilename = __DIR__ . '/../Crypt/_files/_sodium_chachaieft_fixtures.php';
19-
20-
$result = include $fixturesFilename;
24+
$result = include __DIR__ . '/../Crypt/_files/_sodium_chachaieft_fixtures.php';
2125
/* Restore encoded string back to binary */
2226
foreach ($result as &$cryptParams) {
2327
$cryptParams['encrypted'] = base64_decode($cryptParams['encrypted']);
@@ -29,21 +33,30 @@ public function getCryptData(): array
2933

3034
/**
3135
* @dataProvider getCryptData
36+
*
37+
* @param string $key
38+
* @param string $encrypted
39+
* @param string $decrypted
40+
* @throws \SodiumException
3241
*/
33-
public function testEncrypt(string $key, string $encrypted, string $decrypted)
42+
public function testEncrypt(string $key, string $encrypted, string $decrypted): void
3443
{
35-
$crypt = new \Magento\Framework\Encryption\Adapter\SodiumChachaIetf($key);
44+
$crypt = new SodiumChachaIetf($key);
3645
$result = $crypt->encrypt($decrypted);
3746

3847
$this->assertNotEquals($encrypted, $result);
3948
}
4049

4150
/**
4251
* @dataProvider getCryptData
52+
*
53+
* @param string $key
54+
* @param string $encrypted
55+
* @param string $decrypted
4356
*/
44-
public function testDecrypt(string $key, string $encrypted, string $decrypted)
57+
public function testDecrypt(string $key, string $encrypted, string $decrypted): void
4558
{
46-
$crypt = new \Magento\Framework\Encryption\Adapter\SodiumChachaIetf($key);
59+
$crypt = new SodiumChachaIetf($key);
4760
$result = $crypt->decrypt($encrypted);
4861

4962
$this->assertEquals($decrypted, $result);

lib/internal/Magento/Framework/Encryption/Test/Unit/Crypt/_files/_sodium_chachaieft_fixtures.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,14 @@
3232
'encrypted' => 'UglO9dEgslFpwPwejJmrK89PmBicv+I1pfdaXaEI69IrETD8LpdzOLF7',
3333
'decrypted' => 'Hello World!!!',
3434
],
35+
5 => [
36+
'key' => '6wRADHwwCBGgdxbcHhovGB0upmg0mbsN',
37+
'encrypted' => '',
38+
'decrypted' => '',
39+
],
40+
6 => [
41+
'key' => '6wRADHwwCBGgdxbcHhovGB0upmg0mbsN',
42+
'encrypted' => 'bWFsZm9ybWVkLWlucHV0',
43+
'decrypted' => '',
44+
],
3545
];

lib/internal/Magento/Framework/Encryption/Test/Unit/EncryptorTest.php

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,46 @@
88

99
namespace Magento\Framework\Encryption\Test\Unit;
1010

11+
use Magento\Framework\App\DeploymentConfig;
1112
use Magento\Framework\Encryption\Adapter\SodiumChachaIetf;
12-
use Magento\Framework\Encryption\Encryptor;
1313
use Magento\Framework\Encryption\Crypt;
14+
use Magento\Framework\Encryption\Encryptor;
15+
use Magento\Framework\Math\Random;
1416
use Magento\Framework\Encryption\KeyValidator;
1517
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1618

1719
class EncryptorTest extends \PHPUnit\Framework\TestCase
1820
{
19-
const CRYPT_KEY_1 = 'g9mY9KLrcuAVJfsmVUSRkKFLDdUPVkaZ';
20-
const CRYPT_KEY_2 = '7wEjmrliuqZQ1NQsndSa8C8WHvddeEbN';
21+
private const CRYPT_KEY_1 = 'g9mY9KLrcuAVJfsmVUSRkKFLDdUPVkaZ';
22+
private const CRYPT_KEY_2 = '7wEjmrliuqZQ1NQsndSa8C8WHvddeEbN';
2123

2224
/**
23-
* @var \Magento\Framework\Encryption\Encryptor
25+
* @var Encryptor
2426
*/
2527
private $encryptor;
2628

2729
/**
28-
* @var \PHPUnit_Framework_MockObject_MockObject
30+
* @var Random | \PHPUnit_Framework_MockObject_MockObject
2931
*/
3032
private $randomGeneratorMock;
3133

3234
/**
33-
* @var KeyValidator|\PHPUnit_Framework_MockObject_MockObject
35+
* @var KeyValidator | \PHPUnit_Framework_MockObject_MockObject
3436
*/
3537
private $keyValidatorMock;
3638

3739
protected function setUp()
3840
{
39-
$this->randomGeneratorMock = $this->createMock(\Magento\Framework\Math\Random::class);
40-
$deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
41+
$this->randomGeneratorMock = $this->createMock(Random::class);
42+
/** @var DeploymentConfig | \PHPUnit_Framework_MockObject_MockObject $deploymentConfigMock */
43+
$deploymentConfigMock = $this->createMock(DeploymentConfig::class);
4144
$deploymentConfigMock->expects($this->any())
4245
->method('get')
4346
->with(Encryptor::PARAM_CRYPT_KEY)
44-
->will($this->returnValue(self::CRYPT_KEY_1));
47+
->willReturn(self::CRYPT_KEY_1);
4548
$this->keyValidatorMock = $this->createMock(KeyValidator::class);
4649
$this->encryptor = (new ObjectManager($this))->getObject(
47-
\Magento\Framework\Encryption\Encryptor::class,
50+
Encryptor::class,
4851
[
4952
'random' => $this->randomGeneratorMock,
5053
'deploymentConfig' => $deploymentConfigMock,
@@ -53,42 +56,42 @@ protected function setUp()
5356
);
5457
}
5558

56-
public function testGetHashNoSalt()
59+
public function testGetHashNoSalt(): void
5760
{
5861
$this->randomGeneratorMock->expects($this->never())->method('getRandomString');
5962
$expected = '5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8';
6063
$actual = $this->encryptor->getHash('password');
6164
$this->assertEquals($expected, $actual);
6265
}
6366

64-
public function testGetHashSpecifiedSalt()
67+
public function testGetHashSpecifiedSalt(): void
6568
{
6669
$this->randomGeneratorMock->expects($this->never())->method('getRandomString');
6770
$expected = '13601bda4ea78e55a07b98866d2be6be0744e3866f13c00c811cab608a28f322:salt:1';
6871
$actual = $this->encryptor->getHash('password', 'salt');
6972
$this->assertEquals($expected, $actual);
7073
}
7174

72-
public function testGetHashRandomSaltDefaultLength()
75+
public function testGetHashRandomSaltDefaultLength(): void
7376
{
7477
$salt = '-----------random_salt----------';
7578
$this->randomGeneratorMock
7679
->expects($this->once())
7780
->method('getRandomString')
7881
->with(32)
79-
->will($this->returnValue($salt));
82+
->willReturn($salt);
8083
$expected = 'a1c7fc88037b70c9be84d3ad12522c7888f647915db78f42eb572008422ba2fa:' . $salt . ':1';
8184
$actual = $this->encryptor->getHash('password', true);
8285
$this->assertEquals($expected, $actual);
8386
}
8487

85-
public function testGetHashRandomSaltSpecifiedLength()
88+
public function testGetHashRandomSaltSpecifiedLength(): void
8689
{
8790
$this->randomGeneratorMock
8891
->expects($this->once())
8992
->method('getRandomString')
9093
->with(11)
91-
->will($this->returnValue('random_salt'));
94+
->willReturn('random_salt');
9295
$expected = '4c5cab8dd00137d11258f8f87b93fd17bd94c5026fc52d3c5af911dd177a2611:random_salt:1';
9396
$actual = $this->encryptor->getHash('password', 11);
9497
$this->assertEquals($expected, $actual);
@@ -101,7 +104,7 @@ public function testGetHashRandomSaltSpecifiedLength()
101104
*
102105
* @dataProvider validateHashDataProvider
103106
*/
104-
public function testValidateHash($password, $hash, $expected)
107+
public function testValidateHash($password, $hash, $expected): void
105108
{
106109
$actual = $this->encryptor->validateHash($password, $hash);
107110
$this->assertEquals($expected, $actual);
@@ -110,7 +113,7 @@ public function testValidateHash($password, $hash, $expected)
110113
/**
111114
* @return array
112115
*/
113-
public function validateHashDataProvider()
116+
public function validateHashDataProvider(): array
114117
{
115118
return [
116119
['password', 'hash:salt:1', false],
@@ -125,13 +128,13 @@ public function validateHashDataProvider()
125128
* @dataProvider encryptWithEmptyKeyDataProvider
126129
* @expectedException \SodiumException
127130
*/
128-
public function testEncryptWithEmptyKey($key)
131+
public function testEncryptWithEmptyKey($key): void
129132
{
130-
$deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
133+
$deploymentConfigMock = $this->createMock(DeploymentConfig::class);
131134
$deploymentConfigMock->expects($this->any())
132135
->method('get')
133136
->with(Encryptor::PARAM_CRYPT_KEY)
134-
->will($this->returnValue($key));
137+
->willReturn($key);
135138
$model = new Encryptor($this->randomGeneratorMock, $deploymentConfigMock);
136139
$value = 'arbitrary_string';
137140
$this->assertEquals($value, $model->encrypt($value));
@@ -140,7 +143,7 @@ public function testEncryptWithEmptyKey($key)
140143
/**
141144
* @return array
142145
*/
143-
public function encryptWithEmptyKeyDataProvider()
146+
public function encryptWithEmptyKeyDataProvider(): array
144147
{
145148
return [[null], [0], [''], ['0']];
146149
}
@@ -150,13 +153,13 @@ public function encryptWithEmptyKeyDataProvider()
150153
*
151154
* @dataProvider decryptWithEmptyKeyDataProvider
152155
*/
153-
public function testDecryptWithEmptyKey($key)
156+
public function testDecryptWithEmptyKey($key): void
154157
{
155-
$deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
158+
$deploymentConfigMock = $this->createMock(DeploymentConfig::class);
156159
$deploymentConfigMock->expects($this->any())
157160
->method('get')
158161
->with(Encryptor::PARAM_CRYPT_KEY)
159-
->will($this->returnValue($key));
162+
->willReturn($key);
160163
$model = new Encryptor($this->randomGeneratorMock, $deploymentConfigMock);
161164
$value = 'arbitrary_string';
162165
$this->assertEquals('', $model->decrypt($value));
@@ -165,36 +168,35 @@ public function testDecryptWithEmptyKey($key)
165168
/**
166169
* @return array
167170
*/
168-
public function decryptWithEmptyKeyDataProvider()
171+
public function decryptWithEmptyKeyDataProvider(): array
169172
{
170173
return [[null], [0], [''], ['0']];
171174
}
172175

173-
public function testEncrypt()
176+
public function testEncrypt(): void
174177
{
175178
// sample data to encrypt
176179
$data = 'Mares eat oats and does eat oats, but little lambs eat ivy.';
177180

178181
$actual = $this->encryptor->encrypt($data);
179182

180183
// Extract the initialization vector and encrypted data
181-
$parts = explode(':', $actual, 3);
182-
list(, , $encryptedData) = $parts;
184+
[, , $encryptedData] = explode(':', $actual, 3);
183185

184186
$crypt = new SodiumChachaIetf(self::CRYPT_KEY_1);
185187
// Verify decrypted matches original data
186188
$this->assertEquals($data, $crypt->decrypt(base64_decode((string)$encryptedData)));
187189
}
188190

189-
public function testDecrypt()
191+
public function testDecrypt(): void
190192
{
191193
$message = 'Mares eat oats and does eat oats, but little lambs eat ivy.';
192194
$encrypted = $this->encryptor->encrypt($message);
193195

194196
$this->assertEquals($message, $this->encryptor->decrypt($encrypted));
195197
}
196198

197-
public function testLegacyDecrypt()
199+
public function testLegacyDecrypt(): void
198200
{
199201
// sample data to encrypt
200202
$data = '0:2:z3a4ACpkU35W6pV692U4ueCVQP0m0v0p:' .
@@ -203,26 +205,25 @@ public function testLegacyDecrypt()
203205
$actual = $this->encryptor->decrypt($data);
204206

205207
// Extract the initialization vector and encrypted data
206-
$parts = explode(':', $data, 4);
207-
list(, , $iv, $encrypted) = $parts;
208+
[, , $iv, $encrypted] = explode(':', $data, 4);
208209

209210
// Decrypt returned data with RIJNDAEL_256 cipher, cbc mode
210211
$crypt = new Crypt(self::CRYPT_KEY_1, MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC, $iv);
211212
// Verify decrypted matches original data
212213
$this->assertEquals($encrypted, base64_encode($crypt->encrypt($actual)));
213214
}
214215

215-
public function testEncryptDecryptNewKeyAdded()
216+
public function testEncryptDecryptNewKeyAdded(): void
216217
{
217-
$deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
218+
$deploymentConfigMock = $this->createMock(DeploymentConfig::class);
218219
$deploymentConfigMock->expects($this->at(0))
219220
->method('get')
220221
->with(Encryptor::PARAM_CRYPT_KEY)
221-
->will($this->returnValue(self::CRYPT_KEY_1));
222+
->willReturn(self::CRYPT_KEY_1);
222223
$deploymentConfigMock->expects($this->at(1))
223224
->method('get')
224225
->with(Encryptor::PARAM_CRYPT_KEY)
225-
->will($this->returnValue(self::CRYPT_KEY_1 . "\n" . self::CRYPT_KEY_2));
226+
->willReturn(self::CRYPT_KEY_1 . "\n" . self::CRYPT_KEY_2);
226227
$model1 = new Encryptor($this->randomGeneratorMock, $deploymentConfigMock);
227228
// simulate an encryption key is being added
228229
$model2 = new Encryptor($this->randomGeneratorMock, $deploymentConfigMock);
@@ -236,7 +237,7 @@ public function testEncryptDecryptNewKeyAdded()
236237
$this->assertSame($data, $decryptedData, 'Encryptor failed to decrypt data encrypted by old keys.');
237238
}
238239

239-
public function testValidateKey()
240+
public function testValidateKey(): void
240241
{
241242
$this->keyValidatorMock->method('isValid')->willReturn(true);
242243
$this->encryptor->validateKey(self::CRYPT_KEY_1);
@@ -245,7 +246,7 @@ public function testValidateKey()
245246
/**
246247
* @expectedException \Exception
247248
*/
248-
public function testValidateKeyInvalid()
249+
public function testValidateKeyInvalid(): void
249250
{
250251
$this->keyValidatorMock->method('isValid')->willReturn(false);
251252
$this->encryptor->validateKey('----- ');
@@ -254,7 +255,7 @@ public function testValidateKeyInvalid()
254255
/**
255256
* @return array
256257
*/
257-
public function useSpecifiedHashingAlgoDataProvider()
258+
public function useSpecifiedHashingAlgoDataProvider(): array
258259
{
259260
return [
260261
['password', 'salt', Encryptor::HASH_VERSION_MD5,
@@ -276,7 +277,7 @@ public function useSpecifiedHashingAlgoDataProvider()
276277
* @param $hashAlgo
277278
* @param $expected
278279
*/
279-
public function testGetHashMustUseSpecifiedHashingAlgo($password, $salt, $hashAlgo, $expected)
280+
public function testGetHashMustUseSpecifiedHashingAlgo($password, $salt, $hashAlgo, $expected): void
280281
{
281282
$hash = $this->encryptor->getHash($password, $salt, $hashAlgo);
282283
$this->assertEquals($expected, $hash);

0 commit comments

Comments
 (0)