From 12f30a8d09ec0233705cd140c7281e99c4ffaeb1 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Wed, 11 Nov 2026 20:33:51 +0530 Subject: [PATCH 1/2] Add support for encrypting settings model values at rest Settings models store every value as plain JSON in the system_settings table, so credentials such as the SMTP password configured through the backend were readable by anyone with access to the database or a backup. Models using the SettingsModel behavior can now list the settings that should be protected in an optional $settingsEncrypted property. Those values are encrypted with the application encrypter when the record is saved and decrypted when it is loaded, so get(), set() and property access keep working with plain values and the cached/in-memory copies are never double-encrypted. Empty values are stored as they are. Records saved before a setting was marked as encrypted keep working: a value that is not a valid encrypted payload is read as plain text and is encrypted the next time the settings are saved. MailSetting opts in for smtp_password, which resolves the plain text storage reported in #1505. --- modules/system/behaviors/SettingsModel.php | 65 ++++++++- modules/system/models/MailSetting.php | 5 + .../system/tests/models/MailSettingTest.php | 130 ++++++++++++++++++ 3 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 modules/system/tests/models/MailSettingTest.php 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..055d8b5a79 --- /dev/null +++ b/modules/system/tests/models/MailSettingTest.php @@ -0,0 +1,130 @@ +cacheKey); + MailSetting::instance()->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(MailSetting::instance()->cacheKey); + 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'])); + } +} From 1bc1c73c9d67ec6384381d68e785e8a761f1bde5 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Thu, 12 Nov 2026 16:03:05 +0530 Subject: [PATCH 2/2] Clear the settings cache with its real key in MailSettingTest --- modules/system/tests/models/MailSettingTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/system/tests/models/MailSettingTest.php b/modules/system/tests/models/MailSettingTest.php index 055d8b5a79..8c225c2a08 100644 --- a/modules/system/tests/models/MailSettingTest.php +++ b/modules/system/tests/models/MailSettingTest.php @@ -23,7 +23,7 @@ public function setUp(): void public function tearDown(): void { - Cache::forget(MailSetting::instance()->cacheKey); + Cache::forget('system::settings.system_mail_settings'); MailSetting::instance()->resetDefault(); SettingsModel::clearInternalCache(); @@ -43,7 +43,7 @@ protected function getStoredValues(): array */ protected function reloadSettings(): void { - Cache::forget(MailSetting::instance()->cacheKey); + Cache::forget('system::settings.system_mail_settings'); SettingsModel::clearInternalCache(); }