Skip to content
This repository was archived by the owner on Jan 31, 2019. It is now read-only.

Commit 6e1cbd8

Browse files
committed
Add ability to set public key signature
This commit adds support for fetching and assigning a public key-based signature of the request body. Higher in the call stack, we instantiate a public key that is capable of signing a message and inject it into the `Service` base class along with whether or not the request needs to be signed. If it does need to be signed, we add the base64 encoded signature to the headers of the request, similar to HMAC signing. This, paired with metadata that's injected into the webhook body with requests that need to be signed, helps ensure that the request is authentic and not a malicious payload and can help protect against replay attacks.
1 parent c70a096 commit 6e1cbd8

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lib/service.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ def inherited(svc)
484484

485485
attr_reader :remote_calls
486486

487+
attr_accessor :needs_public_key_signature, :public_key
488+
487489
def initialize(event = :push, data = {}, payload = nil)
488490
helper_name = "#{event.to_s.classify}Helpers"
489491
if Service.const_defined?(helper_name)

lib/service/http_helper.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def deliver(url_value, options = {})
1717
body = encode_body(ctype)
1818

1919
set_body_signature(body, secret)
20+
set_public_key_signature(body) if needs_public_key_signature
2021

2122
http_post url, body
2223
end
@@ -77,6 +78,12 @@ def set_body_signature(body, secret)
7778
'sha1='+OpenSSL::HMAC.hexdigest(HMAC_DIGEST, secret, body)
7879
end
7980

81+
def set_public_key_signature(body)
82+
public_key_signature = public_key.sign(message: body).signature
83+
encoded_signature = Base64.strict_encode64(public_key_signature)
84+
http.headers['GITHUB-PUBLIC-KEY-SIGNATURE'] = encoded_signature
85+
end
86+
8087
def original_body
8188
raise NotImplementedError
8289
end

0 commit comments

Comments
 (0)