Skip to content

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

SECURITY: `enable_names` should be respected in Search

Gabriel Grubbae936a523b590
diff --git a/lib/search.rb b/lib/search.rb
index c38ef231a40..0fa414a559c 100644
--- a/lib/search.rb
+++ b/lib/search.rb
@@ -1104,13 +1104,16 @@ class Search
def user_search
return if SiteSetting.hide_user_profiles_from_public && [email protected]
+ # Exlude B weight from search which is the weight `User#name` is indexed with in `SearchIndexer.update_users_index`
+ query = ts_query("simple", weight_filter: SiteSetting.enable_names ? nil : "AC")
+
users =
User
.includes(:user_search_data)
.references(:user_search_data)
.where(active: true)
.where(staged: false)
- .where("user_search_data.search_data @@ #{ts_query("simple")}")
+ .where("user_search_data.search_data @@ #{query}")
.order("CASE WHEN username_lower = '#{@original_term.downcase}' THEN 0 ELSE 1 END")
.order("last_posted_at DESC")
.limit(limit)
diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rb
index 3df973329bb..ca05143c39d 100644
--- a/spec/lib/search_spec.rb
+++ b/spec/lib/search_spec.rb
@@ -291,6 +291,64 @@ RSpec.describe Search do
expect(result.users).to contain_exactly(suspended_user)
end
end
+
+ context "when SiteSetting.enable_names is disabled" do
+ fab!(:evil_trout) { Fabricate(:user, username: "evil_trout", name: "John Doe") }
+
+ before do
+ SiteSetting.enable_names = false
+ SearchIndexer.index(evil_trout, force: true)
+ end
+
+ it "finds users by their usernames only" do
+ result = Search.execute("evil", guardian: Guardian.new(user2))
+ expect(result.users).to contain_exactly(evil_trout)
+
+ result = Search.execute("trout", guardian: Guardian.new(user2))
+ expect(result.users).to contain_exactly(evil_trout)
+
+ result = Search.execute("evil_trout", guardian: Guardian.new(user2))
+ expect(result.users).to contain_exactly(evil_trout)
+
+ result = Search.execute("john", guardian: Guardian.new(user2))
+ expect(result.users).to be_empty
+
+ result = Search.execute("doe", guardian: Guardian.new(user2))
+ expect(result.users).to be_empty
+
+ result = Search.execute("john doe", guardian: Guardian.new(user2))
+ expect(result.users).to be_empty
+ end
+ end
+
+ context "when SiteSetting.enable_names is enabled" do
+ fab!(:evil_trout) { Fabricate(:user, username: "evil_trout", name: "John Doe") }
+
+ before do
+ SiteSetting.enable_names = true
+ SearchIndexer.index(evil_trout, force: true)
+ end
+
+ it "finds users by their usernames and names" do
+ result = Search.execute("evil", guardian: Guardian.new(user2))
+ expect(result.users).to contain_exactly(evil_trout)
+
+ result = Search.execute("trout", guardian: Guardian.new(user2))
+ expect(result.users).to contain_exactly(evil_trout)
+
+ result = Search.execute("evil_trout", guardian: Guardian.new(user2))
+ expect(result.users).to contain_exactly(evil_trout)
+
+ result = Search.execute("john", guardian: Guardian.new(user2))
+ expect(result.users).to contain_exactly(evil_trout)
+
+ result = Search.execute("doe", guardian: Guardian.new(user2))
+ expect(result.users).to contain_exactly(evil_trout)
+
+ result = Search.execute("john doe", guardian: Guardian.new(user2))
+ expect(result.users).to contain_exactly(evil_trout)
+ end
+ end
end
describe "categories" do