Skip to content

Commit

Permalink
Resolve sql injection warnings from brakeman
Browse files Browse the repository at this point in the history
  • Loading branch information
delonnewman committed Oct 9, 2024
1 parent 3099edd commit f7cb58f
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
3 changes: 3 additions & 0 deletions app/models/dragnet/type.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module Dragnet
class Type
# include sql sanitation methods from ActiveRecord
include ActiveRecord::Sanitization::ClassMethods

attr_reader :question_type

def initialize(question_type)
Expand Down
2 changes: 1 addition & 1 deletion app/models/dragnet/type/boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Dragnet
class Type::Boolean < Type
def data_grid_sort(_question, scope, direction, join_name)
scope.order(Arel.sql("#{join_name}.boolean_value") => direction)
scope.order(sanitize_sql_for_order("#{join_name}.boolean_value") => direction)
end

def data_grid_filter(_question, scope, table, value)
Expand Down
2 changes: 1 addition & 1 deletion app/models/dragnet/type/choice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Dragnet
class Type::Choice < Type
def data_grid_sort(_question, scope, direction, join_name)
scope.order(Arel.sql("#{join_name}.question_option_id") => direction)
scope.order(sanitize_sql_for_order("#{join_name}.question_option_id") => direction)
end

def data_grid_filter(_question, scope, table, value)
Expand Down
4 changes: 2 additions & 2 deletions app/models/dragnet/type/number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module Dragnet
class Type::Number < Type
def data_grid_sort(question, scope, direction, join_name)
if question.settings.decimal?
scope.order(Arel.sql("#{join_name}.float_value") => direction)
scope.order(sanitize_sql_for_order("#{join_name}.float_value") => direction)
else
scope.order(Arel.sql("#{join_name}.integer_value") => direction)
scope.order(sanitize_sql_for_order("#{join_name}.integer_value") => direction)
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/models/dragnet/type/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ module Dragnet
class Type::Text < Type
def data_grid_sort(question, scope, direction, join_name)
if question.settings.long_answer?
scope.order(Arel.sql("#{join_name}.long_text_value") => direction)
scope.order(sanitize_sql_for_order("#{join_name}.long_text_value") => direction)
else
scope.order(Arel.sql("#{join_name}.short_text_value") => direction)
scope.order(sanitize_sql_for_order("#{join_name}.short_text_value") => direction)
end
end

Expand Down
9 changes: 6 additions & 3 deletions app/models/dragnet/type/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ module Dragnet
class Type::Time < Type
def data_grid_sort(question, scope, direction, join_name)
if question.settings.include_date_and_time?
scope.order(Arel.sql("#{join_name}.date_value") => direction, Arel.sql("#{join_name}.time_value") => direction)
scope.order(
sanitize_sql_for_order("#{join_name}.date_value") => direction,
sanitize_sql_for_order("#{join_name}.time_value") => direction
)
elsif question.settings.include_time?
scope.order(Arel.sql("#{join_name}.time_value") => direction)
scope.order(sanitize_sql_for_order("#{join_name}.time_value") => direction)
else
scope.order(Arel.sql("#{join_name}.date_value") => direction)
scope.order(sanitize_sql_for_order("#{join_name}.date_value") => direction)
end
end

Expand Down

0 comments on commit f7cb58f

Please sign in to comment.