Skip to content

Commit 391e9ec

Browse files
judithrothCopilot
andcommitted
[#71630] Ensure redirects without security issues
Suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent ba09157 commit 391e9ec

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
@@ -264,7 +264,18 @@ def find_project_by_project_id
264264
def redirect_if_historical_project_identifier(identifier_param_key)
265265
param = params[identifier_param_key]
266266
if request.get? && request.format.html? && param.friendly_id? && param != @project.identifier
267-
redirect_to url_for(params.to_unsafe_h.merge(identifier_param_key => @project.identifier)),
267+
# Reconstruct the URL from path + query parameters, and prevent user-supplied
268+
# options such as :host or :protocol from influencing the redirect target.
269+
safe_path_params = request.path_parameters.symbolize_keys
270+
safe_query_params = request.query_parameters.symbolize_keys
271+
272+
# Ensure the identifier in the path matches the current project identifier.
273+
safe_path_params[identifier_param_key] = @project.identifier
274+
275+
# Remove any URL option keys that could affect the redirect target.
276+
safe_query_params.except!(:host, :protocol, :subdomain, :domain, :port)
277+
278+
redirect_to url_for(safe_path_params.merge(safe_query_params).merge(only_path: true)),
268279
status: :moved_permanently
269280
end
270281
end

0 commit comments

Comments
 (0)