A platform for community discussion. Free, open, simple.
SECURITY: Apply category tag visibility to full-text search (#41668)
Gabriel Grubba9a094a1e5cfc
diff --git a/lib/search.rb b/lib/search.rbindex a3a2f55c58c..899e00c7b1a 100644--- a/lib/search.rb+++ b/lib/search.rb@@ -1200,16 +1200,14 @@ class Searchdef tags_searchreturn unless SiteSetting.tagging_enabledtags =- Tag+ DiscourseTagging+ .visible_tags(@guardian).includes(:tag_search_data).where("tag_search_data.search_data @@ #{ts_query}").references(:tag_search_data).order("name asc").limit(limit)-- hidden_tag_names = DiscourseTagging.hidden_tag_names(@guardian)-- tags.each { |tag| @results.add(tag) if !hidden_tag_names.include?(tag.name) }+ .each { |tag| @results.add(tag) }enddef exclude_topics_searchdiff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rbindex f55a99f04f6..06a67819948 100644--- a/spec/lib/search_spec.rb+++ b/spec/lib/search_spec.rb@@ -1876,7 +1876,7 @@ RSpec.describe Search doexpect(search.tags.map(&:name)).to eq([tag.name, "#{tag.name}9"])end- it "includes category-restricted tags" do+ it "filters category-restricted tags based on category access" docategory_tag = Fabricate(:tag, name: "#{tag.name}9")tag_group.tags = [category_tag]category.set_permissions(admins: :full)@@ -1886,7 +1886,7 @@ RSpec.describe Search doexpect(Search.execute(tag.name, guardian: Guardian.new(admin)).tags).to eq([tag, category_tag],)- expect(search.tags).to eq([tag, category_tag])+ expect(search.tags).to eq([tag])endendenddiff --git a/spec/requests/search_controller_spec.rb b/spec/requests/search_controller_spec.rbindex 41a3b374dcb..aaae6fd09d0 100644--- a/spec/requests/search_controller_spec.rb+++ b/spec/requests/search_controller_spec.rb@@ -153,6 +153,78 @@ RSpec.describe SearchController doexpect(data["users"][0]["id"]).to eq(user.id)end+ context "with category-restricted tags" do+ fab!(:tag_access_group, :group)+ fab!(:private_category) { Fabricate(:private_category, group: tag_access_group) }+ fab!(:category_tag) do+ Fabricate(:tag, name: "classified-category-tag", description: "Category tag description")+ end+ fab!(:category_tag_group_tag) do+ Fabricate(+ :tag,+ name: "classified-category-tag-group",+ description: "Category tag group description",+ )+ end+ fab!(:tag_group) { Fabricate(:tag_group, tags: [category_tag_group_tag]) }+ fab!(:regular_user, :user)+ fab!(:authorized_user, :user)+ fab!(:admin)++ before do+ SiteSetting.tagging_enabled = true+ CategoryTag.create!(category: private_category, tag: category_tag)+ CategoryTagGroup.create!(category: private_category, tag_group: tag_group)+ tag_access_group.add(authorized_user)+ SearchIndexer.index(category_tag, force: true)+ SearchIndexer.index(category_tag_group_tag, force: true)+ end++ it "does not expose either restriction type to anonymous users" do+ get "/search/query.json", params: { term: "classified" }++ expect(response).to have_http_status(:ok)+ expect(response.parsed_body["tags"]).to be_empty+ end++ it "does not expose either restriction type to unauthorized regular users" do+ sign_in(regular_user)++ get "/search/query.json", params: { term: "classified" }++ expect(response).to have_http_status(:ok)+ expect(response.parsed_body["tags"]).to be_empty+ end++ it "exposes both restriction types to users who can access the category" do+ sign_in(authorized_user)++ get "/search/query.json", params: { term: "classified" }++ expect(response).to have_http_status(:ok)+ expect(+ response.parsed_body["tags"].map { |tag| tag.values_at("name", "description") },+ ).to contain_exactly(+ [category_tag.name, category_tag.description],+ [category_tag_group_tag.name, category_tag_group_tag.description],+ )+ end++ it "exposes both restriction types to admins" do+ sign_in(admin)++ get "/search/query.json", params: { term: "classified" }++ expect(response).to have_http_status(:ok)+ expect(+ response.parsed_body["tags"].map { |tag| tag.values_at("name", "description") },+ ).to contain_exactly(+ [category_tag.name, category_tag.description],+ [category_tag_group_tag.name, category_tag_group_tag.description],+ )+ end+ end+context "when searching by topic id" doit "should not be restricted by minimum search term length" doSiteSetting.min_search_term_length = 20_000