Skip to content

Commit 7ac6b3a

Browse files
committed
security: SlackNotifierでのCommand Injection脆弱性を解消
- app/services/slack_notifier.rb を新規追加 - curlコマンドをNet::HTTPに置換し、外部入力を安全に処理 - タイムアウト設定とエラーハンドリングを追加 - 2箇所のCommand Injection警告を解消
1 parent eb3e7b8 commit 7ac6b3a

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
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

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)