Skip to content
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

Add new type encrypted_string for preferences #3676

Merged
merged 4 commits into from
Aug 21, 2020
Merged

Add new type encrypted_string for preferences #3676

merged 4 commits into from
Aug 21, 2020

Conversation

stefano-sarioli
Copy link
Contributor

Description

This pull request wants to resolve the issue Ref #3652 adding a new type encrypted_string as a possible type when declaring preferences.

The new type encrypted_string would encrypt the value assigned to the preference so that when saved to a Database the value would be safe if a malicious actor gets access to the DB or a dump.

The value to be encrypted need to be set/get using the preferences setter and getter method, the encryption would be skipped if the preferences hash is accessed directly.
The idea behind this is that the value would always be encrypted inside the preferences' hash so that the end-user doesn't need to change the way it interacts with the preferences.

This implementation would not handle the encryption key, it's up to the end-user to safely generate, store, and rotate the key.
The key can be passed as an option to the preference (encryption_key), it the option is missing the system would look for an environment variable called SOLIDUS_PREFERENCES_MASTER_KEY, if even this variable is missing the system will use the Rails secret key.

I'm not sure that this solution would cover all the use cases that the author of the original issue had in mind, so I open the pull request as a draft to ask for feedback about possible missing use cases.

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have updated Guides and README accordingly to this change (if needed)
  • I have added tests to cover this change (if needed)
  • I have attached screenshots to this PR for visual changes (if needed)

@stefano-sarioli stefano-sarioli marked this pull request as ready for review June 28, 2020 08:34
@spaghetticode
Copy link
Member

@stefano-sarioli thanks for this PR. I left a few comments, let me know what you think about them.
Also, the Mysql build is failing due to a flaky spec, rebasing with master will fix the problem. thanks!

@stefano-sarioli
Copy link
Contributor Author

@spaghetticode Hi, I've pushed my fix to your comment, thanks again for the feedback.

Copy link
Member

@jarednorman jarednorman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple minor style-related comments.

core/lib/spree/encryptor.rb Outdated Show resolved Hide resolved
Stefano Sarioli added 4 commits August 10, 2020 21:54
Encryptor is a thin wrapper around ActiveSupport::MessageEncryptor,
it can be used when a simple encryption solution is needed.

Encryptor does not have key management features, you have to
generate, store, and rotate key by hand, if needed.
The new type encrypted_string encrypt the content of the preferences,
the original value can be read-only using the preferences accessor method.

It's still recommended to use env variables to manage secrets,
but if it's needed to save the preference on the database, now it's
possible to use encrypted_string to add a security layer.

The encryption key can be passed using the encryption_key option of the preference,
if the option is missing the system fallback to the env variable SOLIDUS_PREFERENCES_MASTER_KEY,
it even this env variable is missing the system will use the Rails master key.

If a default value it's assigned to an encrypted_string preference, this will not be encrypted.
On the backend encrypted_string preferences are treated like normal string,
the are no differences for the end-user.
Update the documentation section about Preferences, now it explains
all the setter/getter methods created for the preferences.

The update is inspired by the Spree documentation about Preferences.
Copy link
Member

@spaghetticode spaghetticode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stefano-sarioli thank you 👍
And thank you for updating the doc as well ❤️

@stefano-sarioli
Copy link
Contributor Author

Hi @jarednorman, does the change I made thanks to your feedbacks looks good to you?

Copy link
Member

@jarednorman jarednorman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, looks all good to me.

@spaghetticode spaghetticode merged commit a7ca703 into solidusio:master Aug 21, 2020
@stefano-sarioli stefano-sarioli deleted the stefano-sarioli/3652-encrypted-preferences branch September 22, 2020 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants