@@ -19,7 +19,7 @@ def load(value)
19
19
20
20
def setup
21
21
@verifier = ActiveSupport ::MessageVerifier . new ( "Hey, I'm a secret!" )
22
- @data = { some : "data" , now : Time . utc ( 2010 ) }
22
+ @data = { " some" => "data" , " now" => Time . utc ( 2010 ) }
23
23
@secret = SecureRandom . random_bytes ( 32 )
24
24
end
25
25
@@ -70,38 +70,13 @@ def test_verify_with_parse_json_times
70
70
ActiveSupport . parse_json_times , Time . zone = previous
71
71
end
72
72
73
- def test_raise_error_when_argument_class_is_not_loaded
74
- # To generate the valid message below:
75
- #
76
- # AutoloadClass = Struct.new(:foo)
77
- # valid_message = @verifier.generate(foo: AutoloadClass.new('foo'))
78
- #
79
- valid_message = "BAh7BjoIZm9vbzonTWVzc2FnZVZlcmlmaWVyVGVzdDo6QXV0b2xvYWRDbGFzcwY6CUBmb29JIghmb28GOgZFVA==--f3ef39a5241c365083770566dc7a9eb5d6ace914"
80
- exception = assert_raise ( ArgumentError , NameError ) do
81
- @verifier . verified ( valid_message )
82
- end
83
- assert_includes [ "uninitialized constant MessageVerifierTest::AutoloadClass" ,
84
- "undefined class/module MessageVerifierTest::AutoloadClass" ] , exception . message
85
- exception = assert_raise ( ArgumentError , NameError ) do
86
- @verifier . verify ( valid_message )
87
- end
88
- assert_includes [ "uninitialized constant MessageVerifierTest::AutoloadClass" ,
89
- "undefined class/module MessageVerifierTest::AutoloadClass" ] , exception . message
90
- end
91
-
92
73
def test_raise_error_when_secret_is_nil
93
74
exception = assert_raise ( ArgumentError ) do
94
75
ActiveSupport ::MessageVerifier . new ( nil )
95
76
end
96
77
assert_equal "Secret should not be nil." , exception . message
97
78
end
98
79
99
- def test_backward_compatibility_messages_signed_without_metadata
100
- signed_message = "BAh7BzoJc29tZUkiCWRhdGEGOgZFVDoIbm93SXU6CVRpbWUNIIAbgAAAAAAHOgtvZmZzZXRpADoJem9uZUkiCFVUQwY7BkY=--d03c52c91dfe4ccc5159417c660461bcce005e96"
101
- assert_equal @data , @verifier . verify ( signed_message )
102
- end
103
-
104
-
105
80
def test_rotating_secret
106
81
old_message = ActiveSupport ::MessageVerifier . new ( "old" , digest : "SHA1" ) . generate ( "old" )
107
82
@@ -124,6 +99,35 @@ def test_multiple_rotations
124
99
assert_equal "older" , verifier . verified ( older_message )
125
100
end
126
101
102
+ def test_rotations_with_metadata
103
+ old_message = ActiveSupport ::MessageVerifier . new ( "old" ) . generate ( "old" , purpose : :rotation )
104
+
105
+ verifier = ActiveSupport ::MessageVerifier . new ( @secret )
106
+ verifier . rotate "old"
107
+
108
+ assert_equal "old" , verifier . verified ( old_message , purpose : :rotation )
109
+ end
110
+ end
111
+
112
+ class DefaultMarshalSerializerMessageVerifierTest < MessageVerifierTest
113
+ def setup
114
+ @default_verifier = ActiveSupport ::MessageVerifier . default_message_verifier_serializer
115
+ ActiveSupport ::MessageVerifier . default_message_verifier_serializer = :marshal
116
+
117
+ @verifier = ActiveSupport ::MessageVerifier . new ( "Hey, I'm a secret!" )
118
+ @data = { some : "data" , now : Time . utc ( 2010 ) }
119
+ @secret = SecureRandom . random_bytes ( 32 )
120
+ end
121
+
122
+ def teardown
123
+ ActiveSupport ::MessageVerifier . default_message_verifier_serializer = @default_verifier
124
+ end
125
+
126
+ def test_backward_compatibility_messages_signed_without_metadata
127
+ signed_message = "BAh7BzoJc29tZUkiCWRhdGEGOgZFVDoIbm93SXU6CVRpbWUNIIAbgAAAAAAHOgtvZmZzZXRpADoJem9uZUkiCFVUQwY7BkY=--d03c52c91dfe4ccc5159417c660461bcce005e96"
128
+ assert_equal @data , @verifier . verify ( signed_message )
129
+ end
130
+
127
131
def test_on_rotation_is_called_and_verified_returns_message
128
132
older_message = ActiveSupport ::MessageVerifier . new ( "older" , digest : "SHA1" ) . generate ( { encoded : "message" } )
129
133
@@ -138,13 +142,127 @@ def test_on_rotation_is_called_and_verified_returns_message
138
142
assert rotated
139
143
end
140
144
141
- def test_rotations_with_metadata
142
- old_message = ActiveSupport ::MessageVerifier . new ( "old" ) . generate ( "old" , purpose : :rotation )
145
+ def test_raise_error_when_argument_class_is_not_loaded
146
+ # To generate the valid message below:
147
+ #
148
+ # AutoloadClass = Struct.new(:foo)
149
+ # valid_message = @verifier.generate(foo: AutoloadClass.new('foo'))
150
+ #
151
+ valid_message = "BAh7BjoIZm9vbzonTWVzc2FnZVZlcmlmaWVyVGVzdDo6QXV0b2xvYWRDbGFzcwY6CUBmb29JIghmb28GOgZFVA==--f3ef39a5241c365083770566dc7a9eb5d6ace914"
152
+ exception = assert_raise ( ArgumentError , NameError ) do
153
+ @verifier . verified ( valid_message )
154
+ end
155
+ assert_includes [ "uninitialized constant MessageVerifierTest::AutoloadClass" ,
156
+ "undefined class/module MessageVerifierTest::AutoloadClass" ] , exception . message
157
+ exception = assert_raise ( ArgumentError , NameError ) do
158
+ @verifier . verify ( valid_message )
159
+ end
160
+ assert_includes [ "uninitialized constant MessageVerifierTest::AutoloadClass" ,
161
+ "undefined class/module MessageVerifierTest::AutoloadClass" ] , exception . message
162
+ end
163
+ end
143
164
144
- verifier = ActiveSupport ::MessageVerifier . new ( @secret )
145
- verifier . rotate "old"
165
+ class MarshalSerializeAndFallbackMessageVerifierTest < DefaultMarshalSerializerMessageVerifierTest
166
+ def setup
167
+ @default_verifier = ActiveSupport ::MessageVerifier . default_message_verifier_serializer
168
+ @default_use_marshal = ActiveSupport ::JsonWithMarshalFallback . use_marshal_serialization
169
+ @default_fallback = ActiveSupport ::JsonWithMarshalFallback . fallback_to_marshal_deserialization
170
+ ActiveSupport ::MessageVerifier . default_message_verifier_serializer = :hybrid
171
+ ActiveSupport ::JsonWithMarshalFallback . use_marshal_serialization = true
172
+ ActiveSupport ::JsonWithMarshalFallback . fallback_to_marshal_deserialization = true
146
173
147
- assert_equal "old" , verifier . verified ( old_message , purpose : :rotation )
174
+ @verifier = ActiveSupport ::MessageVerifier . new ( "Hey, I'm a secret!" )
175
+ @data = { some : "data" , now : Time . utc ( 2010 ) }
176
+ @secret = SecureRandom . random_bytes ( 32 )
177
+ end
178
+
179
+ def teardown
180
+ ActiveSupport ::MessageVerifier . default_message_verifier_serializer = @default_verifier
181
+ ActiveSupport ::JsonWithMarshalFallback . use_marshal_serialization = @default_use_marshal
182
+ ActiveSupport ::JsonWithMarshalFallback . fallback_to_marshal_deserialization = @default_fallback
183
+ end
184
+ end
185
+
186
+ class JsonSerializeMarshalFallbackMessageVerifierTest < MessageVerifierTest
187
+ def setup
188
+ @default_verifier = ActiveSupport ::MessageVerifier . default_message_verifier_serializer
189
+ @default_use_marshal = ActiveSupport ::JsonWithMarshalFallback . use_marshal_serialization
190
+ @default_fallback = ActiveSupport ::JsonWithMarshalFallback . fallback_to_marshal_deserialization
191
+ ActiveSupport ::MessageVerifier . default_message_verifier_serializer = :hybrid
192
+ ActiveSupport ::JsonWithMarshalFallback . use_marshal_serialization = false
193
+ ActiveSupport ::JsonWithMarshalFallback . fallback_to_marshal_deserialization = true
194
+
195
+ @verifier = ActiveSupport ::MessageVerifier . new ( "Hey, I'm a secret!" )
196
+ @data = { "some" => "data" , "now" => Time . utc ( 2010 ) }
197
+ @secret = SecureRandom . random_bytes ( 32 )
198
+ end
199
+
200
+ def teardown
201
+ ActiveSupport ::MessageVerifier . default_message_verifier_serializer = @default_verifier
202
+ ActiveSupport ::JsonWithMarshalFallback . use_marshal_serialization = @default_use_marshal
203
+ ActiveSupport ::JsonWithMarshalFallback . fallback_to_marshal_deserialization = @default_fallback
204
+ end
205
+
206
+ def test_on_rotation_is_called_and_verified_returns_message
207
+ older_message = ActiveSupport ::MessageVerifier . new ( "older" , digest : "SHA1" ) . generate ( { encoded : "message" } )
208
+
209
+ verifier = ActiveSupport ::MessageVerifier . new ( @secret , digest : "SHA512" )
210
+ verifier . rotate "old" , digest : "SHA256"
211
+ verifier . rotate "older" , digest : "SHA1"
212
+
213
+ rotated = false
214
+ message = verifier . verified ( older_message , on_rotation : proc { rotated = true } )
215
+
216
+ assert_equal ( { "encoded" => "message" } , message )
217
+ assert rotated
218
+ end
219
+
220
+ def test_backward_compatibility_messages_signed_marshal_serialized
221
+ marshal_serialized_signed_message = "BAh7B0kiCXNvbWUGOgZFVEkiCWRhdGEGOwBUSSIIbm93BjsAVEl1OglUaW1lDSCAG8AAAAAABjoJem9uZUkiCFVUQwY7AEY=--ae7480422168507f4a8aec6b1d68bfdfd5c6ef48"
222
+ assert_equal @data , @verifier . verify ( marshal_serialized_signed_message )
223
+ end
224
+ end
225
+
226
+ class JsonSerializeAndNoFallbackMessageVerifierTest < JsonSerializeMarshalFallbackMessageVerifierTest
227
+ def setup
228
+ @default_verifier = ActiveSupport ::MessageVerifier . default_message_verifier_serializer
229
+ @default_use_marshal = ActiveSupport ::JsonWithMarshalFallback . use_marshal_serialization
230
+ @default_fallback = ActiveSupport ::JsonWithMarshalFallback . fallback_to_marshal_deserialization
231
+ ActiveSupport ::MessageVerifier . default_message_verifier_serializer = :hybrid
232
+ ActiveSupport ::JsonWithMarshalFallback . use_marshal_serialization = false
233
+ ActiveSupport ::JsonWithMarshalFallback . fallback_to_marshal_deserialization = false
234
+
235
+ @verifier = ActiveSupport ::MessageVerifier . new ( "Hey, I'm a secret!" )
236
+ @data = { "some" => "data" , "now" => Time . utc ( 2010 ) }
237
+ @secret = SecureRandom . random_bytes ( 32 )
238
+ end
239
+
240
+ def teardown
241
+ ActiveSupport ::MessageVerifier . default_message_verifier_serializer = @default_verifier
242
+ ActiveSupport ::JsonWithMarshalFallback . use_marshal_serialization = @default_use_marshal
243
+ ActiveSupport ::JsonWithMarshalFallback . fallback_to_marshal_deserialization = @default_fallback
244
+ end
245
+
246
+ def test_backward_compatibility_messages_signed_marshal_serialized
247
+ marshal_serialized_signed_message = "BAh7B0kiCXNvbWUGOgZFVEkiCWRhdGEGOwBUSSIIbm93BjsAVEl1OglUaW1lDSCAG8AAAAAABjoJem9uZUkiCFVUQwY7AEY=--ae7480422168507f4a8aec6b1d68bfdfd5c6ef48"
248
+ assert_raise ( JSON ::ParserError ) do
249
+ @verifier . verify ( marshal_serialized_signed_message )
250
+ end
251
+ end
252
+ end
253
+
254
+ class DefaultJsonSerializerMessageVerifierTest < JsonSerializeAndNoFallbackMessageVerifierTest
255
+ def setup
256
+ @default_verifier = ActiveSupport ::MessageVerifier . default_message_verifier_serializer
257
+ ActiveSupport ::MessageVerifier . default_message_verifier_serializer = :json
258
+
259
+ @verifier = ActiveSupport ::MessageVerifier . new ( "Hey, I'm a secret!" )
260
+ @data = { "some" => "data" , "now" => Time . utc ( 2010 ) }
261
+ @secret = SecureRandom . random_bytes ( 32 )
262
+ end
263
+
264
+ def teardown
265
+ ActiveSupport ::MessageVerifier . default_message_verifier_serializer = @default_verifier
148
266
end
149
267
end
150
268
@@ -199,7 +317,22 @@ def verifier_options
199
317
end
200
318
end
201
319
202
- class MessageVerifierMetadataJSONTest < MessageVerifierMetadataTest
320
+ class MessageVerifierMetadataJsonWithMarshalFallbackTest < MessageVerifierMetadataTest
321
+ private
322
+ def verifier_options
323
+ { serializer : ActiveSupport ::JsonWithMarshalFallback }
324
+ end
325
+ end
326
+
327
+ class MessageVerifierMetadataJsonTest < MessageVerifierMetadataTest
328
+ private
329
+ def verifier_options
330
+ { serializer : JSON }
331
+ end
332
+ end
333
+
334
+
335
+ class MessageVerifierMetadataCustomJSONTest < MessageVerifierMetadataTest
203
336
private
204
337
def verifier_options
205
338
{ serializer : MessageVerifierTest ::JSONSerializer . new }
0 commit comments