Skip to content

Commit 66a4295

Browse files
committed
AC-8017 system settings improvement
1 parent ec56738 commit 66a4295

File tree

6 files changed

+11
-12
lines changed

6 files changed

+11
-12
lines changed

app/code/Magento/EncryptionKey/Model/ResourceModel/Key/Change.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,8 @@ public function changeEncryptionKey($key = null)
120120
}
121121

122122
if (null === $key) {
123-
$key = $this->random->getRandomBytes(
124-
ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE,
125-
ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX
126-
);
123+
$key = ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX .
124+
$this->random->getRandomBytes(ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE);
127125
}
128126
$this->encryptor->setNewKey($key);
129127

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ private function getArgonHash(
611611
*/
612612
private function decodeKey(string $key)
613613
{
614-
return (strpos($key, ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX) === 0) ?
614+
return (str_starts_with($key, ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX)) ?
615615
base64_decode(substr($key, strlen(ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX))) :
616616
$key;
617617
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ class KeyValidator
2525
*/
2626
public function isValid($value) : bool
2727
{
28-
if (strpos($value, ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX) === 0) {
29-
return (bool)$value;
28+
if (str_starts_with($value, ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX)) {
29+
return (bool)$value
30+
&& preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $value);
3031
} else {
3132
return $value
3233
&& strlen($value) === ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE

lib/internal/Magento/Framework/Math/Random.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,11 @@ public function getUniqueHash($prefix = '')
8989
* Generate a base64 encoded binary string.
9090
*
9191
* @param int $length
92-
* @param string $prefix
9392
* @return string
9493
* @throws Exception
9594
*/
96-
public function getRandomBytes($length, $prefix = '')
95+
public function getRandomBytes($length)
9796
{
98-
return $prefix . base64_encode(random_bytes($length));
97+
return base64_encode(random_bytes($length));
9998
}
10099
}

setup/src/Magento/Setup/Model/CryptKeyGenerator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function generate()
4848
*/
4949
private function getRandomString()
5050
{
51-
return $this->random->getRandomString(ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE);
51+
return ConfigOptionsListConstants::STORE_KEY_ENCODED_RANDOM_STRING_PREFIX .
52+
$this->random->getRandomBytes(ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE);
5253
}
5354
}

setup/src/Magento/Setup/Test/Unit/Model/CryptKeyGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testStringForHashingIsReadFromRandom()
4040
{
4141
$this->randomMock
4242
->expects($this->once())
43-
->method('getRandomString')
43+
->method('getRandomBytes')
4444
->willReturn('');
4545

4646
$this->cryptKeyGenerator->generate();

0 commit comments

Comments
 (0)