Skip to content

Commit edb4748

Browse files
anzinandrewbess
authored andcommitted
AC-3161: fixed logic affected by "RFC: Deprecate passing null"
1 parent 629e9d9 commit edb4748

File tree

12 files changed

+92
-76
lines changed

12 files changed

+92
-76
lines changed

lib/internal/Magento/Framework/Encryption/Crypt.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Magento\Framework\Encryption;
1010

11+
use Magento\Framework\Encryption\Adapter\Mcrypt;
12+
1113
/**
1214
* Class encapsulates cryptographic algorithm
1315
*
@@ -35,7 +37,7 @@ class Crypt
3537
/**
3638
* Mcrypt adapter
3739
*
38-
* @var \Magento\Framework\Encryption\Adapter\Mcrypt
40+
* @var Mcrypt
3941
*/
4042
private $mcrypt;
4143

@@ -50,6 +52,7 @@ class Crypt
5052
* TRUE generates a random initial vector.
5153
* FALSE fills initial vector with zero bytes to not use it.
5254
* @throws \Exception
55+
* phpcs:disable PHPCompatibility.Constants.RemovedConstants
5356
*/
5457
public function __construct(
5558
$key,
@@ -58,24 +61,24 @@ public function __construct(
5861
$initVector = false
5962
) {
6063
if (true === $initVector) {
61-
// @codingStandardsIgnoreStart
64+
//phpcs:disable
6265
$handle = @mcrypt_module_open($cipher, '', $mode, '');
6366
$initVectorSize = @mcrypt_enc_get_iv_size($handle);
64-
// @codingStandardsIgnoreEnd
67+
//phpcs:enable
6568

6669
/* Generate a random vector from human-readable characters */
6770
$allowedCharacters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
6871
$initVector = '';
6972
for ($i = 0; $i < $initVectorSize; $i++) {
7073
$initVector .= $allowedCharacters[random_int(0, strlen($allowedCharacters) - 1)];
7174
}
72-
// @codingStandardsIgnoreStart
75+
//phpcs:disable
7376
@mcrypt_generic_deinit($handle);
7477
@mcrypt_module_close($handle);
75-
// @codingStandardsIgnoreEnd
78+
//phpcs:enable
7679
}
7780

78-
$this->mcrypt = new \Magento\Framework\Encryption\Adapter\Mcrypt(
81+
$this->mcrypt = new Mcrypt(
7982
$key,
8083
$cipher,
8184
$mode,
@@ -121,7 +124,7 @@ public function getInitVector()
121124
*/
122125
public function encrypt($data)
123126
{
124-
if (strlen($data) == 0) {
127+
if (!$data || strlen($data) == 0) {
125128
return $data;
126129
}
127130
// @codingStandardsIgnoreLine

lib/internal/Magento/Framework/Encryption/Encryptor.php

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
use Magento\Framework\App\DeploymentConfig;
1212
use Magento\Framework\App\ObjectManager;
1313
use Magento\Framework\Encryption\Adapter\EncryptionAdapterInterface;
14+
use Magento\Framework\Encryption\Adapter\Mcrypt;
15+
use Magento\Framework\Encryption\Adapter\SodiumChachaIetf;
1416
use Magento\Framework\Encryption\Helper\Security;
1517
use Magento\Framework\Math\Random;
16-
use Magento\Framework\Encryption\Adapter\SodiumChachaIetf;
17-
use Magento\Framework\Encryption\Adapter\Mcrypt;
1818

1919
/**
2020
* Class Encryptor provides basic logic for hashing strings and encrypting/decrypting misc data.
@@ -26,12 +26,12 @@ class Encryptor implements EncryptorInterface
2626
/**
2727
* Key of md5 algorithm
2828
*/
29-
const HASH_VERSION_MD5 = 0;
29+
public const HASH_VERSION_MD5 = 0;
3030

3131
/**
3232
* Key of sha256 algorithm
3333
*/
34-
const HASH_VERSION_SHA256 = 1;
34+
public const HASH_VERSION_SHA256 = 1;
3535

3636
/**
3737
* Key of Argon2ID13 algorithm
@@ -49,44 +49,44 @@ class Encryptor implements EncryptorInterface
4949
* @deprecated Latest version is dynamic based on current setup.
5050
* @see \Magento\Framework\Encryption\Encryptor::getLatestHashVersion
5151
*/
52-
const HASH_VERSION_LATEST = 3;
52+
public const HASH_VERSION_LATEST = 3;
5353

5454
/**
5555
* Default length of salt in bytes
5656
*/
57-
const DEFAULT_SALT_LENGTH = 32;
57+
public const DEFAULT_SALT_LENGTH = 32;
5858

5959
/**#@+
6060
* Exploded password hash keys
6161
*/
62-
const PASSWORD_HASH = 0;
63-
const PASSWORD_SALT = 1;
64-
const PASSWORD_VERSION = 2;
62+
public const PASSWORD_HASH = 0;
63+
public const PASSWORD_SALT = 1;
64+
public const PASSWORD_VERSION = 2;
6565
/**#@-*/
6666

6767
/**
6868
* Array key of encryption key in deployment config
6969
*/
70-
const PARAM_CRYPT_KEY = 'crypt/key';
70+
public const PARAM_CRYPT_KEY = 'crypt/key';
7171

7272
/**#@+
7373
* Cipher versions
7474
*/
75-
const CIPHER_BLOWFISH = 0;
75+
public const CIPHER_BLOWFISH = 0;
7676

77-
const CIPHER_RIJNDAEL_128 = 1;
77+
public const CIPHER_RIJNDAEL_128 = 1;
7878

79-
const CIPHER_RIJNDAEL_256 = 2;
79+
public const CIPHER_RIJNDAEL_256 = 2;
8080

81-
const CIPHER_AEAD_CHACHA20POLY1305 = 3;
81+
public const CIPHER_AEAD_CHACHA20POLY1305 = 3;
8282

83-
const CIPHER_LATEST = 3;
83+
public const CIPHER_LATEST = 3;
8484
/**#@-*/
8585

8686
/**
8787
* Default hash string delimiter
8888
*/
89-
const DELIMITER = ':';
89+
public const DELIMITER = ':';
9090

9191
/**
9292
* Map of simple hash versions
@@ -281,8 +281,8 @@ public function validateHash($password, $hash)
281281
*/
282282
public function isValidHash($password, $hash)
283283
{
284-
$agnosticArgonRegEx = '/^' .self::HASH_VERSION_ARGON2ID13_AGNOSTIC
285-
.'\_(?<seed>\d+)\_(?<ops>\d+)\_(?<mem>\d+)$/';
284+
$agnosticArgonRegEx = '/^' . self::HASH_VERSION_ARGON2ID13_AGNOSTIC
285+
. '\_(?<seed>\d+)\_(?<ops>\d+)\_(?<mem>\d+)$/';
286286
try {
287287
[$hash, $hashSalt, $hashVersions] = $this->explodePasswordHash($hash);
288288
$recreated = $password;
@@ -334,7 +334,7 @@ public function validateHashVersion($hash, $validateCount = false)
334334
if ($this->getLatestHashVersion() === self::HASH_VERSION_ARGON2ID13_AGNOSTIC) {
335335
//Agnostic Argon also stores Argon parameters.
336336
$validVersion = preg_match(
337-
'/^' .self::HASH_VERSION_ARGON2ID13_AGNOSTIC .'\_\d+\_\d+\_\d+$/',
337+
'/^' . self::HASH_VERSION_ARGON2ID13_AGNOSTIC . '\_\d+\_\d+\_\d+$/',
338338
end($hashVersions)
339339
);
340340
} else {
@@ -353,15 +353,15 @@ public function validateHashVersion($hash, $validateCount = false)
353353
*/
354354
private function explodePasswordHash($hash)
355355
{
356-
$explodedPassword = explode(self::DELIMITER, $hash, 3);
356+
$explodedPassword = $hash !== null ? explode(self::DELIMITER, $hash, 3) : [];
357357
if (count($explodedPassword) !== 3) {
358358
throw new \RuntimeException('Hash is not a password hash');
359359
}
360360

361361
//Hashes that have been upgraded will have algorithm version history starting from the oldest one used.
362362
$explodedPassword[self::PASSWORD_VERSION] = explode(
363363
self::DELIMITER,
364-
$explodedPassword[self::PASSWORD_VERSION]
364+
$explodedPassword[self::PASSWORD_VERSION] ?? ''
365365
);
366366

367367
return $explodedPassword;
@@ -423,21 +423,21 @@ public function decrypt($data)
423423
$initVector = $iv ? $iv : null;
424424
$keyVersion = (int)$keyVersion;
425425
$cryptVersion = self::CIPHER_RIJNDAEL_256;
426-
// specified key, specified crypt
426+
// specified key, specified crypt
427427
} elseif (3 === $partsCount) {
428428
list($keyVersion, $cryptVersion, $data) = $parts;
429429
$keyVersion = (int)$keyVersion;
430430
$cryptVersion = (int)$cryptVersion;
431-
// no key version = oldest key, specified crypt
431+
// no key version = oldest key, specified crypt
432432
} elseif (2 === $partsCount) {
433433
list($cryptVersion, $data) = $parts;
434434
$keyVersion = 0;
435435
$cryptVersion = (int)$cryptVersion;
436-
// no key version = oldest key, no crypt version = oldest crypt
436+
// no key version = oldest key, no crypt version = oldest crypt
437437
} elseif (1 === $partsCount) {
438438
$keyVersion = 0;
439439
$cryptVersion = self::CIPHER_BLOWFISH;
440-
// not supported format
440+
// not supported format
441441
} else {
442442
return '';
443443
}
@@ -462,6 +462,7 @@ public function decrypt($data)
462462
*/
463463
public function validateKey($key)
464464
{
465+
// @phpstan-ignore-next-line
465466
if (!$this->keyValidator->isValid($key)) {
466467
// phpcs:ignore Magento2.Exceptions.DirectThrow
467468
throw new \Exception(

lib/internal/Magento/Framework/Encryption/KeyValidator.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22
/**
3-
* Protocol validator
4-
*
53
* Copyright © Magento, Inc. All rights reserved.
64
* See COPYING.txt for license details.
75
*/
@@ -27,7 +25,7 @@ class KeyValidator
2725
*/
2826
public function isValid($value) : bool
2927
{
30-
return strlen($value) === ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE
28+
return $value && strlen($value) === ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE
3129
&& preg_match('/^\S+$/', $value);
3230
}
3331
}

lib/internal/Magento/Framework/EntityManager/Db/CreateRow.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66

77
namespace Magento\Framework\EntityManager\Db;
88

9+
use Exception;
910
use Magento\Framework\EntityManager\MetadataPool;
1011
use Magento\Framework\EntityManager\EntityMetadataInterface;
1112
use Magento\Framework\DB\Adapter\AdapterInterface;
1213
use Magento\Framework\App\ResourceConnection;
1314

14-
/**
15-
* Class CreateRow
16-
*/
1715
class CreateRow
1816
{
1917
/**
@@ -41,6 +39,8 @@ public function __construct(
4139
}
4240

4341
/**
42+
* Method to prepare data.
43+
*
4444
* @param EntityMetadataInterface $metadata
4545
* @param AdapterInterface $connection
4646
* @param array $data
@@ -50,13 +50,13 @@ protected function prepareData(EntityMetadataInterface $metadata, AdapterInterfa
5050
{
5151
$output = [];
5252
foreach ($connection->describeTable($metadata->getEntityTable()) as $column) {
53-
$columnName = strtolower($column['COLUMN_NAME']);
53+
$columnName = strtolower($column['COLUMN_NAME'] ?? '');
5454
if ($this->canNotSetTimeStamp($columnName, $column, $data)) {
5555
continue;
5656
}
5757

5858
if (isset($data[$columnName])) {
59-
$output[strtolower($column['COLUMN_NAME'])] = $data[strtolower($column['COLUMN_NAME'])];
59+
$output[strtolower($column['COLUMN_NAME'] ?? '')] = $data[strtolower($column['COLUMN_NAME'] ?? '')];
6060
} elseif ($column['DEFAULT'] === null) {
6161
$output[strtolower($column['COLUMN_NAME'])] = null;
6262
}
@@ -68,6 +68,8 @@ protected function prepareData(EntityMetadataInterface $metadata, AdapterInterfa
6868
}
6969

7070
/**
71+
* Method to can not set time stamp.
72+
*
7173
* @param string $columnName
7274
* @param string $column
7375
* @param array $data
@@ -80,9 +82,12 @@ private function canNotSetTimeStamp($columnName, $column, array $data)
8082
}
8183

8284
/**
85+
* Method to execute.
86+
*
8387
* @param string $entityType
8488
* @param array $data
8589
* @return array
90+
* @throws Exception
8691
*/
8792
public function execute($entityType, $data)
8893
{

lib/internal/Magento/Framework/EntityManager/Db/UpdateRow.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66

77
namespace Magento\Framework\EntityManager\Db;
88

9+
use Exception;
910
use Magento\Framework\EntityManager\MetadataPool;
1011
use Magento\Framework\EntityManager\EntityMetadataInterface;
1112
use Magento\Framework\DB\Adapter\AdapterInterface;
1213
use Magento\Framework\App\ResourceConnection;
1314

14-
/**
15-
* Class UpdateRow
16-
*/
1715
class UpdateRow
1816
{
1917
/**
@@ -41,6 +39,8 @@ public function __construct(
4139
}
4240

4341
/**
42+
* Method to prepare data.
43+
*
4444
* @param EntityMetadataInterface $metadata
4545
* @param AdapterInterface $connection
4646
* @param array $data
@@ -50,13 +50,13 @@ protected function prepareData(EntityMetadataInterface $metadata, AdapterInterfa
5050
{
5151
$output = [];
5252
foreach ($connection->describeTable($metadata->getEntityTable()) as $column) {
53-
$columnName = strtolower($column['COLUMN_NAME']);
53+
$columnName = strtolower($column['COLUMN_NAME'] ?? '');
5454
if ($this->canNotSetTimeStamp($columnName, $column, $data) || $column['IDENTITY']) {
5555
continue;
5656
}
5757

5858
if (isset($data[$columnName])) {
59-
$output[strtolower($column['COLUMN_NAME'])] = $data[strtolower($column['COLUMN_NAME'])];
59+
$output[strtolower($column['COLUMN_NAME'] ?? '')] = $data[strtolower($column['COLUMN_NAME'] ?? '')];
6060
} elseif (!empty($column['NULLABLE'])) {
6161
$output[strtolower($column['COLUMN_NAME'])] = null;
6262
}
@@ -97,6 +97,8 @@ private function prepareUpdateConditions(
9797
}
9898

9999
/**
100+
* Method to can not set time stamp.
101+
*
100102
* @param string $columnName
101103
* @param string $column
102104
* @param array $data
@@ -109,9 +111,12 @@ private function canNotSetTimeStamp($columnName, $column, array $data)
109111
}
110112

111113
/**
114+
* Method to execute.
115+
*
112116
* @param string $entityType
113117
* @param array $data
114118
* @return array
119+
* @throws Exception
115120
*/
116121
public function execute($entityType, $data)
117122
{

lib/internal/Magento/Framework/EntityManager/EventManager.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

99
use Magento\Framework\Event\ManagerInterface;
1010

11-
/**
12-
* Class EventManager
13-
*/
1411
class EventManager
1512
{
1613
/**
@@ -36,10 +33,12 @@ public function __construct(
3633
*/
3734
private function resolveEntityPrefix($entityType)
3835
{
39-
return strtolower(str_replace("\\", "_", $entityType));
36+
return $entityType !== null ? strtolower(str_replace("\\", "_", $entityType)) : '';
4037
}
4138

4239
/**
40+
* Method to dispatch entity event.
41+
*
4342
* @param string $entityType
4443
* @param string $eventSuffix
4544
* @param array $data
@@ -54,6 +53,8 @@ public function dispatchEntityEvent($entityType, $eventSuffix, array $data = [])
5453
}
5554

5655
/**
56+
* Method to dispatch.
57+
*
5758
* @param string $eventName
5859
* @param array $data
5960
* @return void

0 commit comments

Comments
 (0)