Skip to content

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

SECURITY: Enforce group flair upload ownership on create and update (#44896)

Osama Sayeghf5a4509c234b
diff --git a/app/controllers/admin/groups_controller.rb b/app/controllers/admin/groups_controller.rb
index de0887b854d..a20f3cbaf09 100644
--- a/app/controllers/admin/groups_controller.rb
+++ b/app/controllers/admin/groups_controller.rb
@@ -16,6 +16,10 @@ class Admin::GroupsController < Admin::StaffController
) do |result|
on_success { |group:| render_serialized(group.reload, BasicGroupSerializer) }
on_failed_policy(:can_create_group) { |policy| raise Discourse::InvalidAccess }
+ on_failed_policy(:can_use_flair_upload) do
+ render json: failed_json.merge(errors: [I18n.t("groups.errors.flair_upload_not_owned")]),
+ status: :unprocessable_entity
+ end
on_failed_policy(:can_request_access) do
render json:
failed_json.merge(
diff --git a/app/services/groups/create.rb b/app/services/groups/create.rb
index 2f159c03f94..cce3e08a011 100644
--- a/app/services/groups/create.rb
+++ b/app/services/groups/create.rb
@@ -108,6 +108,7 @@ class Groups::Create
end
policy :can_create_group
+ policy :can_use_flair_upload
policy :can_request_access
model :user_attributes, :build_user_attributes, optional: true
model :group, :instantiate_group
@@ -124,6 +125,10 @@ class Groups::Create
guardian.can_create_group?
end
+ def can_use_flair_upload(guardian:, params:)
+ guardian.can_use_flair_upload?(params.flair_upload_id)
+ end
+
def can_request_access(params:)
params.owner_ids.present? || !params.allow_membership_requests
end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index f86f368a041..436f9eeb170 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -887,6 +887,7 @@ en:
one: "Default notification level can't be set on groups larger than %{count} user. Incorrect group: %{group_name}"
other: "Default notification level can't be set on groups larger than %{count} users. Incorrect group: %{group_name}"
grant_trust_level_not_valid: "'%{trust_level}' is not a valid trust level."
+ flair_upload_not_owned: "You can only use an image you have uploaded as the group flair."
can_not_modify_automatic: "You cannot modify an automatic group"
member_already_exist:
one: "'%{username}' is already a member of this group."
diff --git a/lib/group_updater.rb b/lib/group_updater.rb
index a8ec56f95d8..c2c8a936da3 100644
--- a/lib/group_updater.rb
+++ b/lib/group_updater.rb
@@ -108,6 +108,7 @@ class GroupUpdater
@attributes = ActionController::Parameters.new(attributes).permit(*permitted_params).to_h
group_attributes = attributes_without_disabled_email_settings
+ return false if !valid_flair_upload?(group_attributes)
notification_level, categories, tags = nil
if !group.automatic || guardian.is_admin?
@@ -141,6 +142,18 @@ class GroupUpdater
@can_admin_group = guardian.can_admin_group?(group)
end
+ def valid_flair_upload?(group_attributes)
+ return true if !group_attributes.key?(:flair_upload_id)
+
+ upload_id = group_attributes[:flair_upload_id].presence
+ return true if upload_id.blank?
+ return true if upload_id.to_i == group.flair_upload_id
+ return true if guardian.can_use_flair_upload?(upload_id)
+
+ group.errors.add(:base, I18n.t("groups.errors.flair_upload_not_owned"))
+ false
+ end
+
def attributes_without_disabled_email_settings
return attributes if !group.smtp_enabled
return attributes if !attributes.key?(:smtp_enabled)
diff --git a/lib/guardian/group_guardian.rb b/lib/guardian/group_guardian.rb
index bd08beb9ed8..d8b931e63fa 100644
--- a/lib/guardian/group_guardian.rb
+++ b/lib/guardian/group_guardian.rb
@@ -39,4 +39,13 @@ module GroupGuardian
def can_associate_groups?
is_admin? && AssociatedGroup.has_provider?
end
+
+ def can_use_flair_upload?(upload_id)
+ return true if upload_id.blank?
+ return true if is_admin?
+ return false if user.blank?
+ return true if Upload.exists?(id: upload_id, user_id: user.id)
+
+ UserUpload.exists?(upload_id:, user_id: user.id)
+ end
end
diff --git a/spec/requests/admin/groups_controller_spec.rb b/spec/requests/admin/groups_controller_spec.rb
index 8ba034807f5..6829ebf6b6c 100644
--- a/spec/requests/admin/groups_controller_spec.rb
+++ b/spec/requests/admin/groups_controller_spec.rb
@@ -199,6 +199,26 @@ RSpec.describe Admin::GroupsController do
expect(group.membership_request_template).to eq("Testing")
expect(group.members_visibility_level).to eq(Group.visibility_levels[:staff])
end
+
+ it "does not allow setting the flair to an upload the moderator does not own" do
+ secret_upload = Fabricate(:secure_upload, user: admin)
+
+ expect do
+ post "/admin/groups.json",
+ params: {
+ group: {
+ name: "flair-group",
+ flair_upload_id: secret_upload.id,
+ },
+ }
+ end.to_not change { Group.count }
+
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"]).to include(
+ I18n.t("groups.errors.flair_upload_not_owned"),
+ )
+ expect(UploadReference.exists?(upload: secret_upload)).to eq(false)
+ end
end
context "with moderators_manage_groups disabled" do
diff --git a/spec/requests/groups_controller_spec.rb b/spec/requests/groups_controller_spec.rb
index 72843e25afd..ef3680a676f 100644
--- a/spec/requests/groups_controller_spec.rb
+++ b/spec/requests/groups_controller_spec.rb
@@ -1518,6 +1518,36 @@ RSpec.describe GroupsController do
expect(group.flair_bg_color).to eq("FFF")
end
+ it "does not allow a group owner to set the flair to an upload they do not own" do
+ secret_upload = Fabricate(:secure_upload, user: admin)
+
+ put "/groups/#{group.id}.json", params: { group: { flair_upload_id: secret_upload.id } }
+
+ expect(response.status).to eq(422)
+ expect(group.reload.flair_upload_id).to be_nil
+ expect(group.flair_url).to be_nil
+ expect(UploadReference.exists?(upload: secret_upload, target: group)).to eq(false)
+ end
+
+ it "allows a group owner to set the flair to an upload they own" do
+ own_upload = Fabricate(:upload, user: user)
+
+ put "/groups/#{group.id}.json", params: { group: { flair_upload_id: own_upload.id } }
+
+ expect(response.status).to eq(200)
+ expect(group.reload.flair_upload_id).to eq(own_upload.id)
+ end
+
+ it "allows a group owner to set the flair to a deduplicated upload they uploaded" do
+ shared_upload = Fabricate(:upload, user: admin)
+ Fabricate(:user_upload, upload: shared_upload, user: user)
+
+ put "/groups/#{group.id}.json", params: { group: { flair_upload_id: shared_upload.id } }
+
+ expect(response.status).to eq(200)
+ expect(group.reload.flair_upload_id).to eq(shared_upload.id)
+ end
+
it "does not allow a group owner to modify the title" do
group.update!(title: "Original Title")