ankane / lockbox

Modern encryption for Ruby and Rails
MIT License
1.45k stars 68 forks source link

Unable to rotate encryption for fields having custom key_table #180

Closed kmmndr closed 1 year ago

kmmndr commented 1 year ago

Hi @ankane :-)

Thank you very much for Lockbox, it is a great gem !

I'm trying to rotate encryption key. But it raises exception for custom fields having key_table attribute.

How to reproduce:

Set a previous_key

# lockbox config
Lockbox.master_key = new_key
Lockbox.default_options[:previous_versions] = [{master_key: previous_key}]

# models
class Message
  has_encrypted :description
end

class Event
  has_encrypted :description, key_table: 'messages'
end

In this example Messages descriptions are readable but accessing Events descriptions raise Lockbox::DecryptionError: Decryption failed error.

Message.last.description
=> "..."

Event.last.description
Lockbox::DecryptionError: Decryption failed                                                                                                                                  
ankane commented 1 year ago

Hi @kmmndr, key_table needs to be passed to previous_versions as well.

class Event
  has_encrypted :description, key_table: 'messages', previous_versions:  [{master_key: previous_key, key_table: 'messages'}]
end

It should probably use the top-level value if none is specified, but that'll be a breaking change, so would need to wait until 2.0.

ankane commented 1 year ago

Added to the 2.0 plan (#155).