aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/query_methods.rb
diff options
context:
space:
mode:
authorBen Toews <mastahyeti@users.noreply.github.com>2017-02-08 11:23:26 -0700
committerMatthew Draper <matthew@trebex.net>2017-11-09 22:32:16 +1030
commitf989b341eccc6a86fd1ddfff7f1441920855c84e (patch)
tree9cde6c82ff135be475431e308c1f59b1d57a0cae /activerecord/lib/active_record/relation/query_methods.rb
parentbe6e1b8f7dbce1940f47339657faab2c1fdeaa54 (diff)
downloadrails-f989b341eccc6a86fd1ddfff7f1441920855c84e.tar.gz
rails-f989b341eccc6a86fd1ddfff7f1441920855c84e.tar.bz2
rails-f989b341eccc6a86fd1ddfff7f1441920855c84e.zip
add config to check arguments to unsafe AR methods
Diffstat (limited to 'activerecord/lib/active_record/relation/query_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 897ff5c8af..63b1d8e154 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -295,7 +295,22 @@ module ActiveRecord
spawn.order!(*args)
end
+ # Same as #order but allows raw SQL regardless of `allow_unsafe_raw_sql`
+ # config setting.
+ def unsafe_raw_order(*args) # :nodoc:
+ check_if_method_has_arguments!(:order, args)
+ spawn.unsafe_raw_order!(*args)
+ end
+
+ # Same as #order but operates on relation in-place instead of copying.
def order!(*args) # :nodoc:
+ restrict_order_args(args) unless klass.allow_unsafe_raw_sql == :enabled
+ unsafe_raw_order!(*args)
+ end
+
+ # Same as #order! but allows raw SQL regardless of `allow_unsafe_raw_sql`
+ # config setting.
+ def unsafe_raw_order!(*args) # :nodoc:
preprocess_order_args(args)
self.order_values += args
@@ -316,7 +331,22 @@ module ActiveRecord
spawn.reorder!(*args)
end
+ # Same as #reorder but allows raw SQL regardless of `allow_unsafe_raw_sql`
+ # config setting.
+ def unsafe_raw_reorder(*args) # :nodoc:
+ check_if_method_has_arguments!(:reorder, args)
+ spawn.unsafe_raw_reorder!(*args)
+ end
+
+ # Same as #reorder but operates on relation in-place instead of copying.
def reorder!(*args) # :nodoc:
+ restrict_order_args(args) unless klass.allow_unsafe_raw_sql == :enabled
+ unsafe_raw_reorder!
+ end
+
+ # Same as #reorder! but allows raw SQL regardless of `allow_unsafe_raw_sql`
+ # config setting.
+ def unsafe_raw_reorder!(*args) # :nodoc:
preprocess_order_args(args)
self.reordering_value = true
@@ -1139,6 +1169,25 @@ module ActiveRecord
end.flatten!
end
+ # Only allow column names and directions as arguments to #order and
+ # #reorder. Other arguments will cause an ArugmentError to be raised.
+ def restrict_order_args(args)
+ args = args.dup
+ orderings = args.extract_options!
+ columns = args | orderings.keys
+
+ unrecognized = columns.reject { |c| klass.respond_to_attribute?(c) }
+ if unrecognized.any?
+ raise ArgumentError, "Invalid order column: #{unrecognized}"
+ end
+
+ # TODO: find a better list of modifiers.
+ unrecognized = orderings.values.reject { |d| VALID_DIRECTIONS.include?(d.to_s) }
+ if unrecognized.any?
+ raise ArgumentError, "Invalid order direction: #{unrecognized}"
+ end
+ end
+
# Checks to make sure that the arguments are not blank. Note that if some
# blank-like object were initially passed into the query method, then this
# method will not raise an error.