@@ -71,14 +71,14 @@ def decode_signature(signature)
71
71
72
72
def validate_url ( url , url_hash )
73
73
expected_url_hash = Digest ::SHA256 . hexdigest url
74
- unless JWT :: SecurityUtils . secure_compare ( expected_url_hash , url_hash )
74
+ unless secure_compare ( expected_url_hash , url_hash )
75
75
raise ValidationError , 'invalid jwt: claim url_hash is invalid'
76
76
end
77
77
end
78
78
79
79
def validate_payload ( body , payload_hash )
80
80
if !body . to_s . empty? && !payload_hash . to_s . empty?
81
- unless JWT :: SecurityUtils . secure_compare ( Digest ::SHA256 . hexdigest ( body ) , payload_hash )
81
+ unless secure_compare ( Digest ::SHA256 . hexdigest ( body ) , payload_hash )
82
82
raise ValidationError , 'invalid jwt: claim payload_hash is invalid'
83
83
end
84
84
elsif !body . to_s . empty?
@@ -87,5 +87,15 @@ def validate_payload(body, payload_hash)
87
87
raise ValidationError , 'invalid jwt: claim payload_hash is set but actual payload is missing'
88
88
end
89
89
end
90
+
91
+ # Adaption of https://github.com/rails/rails/blob/cf6ff17e9a3c6c1139040b519a341f55f0be16cf/activesupport/lib/active_support/security_utils.rb#L33
92
+ # Copied here so as to avoid adding a dependency on ActiveSupport to this gem
93
+ #
94
+ # Note that unlike `fixed_length_secure_compare` in the above url we don't fall back to a custom implementation
95
+ # of fixed_length_secure_compare, since OpenSSL.fixed_length_secure_compare is present in OpenSSL 2.2
96
+ # https://github.com/ruby/openssl/blob/master/History.md#version-220 which is included in Ruby 3.0 and above
97
+ def secure_compare ( first , second )
98
+ first . bytesize == second . bytesize && OpenSSL . fixed_length_secure_compare ( first , second )
99
+ end
90
100
end
91
101
end
0 commit comments