Skip to content

Commit

Permalink
Revert "Encryption casting with encrypts before serialize"
Browse files Browse the repository at this point in the history
This reverts commit 4dd2b22.
  • Loading branch information
djmb committed Aug 23, 2024
1 parent affab09 commit c26e47a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def cast(value)
end

def deserialize(value)
decrypt(cast_type.deserialize(value))
cast_type.deserialize decrypt(value)
end

def serialize(value)
Expand Down Expand Up @@ -81,7 +81,7 @@ def previous_type?
@previous_type
end

def decrypt(value)
def decrypt_as_text(value)
with_context do
unless value.nil?
if @default && @default == value
Expand All @@ -99,6 +99,10 @@ def decrypt(value)
end
end

def decrypt(value)
text_to_database_type decrypt_as_text(value)
end

def try_to_deserialize_with_previous_encrypted_types(value)
previous_types.each.with_index do |type, index|
break type.deserialize(value)
Expand All @@ -124,11 +128,12 @@ def serialize_with_oldest(value)
end

def serialize_with_current(value)
value = value&.downcase if downcase?
cast_type.serialize(encrypt(value.to_s)) unless value.nil?
casted_value = cast_type.serialize(value)
casted_value = casted_value&.downcase if downcase?
encrypt(casted_value.to_s) unless casted_value.nil?
end

def encrypt(value)
def encrypt_as_text(value)
with_context do
if encryptor.binary? && !cast_type.binary?
raise Errors::Encoding, "Binary encoded data can only be stored in binary columns"
Expand All @@ -138,6 +143,10 @@ def encrypt(value)
end
end

def encrypt(value)
text_to_database_type encrypt_as_text(value)
end

def encryptor
ActiveRecord::Encryption.encryptor
end
Expand All @@ -153,6 +162,14 @@ def decryption_options
def clean_text_scheme
@clean_text_scheme ||= ActiveRecord::Encryption::Scheme.new(downcase: downcase?, encryptor: ActiveRecord::Encryption::NullEncryptor.new)
end

def text_to_database_type(value)
if value && cast_type.binary?
ActiveModel::Type::Binary::Data.new(value)
else
value
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@
require "models/book_encrypted"
require "active_record/encryption/message_pack_message_serializer"

class ActiveRecord::Encryption::EncryptableRecordMessagePackSerializedTest < ActiveRecord::EncryptionTestCase
class ActiveRecord::Encryption::EncryptableRecordTest < ActiveRecord::EncryptionTestCase
fixtures :encrypted_books

test "binary data can be serialized with message pack" do
all_bytes = (0..255).map(&:chr).join
book = EncryptedBookWithBinaryMessagePackSerialized.create!(logo: all_bytes)
assert_encrypted_attribute(book, :logo, all_bytes)
assert_equal all_bytes, EncryptedBookWithBinaryMessagePackSerialized.create!(logo: all_bytes).logo
end

test "binary data can be encrypted uncompressed and serialized with message pack" do
# Strings below 140 bytes are not compressed
low_bytes = (0..127).map(&:chr).join
high_bytes = (128..255).map(&:chr).join

assert_encrypted_attribute(EncryptedBookWithBinaryMessagePackSerialized.create!(logo: low_bytes), :logo, low_bytes)
assert_encrypted_attribute(EncryptedBookWithBinaryMessagePackSerialized.create!(logo: high_bytes), :logo, high_bytes)
assert_equal low_bytes, EncryptedBookWithBinaryMessagePackSerialized.create!(logo: low_bytes).logo
assert_equal high_bytes, EncryptedBookWithBinaryMessagePackSerialized.create!(logo: high_bytes).logo
end

test "text columns cannot be serialized with message pack" do
Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/encryption/encryptable_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,13 +404,13 @@ def name
test "binary data can be encrypted uncompressed" do
low_bytes = (0..127).map(&:chr).join
high_bytes = (128..255).map(&:chr).join
assert_encrypted_attribute EncryptedBookWithBinary.create!(logo: low_bytes), :logo, low_bytes
assert_encrypted_attribute EncryptedBookWithBinary.create!(logo: high_bytes), :logo, high_bytes
assert_equal low_bytes, EncryptedBookWithBinary.create!(logo: low_bytes).logo
assert_equal high_bytes, EncryptedBookWithBinary.create!(logo: high_bytes).logo
end

test "serialized binary data can be encrypted" do
json_bytes = (32..127).map(&:chr)
assert_encrypted_attribute EncryptedBookWithSerializedBinary.create!(logo: json_bytes), :logo, json_bytes
assert_equal json_bytes, EncryptedBookWithSerializedBinary.create!(logo: json_bytes).logo
end

test "can compress data with custom compressor" do
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/models/book_encrypted.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ class EncryptedBookWithBinary < ActiveRecord::Base
class EncryptedBookWithSerializedBinary < ActiveRecord::Base
self.table_name = "encrypted_books"

encrypts :logo
serialize :logo, coder: JSON
encrypts :logo
end

class EncryptedBookWithCustomCompressor < ActiveRecord::Base
Expand Down
14 changes: 3 additions & 11 deletions activerecord/test/models/traffic_light_encrypted.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@

require "models/traffic_light"

class EncryptedTrafficLight < ActiveRecord::Base
self.table_name = "traffic_lights"

class EncryptedTrafficLight < TrafficLight
encrypts :state
serialize :state, type: Array
serialize :long_state, type: Array
end

class EncryptedTrafficLightWithStoreState < ActiveRecord::Base
self.table_name = "traffic_lights"

encrypts :state
serialize :state, type: Array
serialize :long_state, type: Array
class EncryptedTrafficLightWithStoreState < TrafficLight
store :state, accessors: %i[ color ], coder: ActiveRecord::Coders::JSON
encrypts :state
end

0 comments on commit c26e47a

Please sign in to comment.