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

Comparing another known ruby 3/rails 7 fork #2

Closed
wants to merge 5 commits into from

Conversation

merrington
Copy link

@merrington merrington commented Feb 24, 2023

Why

Until we move to rails AR 7 encryption, we need this gem. Another team has previously used a similar branch, however this one has a couple minor additional changes - notably a ruby 3 fix, and a few other methods were renamed. TBH from what I've seen I think we don't strictly need the other renames, however there are some breaking changes:

  • SomeModel.encrypt/decrypt is renamed to .attr_encrypted_encrypt/.attr_encrypted_decrypt
  • some_record.encrypt/decrypt is renamed to #attr_encrypted_encrypt/#attr_encrypted_decrypt
  • This is so that the methods don't conflict with methods added in AR 7 encryption

What is changing

See above - most methods get prefixed with attr_encrypted_ to clarify they are not using AR 7 encryption

How I tested

Security 🛡 (required)

Checklist

How does this PR handle security?

Screenshot

Next steps

armiiller and others added 5 commits April 12, 2021 15:05
Prefixes our encrypted_attributes with library specific attr_encrypted so we are not clashing with the Rails 7.0 definition of encrypted_attributes
Prefix encrypt and decrypt methods with attr_encrypted so we don't clash with rails 7
@merrington merrington changed the base branch from attr-encrypted-encrypted-attributes to master March 7, 2023 19:18
Copy link
Author

@merrington merrington left a comment

Choose a reason for hiding this comment

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

Highlighted differences between this branch and similar fork - master...wealthsimple:attr_encrypted:attr-encrypted-encrypted-attributes

Comment on lines +163 to +167
instance_variable_get("@#{attribute}") || instance_variable_set("@#{attribute}", attr_encrypted_decrypt(attribute, send(encrypted_attribute_name)))
end

define_method("#{attribute}=") do |value|
send("#{encrypted_attribute_name}=", encrypt(attribute, value))
send("#{encrypted_attribute_name}=", attr_encrypted_encrypt(attribute, value))
Copy link
Author

Choose a reason for hiding this comment

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

Calling attr_encrypted(:secure_field) is supposed to create two methods, #secure_field and #secure_field=. This ensures that those methods use the attr_encrypted implementations for decrypt/encrypt.

Otherwise, in rails 7 this would call the rails-versions of #encrypt/#decrypt?

def decrypt(attribute, encrypted_value, options = {})
options = encrypted_attributes[attribute.to_sym].merge(options)
# email = User.attr_encrypted_decrypt(:email, 'SOME_ENCRYPTED_EMAIL_STRING')
def attr_encrypted_decrypt(attribute, encrypted_value, options = {})
Copy link
Author

Choose a reason for hiding this comment

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

Renames the .decrypt method (I don't think this or the next one impact Rails 7)

def encrypt(attribute, value, options = {})
options = encrypted_attributes[attribute.to_sym].merge(options)
# encrypted_email = User.attr_encrypted_encrypt(:email, '[email protected]')
def attr_encrypted_encrypt(attribute, value, options = {})
Copy link
Author

Choose a reason for hiding this comment

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

Renames the .encrypt method

encrypted_attributes[attribute.to_sym][:operation] = :decrypting
encrypted_attributes[attribute.to_sym][:value_present] = self.class.not_empty?(encrypted_value)
self.class.decrypt(attribute, encrypted_value, evaluated_attr_encrypted_options_for(attribute))
def attr_encrypted_decrypt(attribute, encrypted_value)
Copy link
Author

Choose a reason for hiding this comment

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

Rename the instance method to not collide with AR 7 #decrypt

encrypted_attributes[attribute.to_sym][:value_present] = self.class.not_empty?(value)
self.class.encrypt(attribute, value, evaluated_attr_encrypted_options_for(attribute))
# @user.attr_encrypted_encrypt(:email, '[email protected]')
def attr_encrypted_encrypt(attribute, value)
Copy link
Author

Choose a reason for hiding this comment

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

Rename the instance method to not collide with AR 7 #encrypt


define_method("#{attr}_was") do
attribute_was(attr)
end

if ::ActiveRecord::VERSION::STRING >= "4.1"
define_method("#{attr}_changed?") do |options = {}|
attribute_changed?(attr, options)
attribute_changed?(attr, **options)
Copy link
Author

Choose a reason for hiding this comment

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

Ruby 3 syntax

@merrington merrington marked this pull request as ready for review March 7, 2023 19:50
Copy link
Member

@6 6 left a comment

Choose a reason for hiding this comment

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

🎷🐠 🎷🐋 🎷🐬

@merrington merrington mentioned this pull request Mar 8, 2023
@merrington
Copy link
Author

🙏 thanks @6! To get tests working I ended up creating a new branch based off this (since this is coming from another github org I couldn't add it into this same branch/PR), see #3

@merrington merrington closed this Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants