Skip to content

Commit 84eafc0

Browse files
committed
Fix case when key is subsequently commented out
1 parent fd1513a commit 84eafc0

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

system/Commands/Encryption/GenerateKey.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,24 @@ protected function writeNewEncryptionKeyToFile(string $oldKey, string $newKey):
163163
copy($baseEnv, $envFile);
164164
}
165165

166-
$envFileContents = (string) file_get_contents($envFile);
166+
$oldFileContents = (string) file_get_contents($envFile);
167167
$replacementKey = "\nencryption.key = {$newKey}";
168168

169-
if (strpos($envFileContents, 'encryption.key') === false) {
169+
if (strpos($oldFileContents, 'encryption.key') === false) {
170170
return file_put_contents($envFile, $replacementKey, FILE_APPEND) !== false;
171171
}
172172

173-
return file_put_contents($envFile, preg_replace(
174-
$this->keyPattern($oldKey),
175-
$replacementKey,
176-
$envFileContents
177-
)) !== false;
173+
$newFileContents = preg_replace($this->keyPattern($oldKey), $replacementKey, $oldFileContents);
174+
175+
if ($newFileContents === $oldFileContents) {
176+
$newFileContents = preg_replace(
177+
'/^[#\s]*encryption.key[=\s]*(?:hex2bin\:[a-f0-9]{64}|base64\:(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?)$/m',
178+
$replacementKey,
179+
$oldFileContents
180+
);
181+
}
182+
183+
return file_put_contents($envFile, $newFileContents) !== false;
178184
}
179185

180186
/**

tests/system/Commands/GenerateKeyTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,40 @@ public function testKeyGenerateWhenKeyIsMissingInDotEnvFile()
128128
$this->assertStringContainsString('Application\'s new encryption key was successfully set.', $this->getBuffer());
129129
$this->assertSame("\nencryption.key = " . env('encryption.key'), file_get_contents($this->envPath));
130130
}
131+
132+
public function testKeyGenerateWhenNewHexKeyIsSubsequentlyCommentedOut()
133+
{
134+
command('key:generate');
135+
$key = env('encryption.key', '');
136+
file_put_contents($this->envPath, str_replace(
137+
'encryption.key = ' . $key,
138+
'# encryption.key = ' . $key,
139+
file_get_contents($this->envPath),
140+
$count
141+
));
142+
$this->assertSame(1, $count, 'Failed commenting out the previously set application key.');
143+
144+
CITestStreamFilter::$buffer = '';
145+
command('key:generate --force');
146+
$this->assertStringContainsString('was successfully set.', $this->getBuffer());
147+
$this->assertNotSame($key, env('encryption.key', $key), 'Failed replacing the commented out key.');
148+
}
149+
150+
public function testKeyGenerateWhenNewBase64KeyIsSubsequentlyCommentedOut()
151+
{
152+
command('key:generate --prefix base64');
153+
$key = env('encryption.key', '');
154+
file_put_contents($this->envPath, str_replace(
155+
'encryption.key = ' . $key,
156+
'# encryption.key = ' . $key,
157+
file_get_contents($this->envPath),
158+
$count
159+
));
160+
$this->assertSame(1, $count, 'Failed commenting out the previously set application key.');
161+
162+
CITestStreamFilter::$buffer = '';
163+
command('key:generate --force');
164+
$this->assertStringContainsString('was successfully set.', $this->getBuffer());
165+
$this->assertNotSame($key, env('encryption.key', $key), 'Failed replacing the commented out key.');
166+
}
131167
}

0 commit comments

Comments
 (0)