Skip to content

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

SECURITY: Enforce hidden tag visibility in `fetch_tag`

Alan Guo Xiang Tanad8e51f0d721
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index 99ad6d8f98f..3086e7dc297 100644
--- a/app/controllers/tags_controller.rb
+++ b/app/controllers/tags_controller.rb
@@ -10,8 +10,6 @@ class TagsController < ::ApplicationController
Discourse.anonymous_filters.map { |f| :"show_#{f}" }
end
- before_action :ensure_visible, only: [:show, :info, *show_methods]
-
requires_login except: [:index, :show, :tag_feed, :search, :info, *show_methods]
skip_before_action :check_xhr, only: [:tag_feed, :show, :index, *show_methods]
@@ -565,7 +563,7 @@ class TagsController < ::ApplicationController
elsif params[:tag_name].present?
Tag.find_by_name(params[:tag_name].force_encoding("UTF-8"))
end
- raise Discourse::NotFound if raise_not_found && @tag.nil?
+ raise Discourse::NotFound if @tag ? !guardian.can_see_tag?(@tag) : raise_not_found
@tag
end
@@ -612,12 +610,6 @@ class TagsController < ::ApplicationController
raise Discourse::NotFound unless SiteSetting.tagging_enabled?
end
- def ensure_visible
- if DiscourseTagging.hidden_tag_names(guardian).include?(params[:tag_name])
- raise Discourse::NotFound
- end
- end
-
def self.tag_counts_json(tags, guardian)
show_pm_tags = guardian.can_tag_pms?
target_tags = Tag.where(id: tags.map(&:target_tag_id).compact.uniq).select(:id, :name, :slug)
diff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rb
index 37bdd7562c4..35a31916f55 100644
--- a/spec/requests/tags_controller_spec.rb
+++ b/spec/requests/tags_controller_spec.rb
@@ -450,6 +450,9 @@ RSpec.describe TagsController do
get "/tag/test"
expect(response.status).to eq(404)
+ get "/tag/#{tag.id}.json"
+ expect(response.status).to eq(404)
+
sign_in(admin)
get "/tag/#{tag.slug}/#{tag.id}"
@@ -609,6 +612,9 @@ RSpec.describe TagsController do
_tag_group = Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: ["test"])
get "/tag/test/info.json"
expect(response.status).to eq(404)
+
+ get "/tag/#{tag.id}/info.json"
+ expect(response.status).to eq(404)
end
it "staff-only tags can be retrieved for staff user" do
@@ -686,6 +692,20 @@ RSpec.describe TagsController do
end
end
+ describe "#tag_feed" do
+ fab!(:tag) { Fabricate(:tag, name: "test") }
+
+ it "returns 404 if tag is staff-only" do
+ _tag_group = Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: ["test"])
+
+ get "/tag/test.rss"
+ expect(response.status).to eq(404)
+
+ get "/tag/#{tag.slug}/#{tag.id}.rss"
+ expect(response.status).to eq(404)
+ end
+ end
+
describe "#settings" do
fab!(:tag) { Fabricate(:tag, name: "test-tag", description: "test description") }
fab!(:synonym) { Fabricate(:tag, name: "test-synonym", target_tag: tag) }