A platform for community discussion. Free, open, simple.
SECURITY: Gate private event invitee details
discourse-patch-triage[bot]4d46638041b5
diff --git a/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rb b/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rbindex de2e8b0b0de..e0e9af73be1 100644--- a/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rb+++ b/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rb@@ -125,17 +125,62 @@ module DiscoursePostEventwatching_invitee = Invitee.find_by(user_id: scope.current_user.id, post_id: object.id)end- InviteeSerializer.new(watching_invitee, root: false) if watching_invitee+ InviteeSerializer.new(watching_invitee, root: false, scope:) if watching_invitee+ end++ def include_raw_invitees?+ can_display_invitee_details?enddef sample_inviteesinvitees = object.most_likely_going- ActiveModel::ArraySerializer.new(invitees, each_serializer: InviteeSerializer)+ ActiveModel::ArraySerializer.new(invitees, each_serializer: InviteeSerializer, scope:)+ end++ def include_sample_invitees?+ can_display_invitee_details?+ end++ def include_stats?+ can_display_invitee_details?+ end++ def can_display_invitee_details?+ return @can_display_invitee_details if defined?(@can_display_invitee_details)++ @can_display_invitee_details =+ if !object.private? || scope.can_act_on_discourse_post_event?(object)+ true+ else+ user = scope.current_user+ if user && invited_user?(user)+ true+ else+ visible_invited_groups?(user)+ end+ end+ end++ def visible_invited_groups?(user)+ raw_invitees = Array(object.raw_invitees).uniq+ return false if raw_invitees.blank?++ Group+ .visible_groups(user)+ .members_visible_groups(user)+ .where(name: raw_invitees)+ .distinct+ .count == raw_invitees.length+ end++ def invited_user?(user)+ object.invitees.exists?(user_id: user.id) ||+ user.groups.where(name: Array(object.raw_invitees)).exists?enddef should_display_invitees(object.public? && object.invitees.count > 0) ||- (object.private? && object.raw_invitees.count > 0)+ (object.private? && can_display_invitee_details? && Array(object.raw_invitees).count > 0)enddef include_url?diff --git a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb b/plugins/discourse-calendar/spec/requests/events_controller_spec.rbindex b6d37a8ad11..8afb6e8660c 100644--- a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb+++ b/plugins/discourse-calendar/spec/requests/events_controller_spec.rb@@ -597,6 +597,51 @@ module DiscoursePostEventendend+ context "with a private event" do+ fab!(:viewer, :user)+ fab!(:invitee, :user)+ fab!(:restricted_group) do+ Fabricate(+ :group,+ visibility_level: Group.visibility_levels[:owners],+ members_visibility_level: Group.visibility_levels[:owners],+ ).tap { |group| group.add(invitee) }+ end+ fab!(:private_event_post) do+ Fabricate(+ :post,+ user: Fabricate(:user, admin: true),+ topic: Fabricate(:topic, category: Fabricate(:category)),+ )+ end+ fab!(:private_event) do+ Fabricate(+ :event,+ post: private_event_post,+ status: DiscoursePostEvent::Event.statuses[:private],+ raw_invitees: [restricted_group.name],+ )+ end++ before do+ private_event.create_invitees(+ [{ user_id: invitee.id, status: Invitee.statuses[:going] }],+ )+ sign_in(viewer)+ end++ it "does not serialize invitee details for non-invited viewers who cannot see the invited group" do+ get "/discourse-post-event/events/#{private_event.id}.json"++ expect(response.status).to eq(200)+ event = response.parsed_body["event"]+ expect(event).not_to have_key("raw_invitees")+ expect(event).not_to have_key("sample_invitees")+ expect(event).not_to have_key("stats")+ expect(event["should_display_invitees"]).to eq(false)+ end+ end+context "when filtering by category" dofab!(:category)