Skip to content

Commit

Permalink
NEW: (suer#9) Added secret_key to webhooks . Post now includes HMAC v…
Browse files Browse the repository at this point in the history
…erification

signature.

Currently this is hard-coded to use sha1, this should be made configurable on a global or project basis. (Or perhaps even per hook?).

Direct link to upstream issue: suer#9
  • Loading branch information
ricekab committed Oct 23, 2022
1 parent 5bc9a84 commit 8c3fb0c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
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

0 comments on commit 8c3fb0c

Please sign in to comment.