Skip to content

Commit 72416d2

Browse files
committed
Don't pass unsafe SQL to .order
1 parent e2eeb65 commit 72416d2

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

test/cases/coerced_tests.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,16 +671,16 @@ class RelationTest < ActiveRecord::TestCase
671671
# Use LEN vs LENGTH function.
672672
coerce_tests! :test_reverse_order_with_function
673673
def test_reverse_order_with_function_coerced
674-
topics = Topic.order("LEN(title)").reverse_order
674+
topics = Topic.order(Arel.sql("LEN(title)")).reverse_order
675675
assert_equal topics(:second).title, topics.first.title
676676
end
677677

678678
# Use LEN vs LENGTH function.
679679
coerce_tests! :test_reverse_order_with_function_other_predicates
680680
def test_reverse_order_with_function_other_predicates_coerced
681-
topics = Topic.order("author_name, LEN(title), id").reverse_order
681+
topics = Topic.order(Arel.sql("author_name, LEN(title), id")).reverse_order
682682
assert_equal topics(:second).title, topics.first.title
683-
topics = Topic.order("LEN(author_name), id, LEN(title)").reverse_order
683+
topics = Topic.order(Arel.sql("LEN(author_name), id, LEN(title)")).reverse_order
684684
assert_equal topics(:fifth).title, topics.first.title
685685
end
686686

test/cases/order_test_sqlserver.rb

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ class OrderTestSQLServer < ActiveRecord::TestCase
4444
it 'support quoted column' do
4545
order = "[title]"
4646
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
47-
assert_equal post1, Post.order(order).first
47+
assert_equal post1, Post.order(Arel.sql(order)).first
4848
end
4949

5050
it 'support quoted table and column' do
5151
order = "[posts].[title]"
5252
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
53-
assert_equal post1, Post.order(order).first
53+
assert_equal post1, Post.order(Arel.sql(order)).first
5454
end
5555

5656
it 'support primary: column, secondary: column' do
@@ -73,74 +73,74 @@ class OrderTestSQLServer < ActiveRecord::TestCase
7373
order = "(CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END) DESC, body"
7474
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
7575
post2 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
76-
assert_equal post1, Post.order(order).first
77-
assert_equal post2, Post.order(order).second
76+
assert_equal post1, Post.order(Arel.sql(order)).first
77+
assert_equal post2, Post.order(Arel.sql(order)).second
7878
end
7979

8080
it 'support primary: quoted table and column, secondary: case expresion' do
8181
order = "[posts].[body] DESC, (CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END) DESC"
8282
post1 = Post.create title: 'ZZZ Post', body: 'ZZZ Test cased orders.'
8383
post2 = Post.create title: 'ZZY Post', body: 'ZZZ Test cased orders.'
84-
assert_equal post1, Post.order(order).first
85-
assert_equal post2, Post.order(order).second
84+
assert_equal post1, Post.order(Arel.sql(order)).first
85+
assert_equal post2, Post.order(Arel.sql(order)).second
8686
end
8787

8888
it 'support inline function' do
8989
order = "LEN(title)"
9090
post1 = Post.create title: 'A', body: 'AAA Test cased orders.'
91-
assert_equal post1, Post.order(order).first
91+
assert_equal post1, Post.order(Arel.sql(order)).first
9292
end
9393

9494
it 'support inline function with parameters' do
9595
order = "SUBSTRING(title, 1, 3)"
9696
post1 = Post.create title: 'AAA Post', body: 'Test cased orders.'
97-
assert_equal post1, Post.order(order).first
97+
assert_equal post1, Post.order(Arel.sql(order)).first
9898
end
9999

100100
it 'support inline function with parameters DESC' do
101101
order = "SUBSTRING(title, 1, 3) DESC"
102102
post1 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
103-
assert_equal post1, Post.order(order).first
103+
assert_equal post1, Post.order(Arel.sql(order)).first
104104
end
105105

106106
it 'support primary: inline function, secondary: column' do
107107
order = "LEN(title), body"
108108
post1 = Post.create title: 'A', body: 'AAA Test cased orders.'
109109
post2 = Post.create title: 'A', body: 'Test cased orders.'
110-
assert_equal post1, Post.order(order).first
111-
assert_equal post2, Post.order(order).second
110+
assert_equal post1, Post.order(Arel.sql(order)).first
111+
assert_equal post2, Post.order(Arel.sql(order)).second
112112
end
113113

114114
it 'support primary: inline function, secondary: column with direction' do
115115
order = "LEN(title) ASC, body DESC"
116116
post1 = Post.create title: 'A', body: 'ZZZ Test cased orders.'
117117
post2 = Post.create title: 'A', body: 'Test cased orders.'
118-
assert_equal post1, Post.order(order).first
119-
assert_equal post2, Post.order(order).second
118+
assert_equal post1, Post.order(Arel.sql(order)).first
119+
assert_equal post2, Post.order(Arel.sql(order)).second
120120
end
121121

122122
it 'support primary: column, secondary: inline function' do
123123
order = "body DESC, LEN(title)"
124124
post1 = Post.create title: 'Post', body: 'ZZZ Test cased orders.'
125125
post2 = Post.create title: 'Longer Post', body: 'ZZZ Test cased orders.'
126-
assert_equal post1, Post.order(order).first
127-
assert_equal post2, Post.order(order).second
126+
assert_equal post1, Post.order(Arel.sql(order)).first
127+
assert_equal post2, Post.order(Arel.sql(order)).second
128128
end
129129

130130
it 'support primary: case expression, secondary: inline function' do
131131
order = "CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END DESC, LEN(body) ASC"
132132
post1 = Post.create title: 'ZZZ Post', body: 'Z'
133133
post2 = Post.create title: 'ZZZ Post', body: 'Test cased orders.'
134-
assert_equal post1, Post.order(order).first
135-
assert_equal post2, Post.order(order).second
134+
assert_equal post1, Post.order(Arel.sql(order)).first
135+
assert_equal post2, Post.order(Arel.sql(order)).second
136136
end
137137

138138
it 'support primary: inline function, secondary: case expression' do
139139
order = "LEN(body), CASE WHEN [title] LIKE N'ZZZ%' THEN title ELSE '' END DESC"
140140
post1 = Post.create title: 'ZZZ Post', body: 'Z'
141141
post2 = Post.create title: 'Post', body: 'Z'
142-
assert_equal post1, Post.order(order).first
143-
assert_equal post2, Post.order(order).second
142+
assert_equal post1, Post.order(Arel.sql(order)).first
143+
assert_equal post2, Post.order(Arel.sql(order)).second
144144
end
145145

146146

0 commit comments

Comments
 (0)