Repository navigation
Add support for encrypting settings model values at rest #1602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
officialasishkumar
wants to merge
2
commits into
wintercms:develop
Choose a base branch
from
officialasishkumar:feature/encrypted-settings-fields
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| <?php | ||
|
|
||
| namespace System\Tests\Models; | ||
|
|
||
| use Illuminate\Support\Facades\Cache; | ||
| use Illuminate\Support\Facades\Crypt; | ||
| use Illuminate\Support\Facades\DB; | ||
| use System\Behaviors\SettingsModel; | ||
| use System\Models\MailSetting; | ||
| use System\Tests\Bootstrap\PluginTestCase; | ||
| use System\Tests\Concerns\MigratesDatabaseOnce; | ||
|
|
||
| class MailSettingTest extends PluginTestCase | ||
| { | ||
| use MigratesDatabaseOnce; | ||
|
|
||
| public function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| SettingsModel::clearInternalCache(); | ||
| } | ||
|
|
||
| public function tearDown(): void | ||
| { | ||
| Cache::forget('system::settings.system_mail_settings'); | ||
| 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('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'])); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
sed -n 205,300p modules/system/behaviors/SettingsModel.php rg -n 'applyConfigValues|smtp_password' modules/system/models/MailSetting.phpRepository: wintercms/winter
Length of output: 3303
Script executed:
Repository: wintercms/winter
Length of output: 27811
Do not treat unreadable ciphertext as legacy plaintext.
If the application uses the wrong encryption key,
MailSetting::applyConfigValues()can use the ciphertext as the SMTP password. An unrelated settings save can also encrypt that ciphertext again. Restoring the original key then cannot recover the password through the normal read path.Distinguish unreadable encrypted values from legacy plaintext. Report decryption failures and preserve the stored ciphertext without re-encrypting it. Continue to support migration of legacy plaintext on save.