Skip to content

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

FIX: Prevent tag page render crash on invalid numeric tag id routes (#40201)

Roman Rizzi8baf80625763
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
index cd680bfe619..95ef9764085 100644
--- a/app/controllers/tags_controller.rb
+++ b/app/controllers/tags_controller.rb
@@ -163,6 +163,7 @@ class TagsController < ::ApplicationController
Discourse.filters.each do |filter|
define_method("show_#{filter}") do
fetch_tag(raise_not_found: false)
+ raise Discourse::NotFound if params[:tag_id].present? && @tag.blank?
if @tag
if @tag.target_tag_id.present? && @tag.target_tag_id != @tag.id
diff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rb
index bc581890107..1006e0b2bce 100644
--- a/spec/requests/tags_controller_spec.rb
+++ b/spec/requests/tags_controller_spec.rb
@@ -569,6 +569,11 @@ RSpec.describe TagsController do
expect(response.body).not_to include("ActionView::Template::Error")
end
+ it "returns 404 for missing numeric /tag/:tag_id routes" do
+ get "/tag/9999999"
+ expect(response.status).to eq(404)
+ end
+
it "redirects slug routes for numeric tag names to the canonical slug/id URL" do
numeric_tag_name = (Tag.maximum(:id).to_i + 10_000).to_s
numeric_tag = Fabricate(:tag, name: numeric_tag_name)