|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "base16" |
| 4 | +require "openssl" |
| 5 | + |
| 6 | +module DuffelAPI |
| 7 | + module WebhookEvent |
| 8 | + # An exception raised internally within the library - but not exposed - if the |
| 9 | + # webhook signature provided does not match the format of a valid signature |
| 10 | + class InvalidRequestSignatureError < StandardError; end |
| 11 | + |
| 12 | + SIGNATURE_REGEXP = /\At=(.+),v1=(.+)\z/.freeze |
| 13 | + |
| 14 | + SHA_256 = OpenSSL::Digest.new("sha256") |
| 15 | + |
| 16 | + class << self |
| 17 | + # Checks if a webhook event you received was a genuine webhook event from Duffel by |
| 18 | + # checking that it was signed with your shared secret. |
| 19 | + # |
| 20 | + # Assuming that you've kept that secret secure and only shared it with Duffel, |
| 21 | + # this can give you confidence that a webhook event was genuinely sent by Duffel. |
| 22 | + # |
| 23 | + # @param request_body [String] The raw body of the received request |
| 24 | + # @param request_signature [String] The signature provided with the received |
| 25 | + # request, found in the `X-Duffel-Signature` request header |
| 26 | + # @param webhook_secret [String] The secret of the webhook, registered with Duffel |
| 27 | + # @return [Boolean] whether the webhook signature matches |
| 28 | + def genuine?(request_body:, request_signature:, webhook_secret:) |
| 29 | + parsed_signature = parse_signature!(request_signature) |
| 30 | + |
| 31 | + calculated_hmac = calculate_hmac( |
| 32 | + payload: request_body, |
| 33 | + secret: webhook_secret, |
| 34 | + timestamp: parsed_signature[:timestamp], |
| 35 | + ) |
| 36 | + |
| 37 | + secure_compare(calculated_hmac, parsed_signature[:v1]) |
| 38 | + rescue InvalidRequestSignatureError |
| 39 | + # If the signature doesn't even look like a valid one, then the webhook |
| 40 | + # event can't be genuine |
| 41 | + false |
| 42 | + end |
| 43 | + |
| 44 | + private |
| 45 | + |
| 46 | + # Calculates the signature for a request body in the same way that the Duffel API |
| 47 | + # does it |
| 48 | + # |
| 49 | + # @param secret [String] |
| 50 | + # @param payload [String] |
| 51 | + # @param timestamp [String] |
| 52 | + # @return [String] |
| 53 | + def calculate_hmac(secret:, payload:, timestamp:) |
| 54 | + signed_payload = %(#{timestamp}.#{payload}) |
| 55 | + Base16.encode16(OpenSSL::HMAC.digest(SHA_256, secret, |
| 56 | + signed_payload)).strip.downcase |
| 57 | + end |
| 58 | + |
| 59 | + # Parses a webhook signature and extracts the `v1` and `timestamp` values, if |
| 60 | + # available. |
| 61 | + # |
| 62 | + # @param signature [String] A webhook event signature received in a request |
| 63 | + # @return [Hash] |
| 64 | + # @raise InvalidRequestSignatureError when the signature isn't valid |
| 65 | + def parse_signature!(signature) |
| 66 | + matches = signature.match(SIGNATURE_REGEXP) |
| 67 | + |
| 68 | + if matches |
| 69 | + { |
| 70 | + v1: matches[2], |
| 71 | + timestamp: matches[1], |
| 72 | + } |
| 73 | + else |
| 74 | + raise InvalidRequestSignatureError |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + # Taken from `Rack::Utils` |
| 79 | + # (<https://github.com/rack/rack/blob/03b4b9708f375db46ee214b219f709d08ed6eeb0/lib/rack/utils.rb#L371-L393>). |
| 80 | + # |
| 81 | + # Licensed under the MIT License |
| 82 | + # (<https://github.com/rack/rack/blob/master/MIT-LICENSE>). |
| 83 | + if defined?(OpenSSL.fixed_length_secure_compare) |
| 84 | + # Checks if two strings are equal, performing a constant time string comparison |
| 85 | + # resistant to timing attacks. |
| 86 | + # |
| 87 | + # @param a [String] |
| 88 | + # @param b [String] |
| 89 | + # @return [Boolean] whether the two strings are equal |
| 90 | + # rubocop:disable Naming/MethodParameterName |
| 91 | + def secure_compare(a, b) |
| 92 | + # rubocop:enable Naming/MethodParameterName |
| 93 | + return false unless a.bytesize == b.bytesize |
| 94 | + |
| 95 | + OpenSSL.fixed_length_secure_compare(a, b) |
| 96 | + end |
| 97 | + else |
| 98 | + # Checks if two strings are equal, performing a constant time string comparison |
| 99 | + # resistant to timing attacks. |
| 100 | + # |
| 101 | + # @param [String] a |
| 102 | + # @param [String] b |
| 103 | + # @return [Boolean] whether the two strings are equal |
| 104 | + # rubocop:disable Naming/MethodParameterName |
| 105 | + def secure_compare(a, b) |
| 106 | + # rubocop:enable Naming/MethodParameterName |
| 107 | + return false unless a.bytesize == b.bytesize |
| 108 | + |
| 109 | + l = a.unpack("C*") |
| 110 | + |
| 111 | + r = 0 |
| 112 | + i = -1 |
| 113 | + b.each_byte { |v| r |= v ^ l[i += 1] } |
| 114 | + r.zero? |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | + end |
| 119 | +end |
0 commit comments