Skip to content

Commit 772bc29

Browse files
committed
feat(encryption): Change previousKeys to a comma-separated string for fallback decryption
1 parent 68e05ba commit 772bc29

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
lines changed

app/Config/Encryption.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Encryption extends BaseConfig
3939
* If you want to enable decryption using previous keys, set them here.
4040
* See the user guide for more info.
4141
*/
42-
public array $previousKeys = [];
42+
public string $previousKeys = '';
4343

4444
/**
4545
* --------------------------------------------------------------------------

system/Config/BaseConfig.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,12 @@ public function __construct()
137137
// previousKeysFallbackEnabled must be boolean
138138
$this->{$property} = (bool) $this->{$property};
139139
} elseif ($property === 'previousKeys') {
140-
// previousKeys must be an array
141-
if (is_string($this->{$property})) {
142-
$this->{$property} = array_map(fn ($item): string => $this->parseEncryptionKey($item), explode(',', $this->{$property}));
140+
$keysArray = array_map('trim', explode(',', $this->{$property}));
141+
$parsedKeys = [];
142+
foreach ($keysArray as $key) {
143+
$parsedKeys[] = $this->parseEncryptionKey($key);
143144
}
145+
$this->{$property} = implode(',', $parsedKeys);
144146
}
145147
}
146148
}

system/Encryption/Encryption.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ class Encryption
5959
protected bool $previousKeysFallbackEnabled = false;
6060

6161
/**
62-
* List of previous keys for fallback decryption.
62+
* Comma-separated list of previous keys for fallback decryption.
6363
*
64-
* @var list<string>
64+
* @var string
6565
*/
66-
protected array $previousKeys = [];
66+
protected string $previousKeys = '';
6767

6868
/**
6969
* The derived HMAC key
@@ -132,7 +132,7 @@ public function initialize(?EncryptionConfig $config = null)
132132
if ($config instanceof EncryptionConfig) {
133133
$this->key = $config->key;
134134
$this->previousKeysFallbackEnabled = $config->previousKeysFallbackEnabled ?? false;
135-
$this->previousKeys = $config->previousKeys ?? [];
135+
$this->previousKeys = $config->previousKeys ?? '';
136136
$this->driver = $config->driver;
137137
$this->digest = $config->digest ?? 'SHA512';
138138
}

system/Encryption/Handlers/OpenSSLHandler.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ class OpenSSLHandler extends BaseHandler
6363
/**
6464
* List of previous keys for fallback decryption.
6565
*
66-
* @var list<string>
66+
* @var string
6767
*/
68-
protected array $previousKeys = [];
68+
protected string $previousKeys = '';
6969

7070
/**
7171
* Whether the cipher-text should be raw. If set to false, then it will be base64 encoded.
@@ -138,15 +138,16 @@ public function decrypt($data, $params = null)
138138
throw EncryptionException::forNeedsStarterKey();
139139
}
140140

141+
$result = false;
142+
141143
try {
142144
$result = $this->decryptWithKey($data, $this->key);
143145
} catch (EncryptionException $e) {
144-
$result = false;
145146
$exception = $e;
146147
}
147148

148-
if ($result === false && $this->previousKeysFallbackEnabled && $this->previousKeys !== []) {
149-
foreach ($this->previousKeys as $previousKey) {
149+
if ($result === false && $this->previousKeysFallbackEnabled && $this->previousKeys !== '') {
150+
foreach (explode(',', $this->previousKeys) as $previousKey) {
150151
try {
151152
$result = $this->decryptWithKey($data, $previousKey);
152153
if ($result !== false) {

system/Encryption/Handlers/SodiumHandler.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ class SodiumHandler extends BaseHandler
3838
/**
3939
* List of previous keys for fallback decryption.
4040
*
41-
* @var list<string>
41+
* @var string
4242
*/
43-
protected array $previousKeys = [];
43+
protected string $previousKeys = '';
4444

4545
/**
4646
* Block size for padding message.
@@ -91,17 +91,18 @@ public function decrypt($data, $params = null)
9191
throw EncryptionException::forNeedsStarterKey();
9292
}
9393

94+
$result = false;
95+
9496
try {
9597
$result = $this->decryptWithKey($data, $this->key);
9698
sodium_memzero($this->key);
9799
} catch (EncryptionException $e) {
98-
$result = false;
99100
$exception = $e;
100101
sodium_memzero($this->key);
101102
}
102103

103-
if ($result === false && $this->previousKeysFallbackEnabled && $this->previousKeys !== []) {
104-
foreach ($this->previousKeys as $previousKey) {
104+
if ($result === false && $this->previousKeysFallbackEnabled && $this->previousKeys !== '') {
105+
foreach (explode(',', $this->previousKeys) as $previousKey) {
105106
try {
106107
$result = $this->decryptWithKey($data, $previousKey);
107108
if (isset($result)) {

0 commit comments

Comments
 (0)