Skip to content

A platform for community discussion. Free, open, simple.

FIX: Require join permission to invite users to chat channel (#38016)

Joffrey JAFFEUXf7dbda9d1cc0
diff --git a/plugins/chat/app/controllers/chat/api/channels_invites_controller.rb b/plugins/chat/app/controllers/chat/api/channels_invites_controller.rb
index 9c295d1727b..879f389fbff 100644
--- a/plugins/chat/app/controllers/chat/api/channels_invites_controller.rb
+++ b/plugins/chat/app/controllers/chat/api/channels_invites_controller.rb
@@ -2,10 +2,12 @@
class Chat::Api::ChannelsInvitesController < Chat::ApiController
def create
+ RateLimiter.new(current_user, "invite_to_chat_channel", 5, 1.minute).performed!
+
Chat::InviteUsersToChannel.call(service_params) do
on_success { render(json: success_json) }
on_failure { render(json: failed_json, status: :unprocessable_entity) }
- on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess }
+ on_failed_policy(:can_join_channel) { raise Discourse::InvalidAccess }
on_model_not_found(:channel) { 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/invite_users_to_channel.rb b/plugins/chat/app/services/chat/invite_users_to_channel.rb
index 61f6178e9a0..5edcc1db798 100644
--- a/plugins/chat/app/services/chat/invite_users_to_channel.rb
+++ b/plugins/chat/app/services/chat/invite_users_to_channel.rb
@@ -27,7 +27,7 @@ module Chat
end
model :channel
- policy :can_view_channel
+ policy :can_join_channel
model :users, optional: true
step :send_invite_notifications
@@ -37,8 +37,8 @@ module Chat
::Chat::Channel.find_by(id: params.channel_id)
end
- def can_view_channel(guardian:, channel:)
- guardian.can_preview_chat_channel?(channel)
+ def can_join_channel(guardian:, channel:)
+ guardian.can_join_chat_channel?(channel)
end
def fetch_users(params:)
diff --git a/plugins/chat/spec/requests/chat/api/channels_invites_controller_spec.rb b/plugins/chat/spec/requests/chat/api/channels_invites_controller_spec.rb
index fb74c80f227..d4ca0c36f7d 100644
--- a/plugins/chat/spec/requests/chat/api/channels_invites_controller_spec.rb
+++ b/plugins/chat/spec/requests/chat/api/channels_invites_controller_spec.rb
@@ -50,7 +50,7 @@ RSpec.describe Chat::Api::ChannelsInvitesController do
end
end
- describe "current user can't view channel" do
+ describe "current user can't join channel" do
fab!(:channel_1, :private_category_channel)
it "returns a 403" do
@@ -60,6 +60,32 @@ RSpec.describe Chat::Api::ChannelsInvitesController do
end
end
+ describe "current user can view but not join channel" do
+ fab!(:group)
+
+ before do
+ channel_1.chatable.set_permissions(group => :readonly)
+ channel_1.chatable.save!
+ group.add(current_user)
+ end
+
+ it "returns a 403" do
+ post "/chat/api/channels/#{channel_1.id}/invites?user_ids=#{user_1.id},#{user_2.id}"
+
+ expect(response.status).to eq(403)
+ end
+ end
+
+ describe "rate limiting" do
+ it "rate limits invites" do
+ RateLimiter.enable
+
+ 6.times { post "/chat/api/channels/#{channel_1.id}/invites?user_ids=#{user_1.id}" }
+
+ expect(response.status).to eq(429)
+ end
+ end
+
describe "channel doesn’t exist" do
it "returns a 404" do
post "/chat/api/channels/-1/invites?user_ids=#{user_1.id},#{user_2.id}"
diff --git a/plugins/chat/spec/services/chat/invite_users_to_channel_spec.rb b/plugins/chat/spec/services/chat/invite_users_to_channel_spec.rb
index 3d2b120a78c..80a9cb52ef4 100644
--- a/plugins/chat/spec/services/chat/invite_users_to_channel_spec.rb
+++ b/plugins/chat/spec/services/chat/invite_users_to_channel_spec.rb
@@ -83,11 +83,24 @@ RSpec.describe Chat::InviteUsersToChannel do
it { is_expected.to fail_to_find_a_model(:channel) }
end
- context "when current user can't view channel" do
+ context "when current user can't join channel" do
fab!(:current_user, :user)
fab!(:channel_1, :private_category_channel)
- it { is_expected.to fail_a_policy(:can_view_channel) }
+ it { is_expected.to fail_a_policy(:can_join_channel) }
+ end
+
+ context "when current user can view but not join channel" do
+ fab!(:current_user, :user)
+ fab!(:channel_1, :category_channel)
+
+ before do
+ channel_1.chatable.set_permissions(group_1 => :readonly)
+ channel_1.chatable.save!
+ group_1.add(current_user)
+ end
+
+ it { is_expected.to fail_a_policy(:can_join_channel) }
end
end
end