提交 5e6dab8b 编写于 作者: C Coda Hale 提交者: Michael Koziarski

Fix timing attack vulnerability in ActiveSupport::MessageVerifier.

Use a constant-time comparison algorithm to compare the candidate HMAC with the calculated HMAC to prevent leaking information about the calculated HMAC.
Signed-off-by: NMichael Koziarski <michael@koziarski.com>
上级 bd97c304
......@@ -25,10 +25,10 @@ def initialize(secret, digest = 'SHA1')
def verify(signed_message)
data, digest = signed_message.split("--")
if digest != generate_digest(data)
raise InvalidSignature
else
if secure_compare(digest, generate_digest(data))
Marshal.load(ActiveSupport::Base64.decode64(data))
else
raise InvalidSignature
end
end
......@@ -38,6 +38,19 @@ def generate(value)
end
private
# constant-time comparison algorithm to prevent timing attacks
def secure_compare(a, b)
if a.length == b.length
result = 0
for i in 0..(a.length - 1)
result |= a[i] ^ b[i]
end
result == 0
else
false
end
end
def generate_digest(data)
require 'openssl' unless defined?(OpenSSL)
OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new(@digest), @secret, data)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册