Skip to content

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

FIX: Issue 404 for invalid `/tags/c/...` routes

David Taylor80dd7695308d
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index 05e0f9b809a..84712001e53 100644
--- a/app/controllers/tags_controller.rb
+++ b/app/controllers/tags_controller.rb
@@ -375,14 +375,15 @@ class TagsController < ::ApplicationController
@filter_on_category = Category.query_category(slug_or_id, nil)
end
- guardian.ensure_can_see!(@filter_on_category)
-
if !@filter_on_category
permalink = Permalink.find_by_url("c/#{params[:category_slug_path_with_id]}")
if permalink.present? && permalink.category_id
return redirect_to "#{Discourse::base_path}/tags#{permalink.target_url}/#{params[:tag_id]}", status: :moved_permanently
end
+ end
+ if !(@filter_on_category && guardian.can_see?(@filter_on_category))
+ # 404 on 'access denied' to avoid leaking existence of category
raise Discourse::NotFound
end
end
diff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rb
index 17a4b969e5f..58b28f3870a 100644
--- a/spec/requests/tags_controller_spec.rb
+++ b/spec/requests/tags_controller_spec.rb
@@ -285,6 +285,19 @@ describe TagsController do
expect(response.parsed_body['topic_list']['more_topics_url'])
.to start_with("/tags/c/#{category.slug_path.join('/')}/#{category.id}/#{tag.name}")
end
+
+ it "should 404 for invalid category path" do
+ get "/tags/c/#{category.slug_path.join("/")}/#{category.id}/somerandomstring/#{tag.name}.json?per_page=1"
+
+ expect(response.status).to eq(404)
+ end
+
+ it "should 404 for secure categories" do
+ c = Fabricate(:private_category, group: Fabricate(:group))
+ get "/tags/c/#{c.slug_path.join("/")}/#{c.id}/#{tag.name}.json"
+
+ expect(response.status).to eq(404)
+ end
end
context "with a subcategory in the path" do