Skip to content

Commit 99a6421

Browse files
authored
Merge pull request #1736 from coderdojo-japan/fix/brakeman-command-injection
Command Injection警告の解消(Slack通知処理の安全化)
2 parents ee1eead + 72ea9d2 commit 99a6421

File tree

4 files changed

+36
-48
lines changed

4 files changed

+36
-48
lines changed

app/services/slack_notifier.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require "net/http"
2+
require "uri"
3+
require "json"
4+
5+
class SlackNotifier
6+
class << self
7+
# @param message [String] 通知するメッセージ
8+
# @param webhook_url [String] SlackのWebhook URL
9+
# @return [Boolean] 送信成功なら true / 失敗なら false
10+
def post_message(message, webhook_url)
11+
return false if webhook_url.blank?
12+
13+
uri = URI.parse(webhook_url)
14+
15+
request = Net::HTTP::Post.new(uri)
16+
request["Content-Type"] = "application/json"
17+
request.body = { text: message.to_s }.to_json
18+
19+
response = Net::HTTP.start(
20+
uri.host,
21+
uri.port,
22+
use_ssl: uri.scheme == "https",
23+
open_timeout: 5,
24+
read_timeout: 5
25+
) { |http| http.request(request) }
26+
27+
Rails.logger.info("Slack通知レスポンスコード: #{response.code}")
28+
response.code.to_i.between?(200, 299)
29+
rescue StandardError => e
30+
Rails.logger.warn("Slack通知エラー: #{e.class} #{e.message}")
31+
false
32+
end
33+
end
34+
end

config/brakeman.ignore

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
11
{
22
"ignored_warnings": [
3-
{
4-
"warning_type": "Command Injection",
5-
"warning_code": 14,
6-
"fingerprint": "7307f11036b1ab86f410d8d967d3972618705df73cafdd17f8e311c10c76c1f1",
7-
"check_name": "Execute",
8-
"message": "Possible command injection",
9-
"file": "lib/statistics/aggregation.rb",
10-
"line": 163,
11-
"link": "https://brakemanscanner.org/docs/warning_types/command_injection/",
12-
"code": "`curl -X POST -H 'Content-type: application/json' --data '{\"text\":\"#{msg}\"}' #{slack_hook_url} -o /dev/null -w \"slack: %{http_code}\"`",
13-
"render_path": null,
14-
"location": {
15-
"type": "method",
16-
"class": "Statistics::Statistics::Aggregation::Notifier",
17-
"method": "s(:self).notify"
18-
},
19-
"user_input": "msg",
20-
"confidence": "Medium",
21-
"cwe_id": [
22-
77
23-
],
24-
"note": ""
25-
},
263
{
274
"warning_type": "Cross-Site Scripting",
285
"warning_code": 4,
@@ -198,29 +175,6 @@
198175
79
199176
],
200177
"note": ""
201-
},
202-
{
203-
"warning_type": "Command Injection",
204-
"warning_code": 14,
205-
"fingerprint": "e5394a11f2e9bb6bc213b7ebd34fbcead20048858592aa19e5ae2961f33c636d",
206-
"check_name": "Execute",
207-
"message": "Possible command injection",
208-
"file": "lib/upcoming_events/aggregation.rb",
209-
"line": 89,
210-
"link": "https://brakemanscanner.org/docs/warning_types/command_injection/",
211-
"code": "`curl -X POST -H 'Content-type: application/json' --data '{\"text\":\"#{msg}\"}' #{slack_hook_url} -o /dev/null -w \"slack: %{http_code}\"`",
212-
"render_path": null,
213-
"location": {
214-
"type": "method",
215-
"class": "UpcomingEvents::UpcomingEvents::Aggregation::Notifier",
216-
"method": "s(:self).notify"
217-
},
218-
"user_input": "msg",
219-
"confidence": "Medium",
220-
"cwe_id": [
221-
77
222-
],
223-
"note": ""
224178
}
225179
],
226180
"brakeman_version": "7.1.0"

lib/statistics/aggregation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def notifierable?
160160

161161
def notify(msg)
162162
$stdout.puts msg
163-
puts `curl -X POST -H 'Content-type: application/json' --data '{"text":"#{msg}"}' #{slack_hook_url} -o /dev/null -w "slack: %{http_code}"` if notifierable?
163+
SlackNotifier.post_message(msg, slack_hook_url) if notifierable?
164164
end
165165
end
166166
end

lib/upcoming_events/aggregation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def notifierable?
8686

8787
def notify(msg)
8888
$stdout.puts msg
89-
puts `curl -X POST -H 'Content-type: application/json' --data '{"text":"#{msg}"}' #{slack_hook_url} -o /dev/null -w "slack: %{http_code}"` if notifierable?
89+
SlackNotifier.post_message(msg, slack_hook_url) if notifierable?
9090
end
9191
end
9292
end

0 commit comments

Comments
 (0)