discourse-ai/lib/inference/discourse_classifier.rb
Roman Rizzi 94ba0dadc2
SECURITY: Place a SSRF protection when calling services from the plugin. (#485)
The Faraday adapter and `FinalDestionation::HTTP` will protect us from admin-initiated SSRF attacks when interacting with the external services powering this plugin features.:
2024-02-21 17:14:50 -03:00

21 lines
646 B
Ruby

# frozen_string_literal: true
module ::DiscourseAi
module Inference
class DiscourseClassifier
def self.perform!(endpoint, model, content, api_key)
headers = { "Referer" => Discourse.base_url, "Content-Type" => "application/json" }
headers["X-API-KEY"] = api_key if api_key.present?
conn = Faraday.new { |f| f.adapter FinalDestination::FaradayAdapter }
response = conn.post(endpoint, { model: model, content: content }.to_json, headers)
raise Net::HTTPBadResponse if ![200, 415].include?(response.status)
JSON.parse(response.body, symbolize_names: true)
end
end
end
end