File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -41,7 +41,23 @@ def parse(body, signature)
4141
4242 def verify_signature ( body :, signature :)
4343 hash = OpenSSL ::HMAC . digest ( OpenSSL ::Digest . new ( 'SHA256' ) , @channel_secret , body )
44- signature == Base64 . strict_encode64 ( hash . to_s )
44+ expected = Base64 . strict_encode64 ( hash )
45+ variable_secure_compare ( signature , expected )
46+ end
47+
48+ # To avoid timing attacks
49+ def variable_secure_compare ( a , b )
50+ secure_compare ( ::Digest ::SHA256 . hexdigest ( a ) , ::Digest ::SHA256 . hexdigest ( b ) )
51+ end
52+
53+ def secure_compare ( a , b )
54+ return false unless a . bytesize == b . bytesize
55+
56+ l = a . unpack ( "C#{ a . bytesize } " )
57+
58+ res = 0
59+ b . each_byte { |byte | res |= byte ^ l . shift }
60+ res == 0
4561 end
4662
4763 def create_instance ( klass , attributes )
Original file line number Diff line number Diff line change @@ -8,6 +8,10 @@ module Line
88
99 private
1010
11+ def variable_secure_compare : (a: String, b: String) -> bool
12+
13+ def secure_compare : (a: String, b: String) -> bool
14+
1115 def verify_signature : (body: String, signature: String) -> bool
1216
1317 def create_instance : (untyped klass, Hash[Symbol, untyped ] attributes) -> untyped
You can’t perform that action at this time.
0 commit comments