DEV: Modifier to add params to TopicsController redirect url (#26470)

This commit is contained in:
Mark VanLandingham 2024-04-02 15:35:44 -05:00 committed by GitHub
parent 51006b5591
commit 797ab30d95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 1 deletions

View File

@ -1255,7 +1255,19 @@ class TopicsController < ApplicationController
raise(SiteSetting.detailed_404 ? ex : Discourse::NotFound) raise(SiteSetting.detailed_404 ? ex : Discourse::NotFound)
end end
opts = params.slice(:page, :print, :filter_top_level_replies, :preview_theme_id) # Allow plugins to append allowed query parameters, so they aren't scrubbed on redirect to proper topic URL
additional_allowed_query_parameters =
DiscoursePluginRegistry.apply_modifier(
:redirect_to_correct_topic_additional_query_parameters,
[],
)
opts =
params.slice(
*%i[page print filter_top_level_replies preview_theme_id].concat(
additional_allowed_query_parameters,
),
)
opts.delete(:page) if params[:page] == 0 opts.delete(:page) if params[:page] == 0
url = topic.relative_url url = topic.relative_url

View File

@ -2317,6 +2317,12 @@ RSpec.describe TopicsController do
expect(response).to redirect_to("#{topic.relative_url}/42?page=1") expect(response).to redirect_to("#{topic.relative_url}/42?page=1")
end end
it "scrubs invalid query parameters when redirecting" do
get "/t/#{topic.slug}", params: { silly_param: "hehe" }
expect(response).to redirect_to(topic.relative_url)
end
it "returns 404 when an invalid slug is given and no id" do it "returns 404 when an invalid slug is given and no id" do
get "/t/nope-nope.json" get "/t/nope-nope.json"
@ -2386,6 +2392,28 @@ RSpec.describe TopicsController do
expect(second_request_queries.count).to eq(first_request_queries.count) expect(second_request_queries.count).to eq(first_request_queries.count)
end end
context "with registered redirect_to_correct_topic_additional_query_parameters" do
let(:modifier_block) { Proc.new { |allowed_params| allowed_params << :silly_param } }
it "retains the permitted query param when redirecting" do
plugin_instance = Plugin::Instance.new
plugin_instance.register_modifier(
:redirect_to_correct_topic_additional_query_parameters,
&modifier_block
)
get "/t/#{topic.slug}", params: { silly_param: "hehe" }
expect(response).to redirect_to("#{topic.relative_url}?silly_param=hehe")
ensure
DiscoursePluginRegistry.unregister_modifier(
plugin_instance,
:redirect_to_correct_topic_additional_query_parameters,
&modifier_block
)
end
end
context "when a topic with nil slug exists" do context "when a topic with nil slug exists" do
before do before do
nil_slug_topic = Fabricate(:topic) nil_slug_topic = Fabricate(:topic)