SECURITY: Hide user profiles from public

User profiles, including the summary, should be private to anonymous
users if hide_user_profiles_from_public is enabled.
This commit is contained in:
Bianca Nenciu 2023-10-16 10:51:25 -04:00 committed by Penar Musaraj
parent 265b3dbb4c
commit c9888163d7
No known key found for this signature in database
GPG Key ID: E390435D881FF0F7
3 changed files with 26 additions and 2 deletions

View File

@ -106,7 +106,7 @@ class UsersController < ApplicationController
end
def show(for_card: false)
return redirect_to path("/login") if SiteSetting.hide_user_profiles_from_public && !current_user
return redirect_to path("/login") if !guardian.public_can_see_profiles?
@user =
fetch_user_from_params(
@ -155,7 +155,7 @@ class UsersController < ApplicationController
# This route is not used in core, but is used by theme components (e.g. https://meta.discourse.org/t/144479)
def cards
return redirect_to path("/login") if SiteSetting.hide_user_profiles_from_public && !current_user
return redirect_to path("/login") if !guardian.public_can_see_profiles?
user_ids = params.require(:user_ids).split(",").map(&:to_i)
raise Discourse::InvalidParameters.new(:user_ids) if user_ids.length > 50
@ -484,6 +484,8 @@ class UsersController < ApplicationController
end
def summary
return redirect_to path("/login") if !guardian.public_can_see_profiles?
@user =
fetch_user_from_params(
include_inactive:

View File

@ -122,6 +122,10 @@ module UserGuardian
true
end
def public_can_see_profiles?
!SiteSetting.hide_user_profiles_from_public || !anonymous?
end
def can_see_profile?(user)
return false if user.blank?
return true if !SiteSetting.allow_users_to_hide_profile?

View File

@ -4087,6 +4087,24 @@ RSpec.describe UsersController do
expect(json["user_summary"]["post_count"]).to eq(0)
end
context "when `hide_user_profiles_from_public` site setting is enabled" do
before { SiteSetting.hide_user_profiles_from_public = true }
it "returns 200 for logged in users" do
sign_in(Fabricate(:user))
get "/u/#{user.username_lower}/summary.json"
expect(response.status).to eq(200)
end
it "returns 403 for anonymous users" do
get "/u/#{user.username_lower}/summary.json"
expect(response).to redirect_to "/login"
end
end
context "when `hide_profile_and_presence` user option is checked" do
before_all { user1.user_option.update_columns(hide_profile_and_presence: true) }