Skip to content

Commit 8c09d21

Browse files
authored
Merge pull request #91 from BloomAndWild/master
Add support for ruby-jwt v2.6.0 and above, bump development deps, new Ruby vers
2 parents 64bcb3f + dae4ec0 commit 8c09d21

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ jobs:
1111

1212
strategy:
1313
matrix:
14-
ruby-version: [ "3.0.3", "3.1.1" ]
15-
14+
ruby-version: [ "3.0.7", "3.1.6", "3.2.6", "3.3.7", "3.4.1" ]
15+
1616
steps:
1717
- uses: actions/checkout@v2
1818

lib/messagebird/request_validator.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ def decode_signature(signature)
7171

7272
def validate_url(url, url_hash)
7373
expected_url_hash = Digest::SHA256.hexdigest url
74-
unless JWT::SecurityUtils.secure_compare(expected_url_hash, url_hash)
74+
unless secure_compare(expected_url_hash, url_hash)
7575
raise ValidationError, 'invalid jwt: claim url_hash is invalid'
7676
end
7777
end
7878

7979
def validate_payload(body, payload_hash)
8080
if !body.to_s.empty? && !payload_hash.to_s.empty?
81-
unless JWT::SecurityUtils.secure_compare(Digest::SHA256.hexdigest(body), payload_hash)
81+
unless secure_compare(Digest::SHA256.hexdigest(body), payload_hash)
8282
raise ValidationError, 'invalid jwt: claim payload_hash is invalid'
8383
end
8484
elsif !body.to_s.empty?
@@ -87,5 +87,15 @@ def validate_payload(body, payload_hash)
8787
raise ValidationError, 'invalid jwt: claim payload_hash is set but actual payload is missing'
8888
end
8989
end
90+
91+
# Adaption of https://github.com/rails/rails/blob/cf6ff17e9a3c6c1139040b519a341f55f0be16cf/activesupport/lib/active_support/security_utils.rb#L33
92+
# Copied here so as to avoid adding a dependency on ActiveSupport to this gem
93+
#
94+
# Note that unlike `fixed_length_secure_compare` in the above url we don't fall back to a custom implementation
95+
# of fixed_length_secure_compare, since OpenSSL.fixed_length_secure_compare is present in OpenSSL 2.2
96+
# https://github.com/ruby/openssl/blob/master/History.md#version-220 which is included in Ruby 3.0 and above
97+
def secure_compare(first, second)
98+
first.bytesize == second.bytesize && OpenSSL.fixed_length_secure_compare(first, second)
99+
end
90100
end
91101
end

messagebird-rest.gemspec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ Gem::Specification.new do |s|
2323
s.files = Dir.glob('lib/**/*') + %w(LICENSE README.md)
2424
s.require_path = 'lib'
2525

26-
s.add_dependency "jwt", "~> 2.3"
26+
# This code works with at least version 3.0.0.beta1 of jwt,
27+
# so we are supporting up to version 4 to help reduce
28+
# the necessity for future version bumps
29+
s.add_dependency "jwt", "< 4"
2730

2831
s.add_development_dependency "rspec", "~> 3.11.0"
2932
s.add_development_dependency "rubocop", "~> 1.26.1"

0 commit comments

Comments
 (0)