Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#59184] use Arel to build the query to get rid of injection warnings

Tobias Dillmanne8ba609997e4
diff --git a/app/models/queries/projects/orders/life_cycle_step_order.rb b/app/models/queries/projects/orders/life_cycle_step_order.rb
index 2a279f6e38f..11899c5a1bb 100644
--- a/app/models/queries/projects/orders/life_cycle_step_order.rb
+++ b/app/models/queries/projects/orders/life_cycle_step_order.rb
@@ -75,11 +75,29 @@ class Queries::Projects::Orders::LifeCycleStepOrder < Queries::Orders::Base
def order(scope)
with_raise_on_invalid do
- # Note that a gate does not define an end_date. This code still works.
- direction_clause = Arel.sql("#{cte_name}.start_date #{direction}, #{cte_name}.end_date #{direction}")
-
- scope.where("#{cte_name}.def_id = :def_id OR #{cte_name}.def_id IS NULL", def_id: life_cycle_step_definition.id)
- .order(direction_clause)
+ scope.where(order_condition)
+ .order(*order_by_start_and_end_date)
end
end
+
+ def order_condition
+ # To avoid SQL injection warnings, we use Arel to build the condition.
+ # Note that this SQL query uses the CTE defined in `cte_statement`.
+ steps_cte = Arel::Table.new(cte_name.to_s)
+
+ # WHERE cte_name.def_id = life_cycle_step_definition.id OR cte_name.def_id IS NULL
+ steps_cte[:def_id]
+ .eq(life_cycle_step_definition.id)
+ .or(steps_cte[:def_id].eq(nil))
+ end
+
+ def order_by_start_and_end_date
+ steps_table = Arel::Table.new(cte_name.to_s)
+
+ # Even though a gate does not define an end_date, this code still works.
+ [
+ steps_table[:start_date].send(direction),
+ steps_table[:end_date].send(direction)
+ ]
+ end
end