Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added HMAC signature to webhook payloads #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions app/controllers/webhook_settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def index
def create
webhook = Webhook.new(:project_id => @project.id)
webhook.url = params[:url]
webhook.secret_key = params[:secret_key]
if webhook.save
flash[:notice] = l(:notice_successful_create_webhook)
else
Expand All @@ -20,6 +21,7 @@ def update
id = params[:webhook_id]
webhook = Webhook.where(:project_id => @project.id).where(:id => id).first
webhook.url = params[:url]
webhook.secret_key = params[:secret_key]
if webhook.url.blank? ? webhook.destroy : webhook.save
flash[:notice] = l(:notice_successful_update_webhook)
else
Expand Down
4 changes: 4 additions & 0 deletions app/views/webhook_settings/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<span>
<span><strong>URL</strong></span>
<%= text_field_tag :url, webhook.url, :size => 80 %>
<span><strong>Secret key</strong></span>
<%= password_field_tag :secret_key, webhook.secret_key, :size => 40 %>
<%= submit_tag l(:button_update) %>
</span>
<% end %>
Expand All @@ -19,6 +21,8 @@
<span>
<span><strong>URL</strong></span>
<%= text_field_tag :url, '', :size => 80 %>
<span><strong>Secret key</strong></span>
<%= password_field_tag :secret_key, '', :size => 40 %>
<%= submit_tag l(:button_add) %>
</span>
</div>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20221023_add_webhook_secret_key.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddWebhookSecretKey < ActiveRecord::Migration[4.2]
def change
add_column :webhooks, :secret_key, :text
end
end
7 changes: 7 additions & 0 deletions lib/redmine_webhook/webhook_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,16 @@ def post(webhooks, request_body)
Thread.start do
webhooks.each do |webhook|
begin
# Sign payload
key = webhook.secret_key
# TODO: Allow configuration of algorithm in redmine configuration
hmac_alg = "sha1"
mac = OpenSSL::HMAC.hexdigest(hmac_alg, key, request_body)
Faraday.post do |req|
req.url webhook.url
req.headers['Content-Type'] = 'application/json'
req.headers['X-RedmineWebhook-HMAC-Alg'] = hmac_alg
req.headers['X-RedmineWebhook-HMAC-Signature'] = mac
req.body = request_body
end
rescue => e
Expand Down