A platform for community discussion. Free, open, simple.
SECURITY: Add authorization to policy `add-users-to-group`
Alan Guo Xiang Tandcde9de530f5
diff --git a/plugins/discourse-policy/app/controllers/discourse_policy/policy_controller.rb b/plugins/discourse-policy/app/controllers/discourse_policy/policy_controller.rbindex 657d442d725..10dbc515050 100644--- a/plugins/discourse-policy/app/controllers/discourse_policy/policy_controller.rb+++ b/plugins/discourse-policy/app/controllers/discourse_policy/policy_controller.rb@@ -10,10 +10,7 @@ class DiscoursePolicy::PolicyController < ::ApplicationControllerdef acceptPolicyUser.add!(current_user, @post.post_policy)@post.publish_change_to_clients!(:policy_change)-- if @post.post_policy.add_users_to_group.present?- @post.post_policy.add_users_group.add(current_user)- end+ @add_users_group&.add(current_user)render json: success_jsonend@@ -21,10 +18,7 @@ class DiscoursePolicy::PolicyController < ::ApplicationControllerdef unacceptPolicyUser.remove!(current_user, @post.post_policy)@post.publish_change_to_clients!(:policy_change)-- if @post.post_policy.add_users_to_group.present?- @post.post_policy.add_users_group.remove(current_user)- end+ @add_users_group&.remove(current_user)render json: success_jsonend@@ -71,6 +65,11 @@ class DiscoursePolicy::PolicyController < ::ApplicationControllerreturn render_json_error(I18n.t("discourse_policy.errors.user_missing"))end+ @add_users_group = @post.post_policy.add_users_group+ if @add_users_group && !Guardian.new(@post.user).can_edit_group?(@add_users_group)+ return render_json_error(I18n.t("discourse_policy.errors.policy_group_inaccessible"))+ end+trueenddiff --git a/plugins/discourse-policy/app/models/post_policy.rb b/plugins/discourse-policy/app/models/post_policy.rbindex ace80044834..7873bf084ad 100644--- a/plugins/discourse-policy/app/models/post_policy.rb+++ b/plugins/discourse-policy/app/models/post_policy.rb@@ -48,7 +48,7 @@ class PostPolicy < ActiveRecord::Baseenddef add_users_group- return if add_users_to_group.nil?+ return if add_users_to_group.blank?Group.find_by(id: add_users_to_group)enddiff --git a/plugins/discourse-policy/config/locales/server.en.yml b/plugins/discourse-policy/config/locales/server.en.ymlindex ad200ec4cf2..3753fe91665 100644--- a/plugins/discourse-policy/config/locales/server.en.yml+++ b/plugins/discourse-policy/config/locales/server.en.yml@@ -10,6 +10,7 @@ en:group_not_found: "Group not found for policy"no_policy: "No policy exists for post"staff_only: "Policies may only apply to staff posts"+ policy_group_inaccessible: "This policy cannot be accepted at this time. Please contact a site administrator."error:no_permission: "Not authorized"reports:diff --git a/plugins/discourse-policy/plugin.rb b/plugins/discourse-policy/plugin.rbindex 7a888d93693..a7aeb7c8e81 100644--- a/plugins/discourse-policy/plugin.rb+++ b/plugins/discourse-policy/plugin.rb@@ -143,7 +143,11 @@ after_initialize dopost_policy.private = policy["data-private"] == "true"if policy["data-add-users-to-group"].present?- post_policy.add_users_to_group = Group.find_by_name(policy["data-add-users-to-group"])&.id+ add_to_group = Group.find_by_name(policy["data-add-users-to-group"])+ post_policy.add_users_to_group =+ if add_to_group && Guardian.new(post.user).can_edit_group?(add_to_group)+ add_to_group.id+ endendif has_groupdiff --git a/plugins/discourse-policy/spec/plugin_spec.rb b/plugins/discourse-policy/spec/plugin_spec.rbindex 8b4508c8f3f..c3953527b3c 100644--- a/plugins/discourse-policy/spec/plugin_spec.rb+++ b/plugins/discourse-policy/spec/plugin_spec.rb@@ -64,7 +64,7 @@ describe DiscoursePolicy docontext "with add_users_to_group present" dofab!(:group2, :group)fab!(:post) { Fabricate(:post, user: moderator) }- fab!(:policy666) do+ fab!(:post_policy) dopolicy = Fabricate(:post_policy, post: post, add_users_to_group: group2.id)PostPolicyGroup.create!(post_policy_id: policy.id, group_id: group.id)policy@@ -72,16 +72,61 @@ describe DiscoursePolicy dobefore { group.add(user1) }+ it "persists the group that accepting users are added to when the post author can manage it" do+ group2.add_owner(moderator)+ updated_policy = <<~MD+ [policy group=#{group.name} add-users-to-group=#{group2.name}]+ Here's the new policy+ [/policy]+ MD++ post.update!(raw: updated_policy)+ post.rebake!+ post_policy.reload++ expect(post_policy.add_users_to_group).to eq(group2.id)+ end++ it "does not persist the group that accepting users are added to when the post author cannot manage it" do+ auto_group = Group.find(Group::AUTO_GROUPS[:admins])++ updated_policy = <<~MD+ [policy group=#{group.name} add-users-to-group=#{auto_group.name}]+ Here's the new policy+ [/policy]+ MD++ post.update!(raw: updated_policy)+ post.rebake!+ post_policy.reload++ expect(post_policy.add_users_to_group).to be_nil+ end++ it "does not persist the group that accepting users are added to when it does not exist" do+ updated_policy = <<~MD+ [policy group=#{group.name} add-users-to-group=nonexistent_group_xyz]+ Here's the new policy+ [/policy]+ MD++ post.update!(raw: updated_policy)+ post.rebake!+ post_policy.reload++ expect(post_policy.add_users_to_group).to be_nil+ end+it "removes all users from the group upon version change" doupdated_policy = <<~MD- [policy group=#{group.name} version=2 add_users_to_group=#{group2.id}]+ [policy group=#{group.name} version=2 add-users-to-group=#{group2.name}]Here's the new policy[/policy]MDpost.update!(raw: updated_policy)post.rebake!- post.post_policy.reload+ post_policy.reloadexpect(group2.users).to contain_exactlyenddiff --git a/plugins/discourse-policy/spec/requests/policy_controller_spec.rb b/plugins/discourse-policy/spec/requests/policy_controller_spec.rbindex 986be92d871..36bcafd0f44 100644--- a/plugins/discourse-policy/spec/requests/policy_controller_spec.rb+++ b/plugins/discourse-policy/spec/requests/policy_controller_spec.rb@@ -48,19 +48,21 @@ describe DiscoursePolicy::PolicyController dopolicyend+ before { group2.add_owner(moderator) }+it "adds/removes users to the group when they accept the policy" dosign_in(user1)put "/policy/accept.json", params: { post_id: post.id }expect(response.status).to eq(200)expect(post.reload.post_policy.accepted_by.map(&:id)).to eq([user1.id])- expect(group2.users.pluck(:id)).to contain_exactly(user1.id)+ expect(group2.users.pluck(:id)).to contain_exactly(moderator.id, user1.id)put "/policy/unaccept.json", params: { post_id: post.id }expect(response.status).to eq(200)expect(post.reload.post_policy.accepted_by.map(&:id)).to eq([])- expect(group2.users.pluck(:id)).to contain_exactly+ expect(group2.users.pluck(:id)).to contain_exactly(moderator.id)endend@@ -248,4 +250,45 @@ describe DiscoursePolicy::PolicyController doexpect(response.parsed_body["users"].map { |x| x["id"] }).to contain_exactly(user1.id)endend++ describe "#accept" do+ fab!(:group2, :group)+ fab!(:post) { Fabricate(:post, user: moderator) }+ fab!(:post_policy) do+ policy = Fabricate(:post_policy, post: post, add_users_to_group: group2.id)+ PostPolicyGroup.create!(post_policy_id: policy.id, group_id: group.id)+ policy+ end++ it "returns 422 when the post author cannot manage the group that accepting users are added to" do+ post_policy.update!(add_users_to_group: Group::AUTO_GROUPS[:admins])+ sign_in(user1)+ put "/policy/accept.json", params: { post_id: post.id }+ expect(response.status).to eq(422)+ expect(response.parsed_body["errors"]).to include(+ I18n.t("discourse_policy.errors.policy_group_inaccessible"),+ )+ end+ end++ describe "#unaccept" do+ fab!(:group2, :group)+ fab!(:post) { Fabricate(:post, user: moderator) }+ fab!(:post_policy) do+ policy = Fabricate(:post_policy, post: post, add_users_to_group: group2.id)+ PostPolicyGroup.create!(post_policy_id: policy.id, group_id: group.id)+ policy+ end++ it "returns 422 when the post author cannot manage the group that accepting users are added to" do+ post_policy.update!(add_users_to_group: Group::AUTO_GROUPS[:admins])+ PolicyUser.add!(user1, post_policy)+ sign_in(user1)+ put "/policy/unaccept.json", params: { post_id: post.id }+ expect(response.status).to eq(422)+ expect(response.parsed_body["errors"]).to include(+ I18n.t("discourse_policy.errors.policy_group_inaccessible"),+ )+ end+ endend