A platform for community discussion. Free, open, simple.
SECURITY: Gate include_staged_users in UserSearch with a Guardian policy (#42700)
Chris Alberti6ca363c9b69c
diff --git a/app/controllers/directory_items_controller.rb b/app/controllers/directory_items_controller.rbindex a6e740fdcfb..eb0396ef316 100644--- a/app/controllers/directory_items_controller.rb+++ b/app/controllers/directory_items_controller.rb@@ -69,7 +69,12 @@ class DirectoryItemsController < ApplicationControlleruser_ids = nilif params[:name].present?- opts = { include_staged_users: true, limit: 200, search_custom_fields: true }+ opts = {+ include_staged_users: true,+ user_directory_search: true,+ limit: 200,+ search_custom_fields: true,+ }user_ids = UserSearch.new(params[:name], opts).search.pluck(:id)if user_ids.present?# Add the current user if we have at least one other matchdiff --git a/app/models/user_search.rb b/app/models/user_search.rbindex ed0378307e3..3017dd56978 100644--- a/app/models/user_search.rb+++ b/app/models/user_search.rb@@ -13,8 +13,11 @@ class UserSearch@prioritized_user_id = opts[:prioritized_user_id]@topic_allowed_users = opts[:topic_allowed_users]@searching_user = opts[:searching_user]- @include_staged_users = opts[:include_staged_users] || false- @last_seen_users = opts[:last_seen_users] || false+ @guardian = Guardian.new(@searching_user)+ @include_staged_users =+ !!opts[:include_staged_users] &&+ @guardian.can_search_staged_users?(user_directory_search: opts[:user_directory_search])+ @last_seen_users = !!opts[:last_seen_users] && !@include_staged_users@limit = opts[:limit] || 20@groups = opts[:groups]@can_review = opts[:can_review] || false@@ -23,17 +26,16 @@ class UserSearch@topic = Topic.find(@topic_id) if @topic_id@category = Category.find(@category_id) if @category_id- guardian = @searching_user&.guardian || Guardian.new- @groups&.each { |group| guardian.ensure_can_see_group_and_members!(group) }- guardian.ensure_can_see_category!(@category) if @category- guardian.ensure_can_see_topic!(@topic) if @topic+ @groups&.each { |group| @guardian.ensure_can_see_group_and_members!(group) }+ @guardian.ensure_can_see_category!(@category) if @category+ @guardian.ensure_can_see_topic!(@topic) if @topicenddef scoped_usersusers = User.where(active: true)users = users.where(approved: true) if SiteSetting.must_approve_users?users = users.where(staged: false) unless @include_staged_users- users = users.not_suspended unless @searching_user&.staff?+ users = users.not_suspended unless @guardian.is_staff?if @groupsusers = users.joins(:group_users).where("group_users.group_id IN (?)", @groups.map(&:id))diff --git a/lib/guardian/user_guardian.rb b/lib/guardian/user_guardian.rbindex 680887515d8..be4fde3b517 100644--- a/lib/guardian/user_guardian.rb+++ b/lib/guardian/user_guardian.rb@@ -154,6 +154,10 @@ module UserGuardiantrueend+ def can_search_staged_users?(user_directory_search: false)+ is_staff? || (user_directory_search == true && SiteSetting.enable_user_directory?)+ end+def public_can_see_profiles?!SiteSetting.hide_user_profiles_from_public || !anonymous?enddiff --git a/spec/models/user_search_spec.rb b/spec/models/user_search_spec.rbindex 62c0a030c7d..3e3453f1d3d 100644--- a/spec/models/user_search_spec.rb+++ b/spec/models/user_search_spec.rb@@ -354,6 +354,12 @@ RSpec.describe UserSearch doexpect(results).to be_blankresults = search_for(staged.username, include_staged_users: true)+ expect(results).to be_blank++ results = search_for(staged.username, include_staged_users: true, searching_user: admin)+ expect(results).to eq [staged.username]++ results = search_for(staged.username, include_staged_users: true, user_directory_search: true)expect(results).to eq [staged.username]# mrb is omitted since they're the searching user@@ -361,6 +367,13 @@ RSpec.describe UserSearch doexpect(results).to eq [mr_pink, mr_orange].map(&:username)end+ it "does not return last-seen suggestions when staged users are included" do+ results =+ search_for("", include_staged_users: true, last_seen_users: true, searching_user: admin)++ expect(results).to be_blank+ end+it "works with last_seen_users option" doresults = search_for("", last_seen_users: true)diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rbindex b8f2fad638a..809f1144772 100644--- a/spec/requests/users_controller_spec.rb+++ b/spec/requests/users_controller_spec.rb@@ -6131,10 +6131,33 @@ RSpec.describe UsersController doendcontext "with `include_staged_users`" do- it "includes staged users when the param is true" do+ it "does not include staged users for public callers" doget "/u/search/users.json", params: { term: staged_user.name, include_staged_users: true }- json = response.parsed_body- expect(json["users"].map { |u| u["name"] }).to include(staged_user.name)++ expect(response.status).to eq(200)+ expect(+ response.parsed_body["users"].map { |found_user| found_user["name"] },+ ).not_to include(staged_user.name)+ end++ it "includes staged users for staff callers" do+ sign_in(Fabricate(:admin))++ get "/u/search/users.json", params: { term: staged_user.name, include_staged_users: true }++ expect(response.status).to eq(200)+ expect(response.parsed_body["users"].map { |found_user| found_user["name"] }).to include(+ staged_user.name,+ )+ end++ it "does not allow last-seen suggestions when staged users are included" do+ sign_in(Fabricate(:admin))++ get "/u/search/users.json", params: { include_staged_users: true, last_seen_users: true }++ expect(response.status).to eq(200)+ expect(response.parsed_body["users"]).to be_emptyendit "doesn't include staged users when the param is not passed" do