Skip to content

Commit 2aafac4

Browse files
committed
Use secure_compare during signature verification
1 parent f2423d4 commit 2aafac4

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

lib/slack/events/request.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def valid?
5959
signature_basestring = [version, timestamp, body].join(':')
6060
hex_hash = OpenSSL::HMAC.hexdigest(digest, signing_secret, signature_basestring)
6161
computed_signature = [version, hex_hash].join('=')
62-
computed_signature == signature
62+
secure_compare(computed_signature, signature)
6363
end
6464

6565
# Validates the request signature and its expiration.
@@ -69,6 +69,19 @@ def verify!
6969

7070
true
7171
end
72+
73+
private
74+
75+
def secure_compare(computed_signature, signature)
76+
return false if computed_signature.bytesize != signature.bytesize
77+
78+
l = computed_signature.unpack "C#{computed_signature.bytesize}"
79+
80+
result = 0
81+
signature.each_byte { |byte| result |= byte ^ l.shift }
82+
83+
result.zero?
84+
end
7285
end
7386
end
7487
end

0 commit comments

Comments
 (0)