Commit 4faaad06 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Make attr_encrypted thread-safe

We ran into issues when encrypting in multiple threads because
attr_encrypted instances were sharing one encrypted_attributes
hash.

This was fixed in https://github.com/attr-encrypted/attr_encrypted/commit/d4ca0e2073ca6ba5035997ce25f7fc0b4bfbe39e
but a new version wasn't released. So we need to patch it
ourselves here.
parent e178d5e5
# frozen_string_literal: true
# As of v3.1.0, attr_encrypted is not thread-safe because all instances share the same `encrypted_attributes`
# This was fixed in https://github.com/attr-encrypted/attr_encrypted/commit/d4ca0e2073ca6ba5035997ce25f7fc0b4bfbe39e
# but no release was made after that so we have to patch it ourselves here
module AttrEncrypted
module InstanceMethods
def encrypted_attributes
@encrypted_attributes ||= begin
duplicated = {}
self.class.encrypted_attributes.map { |key, value| duplicated[key] = value.dup }
duplicated
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe AttrEncrypted do
describe '#encrypted_attributes' do
subject do
Class.new(ActiveRecord::Base) do
self.table_name = 'projects'
attr_accessor :encrypted_foo
attr_accessor :encrypted_foo_iv
attr_encrypted :foo, key: 'This is a key that is 256 bits!!'
end
end
it 'does not share state with other instances' do
instance = subject.new
instance.foo = 'bar'
another_instance = subject.new
expect(instance.encrypted_attributes[:foo][:operation]).to eq(:encrypting)
expect(another_instance.encrypted_attributes[:foo][:operation]).to be_nil
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment