attr_encrypted 3.1.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f602391484e3437daae62ddb60596f0b4cbc83e0
4
- data.tar.gz: 5e407d7e299a43380057a2f0fe816da3a6ad1fca
2
+ SHA256:
3
+ metadata.gz: 36ebe1f889eadcc15b60089547792d023a5b32e4877a17789da28bc2ca33b3c1
4
+ data.tar.gz: 54540410056e0b49d7cc6af2077af4e45cf06aef354e0780757b93ff4af1e134
5
5
  SHA512:
6
- metadata.gz: 77b697d9e450d6baae65fb3ed339824b8bf730898296f172c2ae6b4e0bd7368add0da29c76596b9bbf71d298fccd038195f7ffb7d153ffaeaf2aabc7bfc60f9f
7
- data.tar.gz: 211f1c52e7f0505d55eef59f06bb139f47fb1ae0158b39a643b5c7c5251865f13b871ab7b3194d44cd2fc6a128e86a5c6b9c8669bab6d2870a7914ed5dacb6c3
6
+ metadata.gz: 55a0639ffee8def55645e2444d063ab18df7030f74404289735b2e9329ff65461ac0af3dab29248c337f9b5344aeb7c78d72f3d9155edabde689997c519c88b2
7
+ data.tar.gz: 22af4440292a3950cb2ca1ab4ed795972626fb974b962d5dc86ebec5492fb7f3a83b691d2432d691d39de66df0a244bfce31576af0b58ab65881b8faf1db8323
data/.travis.yml CHANGED
@@ -1,60 +1,22 @@
1
- sudo: false
2
1
  language: ruby
2
+ dist: focal
3
+ os: linux
3
4
  cache: bundler
4
5
  rvm:
5
- - 2.0
6
- - 2.1
7
- - 2.2.2
8
- - 2.3.0
9
- - 2.4.0
10
- - 2.5.0
11
- - rbx
6
+ - 2.6.10
7
+ - 2.7.6
12
8
  env:
13
- - ACTIVERECORD=3.0.0
14
- - ACTIVERECORD=3.1.0
15
- - ACTIVERECORD=3.2.0
16
- - ACTIVERECORD=4.0.0
17
- - ACTIVERECORD=4.1.0
18
- - ACTIVERECORD=4.2.0
19
- - ACTIVERECORD=5.0.0
20
9
  - ACTIVERECORD=5.1.1
21
- matrix:
10
+ - ACTIVERECORD=5.2.8
11
+ - ACTIVERECORD=6.0.6
12
+ - ACTIVERECORD=6.1.7
13
+ - ACTIVERECORD=7.0.4
14
+ jobs:
15
+ fast_finish: false
22
16
  exclude:
23
- - rvm: 2.0
24
- env: ACTIVERECORD=5.0.0
25
- - rvm: 2.0
26
- env: ACTIVERECORD=5.1.1
27
- - rvm: 2.1
28
- env: ACTIVERECORD=5.0.0
29
- - rvm: 2.1
30
- env: ACTIVERECORD=5.1.1
31
- - rvm: 2.4.0
32
- env: ACTIVERECORD=3.0.0
33
- - rvm: 2.4.0
34
- env: ACTIVERECORD=3.1.0
35
- - rvm: 2.4.0
36
- env: ACTIVERECORD=3.2.0
37
- - rvm: 2.4.0
38
- env: ACTIVERECORD=4.0.0
39
- - rvm: 2.4.0
40
- env: ACTIVERECORD=4.1.0
41
- - rvm: 2.5.0
42
- env: ACTIVERECORD=3.0.0
43
- - rvm: 2.5.0
44
- env: ACTIVERECORD=3.1.0
45
- - rvm: 2.5.0
46
- env: ACTIVERECORD=3.2.0
47
- - rvm: 2.5.0
48
- env: ACTIVERECORD=4.0.0
49
- - rvm: 2.5.0
50
- env: ACTIVERECORD=4.1.0
51
- - rvm: rbx
52
- env: ACTIVERECORD=5.0.0
53
- - rvm: rbx
54
- env: ACTIVERECORD=5.1.1
55
- allow_failures:
56
- - rvm: rbx
57
- fast_finish: true
17
+ - rvm: 2.6.10
18
+ env: ACTIVERECORD=7.0.4
19
+
58
20
  addons:
59
21
  code_climate:
60
22
  repo_token: a90435ed4954dd6e9f3697a20c5bc3754f67d94703f870e8fc7b00f69f5b2d06
data/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
- # attr_encrypted #
1
+ # attr_encrypted
2
+
3
+ ## 4.0.0
4
+
5
+ * Added: Support for Ruby >= 3.0.
6
+ * Added: Rails 7 support.
7
+ * Changed: Using `#encrypted_attributes` is no longer supported. Instead, use `#attr_encrypted_encrypted_attributes` to avoid
8
+ collision with Active Record 7 native encryption.
9
+
10
+ ## 3.1.0
2
11
 
