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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## v7.8.3 (2026-12-04)

- Fixes `validate_webhook` comparing HMAC signatures with a non-constant-time `==`, which could leak timing information

## v7.8.2 (2026-11-30)

- Fixes `get_next_page` (claims, events, insurances, pickups, refunds, scan forms, shipments and trackers) dropping the `start_datetime` and `end_datetime` filters of the original `all` call
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.8.2
7.8.3
14 changes: 13 additions & 1 deletion lib/easypost/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,22 @@ def self.validate_webhook(event_body, headers, webhook_secret)

expected_signature = OpenSSL::HMAC.hexdigest('sha256', encoded_webhook_secret, event_body)
digest = "hmac-sha256-hex=#{expected_signature}"
unless digest == easypost_hmac_signature
unless secure_compare(digest, easypost_hmac_signature)
raise EasyPost::Errors::SignatureVerificationError.new(EasyPost::Constants::WEBHOOK_SIGNATURE_MISMATCH)
end

JSON.parse(event_body)
end

# Compare two strings in constant time so signature checks don't leak timing information.
def self.secure_compare(expected, actual)
return OpenSSL.secure_compare(expected, actual) if OpenSSL.respond_to?(:secure_compare)

# TODO: This fallback is for Ruby 2.7 backwards compatibility (its bundled openssl lacks `secure_compare`),
# remove it once we only support Ruby 3.0+.
return false unless expected.bytesize == actual.bytesize

expected.bytes.zip(actual.bytes).reduce(0) { |acc, (x, y)| acc | (x ^ y) }.zero?
Comment thread
Justintime50 marked this conversation as resolved.
Resolved
end
private_class_method :secure_compare
end
17 changes: 17 additions & 0 deletions spec/webhook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@
)
end

# TODO: Remove this test along with the Ruby 2.7 `secure_compare` fallback once we only support Ruby 3.0+.
it 'validate a webhook with a same-length invalid signature using the Ruby 2.7 fallback' do
allow(OpenSSL).to receive(:respond_to?).and_call_original
allow(OpenSSL).to receive(:respond_to?).with(:secure_compare).and_return(false)
signature = Fixture.webhook_hmac_signature
headers = {
'X-Hmac-Signature' => signature[0...-1] + (signature[-1] == '0' ? '1' : '0'),
}

expect {
EasyPost::Util.validate_webhook(Fixture.event_bytes, headers, Fixture.webhook_secret)
}.to raise_error(
EasyPost::Errors::SignatureVerificationError,
EasyPost::Constants::WEBHOOK_SIGNATURE_MISMATCH,
)
end

it 'validate a webhook with missing secret' do
webhook_secret = '123'
headers = {
Expand Down
Loading