Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/line/bot/v2/webhook_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,23 @@ def parse(body, signature)

def verify_signature(body:, signature:)
hash = OpenSSL::HMAC.digest(OpenSSL::Digest.new('SHA256'), @channel_secret, body)
signature == Base64.strict_encode64(hash.to_s)
expected = Base64.strict_encode64(hash)
variable_secure_compare(signature, expected)
end

# To avoid timing attacks
def variable_secure_compare(a, b)
secure_compare(::Digest::SHA256.hexdigest(a), ::Digest::SHA256.hexdigest(b))
end

def secure_compare(a, b)
return false unless a.bytesize == b.bytesize

l = a.unpack("C#{a.bytesize}")

res = 0
b.each_byte { |byte| res |= byte ^ l.shift }
res == 0
end

def create_instance(klass, attributes)
Expand Down
4 changes: 4 additions & 0 deletions sig/line/bot/v2/webhook_parser.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module Line

private

def variable_secure_compare: (a: String, b: String) -> bool

def secure_compare: (a: String, b: String) -> bool

def verify_signature: (body: String, signature: String) -> bool

def create_instance: (untyped klass, Hash[Symbol, untyped] attributes) -> untyped
Expand Down