3
- ## 3.1.0 ##
4
12
  * Added: Abitilty to encrypt empty values. (@tamird)
5
13
  * Added: MIT license
6
14
  * Added: MRI 2.5.x support (@saghaulor)
@@ -11,23 +19,28 @@
11
19
  * Fixed: Only check empty on strings, allows for encrypting non-string type objects
12
20
  * Fixed: Fixed how accessors for db columns are defined in the ActiveRecord adapter, preventing premature definition. (@nagachika)
13
21
 
14
- ## 3.0.3 ##
22
+ ## 3.0.3
23
+
15
24
  * Fixed: attr_was would decrypt the attribute upon every call. This is inefficient and introduces problems when the options change between decrypting an old value and encrypting a new value; for example, when rotating the encryption key. As such, the new approach caches the decrypted value of the old encrypted value such that the old options are no longer needed. (@johnny-lai) (@saghaulor)
16
25
 
17
- ## 3.0.2 ##
26
+ ## 3.0.2
27
+
18
28
  * Changed: Removed alias_method_chain for compatibility with Rails v5.x (@grosser)
19
29
  * Changed: Updated Travis build matrix to include Rails 5. (@saghaulor) (@connorshea)
20
30
  * Changed: Removed `.silence_stream` from tests as it has been removed from Rails 5. (@sblackstone)
21
31
 
22
- ## 3.0.1 ##
32
+ ## 3.0.1
33
+
23
34
  * Fixed: attr_was method no longer calls undefined methods. (@saghaulor)
24
35
 
25
- ## 3.0.0 ##
36
+ ## 3.0.0
37
+
26
38
  * Changed: Updated gemspec to use Encryptor v3.0.0. (@saghaulor)
27
39
  * Changed: Updated README with instructions related to moving from v2.0.0 to v3.0.0. (@saghaulor)
28
40
  * Fixed: ActiveModel::Dirty methods in the ActiveRecord adapter. (@saghaulor)
29
41
 
30
- ## 2.0.0 ##
42
+ ## 2.0.0
43
+
31
44
  * Added: Now using Encryptor v2.0.0 (@saghaulor)
32
45
  * Added: Options are copied to the instance. (@saghaulor)
33
46
  * Added: Operation option is set during encryption/decryption to allow options to be evaluated in the context of the current operation. (@saghaulor)
@@ -48,51 +61,62 @@
48
61
  * Removed: Support for Rails < 3.x (@saghaulor)
49
62
  * Removed: Unnecessary use of `alias_method` from ActiveRecord adapter. (@saghaulor)
50
63
 
51
- ## 1.4.0 ##
64
+ ## 1.4.0
65
+
52
66
  * Added: ActiveModel::Dirty#attribute_was (@saghaulor)
53
67
  * Added: ActiveModel::Dirty#attribute_changed? (@mwean)
54
68
 
55
- ## 1.3.5 ##
69
+ ## 1.3.5
70
+
56
71
  * Changed: Fixed gemspec to explicitly depend on Encryptor v1.3.0 (@saghaulor)
57
72
  * Fixed: Evaluate `:mode` option as a symbol or proc. (@cheynewallace)
58
73
 
59
- ## 1.3.4 ##
74
+ ## 1.3.4
75
+
60
76
  * Added: ActiveRecord::Base.reload support. (@rcook)
61
77
  * Fixed: ActiveRecord adapter no longer forces attribute hashes to be string-keyed. (@tamird)
62
78
  * Fixed: Mass assignment protection in ActiveRecord 4. (@tamird)
63
79
  * Changed: Now using rubygems over https. (@tamird)
64
80
  * Changed: Let ActiveRecord define attribute methods. (@saghaulor)
65
81
 
66
- ## 1.3.3 ##
82
+ ## 1.3.3
83
+
67
84
  * Added: Alias attr_encryptor and attr_encrpted. (@Billy Monk)
68
85
 
69
- ## 1.3.2 ##
86
+ ## 1.3.2
87
+
70
88
  * Fixed: Bug regarding strong parameters. (@S. Brent Faulkner)
71
89
  * Fixed: Bug regarding loading per instance IV and salt. (@S. Brent Faulkner)
72
90
  * Fixed: Bug regarding assigning nil. (@S. Brent Faulkner)
73
91
  * Added: Support for protected attributes. (@S. Brent Faulkner)
74
92
  * Added: Support for ActiveRecord 4. (@S. Brent Faulkner)
75
93
 
76
- ## 1.3.1 ##
94
+ ## 1.3.1
95
+
77
96
  * Added: Support for Rails 2.3.x and 3.1.x. (@S. Brent Faulkner)
78
97
 
