diff --git a/modules/system/behaviors/SettingsModel.php b/modules/system/behaviors/SettingsModel.php index c1acba1818..4f9877a486 100644 --- a/modules/system/behaviors/SettingsModel.php +++ b/modules/system/behaviors/SettingsModel.php @@ -1,10 +1,12 @@ fieldValues = $this->model->value ?: []; + $this->fieldValues = $this->decryptSettingsValues($this->model->value ?: []); $this->model->attributes = array_merge($this->fieldValues, $this->model->attributes); } @@ -223,10 +234,60 @@ public function beforeModelSave() { $this->model->item = $this->recordCode; if ($this->fieldValues) { - $this->model->value = $this->fieldValues; + $this->model->value = $this->encryptSettingsValues($this->fieldValues); } } + /** + * Returns the names of the settings that are encrypted at rest. + */ + protected function getEncryptedKeys(): array + { + if (!$this->model->propertyExists('settingsEncrypted')) { + return []; + } + + return (array) $this->model->settingsEncrypted; + } + + /** + * Encrypts the values of the settings that are flagged as encrypted. Empty values are + * left as they are, there is nothing to protect in them. + */ + protected function encryptSettingsValues(array $values): array + { + foreach ($this->getEncryptedKeys() as $key) { + $value = $values[$key] ?? null; + + if ($value !== null && $value !== '') { + $values[$key] = Crypt::encrypt($value); + } + } + + return $values; + } + + /** + * Decrypts the values of the settings that are flagged as encrypted. Values that cannot + * be decrypted were stored before the setting was encrypted and are returned unchanged. + */ + protected function decryptSettingsValues(array $values): array + { + foreach ($this->getEncryptedKeys() as $key) { + if (!isset($values[$key]) || !is_string($values[$key])) { + continue; + } + + try { + $values[$key] = Crypt::decrypt($values[$key]); + } catch (DecryptException $e) { + // Stored as plain text, it will be encrypted on the next save + } + } + + return $values; + } + /** * After the model is saved, clear the cached query entry * and restart queue workers so they have the latest settings diff --git a/modules/system/models/MailSetting.php b/modules/system/models/MailSetting.php index 44147f69ff..3bff5ec525 100644 --- a/modules/system/models/MailSetting.php +++ b/modules/system/models/MailSetting.php @@ -36,6 +36,11 @@ class MailSetting extends Model */ public $settingsFields = 'fields.yaml'; + /** + * @var array Settings that are encrypted at rest + */ + public $settingsEncrypted = ['smtp_password']; + /* * Validation rules */ diff --git a/modules/system/tests/models/MailSettingTest.php b/modules/system/tests/models/MailSettingTest.php new file mode 100644 index 0000000000..8c225c2a08 --- /dev/null +++ b/modules/system/tests/models/MailSettingTest.php @@ -0,0 +1,130 @@ +resetDefault(); + SettingsModel::clearInternalCache(); + + parent::tearDown(); + } + + /** + * Reads the stored settings the way they sit in the database, bypassing the model. + */ + protected function getStoredValues(): array + { + return json_decode(DB::table('system_settings')->where('item', 'system_mail_settings')->value('value'), true); + } + + /** + * Drops every cached copy of the settings so that the next read comes from the database. + */ + protected function reloadSettings(): void + { + Cache::forget('system::settings.system_mail_settings'); + SettingsModel::clearInternalCache(); + } + + public function testEncryptedSettingIsEncryptedInTheDatabase() + { + MailSetting::set('smtp_password', 'hunter2'); + + $stored = $this->getStoredValues(); + + $this->assertNotSame('hunter2', $stored['smtp_password']); + $this->assertStringNotContainsString('hunter2', json_encode($stored)); + $this->assertSame('hunter2', Crypt::decrypt($stored['smtp_password'])); + } + + public function testEncryptedSettingIsReadAsPlainValue() + { + MailSetting::set('smtp_password', 'hunter2'); + + $this->assertSame('hunter2', MailSetting::get('smtp_password')); + + $this->reloadSettings(); + + $this->assertSame('hunter2', MailSetting::get('smtp_password')); + $this->assertSame('hunter2', MailSetting::instance()->smtp_password); + } + + public function testOtherSettingsAreNotEncrypted() + { + MailSetting::set([ + 'smtp_user' => 'mailer', + 'smtp_password' => 'hunter2', + ]); + + $this->assertSame('mailer', $this->getStoredValues()['smtp_user']); + } + + public function testSavingAgainDoesNotEncryptTwice() + { + MailSetting::set('smtp_password', 'hunter2'); + + $this->reloadSettings(); + MailSetting::set('smtp_user', 'mailer'); + + $this->reloadSettings(); + MailSetting::set('smtp_port', 2525); + + $this->assertSame('hunter2', Crypt::decrypt($this->getStoredValues()['smtp_password'])); + + $this->reloadSettings(); + + $this->assertSame('hunter2', MailSetting::get('smtp_password')); + } + + public function testEmptyValueIsNotEncrypted() + { + MailSetting::set('smtp_password', ''); + + $this->assertSame('', $this->getStoredValues()['smtp_password']); + + $this->reloadSettings(); + + $this->assertSame('', MailSetting::get('smtp_password')); + } + + public function testPlainTextValueStoredBeforeEncryptionIsStillReadable() + { + MailSetting::set('smtp_user', 'mailer'); + + $values = $this->getStoredValues(); + $values['smtp_password'] = 'legacy-secret'; + DB::table('system_settings') + ->where('item', 'system_mail_settings') + ->update(['value' => json_encode($values)]); + + $this->reloadSettings(); + + $this->assertSame('legacy-secret', MailSetting::get('smtp_password')); + + // It gets encrypted the next time the settings are saved + MailSetting::set('smtp_user', 'other'); + + $this->assertSame('legacy-secret', Crypt::decrypt($this->getStoredValues()['smtp_password'])); + } +}