Skip to content

Commit d01ab84

Browse files
author
OuYangJinTing
committed
[AR] Fix typo of disallow_raw_sql! exception msg
1 parent 2f012f7 commit d01ab84

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

activerecord/lib/active_record/sanitization.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def sanitize_sql_for_assignment(assignments, default_table_name = table_name)
5454
# Accepts an array, or string of SQL conditions and sanitizes
5555
# them into a valid SQL fragment for an ORDER clause.
5656
#
57-
# sanitize_sql_for_order(["field(id, ?)", [1,3,2]])
57+
# sanitize_sql_for_order([Arel.sql("field(id, ?)"), [1,3,2]])
5858
# # => "field(id, 1,3,2)"
5959
#
6060
# sanitize_sql_for_order("id ASC")
@@ -143,8 +143,12 @@ def disallow_raw_sql!(args, permit: connection.column_name_matcher) # :nodoc:
143143

144144
if unexpected
145145
raise(ActiveRecord::UnknownAttributeReference,
146-
"Query method called with non-attribute argument(s): " +
147-
unexpected.map(&:inspect).join(", ")
146+
"Dangerous query method (method whose arguments are used as raw " \
147+
"SQL) called with non-attribute argument(s): " \
148+
"#{unexpected.map(&:inspect).join(", ")}." \
149+
"This method should not be called with user-provided values, such as request " \
150+
"parameters or model attributes. Known-safe values can be passed " \
151+
"by wrapping them in Arel.sql()."
148152
)
149153
end
150154
end

activerecord/test/cases/sanitize_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ def self.search_as_method(term)
9595
end
9696
end
9797

98+
def test_disallow_raw_sql_with_unknown_attribute_string
99+
assert_raise(ActiveRecord::UnknownAttributeReference) { Binary.disallow_raw_sql!(["field(id, ?)"]) }
100+
end
101+
102+
def test_disallow_raw_sql_with_unknown_attribute_sql_literal
103+
assert_nothing_raised { Binary.disallow_raw_sql!([Arel.sql("field(id, ?)")]) }
104+
end
105+
98106
def test_bind_arity
99107
assert_nothing_raised { bind "" }
100108
assert_raise(ActiveRecord::PreparedStatementInvalid) { bind "", 1 }

activerecord/test/cases/unsafe_raw_sql_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class UnsafeRawSqlTest < ActiveRecord::TestCase
171171
Post.order("REPLACE(title, 'misc', 'zzzz')")
172172
end
173173

174-
assert_match(/Query method called with non-attribute argument\(s\):/, e.message)
174+
assert_match(/Dangerous query method \(method whose arguments are used as raw SQL\) called with non-attribute argument\(s\):/, e.message)
175175
end
176176

177177
test "pluck: allows string column name" do
@@ -269,6 +269,6 @@ class UnsafeRawSqlTest < ActiveRecord::TestCase
269269
Post.includes(:comments).pluck(:title, "REPLACE(title, 'misc', 'zzzz')")
270270
end
271271

272-
assert_match(/Query method called with non-attribute argument\(s\):/, e.message)
272+
assert_match(/Dangerous query method \(method whose arguments are used as raw SQL\) called with non-attribute argument\(s\):/, e.message)
273273
end
274274
end

0 commit comments

Comments
 (0)