Skip to content

Commit ebf14dc

Browse files
authored
Accept nested functions in Dangerous Query Methods (#1082)
See rails/rails#44010
1 parent 440c5da commit ebf14dc

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

lib/active_record/connection_adapters/sqlserver/quoting.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def column_name_with_order_matcher
8585
(
8686
(?:
8787
# [database_name].[database_owner].[table_name].[column_name] | function(one or no argument)
88-
((?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+|\[\w+\])) | \w+\((?:|\g<2>)\)
88+
((?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+|\[\w+\]) | \w+\((?:|\g<2>)\))
8989
)
9090
(?:\s+AS\s+(?:\w+|\[\w+\]))?
9191
)
@@ -98,7 +98,7 @@ def column_name_with_order_matcher
9898
(
9999
(?:
100100
# [database_name].[database_owner].[table_name].[column_name] | function(one or no argument)
101-
((?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+|\[\w+\])) | \w+\((?:|\g<2>)\)
101+
((?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+|\[\w+\]) | \w+\((?:|\g<2>)\))
102102
)
103103
(?:\s+ASC|\s+DESC)?
104104
(?:\s+NULLS\s+(?:FIRST|LAST))?

test/cases/coerced_tests.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,27 @@ class UnsafeRawSqlTest < ActiveRecord::TestCase
17661766
assert_equal ids_expected, ids
17671767
end
17681768

1769+
# Use LEN() vs length() function.
1770+
coerce_tests! %r{order: allows nested functions}
1771+
test "order: allows nested functions" do
1772+
ids_expected = Post.order(Arel.sql("author_id, len(trim(title))")).pluck(:id)
1773+
1774+
# $DEBUG = true
1775+
ids = Post.order("author_id, len(trim(title))").pluck(:id)
1776+
1777+
assert_equal ids_expected, ids
1778+
end
1779+
1780+
# Use LEN() vs length() function.
1781+
coerce_tests! %r{pluck: allows nested functions}
1782+
test "pluck: allows nested functions" do
1783+
title_lengths_expected = Post.pluck(Arel.sql("len(trim(title))"))
1784+
1785+
title_lengths = Post.pluck("len(trim(title))")
1786+
1787+
assert_equal title_lengths_expected, title_lengths
1788+
end
1789+
17691790
test "order: allows string column names that are quoted" do
17701791
ids_expected = Post.order(Arel.sql("id")).pluck(:id)
17711792

@@ -2157,7 +2178,7 @@ def test_in_order_of_with_enums_keys_coerced
21572178
coerce_tests! :test_in_order_of_with_nil
21582179
def test_in_order_of_with_nil_coerced
21592180
Book.connection.remove_index(:books, column: [:author_id, :name])
2160-
2181+
21612182
original_test_in_order_of_with_nil
21622183
ensure
21632184
Book.where(author_id: nil, name: nil).delete_all

0 commit comments

Comments
 (0)