79
- ## 1.3.0 ##
98
+ ## 1.3.0
99
+
80
100
  * Fixed: Serialization bug. (@Billy Monk)
81
101
  * Added: Support for :per_attribute_iv_and_salt mode. (@rcook)
82
102
  * Fixed: Added dependencies to gemspec. (@jmazzi)
83
103
 
84
- ## 1.2.1 ##
104
+ ## 1.2.1
105
+
85
106
  * Added: Force encoding when not marshaling. (@mosaicxm)
86
107
  * Fixed: Issue specifying multiple attributes on the same line. (@austintaylor)
87
108
  * Added: Typecasting to String before encryption (@shuber)
88
109
  * Added: `"#{attribute}?"` method. (@shuber)
89
110
 
90
- ## 1.2.0 ##
111
+ ## 1.2.0
112
+
91
113
  * Changed: General code refactoring (@shuber)
92
114
 
93
- ## 1.1.2 ##
115
+ ## 1.1.2
116
+
94
117
  * No significant changes
95
118
 
96
- ## 1.1.1 ##
119
+ ## 1.1.1
120
+
97
121
  * Changled: Updated README. (@shuber)
98
122
  * Added: `before_type_cast` alias to ActiveRecord adapter. (@shuber)
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # attr_encrypted
2
+
2
3
  [![Build Status](https://secure.travis-ci.org/attr-encrypted/attr_encrypted.svg)](https://travis-ci.org/attr-encrypted/attr_encrypted) [![Test Coverage](https://codeclimate.com/github/attr-encrypted/attr_encrypted/badges/coverage.svg)](https://codeclimate.com/github/attr-encrypted/attr_encrypted/coverage) [![Code Climate](https://codeclimate.com/github/attr-encrypted/attr_encrypted/badges/gpa.svg)](https://codeclimate.com/github/attr-encrypted/attr_encrypted) [![Gem Version](https://badge.fury.io/rb/attr_encrypted.svg)](https://badge.fury.io/rb/attr_encrypted) [![security](https://hakiri.io/github/attr-encrypted/attr_encrypted/master.svg)](https://hakiri.io/github/attr-encrypted/attr_encrypted/master)
3
4
 
4
5
  Generates attr_accessors that transparently encrypt and decrypt attributes.
@@ -11,7 +12,7 @@ It works with ANY class, however, you get a few extra features when you're using
11
12
  Add attr_encrypted to your gemfile:
12
13
 
13
14
  ```ruby
14
- gem "attr_encrypted", "~> 3.0.0"
15
+ gem "attr_encrypted"
15
16
  ```
16
17
 
17
18
  Then install the gem:
@@ -87,6 +88,12 @@ Create or modify the table that your model uses to add a column with the `encryp
87
88
 
88
89
  You can use a string or binary column type. (See the encode option section below for more info)
89
90
 
91
+ If you use the same key for each record, add a unique index on the IV. Repeated IVs with AES-GCM (the default algorithm) allow an attacker to recover the key.
92
+
93
+ ```ruby
94
+ add_index :users, :encrypted_ssn_iv, unique: true
95
+ ```
96
+
90
97
  ### Specifying the encrypted attribute name
91
98
 
92
99
  By default, the encrypted attribute name is `encrypted_#{attribute}` (e.g. `attr_encrypted :email` would create an attribute named `encrypted_email`). So, if you're storing the encrypted attribute in the database, you need to make sure the `encrypted_#{attribute}` field exists in your table. You have a couple of options if you want to name your attribute or db column something else, see below for more details.
@@ -403,7 +410,7 @@ Then modify your models using attr\_encrypted to account for the changes in defa
403
410
 
404
411
  ## Upgrading from attr_encrypted v2.x to v3.x
405
412
 
406
- A bug was discovered in Encryptor v2.0.0 that inccorectly set the IV when using an AES-\*-GCM algorithm. Unfornately fixing this major security issue results in the inability to decrypt records encrypted using an AES-*-GCM algorithm from Encryptor v2.0.0. Please see [Upgrading to Encryptor v3.0.0](https://github.com/attr-encrypted/encryptor#upgrading-from-v200-to-v300) for more info.
413
+ A bug was discovered in Encryptor v2.0.0 that incorrectly set the IV when using an AES-\*-GCM algorithm. Unfornately fixing this major security issue results in the inability to decrypt records encrypted using an AES-*-GCM algorithm from Encryptor v2.0.0. Please see [Upgrading to Encryptor v3.0.0](https://github.com/attr-encrypted/encryptor#upgrading-from-v200-to-v300) for more info.
407
414
 
408
415
  It is strongly advised that you re-encrypt your data encrypted with Encryptor v2.0.0. However, you'll have to take special care to re-encrypt. To decrypt data encrypted with Encryptor v2.0.0 using an AES-\*-GCM algorithm you can use the `:v2_gcm_iv` option.
409
416
 
@@ -414,7 +421,7 @@ It is recommended that you implement a strategy to insure that you do not mix th
414
421
  attr_encrypted :ssn, key: :encryption_key, v2_gcm_iv: is_decrypting?(:ssn)
415
422
 
416
423
  def is_decrypting?(attribute)
417
- encrypted_attributes[attribute][:operation] == :decrypting
424
+ attr_encrypted_encrypted_attributes[attribute][:operation] == :decrypting
418
425
  end
419
426
  end
420
427
 
@@ -431,7 +438,7 @@ It is recommended that you implement a strategy to insure that you do not mix th
431
438
  While choosing to encrypt at the attribute level is the most secure solution, it is not without drawbacks. Namely, you cannot search the encrypted data, and because you can't search it, you can't index it either. You also can't use joins on the encrypted data. Data that is securely encrypted is effectively noise. So any operations that rely on the data not being noise will not work. If you need to do any of the aforementioned operations, please consider using database and file system encryption along with transport encryption as it moves through your stack.
432
439
 
433
440
  #### Data leaks
434
- Please also consider where your data leaks. If you're using attr_encrypted with Rails, it's highly likely that this data will enter your app as a request parameter. You'll want to be sure that you're filtering your request params from you logs or else your data is sitting in the clear in your logs. [Parameter Filtering in Rails](http://apidock.com/rails/ActionDispatch/Http/FilterParameters) Please also consider other possible leak points.
441
+ Please also consider where your data leaks. If you're using attr_encrypted with Rails, it's highly likely that this data will enter your app as a request parameter. You'll want to be sure that you're filtering your request params from your logs or else your data is sitting in the clear in your logs. [Parameter Filtering in Rails](http://apidock.com/rails/ActionDispatch/Http/FilterParameters) Please also consider other possible leak points.
435
442
 
436
443
  #### Storage requirements
437
444
  When storing your encrypted data, please consider the length requirements of the db column that you're storing the cipher text in. Older versions of Mysql attempt to 'help' you by truncating strings that are too large for the column. When this happens, you will not be able to decrypt your data. [MySQL Strict Trans](http://www.davidpashley.com/2009/02/15/silently-truncated/)
@@ -440,7 +447,7 @@ When storing your encrypted data, please consider the length requirements of the
440
447
  It is advisable to also store metadata regarding the circumstances of your encrypted data. Namely, you should store information about the key used to encrypt your data, as well as the algorithm. Having this metadata with every record will make key rotation and migrating to a new algorithm signficantly easier. It will allow you to continue to decrypt old data using the information provided in the metadata and new data can be encrypted using your new key and algorithm of choice.
441
448
 
442
449
  #### Enforcing the IV as a nonce
443
- On a related note, most alorithms require that your IV be unique for every record and key combination. You can enforce this using composite unique indexes on your IV and encryption key name/id column. [RFC 5084](https://tools.ietf.org/html/rfc5084#section-1.5)
450
+ On a related note, most algorithms require that your IV be unique for every record and key combination. You can enforce this using composite unique indexes on your IV and encryption key name/id column. [RFC 5084](https://tools.ietf.org/html/rfc5084#section-1.5)
444
451
 
445
452
  #### Unique key per record
446
453
  Lastly, while the `:per_attribute_iv_and_salt` mode is more secure than `:per_attribute_iv` mode because it uses a unique key per record, it uses a PBKDF function which introduces a huge performance hit (175x slower by my benchmarks). There are other ways of deriving a unique key per record that would be much faster.
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake/testtask'
2
4
  require 'rdoc/task'
3
5
  require "bundler/gem_tasks"
@@ -19,15 +19,12 @@ Gem::Specification.new do |s|
19
19
  s.homepage = 'http://github.com/attr-encrypted/attr_encrypted'
20
20
  s.license = 'MIT'
21
21
 
22
- s.has_rdoc = false
23
- s.rdoc_options = ['--line-numbers', '--inline-source', '--main', 'README.rdoc']
24
-
25
22
  s.require_paths = ['lib']
26
23
 
27
24
  s.files = `git ls-files`.split("\n")
28
25
  s.test_files = `git ls-files -- test/*`.split("\n")
29
26
 
30
- s.required_ruby_version = '>= 2.0.0'
27
+ s.required_ruby_version = '>= 2.6.0'
31
28
 
32
29
  s.add_dependency('encryptor', ['~> 3.0.0'])
33
30
  # support for testing with specific active record version
@@ -42,27 +39,18 @@ Gem::Specification.new do |s|
42
39
  s.add_development_dependency('rake')
43
40
  s.add_development_dependency('minitest')
44
41
  s.add_development_dependency('sequel')
45
- if RUBY_VERSION < '2.1.0'
46
- s.add_development_dependency('nokogiri', '< 1.7.0')
47
- s.add_development_dependency('public_suffix', '< 3.0.0')
48
- end
49
42
  if defined?(RUBY_ENGINE) && RUBY_ENGINE.to_sym == :jruby
50
43
  s.add_development_dependency('activerecord-jdbcsqlite3-adapter')
51
44
  s.add_development_dependency('jdbc-sqlite3', '< 3.8.7') # 3.8.7 is nice and broke
52
45
  else
53
- s.add_development_dependency('sqlite3')
46
+ s.add_development_dependency('sqlite3', '= 1.5.4')
54
47
  end
55
48
  s.add_development_dependency('dm-sqlite-adapter')
49
+ s.add_development_dependency('pry')
56
50
  s.add_development_dependency('simplecov')
57
51
  s.add_development_dependency('simplecov-rcov')
58
- s.add_development_dependency("codeclimate-test-reporter", '<= 0.6.0')
59
-
60
- s.cert_chain = ['certs/saghaulor.pem']
61
- s.signing_key = File.expand_path("~/.ssh/gem-private_key.pem") if $0 =~ /gem\z/
62
52
 
63
- s.post_install_message = "\n\n\nWARNING: Several insecure default options and features were deprecated in attr_encrypted v2.0.0.\n
64
- Additionally, there was a bug in Encryptor v2.0.0 that insecurely encrypted data when using an AES-*-GCM algorithm.\n
65
- This bug was fixed but introduced breaking changes between v2.x and v3.x.\n
66
- Please see the README for more information regarding upgrading to attr_encrypted v3.0.0.\n\n\n"
53
+ s.post_install_message = "\n\n\nWARNING: Using `#encrypted_attributes` is no longer supported. Instead, use `#attr_encrypted_encrypted_attributes` to avoid
54
+ collision with Active Record 7 native encryption.\n\n\n"
67
55
 
68
56
  end
@@ -0,0 +1 @@
1
+ 4f0682604714ed4599cf00771ad27e82f0b51b0ed8644af51a43d21fbe129b59
@@ -0,0 +1 @@
1
+ dc757a50c53175dc05adbfccb25b536f7f1a060c7bd626bfc72ff577479c6fe28d3b65747033276bd4bce3dad741b67235f3e01ea61409f6c67959da65df445b
@@ -1,7 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  if defined?(ActiveRecord::Base)
2
4
  module AttrEncrypted
3
5
  module Adapters
4
6
  module ActiveRecord
7
+ RAILS_VERSION = Gem::Version.new(::ActiveRecord::VERSION::STRING).freeze
8
+
5
9
  def self.extended(base) # :nodoc:
6
10
  base.class_eval do
7
11
 
@@ -9,7 +13,7 @@ if defined?(ActiveRecord::Base)
9
13
  alias_method :reload_without_attr_encrypted, :reload
10
14
  def reload(*args, &block)
11
15
  result = reload_without_attr_encrypted(*args, &block)
12
- self.class.encrypted_attributes.keys.each do |attribute_name|
16
+ self.class.attr_encrypted_encrypted_attributes.keys.each do |attribute_name|
13
17
  instance_variable_set("@#{attribute_name}", nil)
14
18
  end
15
19
  result
@@ -25,12 +29,12 @@ if defined?(ActiveRecord::Base)
25
29
  def perform_attribute_assignment(method, new_attributes, *args)
26
30
  return if new_attributes.blank?
27
31
 
28
- send method, new_attributes.reject { |k, _| self.class.encrypted_attributes.key?(k.to_sym) }, *args
29
- send method, new_attributes.reject { |k, _| !self.class.encrypted_attributes.key?(k.to_sym) }, *args
32
+ send method, new_attributes.reject { |k, _| self.class.attr_encrypted_encrypted_attributes.key?(k.to_sym) }, *args
33
+ send method, new_attributes.reject { |k, _| !self.class.attr_encrypted_encrypted_attributes.key?(k.to_sym) }, *args
30
34
  end
31
35
  private :perform_attribute_assignment
32
36
 
33
- if ::ActiveRecord::VERSION::STRING > "3.1"
37
+ if Gem::Requirement.new('> 3.1').satisfied_by?(RAILS_VERSION)
34
38
  alias_method :assign_attributes_without_attr_encrypted, :assign_attributes
35
39
  def assign_attributes(*args)
36
40
  perform_attribute_assignment :assign_attributes_without_attr_encrypted, *args
@@ -51,21 +55,15 @@ if defined?(ActiveRecord::Base)
51
55
  super
52
56
  options = attrs.extract_options!
53
57
  attr = attrs.pop
54
- attribute attr if ::ActiveRecord::VERSION::STRING >= "5.1.0"
55
- options.merge! encrypted_attributes[attr]
58
+ attribute attr
59
+ options.merge! attr_encrypted_encrypted_attributes[attr]
56
60
 
57
61
  define_method("#{attr}_was") do
58
62
  attribute_was(attr)
59
63
  end
60
64
 
61
- if ::ActiveRecord::VERSION::STRING >= "4.1"
62
- define_method("#{attr}_changed?") do |options = {}|
63
- attribute_changed?(attr, options)
64
- end
65
- else
66
- define_method("#{attr}_changed?") do
67
- attribute_changed?(attr)
68
- end
65
+ define_method("#{attr}_changed?") do |options = {}|
66
+ attribute_changed?(attr, **options)
69
67
  end
70
68
 
71
69
  define_method("#{attr}_change") do
@@ -73,6 +71,19 @@ if defined?(ActiveRecord::Base)
73
71
  end
74
72
 
75
73
  define_method("#{attr}_with_dirtiness=") do |value|
74
+ ## Source: https://github.com/priyankatapar/attr_encrypted/commit/7e8702bd5418c927a39d8dd72c0adbea522d5663
75
+ # In ActiveRecord 5.2+, due to changes to the way virtual
76
+ # attributes are handled, @attributes[attr].value is nil which
77
+ # breaks attribute_was. Setting it here returns us to the expected
78
+ # behavior.
79
+ if RAILS_VERSION >= Gem::Version.new(5.2)
80
+ # This is needed support attribute_was before a record has
81
+ # been saved
82
+ @attributes.write_cast_value(attr.to_s, __send__(attr)) if value != __send__(attr)
83
+ # This is needed to support attribute_was after a record has
84
+ # been saved
85
+ @attributes.write_from_user(attr.to_s, value) if value != __send__(attr)
86
+ end
76
87
  attribute_will_change!(attr) if value != __send__(attr)
77
88
  __send__("#{attr}_without_dirtiness=", value)
78
89
  end
@@ -120,10 +131,10 @@ if defined?(ActiveRecord::Base)
120
131
  if match = /^(find|scoped)_(all_by|by)_([_a-zA-Z]\w*)$/.match(method.to_s)
121
132
  attribute_names = match.captures.last.split('_and_')
122
133
  attribute_names.each_with_index do |attribute, index|
123
- if attr_encrypted?(attribute) && encrypted_attributes[attribute.to_sym][:mode] == :single_iv_and_salt
134
+ if attr_encrypted?(attribute) && attr_encrypted_encrypted_attributes[attribute.to_sym][:mode] == :single_iv_and_salt
124
135
  args[index] = send("encrypt_#{attribute}", args[index])
125
136
  warn "DEPRECATION WARNING: This feature will be removed in the next major release."
126
- attribute_names[index] = encrypted_attributes[attribute.to_sym][:attribute]
137
+ attribute_names[index] = attr_encrypted_encrypted_attributes[attribute.to_sym][:attribute]
127
138
  end
128
139
  end
129
140
  method = "#{match.captures[0]}_#{match.captures[1]}_#{attribute_names.join('_and_')}".to_sym
@@ -134,6 +145,8 @@ if defined?(ActiveRecord::Base)
134
145
  end
135
146
  end
136
147
 
137
- ActiveRecord::Base.extend AttrEncrypted
138
- ActiveRecord::Base.extend AttrEncrypted::Adapters::ActiveRecord
148
+ ActiveSupport.on_load(:active_record) do
149
+ extend AttrEncrypted
150
+ extend AttrEncrypted::Adapters::ActiveRecord
151
+ end
139
152
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  if defined?(DataMapper)
2
4
  module AttrEncrypted
3
5
  module Adapters
@@ -19,4 +21,4 @@ if defined?(DataMapper)
19
21
  end
20
22
 
21
23
  DataMapper::Resource.extend AttrEncrypted::Adapters::DataMapper
22
- end
24
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  if defined?(Sequel)
2
4
  module AttrEncrypted
3
5
  module Adapters
@@ -11,4 +13,4 @@ if defined?(Sequel)
11
13
 
12
14
  Sequel::Model.extend AttrEncrypted
13
15
  Sequel::Model.extend AttrEncrypted::Adapters::Sequel
14
- end
16
+ end
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AttrEncrypted
2
4
  # Contains information about this gem's version
3
5
  module Version
4
- MAJOR = 3
5
- MINOR = 1
6
+ MAJOR = 4
7
+ MINOR = 0
6
8
  PATCH = 0
7
9
 
8
10
  # Returns a version string by joining <tt>MAJOR</tt>, <tt>MINOR</tt>, and <tt>PATCH</tt> with <tt>'.'</tt>
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'encryptor'
2
4
 
3
5
  # Adds attr_accessors that encrypt and decrypt an object's attributes
@@ -8,7 +10,7 @@ module AttrEncrypted
8
10
  base.class_eval do
9
11
  include InstanceMethods
10
12
  attr_writer :attr_encrypted_options
11
- @attr_encrypted_options, @encrypted_attributes = {}, {}
13
+ @attr_encrypted_options, @attr_encrypted_encrypted_attributes = {}, {}
12
14
  end
13
15
  end
14
16
 
@@ -158,11 +160,11 @@ module AttrEncrypted
158
160
  end
159
161
 
160
162
  define_method(attribute) do
161
- instance_variable_get("@#{attribute}") || instance_variable_set("@#{attribute}", decrypt(attribute, send(encrypted_attribute_name)))
163
+ instance_variable_get("@#{attribute}") || instance_variable_set("@#{attribute}", attr_encrypted_decrypt(attribute, send(encrypted_attribute_name)))
162
164
  end
163
165
 
164
166
  define_method("#{attribute}=") do |value|
165
- send("#{encrypted_attribute_name}=", encrypt(attribute, value))
167
+ send("#{encrypted_attribute_name}=", attr_encrypted_encrypt(attribute, value))
166
168
  instance_variable_set("@#{attribute}", value)
167
169
  end
168
170
 
@@ -171,7 +173,7 @@ module AttrEncrypted
171
173
  value.respond_to?(:empty?) ? !value.empty? : !!value
172
174
  end
173
175
 
174
- encrypted_attributes[attribute.to_sym] = options.merge(attribute: encrypted_attribute_name)
176
+ self.attr_encrypted_encrypted_attributes[attribute.to_sym] = options.merge(attribute: encrypted_attribute_name)
175
177
  end
176
178
  end
177
179
 
@@ -221,7 +223,7 @@ module AttrEncrypted
221
223
  # User.attr_encrypted?(:name) # false
222
224
  # User.attr_encrypted?(:email) # true
223
225
  def attr_encrypted?(attribute)
224
- encrypted_attributes.has_key?(attribute.to_sym)
226
+ attr_encrypted_encrypted_attributes.has_key?(attribute.to_sym)
225
227
  end
226
228
 
227
229
  # Decrypts a value for the attribute specified
@@ -232,9 +234,9 @@ module AttrEncrypted
232
234
  # attr_encrypted :email
233
235
  # end
234
236
  #
235
- # email = User.decrypt(:email, 'SOME_ENCRYPTED_EMAIL_STRING')
236
- def decrypt(attribute, encrypted_value, options = {})
237
- options = encrypted_attributes[attribute.to_sym].merge(options)
237
+ # email = User.attr_encrypted_decrypt(:email, 'SOME_ENCRYPTED_EMAIL_STRING')
238
+ def attr_encrypted_decrypt(attribute, encrypted_value, options = {})
239
+ options = attr_encrypted_encrypted_attributes[attribute.to_sym].merge(options)
238
240
  if options[:if] && !options[:unless] && not_empty?(encrypted_value)
239
241
  encrypted_value = encrypted_value.unpack(options[:encode]).first if options[:encode]
240
242
  value = options[:encryptor].send(options[:decrypt_method], options.merge!(value: encrypted_value))
@@ -258,9 +260,9 @@ module AttrEncrypted
258
260
  # attr_encrypted :email
259
261
  # end
260
262
  #
261
- # encrypted_email = User.encrypt(:email, 'test@example.com')
262
- def encrypt(attribute, value, options = {})
263
- options = encrypted_attributes[attribute.to_sym].merge(options)
263
+ # encrypted_email = User.attr_encrypted_encrypt(:email, 'test@example.com')
264
+ def attr_encrypted_encrypt(attribute, value, options = {})
265
+ options = attr_encrypted_encrypted_attributes[attribute.to_sym].merge(options)
264
266
  if options[:if] && !options[:unless] && (options[:allow_empty_value] || not_empty?(value))
265
267
  value = options[:marshal] ? options[:marshaler].send(options[:dump_method], value) : value.to_s
266
268
  encrypted_value = options[:encryptor].send(options[:encrypt_method], options.merge!(value: value))
@@ -284,9 +286,9 @@ module AttrEncrypted
284
286
  # attr_encrypted :email, key: 'my secret key'
285
287
  # end
286
288
  #
287
- # User.encrypted_attributes # { email: { attribute: 'encrypted_email', key: 'my secret key' } }
288
- def encrypted_attributes
289
- @encrypted_attributes ||= superclass.encrypted_attributes.dup
289
+ # User.attr_encrypted_encrypted_attributes # { email: { attribute: 'encrypted_email', key: 'my secret key' } }
290
+ def attr_encrypted_encrypted_attributes
291
+ @attr_encrypted_encrypted_attributes ||= superclass.attr_encrypted_encrypted_attributes.dup
290
292
  end
291
293
 
292
294
  # Forwards calls to :encrypt_#{attribute} or :decrypt_#{attribute} to the corresponding encrypt or decrypt method
@@ -301,7 +303,7 @@ module AttrEncrypted
301
303
  # User.encrypt_email('SOME_ENCRYPTED_EMAIL_STRING')
302
304
  def method_missing(method, *arguments, &block)
303
305
  if method.to_s =~ /^((en|de)crypt)_(.+)$/ && attr_encrypted?($3)
304
- send($1, $3, *arguments)
306
+ send("attr_encrypted_#{$1}", $3, *arguments)
305
307
  else
306
308
  super
307
309
  end
@@ -323,10 +325,10 @@ module AttrEncrypted
323
325
  #
324
326
  # @user = User.new('some-secret-key')
325
327
  # @user.decrypt(:email, 'SOME_ENCRYPTED_EMAIL_STRING')
326
- def decrypt(attribute, encrypted_value)
327
- encrypted_attributes[attribute.to_sym][:operation] = :decrypting
328
- encrypted_attributes[attribute.to_sym][:value_present] = self.class.not_empty?(encrypted_value)
329
- self.class.decrypt(attribute, encrypted_value, evaluated_attr_encrypted_options_for(attribute))
328
+ def attr_encrypted_decrypt(attribute, encrypted_value)
329
+ attr_encrypted_encrypted_attributes[attribute.to_sym][:operation] = :decrypting
330
+ attr_encrypted_encrypted_attributes[attribute.to_sym][:value_present] = self.class.not_empty?(encrypted_value)
331
+ self.class.attr_encrypted_decrypt(attribute, encrypted_value, evaluated_attr_encrypted_options_for(attribute))
330
332
  end
331
333
 
332
334
  # Encrypts a value for the attribute specified using options evaluated in the current object's scope
@@ -343,18 +345,22 @@ module AttrEncrypted
343
345
  # end
344
346
  #
345
347
  # @user = User.new('some-secret-key')
346
- # @user.encrypt(:email, 'test@example.com')
347
- def encrypt(attribute, value)
348
- encrypted_attributes[attribute.to_sym][:operation] = :encrypting
349
- encrypted_attributes[attribute.to_sym][:value_present] = self.class.not_empty?(value)
350
- self.class.encrypt(attribute, value, evaluated_attr_encrypted_options_for(attribute))
348
+ # @user.attr_encrypted_encrypt(:email, 'test@example.com')
349
+ def attr_encrypted_encrypt(attribute, value)
350
+ attr_encrypted_encrypted_attributes[attribute.to_sym][:operation] = :encrypting
351
+ attr_encrypted_encrypted_attributes[attribute.to_sym][:value_present] = self.class.not_empty?(value)
352
+ self.class.attr_encrypted_encrypt(attribute, value, evaluated_attr_encrypted_options_for(attribute))
351
353
  end
352
354
 
353
355
  # Copies the class level hash of encrypted attributes with virtual attribute names as keys
354
356
  # and their corresponding options as values to the instance
355
357
  #
356
- def encrypted_attributes
357
- @encrypted_attributes ||= self.class.encrypted_attributes.dup
358
+ def attr_encrypted_encrypted_attributes
359
+ @attr_encrypted_encrypted_attributes ||= begin
360
+ duplicated= {}
361
+ self.class.attr_encrypted_encrypted_attributes.map { |key, value| duplicated[key] = value.dup }
362
+ duplicated
363
+ end
358
364
  end
359
365
 
360
366
  protected
@@ -362,15 +368,21 @@ module AttrEncrypted
362
368
  # Returns attr_encrypted options evaluated in the current object's scope for the attribute specified
363
369
  def evaluated_attr_encrypted_options_for(attribute)
364
370
  evaluated_options = Hash.new
365
- attribute_option_value = encrypted_attributes[attribute.to_sym][:attribute]
366
- encrypted_attributes[attribute.to_sym].map do |option, value|
367
- evaluated_options[option] = evaluate_attr_encrypted_option(value)
371
+ attributes = attr_encrypted_encrypted_attributes[attribute.to_sym]
372
+ attribute_option_value = attributes[:attribute]
373
+
374
+ [:if, :unless, :value_present, :allow_empty_value].each do |option|
375
+ evaluated_options[option] = evaluate_attr_encrypted_option(attributes[option])
368
376
  end
369
377
 
370
378
  evaluated_options[:attribute] = attribute_option_value
371
379
 
372
380
  evaluated_options.tap do |options|
373
381
  if options[:if] && !options[:unless] && options[:value_present] || options[:allow_empty_value]
382
+ (attributes.keys - evaluated_options.keys).each do |option|
383
+ options[option] = evaluate_attr_encrypted_option(attributes[option])
384
+ end
385
+
374
386
  unless options[:mode] == :single_iv_and_salt
375
387
  load_iv_for_attribute(attribute, options)
376
388
  end