svix 0.16.0 → 0.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e86e21aa5b9f2ed4b01ce230acd8a58e019d705d9d7c0ee560f0286e4b657e38
4
- data.tar.gz: d832d554e538c75a51eda9d4e6486f4f7fbfec8de271b308499f06d2b5038766
3
+ metadata.gz: 8651d1d4cf9fa7f6dd65198a111215eaa22ec98bdbe8df8e003b701c15fc7d4f
4
+ data.tar.gz: 2053ab0dd50d3c0136de7e83d92edfe21f87e73c663c318fab208c0101345776
5
5
  SHA512:
6
- metadata.gz: 553701612ddad362213e53389d5f582b22350daaee86bfdc9a81e5d7fade59959c38f0becdff166acfecebbbfca218583d34df2ae85e8d05116e007f1dfb232c
7
- data.tar.gz: 4b959ceeca4f3847062c3f14d25445ac808504d153a67e5ed65c37d2e3b8482cdb509238f7232f2b87597c4b331b1d485415bb1146de114c59563f3fa57d741b
6
+ metadata.gz: 06c768854e7f5a34cacd15de32092a2ca1b7d18fde8ad07f00e5eeefb84d6b26926e574c70db168060974eae77fd820d80a395d9a6eae5aecaf905b067c18cb6
7
+ data.tar.gz: a19267a68e6472ec9f966a5b1a7441c64c648fdc9202b0cf5c0427407761a3582ba8eb7fb85f63cd1e7c10f3ecc26e5b7df766ae1414012db011078ffc01ef00
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- svix (0.16.0)
4
+ svix (0.21.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/src/svix/errors.rb CHANGED
@@ -11,4 +11,7 @@ module Svix
11
11
 
12
12
  class WebhookVerificationError < SvixError
13
13
  end
14
- end
14
+
15
+ class WebhookSigningError < SvixError
16
+ end
17
+ end
data/src/svix/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Svix
2
- VERSION = "0.16.0"
4
+ VERSION = "0.21.0"
3
5
  end
data/src/svix/webhook.rb CHANGED
@@ -2,7 +2,12 @@
2
2
 
3
3
  module Svix
4
4
  class Webhook
5
+
5
6
  def initialize(secret)
7
+ if secret.start_with?(SECRET_PREFIX)
8
+ secret = secret[SECRET_PREFIX.length..-1]
9
+ end
10
+
6
11
  @secret = Base64.decode64(secret)
7
12
  end
8
13
 
@@ -14,12 +19,13 @@ module Svix
14
19
  raise WebhookVerificationError, "Missing required headers"
15
20
  end
16
21
 
17
- toSign = "#{msgId}.#{msgTimestamp}.#{payload}"
18
- signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), @secret, toSign)).strip()
22
+ verify_timestamp(msgTimestamp)
23
+
24
+ _, signature = sign(msgId, msgTimestamp, payload).split(",", 2)
19
25
 
20
26
  passedSignatures = msgSignature.split(" ")
21
27
  passedSignatures.each do |versionedSignature|
22
- version, expectedSignature = versionedSignature.split(',', 2)
28
+ version, expectedSignature = versionedSignature.split(",", 2)
23
29
  if version != "v1"
24
30
  next
25
31
  end
@@ -29,5 +35,36 @@ module Svix
29
35
  end
30
36
  raise WebhookVerificationError, "No matching signature found"
31
37
  end
38
+
39
+ def sign(msgId, timestamp, payload)
40
+ begin
41
+ now = Integer(timestamp)
42
+ rescue
43
+ raise WebhookSigningError, "Invalid timestamp"
44
+ end
45
+ toSign = "#{msgId}.#{timestamp}.#{payload}"
46
+ signature = Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest.new("sha256"), @secret, toSign)).strip
47
+ return "v1,#{signature}"
48
+ end
49
+
50
+ private
51
+ SECRET_PREFIX = "whsec_"
52
+ TOLERANCE = 5 * 60
53
+
54
+ def verify_timestamp(timestampHeader)
55
+ begin
56
+ now = Integer(Time.now)
57
+ timestamp = Integer(timestampHeader)
58
+ rescue
59
+ raise WebhookVerificationError, "Invalid Signature Headers"
60
+ end
61
+
62
+ if timestamp < (now - TOLERANCE)
63
+ raise WebhookVerificationError, "Message timestamp too old"
64
+ end
65
+ if timestamp > (now + TOLERANCE)
66
+ raise WebhookVerificationError, "Message timestamp too new"
67
+ end
68
+ end
32
69
  end
33
- end
70
+ end
data/svix.gemspec CHANGED
@@ -21,8 +21,8 @@ Gem::Specification.new do |spec|
21
21
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
22
22
 
23
23
  spec.metadata["homepage_uri"] = spec.homepage
24
- spec.metadata["source_code_uri"] = "https://github.com/svixhq/svix-libs"
25
- spec.metadata["changelog_uri"] = "https://github.com/svixhq/svix-libs/blob/main/ChangeLog.md"
24
+ spec.metadata["source_code_uri"] = "https://github.com/svix/svix-libs"
25
+ spec.metadata["changelog_uri"] = "https://github.com/svix/svix-libs/blob/main/ChangeLog.md"
26
26
  else
27
27
  raise "RubyGems 2.0 or newer is required to protect against " \
28
28
  "public gem pushes."
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: svix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Svix
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-08 00:00:00.000000000 Z
11
+ date: 2021-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,8 +76,8 @@ licenses:
76
76
  metadata:
77
77
  allowed_push_host: https://rubygems.org
78
78
  homepage_uri: https://www.svix.com
79
- source_code_uri: https://github.com/svixhq/svix-libs
80
- changelog_uri: https://github.com/svixhq/svix-libs/blob/main/ChangeLog.md
79
+ source_code_uri: https://github.com/svix/svix-libs
80
+ changelog_uri: https://github.com/svix/svix-libs/blob/main/ChangeLog.md
81
81
  post_install_message:
82
82
  rdoc_options: []
83
83
  require_paths: