Skip to content

Commit e1ec453

Browse files
committed
Filter decrypted attributes
e19445a introduced marking attr_encrypted attributes as virtual attributes to avoid an ActiveRecord deprecation warning in AR 5.1. This had the side effect of exposing the decrypted versions of the attributes in `ActiveRecord::Base#attributes`. This is problematic since the method is leveraged for things like `#as_json` and `respond_with`, meaning a user could inadvertely expose sensitive info with an action like: ``` def show respond_with @user end ```
1 parent 3e5b7fa commit e1ec453

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/attr_encrypted/adapters/active_record.rb

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def assign_attributes(*args)
4343
def attributes=(*args)
4444
perform_attribute_assignment :attributes_without_attr_encrypted=, *args
4545
end
46+
47+
alias_method :attributes_without_attr_encrypted, :attributes
48+
def attributes
49+
encrypted_keys = self.class.encrypted_attributes.keys
50+
attributes_without_attr_encrypted.reject { |k, _| encrypted_keys.include?(k.to_sym) }
51+
end
4652
end
4753
end
4854

test/active_record_test.rb

+5
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,9 @@ def test_should_evaluate_proc_based_mode
337337
refute_equal address.encrypted_zipcode, zipcode
338338
assert_equal address.zipcode, zipcode
339339
end
340+
341+
def test_should_filter_decrypted_attributes
342+
@person = Person.new(email: '[email protected]')
343+
refute @person.attributes.keys.include? "email"
344+
end
340345
end

0 commit comments

Comments
 (0)