Skip to content

Commit

Permalink
Accept nested functions in Dangerous Query Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
aidanharan committed Sep 13, 2023
1 parent 440c5da commit c015641
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/active_record/connection_adapters/sqlserver/quoting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def column_name_with_order_matcher
(
(?:
# [database_name].[database_owner].[table_name].[column_name] | function(one or no argument)
((?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+|\[\w+\])) | \w+\((?:|\g<2>)\)
((?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+|\[\w+\]) | \w+\((?:|\g<2>)\))
)
(?:\s+AS\s+(?:\w+|\[\w+\]))?
)
Expand All @@ -98,7 +98,7 @@ def column_name_with_order_matcher
(
(?:
# [database_name].[database_owner].[table_name].[column_name] | function(one or no argument)
((?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+|\[\w+\])) | \w+\((?:|\g<2>)\)
((?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+\.|\[\w+\]\.)?(?:\w+|\[\w+\]) | \w+\((?:|\g<2>)\))
)
(?:\s+ASC|\s+DESC)?
(?:\s+NULLS\s+(?:FIRST|LAST))?
Expand Down
23 changes: 22 additions & 1 deletion test/cases/coerced_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,27 @@ class UnsafeRawSqlTest < ActiveRecord::TestCase
assert_equal ids_expected, ids
end

# Use LEN() vs length() function.
coerce_tests! %r{order: allows nested functions}
test "order: allows nested functions" do
ids_expected = Post.order(Arel.sql("author_id, len(trim(title))")).pluck(:id)

# $DEBUG = true
ids = Post.order("author_id, len(trim(title))").pluck(:id)

assert_equal ids_expected, ids
end

# Use LEN() vs length() function.
coerce_tests! %r{pluck: allows nested functions}
test "pluck: allows nested functions" do
title_lengths_expected = Post.pluck(Arel.sql("len(trim(title))"))

title_lengths = Post.pluck("len(trim(title))")

assert_equal title_lengths_expected, title_lengths
end

test "order: allows string column names that are quoted" do
ids_expected = Post.order(Arel.sql("id")).pluck(:id)

Expand Down Expand Up @@ -2157,7 +2178,7 @@ def test_in_order_of_with_enums_keys_coerced
coerce_tests! :test_in_order_of_with_nil
def test_in_order_of_with_nil_coerced
Book.connection.remove_index(:books, column: [:author_id, :name])

original_test_in_order_of_with_nil
ensure
Book.where(author_id: nil, name: nil).delete_all
Expand Down

0 comments on commit c015641

Please sign in to comment.