A platform for community discussion. Free, open, simple.
FIX: limit visibility on policy endpoints (#38233)
David Battersby2d4b6ca94613
diff --git a/plugins/discourse-policy/app/controllers/discourse_policy/policy_controller.rb b/plugins/discourse-policy/app/controllers/discourse_policy/policy_controller.rbindex 3340a58129f..657d442d725 100644--- a/plugins/discourse-policy/app/controllers/discourse_policy/policy_controller.rb+++ b/plugins/discourse-policy/app/controllers/discourse_policy/policy_controller.rb@@ -5,6 +5,7 @@ class DiscoursePolicy::PolicyController < ::ApplicationControllerbefore_action :ensure_logged_in, :set_postbefore_action :ensure_can_accept, only: %i[accept unaccept]+ before_action :ensure_can_see_policy_users, only: %i[accepted not_accepted]def acceptPolicyUser.add!(current_user, @post.post_policy)@@ -29,15 +30,6 @@ class DiscoursePolicy::PolicyController < ::ApplicationControllerenddef accepted- # Check if user has permission to see group members- groups = @post.post_policy.groups- return render_json_error(I18n.t("discourse_policy.errors.group_not_found")) if groups.blank?-- guardian = Guardian.new(current_user)- unless guardian.can_see_groups_members?(groups)- return render_json_error(I18n.t("discourse_policy.error.no_permission"))- end-users =@post.post_policy@@ -49,15 +41,6 @@ class DiscoursePolicy::PolicyController < ::ApplicationControllerenddef not_accepted- # Check if user has permission to see group members- groups = @post.post_policy.groups- return render_json_error(I18n.t("discourse_policy.errors.group_not_found")) if groups.blank?-- guardian = Guardian.new(current_user)- unless guardian.can_see_groups_members?(groups)- return render_json_error(I18n.t("discourse_policy.error.no_permission"))- end-users =@post.post_policy@@ -70,6 +53,15 @@ class DiscoursePolicy::PolicyController < ::ApplicationControllerprivate+ def ensure_can_see_policy_users+ post_policy = @post.post_policy+ raise Discourse::InvalidAccess if post_policy.private? && !guardian.is_admin?++ unless guardian.can_see_groups_members?(post_policy.groups)+ render_json_error(I18n.t("discourse_policy.error.no_permission"))+ end+ end+def ensure_can_acceptif !GroupUser.where("group_id in (:group_ids) and user_id = :user_id",diff --git a/plugins/discourse-policy/spec/requests/policy_controller_spec.rb b/plugins/discourse-policy/spec/requests/policy_controller_spec.rbindex 1b542a7bb6a..986be92d871 100644--- a/plugins/discourse-policy/spec/requests/policy_controller_spec.rb+++ b/plugins/discourse-policy/spec/requests/policy_controller_spec.rb@@ -151,6 +151,60 @@ describe DiscoursePolicy::PolicyController doendend+ describe "private policy restrictions" do+ fab!(:admin)++ def private_raw+ <<~MD+ [policy group=#{group.name} private=true]+ I always open **doors**!+ [/policy]+ MD+ end++ it "denies non-admin access to accepted users for a private policy" do+ post = create_post(raw: private_raw, user: moderator)+ PolicyUser.add!(user1, post.post_policy)++ sign_in(user1)+ get "/policy/accepted.json", params: { post_id: post.id, offset: 0 }+ expect(response.status).to eq(403)+ end++ it "denies non-admin access to not_accepted users for a private policy" do+ post = create_post(raw: private_raw, user: moderator)++ sign_in(user1)+ get "/policy/not-accepted.json", params: { post_id: post.id, offset: 0 }+ expect(response.status).to eq(403)+ end++ it "allows admin access to accepted users for a private policy" do+ group.add(admin)+ post = create_post(raw: private_raw, user: moderator)+ PolicyUser.add!(user1, post.post_policy)++ sign_in(admin)+ get "/policy/accepted.json", params: { post_id: post.id, offset: 0 }+ expect(response.status).to eq(200)+ expect(response.parsed_body["users"].map { |x| x["id"] }).to include(user1.id)+ end++ it "allows admin access to not_accepted users for a private policy" do+ group.add(admin)+ post = create_post(raw: private_raw, user: moderator)++ sign_in(admin)+ get "/policy/not-accepted.json", params: { post_id: post.id, offset: 0 }+ expect(response.status).to eq(200)+ expect(response.parsed_body["users"].map { |x| x["id"] }).to contain_exactly(+ user1.id,+ user2.id,+ admin.id,+ )+ end+ end+describe "group member visibility restrictions" dofab!(:owner, :user)let!(:post) do@@ -186,10 +240,12 @@ describe DiscoursePolicy::PolicyController doendit "allows owner to see group members" do+ PolicyUser.add!(user1, post.post_policy)+sign_in(owner)get "/policy/accepted.json", params: { post_id: post.id, offset: 0 }expect(response.status).to eq(200)- expect(response.parsed_body["users"]).to be_an(Array)+ expect(response.parsed_body["users"].map { |x| x["id"] }).to contain_exactly(user1.id)endendend