From 64d8c54e16ee9ad3b591501401d6c437304e1308 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 9 Jun 2019 08:01:10 +0900 Subject: Allow column name with function (e.g. `length(title)`) as safe SQL string Currently, almost all "Dangerous query method" warnings are false alarm. As long as almost all the warnings are false alarm, developers think "Let's ignore the warnings by using `Arel.sql()`, it actually is false alarm in practice.", so I think we should effort to reduce false alarm in order to make the warnings valuable. This allows column name with function (e.g. `length(title)`) as safe SQL string, which is very common false alarm pattern, even in the our codebase. Related 6c82b6c99, 6607ecb2a, #36420. Fixes #32995. --- .../lib/active_record/connection_adapters/abstract/quoting.rb | 10 ++++++++-- .../lib/active_record/connection_adapters/mysql/quoting.rb | 10 ++++++++-- .../active_record/connection_adapters/postgresql/quoting.rb | 10 ++++++++-- .../lib/active_record/connection_adapters/sqlite3/quoting.rb | 10 ++++++++-- 4 files changed, 32 insertions(+), 8 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb index e34f4f745f..1b6ba8ce97 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb @@ -158,7 +158,10 @@ module ActiveRecord COLUMN_NAME = / \A ( - (?:\w+\.)?\w+ + (?: + # table_name.column_name | function(one or no argument) + ((?:\w+\.)?\w+) | \w+\((?:|\g<2>)\) + ) (?:(?:\s+AS)?\s+\w+)? ) (?:\s*,\s*\g<1>)* @@ -179,7 +182,10 @@ module ActiveRecord COLUMN_NAME_WITH_ORDER = / \A ( - (?:\w+\.)?\w+ + (?: + # table_name.column_name | function(one or no argument) + ((?:\w+\.)?\w+) | \w+\((?:|\g<2>)\) + ) (?:\s+ASC|\s+DESC)? (?:\s+NULLS\s+(?:FIRST|LAST))? ) diff --git a/activerecord/lib/active_record/connection_adapters/mysql/quoting.rb b/activerecord/lib/active_record/connection_adapters/mysql/quoting.rb index a0829b1115..dfed5471f4 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/quoting.rb @@ -43,7 +43,10 @@ module ActiveRecord COLUMN_NAME = / \A ( - (?:\w+\.|`\w+`\.)?(?:\w+|`\w+`) + (?: + # `table_name`.`column_name` | function(one or no argument) + ((?:\w+\.|`\w+`\.)?(?:\w+|`\w+`)) | \w+\((?:|\g<2>)\) + ) (?:(?:\s+AS)?\s+(?:\w+|`\w+`))? ) (?:\s*,\s*\g<1>)* @@ -53,7 +56,10 @@ module ActiveRecord COLUMN_NAME_WITH_ORDER = / \A ( - (?:\w+\.|`\w+`\.)?(?:\w+|`\w+`) + (?: + # `table_name`.`column_name` | function(one or no argument) + ((?:\w+\.|`\w+`\.)?(?:\w+|`\w+`)) | \w+\((?:|\g<2>)\) + ) (?:\s+ASC|\s+DESC)? ) (?:\s*,\s*\g<1>)* diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb index d18c5c5c12..0c800dca83 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb @@ -89,7 +89,10 @@ module ActiveRecord COLUMN_NAME = / \A ( - (?:\w+\.|"\w+"\.)?(?:\w+|"\w+")(?:::\w+)? + (?: + # "table_name"."column_name"::type_name | function(one or no argument)::type_name + ((?:\w+\.|"\w+"\.)?(?:\w+|"\w+")(?:::\w+)?) | \w+\((?:|\g<2>)\)(?:::\w+)? + ) (?:(?:\s+AS)?\s+(?:\w+|"\w+"))? ) (?:\s*,\s*\g<1>)* @@ -99,7 +102,10 @@ module ActiveRecord COLUMN_NAME_WITH_ORDER = / \A ( - (?:\w+\.|"\w+"\.)?(?:\w+|"\w+")(?:::\w+)? + (?: + # "table_name"."column_name"::type_name | function(one or no argument)::type_name + ((?:\w+\.|"\w+"\.)?(?:\w+|"\w+")(?:::\w+)?) | \w+\((?:|\g<2>)\)(?:::\w+)? + ) (?:\s+ASC|\s+DESC)? (?:\s+NULLS\s+(?:FIRST|LAST))? ) diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3/quoting.rb b/activerecord/lib/active_record/connection_adapters/sqlite3/quoting.rb index 5d6932e4ca..54808de714 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3/quoting.rb @@ -56,7 +56,10 @@ module ActiveRecord COLUMN_NAME = / \A ( - (?:\w+\.|"\w+"\.)?(?:\w+|"\w+") + (?: + # "table_name"."column_name" | function(one or no argument) + ((?:\w+\.|"\w+"\.)?(?:\w+|"\w+")) | \w+\((?:|\g<2>)\) + ) (?:(?:\s+AS)?\s+(?:\w+|"\w+"))? ) (?:\s*,\s*\g<1>)* @@ -66,7 +69,10 @@ module ActiveRecord COLUMN_NAME_WITH_ORDER = / \A ( - (?:\w+\.|"\w+"\.)?(?:\w+|"\w+") + (?: + # "table_name"."column_name" | function(one or no argument) + ((?:\w+\.|"\w+"\.)?(?:\w+|"\w+")) | \w+\((?:|\g<2>)\) + ) (?:\s+ASC|\s+DESC)? ) (?:\s*,\s*\g<1>)* -- cgit v1.2.3