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.rbindex cd680bfe619..95ef9764085 100644--- a/app/controllers/tags_controller.rb+++ b/app/controllers/tags_controller.rb@@ -163,6 +163,7 @@ class TagsController < ::ApplicationControllerDiscourse.filters.each do |filter|define_method("show_#{filter}") dofetch_tag(raise_not_found: false)+ raise Discourse::NotFound if params[:tag_id].present? && @tag.blank?if @tagif @tag.target_tag_id.present? && @tag.target_tag_id != @tag.iddiff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rbindex bc581890107..1006e0b2bce 100644--- a/spec/requests/tags_controller_spec.rb+++ b/spec/requests/tags_controller_spec.rb@@ -569,6 +569,11 @@ RSpec.describe TagsController doexpect(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" donumeric_tag_name = (Tag.maximum(:id).to_i + 10_000).to_snumeric_tag = Fabricate(:tag, name: numeric_tag_name)