Skip to content

Commit 84633e8

Browse files
judithrothCopilot
andauthored
[#72917] Improve / harden redirect logic for historic identifiers
Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent fc37981 commit 84633e8

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

app/controllers/application_controller.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,18 @@ def find_project_by_project_id
263263
def redirect_if_historical_project_identifier(identifier_param_key)
264264
param = params[identifier_param_key]
265265
if request.get? && param.friendly_id? && param != @project.identifier
266-
redirect_to url_for(params.to_unsafe_h.merge(identifier_param_key => @project.identifier)),
266+
# Reconstruct the URL from path + query parameters, and prevent user-supplied
267+
# options such as :host or :protocol from influencing the redirect target.
268+
safe_path_params = request.path_parameters.symbolize_keys
269+
safe_query_params = request.query_parameters.symbolize_keys
270+
271+
# Ensure the identifier in the path matches the current project identifier.
272+
safe_path_params[identifier_param_key] = @project.identifier
273+
274+
# Remove any URL option keys that could affect the redirect target.
275+
safe_query_params.except!(:host, :protocol, :subdomain, :domain, :port)
276+
277+
redirect_to url_for(safe_path_params.merge(safe_query_params).merge(only_path: true)),
267278
status: :moved_permanently
268279
end
269280
end

0 commit comments

Comments
 (0)