ckeditor-webhook 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ef8f6eb49dfa6e250f101699978f2bf642674e16794fa86fc1085fd4ebd7f95c
4
- data.tar.gz: 330faa6171d065e911ecfda3e672e2905ae0ce6120e96d7704f2d2562fd82b9a
3
+ metadata.gz: 64bbb117f61aef23c585bb7a5df47bc3faa8f7a7b28e69957fbd2610ce32287e
4
+ data.tar.gz: ba4f9e85a24db7a47e1a1c0aee7d82e2e2559694088bcd8acebcc30d397bcd2f
5
5
  SHA512:
6
- metadata.gz: e2311ab34648d0469656e4013ff908ae50543441d7934edc34d7eefbe1efb142832a4a8742d7145fe3d7b6f3170775603a3016594a9a71fe9fe17bc4199aaf4e
7
- data.tar.gz: db74afea88c2eb14393456f0b91db602790d6069b9a9b9b201cd87c7f3032f8f22a633d3447a4e5853cf9f99e049fdbf82f72267f330b121da7c69d48a120f1f
6
+ metadata.gz: bf801abff9f8e7ac9b31c40d83ac138685664ad704065175318c7626620b2568b037b72a9c8cd130cc2efbc9df828ab704adc401c0ee603b8c38b2ec0b858caa
7
+ data.tar.gz: d4e49630392ccd01630405fc9bbe2792dbbc7ba587ece5e4d9e436a585ddd254779ce7acc81e0461502b0d5edf2d4688f38f0e3d22b851d86cc7f4988ccaf863
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## 0.3.0 - 2020-11-09
9
+
10
+ - Change the `payload` keyword argument to `Ckeditor::Webhook.construct_event` from a `Hash` to a `String`. We can parse the JSON Rather than requiring callers to do so.
11
+ - Change the internals to avoid the `to_json` method. Rails' [ActiveSupport::Hash](https://github.com/rails/activesupport-json_encoder/blob/master/lib/active_support/json/encoding/active_support_encoder.rb) appears to extend `to_json` to encode certain characters in HTML. Since the encoded payload does not equal the original payload, the signature verification fails.
12
+
8
13
  ## 0.2.1 - 2020-10-31
9
14
 
10
15
  - Remove "?" character from `path` if the URL's query string does not exist. This should fix signature verification errors for URLs without a query string.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ckeditor-webhook (0.2.0)
4
+ ckeditor-webhook (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -29,7 +29,7 @@ $ gem install ckeditor-webhook
29
29
  Call `Ckeditor::Webhook::construct_event` with the following keyword arguments to create a verified webhook event (if the webhook is invalid, a `Ckedior::Webhook::SignatureVerificationError` will be raised):
30
30
 
31
31
  - `secret` (`String`), the CKEditor Cloud Services [API secret](https://ckeditor.com/docs/cs/latest/guides/security/api-secret.html).
32
- - `payload` (`Hash`), the webhook's payload
32
+ - `payload` (`String`), the webhook's payload
33
33
  - `signature` (`String`), the request's `X-CS-Signature` header
34
34
  - `timestamp` (`Integer`), the request's `X-CS-Timestamp` header
35
35
  - `url` (`String`), the request's url
@@ -41,8 +41,8 @@ For example:
41
41
  # Store your CKEditor Cloud Services API key safely.
42
42
  secret = "SECRET"
43
43
 
44
- payload = JSON.parse(request.body.read, symbolize_names: true)
45
- # => { event: "foo", environment_id: "bar", payload: { baz: "qux" }, sent_at: Time.now.utc }
44
+ payload = request.body.read
45
+ # => '{ "event": "foo", "environment_id": "bar", "payload": { baz: "qux" }, "sent_at": Time.now.utc }
46
46
 
47
47
  url = request.original_url
48
48
  # => "http://demo.example.com/webhook?a=1"
@@ -15,7 +15,7 @@ module Ckeditor
15
15
  # Returns an Event if the webhook signature is valid.
16
16
  #
17
17
  # @param secret [String] the CKEditor Cloud Services API secret
18
- # @param payload [Hash] the webhook's payload as a hash
18
+ # @param payload [String] the webhook's string payload
19
19
  # @param signature [String] the request's `X-CS-Signature` header
20
20
  # @param timestamp [Integer] the request's `X-CS-Timestamp` header
21
21
  # @param method [String] the request's method (defaults to "POST")
@@ -30,7 +30,7 @@ module Ckeditor
30
30
 
31
31
  raise SignatureVerificationError if signature != message_signature(message: event, secret: secret)
32
32
 
33
- Event.new(payload)
33
+ Event.new(parse_payload(payload))
34
34
  end
35
35
 
36
36
  private
@@ -42,7 +42,7 @@ module Ckeditor
42
42
  uri = URI.parse(url)
43
43
  path = uri.path + (uri.query.nil? ? "" : "?#{uri.query}" )
44
44
  method = method.upcase
45
- body = payload.to_json
45
+ body = sanitize_payload(payload)
46
46
 
47
47
  "#{method}#{path}#{timestamp}#{body}"
48
48
  end
@@ -54,6 +54,31 @@ module Ckeditor
54
54
  message
55
55
  )
56
56
  end
57
+
58
+ # Returns the string payload as a Hash with symbol keys.
59
+ #
60
+ # @return [Hash]
61
+ # @raise JSON::ParserError if JSON is invalid
62
+ def parse_payload(payload)
63
+ JSON.parse(payload, symbolize_names: true)
64
+ end
65
+
66
+ # Returns the string payload... as a string.
67
+ #
68
+ # 1. I remove any whitespace. The signature is generated from JSON
69
+ # without whitespace (e.g., '{"a":"ba"}'). Any unexpected spaces
70
+ # (e.g., '{ "a": "b" }') will cause a signature verification failure.
71
+ #
72
+ # 2. I avoid the `to_json` method. Rails ActiveSupport extends the
73
+ # method to encode HTML entities. For example, the "<" character is
74
+ # encoded to "\u003c"). The encoded payload does not match the
75
+ # original payload and will cause a signature verification failure.
76
+ #
77
+ # @return [String]
78
+ # @raise JSON::ParserError if JSON is invalid
79
+ def sanitize_payload(payload)
80
+ JSON.generate(JSON.parse(payload))
81
+ end
57
82
  end
58
83
  end
59
84
  end
@@ -1,5 +1,5 @@
1
1
  module Ckeditor
2
2
  module Webhook
3
- VERSION = "0.2.1"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ckeditor-webhook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jack Clayton
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-31 00:00:00.000000000 Z
11
+ date: 2020-11-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: