Skip to content

Commit bdff414

Browse files
authored
Merge pull request #11 from opf/bug/56435-56439-56813-validate-webhook-url
Fix a couple of bugs.
2 parents a50b8b7 + af69e70 commit bdff414

File tree

5 files changed

+35
-24
lines changed

5 files changed

+35
-24
lines changed

app/views/settings/_slack.html.erb

+23-20
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,35 @@
66
end
77
%>
88

9-
<section class="form--section">
10-
<input type="hidden" name="settings[enabled]" value="1"/>
9+
<div id="settings">
10+
<%= styled_form_tag({controller: '/admin/settings', action: 'update_plugin' }) do %>
11+
<input type="hidden" name="settings[enabled]" value="1"/>
1112

12-
<div class="form--field">
13-
<%= styled_label_tag("settings[webhook_url]", t("slack.default_webhook_url")) %>
14-
<div class="form--field-container">
15-
<%=
13+
<div class="form--field">
14+
<%= styled_label_tag("settings[webhook_url]", t("slack.default_webhook_url")) %>
15+
<div class="form--field-container">
16+
<%=
1617
styled_text_field_tag(
1718
"settings[webhook_url]",
1819
Setting.plugin_openproject_slack["webhook_url"],
19-
container_class: '-xwide'
20+
container_class: '-xwide',
21+
type: 'url',
22+
pattern: "[ -~]*",
23+
title: t("slack.only_ascii_chars_url"),
2024
)
21-
%>
25+
%>
26+
</div>
2227
</div>
23-
</div>
2428

25-
<span class="form--field-instructions">
26-
<%# Creates the custom field if not yet present. %>
27-
<% custom_field = OpenProject::Slack.project_custom_field %>
28-
<% edit_url = edit_admin_settings_project_custom_field_path custom_field %>
29-
<%=
30-
t(
29+
<span class="form--field-instructions">
30+
<%# Creates the custom field if not yet present. %>
31+
<% custom_field = OpenProject::Slack.project_custom_field %>
32+
<%= t(
3133
"slack.per_project_instructions_html",
32-
custom_field_url: edit_url,
34+
custom_field_url: edit_admin_settings_project_custom_field_path(custom_field),
3335
custom_field_name: custom_field.name
34-
)
35-
%>
36-
</span>
37-
</section>
36+
) %>
37+
</span>
38+
<%= styled_submit_tag t(:button_apply), class: '-primary' %>
39+
<% end %>
40+
</div>

app/workers/slack_notification_job.rb

+7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ def perform(params:, webhook_url:)
3838
return
3939
end
4040

41+
# prevent https://community.openproject.org/work_packages/56435/activity
42+
if !URI(webhook_url).respond_to?(:request_uri)
43+
OpenProject.logger.warn("Slack webhook URL is misconfigured: #{webhook_url}")
44+
45+
return
46+
end
47+
4148
notifier(webhook_url: webhook_url).post params
4249
rescue Slack::Notifier::APIError => e
4350
OpenProject.logger.warn "Error posting to Slack: #{e.message}"

config/locales/en.yml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ en:
77
<a href="%{custom_field_url}">%{custom_field_name}</a> custom field
88
which can be set in the project settings. You must not rename or delete
99
the custom field. If you do, a new one will be created automatically.
10+
only_ascii_chars_url: "Only ASCII characters are allowed in URL"
1011
field_type: Type
1112
field_project: Project
1213
field_subject: Subject

lib/open_project/slack.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ def project_custom_field_params
5252
{
5353
name: webhook_url_label,
5454
type: 'ProjectCustomField',
55-
field_format: 'string',
55+
field_format: 'link',
56+
regex: "^[ -~]*$", # only ASCII chars, because later URI.parse will not accept it
5657
custom_field_section_id: CustomFieldSection.first.id
5758
}
5859
end
5960

6061
def project_custom_field
61-
@project_custom_field ||= CustomField.find_by(name: webhook_url_label) ||
62-
CustomField.create(project_custom_field_params)
62+
CustomField.find_by(name: webhook_url_label) || CustomField.create(project_custom_field_params)
6363
end
6464
end
6565
end

lib/open_project/slack/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module OpenProject
22
module Slack
3-
VERSION = "14.0.1"
3+
VERSION = "14.4.0"
44
end
55
end

0 commit comments

Comments
 (0)