A platform for community discussion. Free, open, simple.
FIX: prevents message enumeration (#38014)
Joffrey JAFFEUX50f17d2299e5
diff --git a/plugins/chat/app/controllers/chat/api/channels_messages_interactions_controller.rb b/plugins/chat/app/controllers/chat/api/channels_messages_interactions_controller.rbindex db6123c1c98..f4d1a946512 100644--- a/plugins/chat/app/controllers/chat/api/channels_messages_interactions_controller.rb+++ b/plugins/chat/app/controllers/chat/api/channels_messages_interactions_controller.rb@@ -8,6 +8,7 @@ class Chat::Api::ChannelsMessagesInteractionsController < Chat::ApiControllerendon_failure { render(json: failed_json, status: :unprocessable_entity) }on_model_not_found(:message) { raise Discourse::NotFound }+ on_failed_policy(:can_interact_with_message) { raise Discourse::NotFound }on_model_not_found(:action) { raise Discourse::NotFound }on_failed_contract do |contract|render(json: failed_json.merge(errors: contract.errors.full_messages), status: :bad_request)diff --git a/plugins/chat/app/services/chat/create_message_interaction.rb b/plugins/chat/app/services/chat/create_message_interaction.rbindex 983c67ebd10..42483c3b58d 100644--- a/plugins/chat/app/services/chat/create_message_interaction.rb+++ b/plugins/chat/app/services/chat/create_message_interaction.rb@@ -18,9 +18,11 @@ module Chatparams doattribute :message_id, :integerattribute :action_id, :string+ attribute :channel_id, :integervalidates :action_id, presence: truevalidates :message_id, presence: true+ validates :channel_id, presence: trueendmodel :message@@ -35,7 +37,7 @@ module Chatprivatedef fetch_message(params:)- Chat::Message.find_by(id: params.message_id)+ Chat::Message.find_by(id: params.message_id, chat_channel_id: params.channel_id)enddef fetch_action(params:, message:)diff --git a/plugins/chat/spec/requests/chat/api/channels_messages_interactions_controller_spec.rb b/plugins/chat/spec/requests/chat/api/channels_messages_interactions_controller_spec.rbnew file mode 100644index 00000000000..b3fb1433ce5--- /dev/null+++ b/plugins/chat/spec/requests/chat/api/channels_messages_interactions_controller_spec.rb@@ -0,0 +1,161 @@+# frozen_string_literal: true++RSpec.describe Chat::Api::ChannelsMessagesInteractionsController do+ fab!(:current_user, :user)+ fab!(:channel, :private_category_channel)+ fab!(:message) do+ Fabricate(+ :chat_message,+ chat_channel: channel,+ user: Discourse.system_user,+ blocks: [+ {+ type: "actions",+ elements: [+ {+ action_id: "xxx",+ value: "foo",+ type: "button",+ text: {+ type: "plain_text",+ text: "Click Me",+ },+ },+ ],+ },+ ],+ )+ end++ before do+ SiteSetting.chat_enabled = true+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]+ sign_in(current_user)+ end++ describe "#create" do+ context "when user has access to the channel" do+ fab!(:accessible_channel, :category_channel)+ fab!(:accessible_message) do+ Fabricate(+ :chat_message,+ chat_channel: accessible_channel,+ user: Discourse.system_user,+ blocks: [+ {+ type: "actions",+ elements: [+ {+ action_id: "xxx",+ value: "foo",+ type: "button",+ text: {+ type: "plain_text",+ text: "Click Me",+ },+ },+ ],+ },+ ],+ )+ end++ before { accessible_channel.add(current_user) }++ it "creates an interaction and returns serialized data" do+ post "/chat/api/channels/#{accessible_channel.id}/messages/#{accessible_message.id}/interactions",+ params: {+ action_id: "xxx",+ }++ expect(response.status).to eq(200)+ expect(response.parsed_body["interaction"]).to include(+ "user" => hash_including("id" => current_user.id),+ "message" => hash_including("id" => accessible_message.id),+ "channel" => hash_including("id" => accessible_channel.id),+ "action" => hash_including("action_id" => "xxx"),+ )+ end++ it "returns 404 when action_id does not match any block element" do+ post "/chat/api/channels/#{accessible_channel.id}/messages/#{accessible_message.id}/interactions",+ params: {+ action_id: "nonexistent",+ }++ expect(response.status).to eq(404)+ end++ it "returns 400 when action_id is missing" do+ post "/chat/api/channels/#{accessible_channel.id}/messages/#{accessible_message.id}/interactions",+ params: {+ }++ expect(response.status).to eq(400)+ end+ end++ context "when user is not logged in" do+ before { sign_out }++ it "returns 404" do+ post "/chat/api/channels/#{channel.id}/messages/#{message.id}/interactions",+ params: {+ action_id: "xxx",+ }++ expect(response.status).to eq(404)+ end+ end++ context "when chat is disabled" do+ before { SiteSetting.chat_enabled = false }++ it "returns 404" do+ post "/chat/api/channels/#{channel.id}/messages/#{message.id}/interactions",+ params: {+ action_id: "xxx",+ }++ expect(response.status).to eq(404)+ end+ end++ context "when message exists in a channel the user cannot access" do+ it "returns 404 to avoid leaking message existence" do+ post "/chat/api/channels/#{channel.id}/messages/#{message.id}/interactions",+ params: {+ action_id: "xxx",+ }++ expect(response.status).to eq(404)+ end+ end++ context "when message does not exist" do+ it "returns 404" do+ post "/chat/api/channels/#{channel.id}/messages/-999/interactions",+ params: {+ action_id: "xxx",+ }++ expect(response.status).to eq(404)+ end+ end++ context "when message exists but in a different channel than the URL" do+ fab!(:other_channel, :category_channel)++ before { other_channel.add(current_user) }++ it "returns 404 to avoid cross-channel message lookup" do+ post "/chat/api/channels/#{other_channel.id}/messages/#{message.id}/interactions",+ params: {+ action_id: "xxx",+ }++ expect(response.status).to eq(404)+ end+ end+ end+enddiff --git a/plugins/chat/spec/services/chat/create_message_interaction_spec.rb b/plugins/chat/spec/services/chat/create_message_interaction_spec.rbindex 9ca0cbc0de2..a99b72af2f0 100644--- a/plugins/chat/spec/services/chat/create_message_interaction_spec.rb+++ b/plugins/chat/spec/services/chat/create_message_interaction_spec.rb@@ -4,6 +4,7 @@ RSpec.describe Chat::CreateMessageInteraction dodescribe described_class::Contract, type: :model doit { is_expected.to validate_presence_of :message_id }it { is_expected.to validate_presence_of :action_id }+ it { is_expected.to validate_presence_of :channel_id }enddescribe ".call" do@@ -34,7 +35,9 @@ RSpec.describe Chat::CreateMessageInteraction doendlet(:guardian) { Guardian.new(current_user) }- let(:params) { { message_id: message.id, action_id: "xxx" } }+ let(:params) do+ { message_id: message.id, action_id: "xxx", channel_id: message.chat_channel_id }+ endlet(:dependencies) { { guardian: } }context "when all steps pass" do