Skip to content

Commit

Permalink
Perform constant time string comparison when validating signatures
Browse files Browse the repository at this point in the history
String#== is not safe for the purposes of validating crytographic
signatures because it enables timing attacks.
  • Loading branch information
stevegraham committed Dec 29, 2014
1 parent 462a81c commit a8f9c92
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/signature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,18 @@ def validate_timestamp!(grace)
end

def validate_signature!(token)
unless @auth_hash["auth_signature"] == signature(token)
unless identical? @auth_hash["auth_signature"], signature(token)
raise AuthenticationError, "Invalid signature: you should have "\
"sent HmacSHA256Hex(#{string_to_sign.inspect}, your_secret_key)"\
", but you sent #{@auth_hash["auth_signature"].inspect}"
end
return true
end

# Constant time string comparison
def identical?(a, b)
return false unless a.bytesize == b.bytesize
a.bytes.zip(b.bytes).reduce(0) { |memo, (a, b)| memo += a ^ b } == 0
end
end
end

0 comments on commit a8f9c92

Please sign in to comment.