File tree Expand file tree Collapse file tree 3 files changed +34
-1
lines changed
lib/active_record/encryption Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Original file line number Diff line number Diff line change
1
+ * Add an option to ` ActiveRecord::Encryption::Encryptor ` to disable compression
2
+
3
+ Allow compression to be disabled by setting ` compress: false `
4
+
5
+ ``` ruby
6
+ class User
7
+ encrypts :name , encryptor: ActiveRecord ::Encryption ::Encryptor .new (compress: false )
8
+ end
9
+ ```
10
+
11
+ * Donal McBreen *
12
+
1
13
* Deprecate passing strings to ` ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename` .
2
14
3
15
A ` ActiveRecord::DatabaseConfigurations::DatabaseConfig` object should be passed instead.
Original file line number Diff line number Diff line change @@ -12,6 +12,14 @@ module Encryption
12
12
# It interacts with a KeyProvider for getting the keys, and delegate to
13
13
# ActiveRecord::Encryption::Cipher the actual encryption algorithm.
14
14
class Encryptor
15
+ # === Options
16
+ #
17
+ # * <tt>:compress</tt> - Boolean indicating whether records should be compressed before encryption.
18
+ # Defaults to +true+.
19
+ def initialize ( compress : true )
20
+ @compress = compress
21
+ end
22
+
15
23
# Encrypts +clean_text+ and returns the encrypted result
16
24
#
17
25
# Internally, it will:
@@ -111,13 +119,17 @@ def serializer
111
119
112
120
# Under certain threshold, ZIP compression is actually worse that not compressing
113
121
def compress_if_worth_it ( string )
114
- if string . bytesize > THRESHOLD_TO_JUSTIFY_COMPRESSION
122
+ if compress? && string . bytesize > THRESHOLD_TO_JUSTIFY_COMPRESSION
115
123
[ compress ( string ) , true ]
116
124
else
117
125
[ string , false ]
118
126
end
119
127
end
120
128
129
+ def compress?
130
+ @compress
131
+ end
132
+
121
133
def compress ( data )
122
134
Zlib ::Deflate . deflate ( data ) . tap do |compressed_data |
123
135
compressed_data . force_encoding ( data . encoding )
Original file line number Diff line number Diff line change @@ -48,6 +48,15 @@ class ActiveRecord::Encryption::EncryptorTest < ActiveRecord::EncryptionTestCase
48
48
assert cipher_text . bytesize < content . bytesize
49
49
end
50
50
51
+ test "content is not compressed, when disabled" do
52
+ @encryptor = ActiveRecord ::Encryption ::Encryptor . new ( compress : false )
53
+ content = SecureRandom . hex ( 5 . kilobytes )
54
+ cipher_text = @encryptor . encrypt ( content )
55
+
56
+ assert_encrypt_text content
57
+ assert cipher_text . bytesize > content . bytesize
58
+ end
59
+
51
60
test "trying to encrypt custom classes raises a ForbiddenClass exception" do
52
61
assert_raises ActiveRecord ::Encryption ::Errors ::ForbiddenClass do
53
62
@encryptor . encrypt ( Struct . new ( :name ) . new ( "Jorge" ) )
You can’t perform that action at this time.
0 commit comments