Skip to content

Commit 33aae97

Browse files
committed
Fix SQL injection in PostgreSQL adapter table name interpolation
Use connection.quote via quote_value helper instead of raw string interpolation for regclass casts in estimation queries.
1 parent 4a51514 commit 33aae97

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

lib/quick_count/adapters/postgresql.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def count_estimate(query)
4242
private
4343

4444
def get_table_estimate(table_name)
45+
quoted_table = quote_value(table_name.to_s)
46+
4547
# Enhanced estimation using both reltuples and live stats
4648
result = execute_sql(<<~SQL)
4749
SELECT COALESCE(
@@ -57,7 +59,7 @@ def get_table_estimate(table_name)
5759
)::bigint AS estimated_count
5860
FROM pg_class
5961
LEFT JOIN pg_stat_user_tables pg_stat ON pg_stat.relid = pg_class.oid
60-
WHERE pg_class.oid = '#{table_name}'::regclass
62+
WHERE pg_class.oid = #{quoted_table}::regclass
6163
6264
-- Handle partitioned tables
6365
UNION ALL
@@ -71,7 +73,7 @@ def get_table_estimate(table_name)
7173
FROM pg_inherits
7274
JOIN pg_class ON pg_inherits.inhrelid = pg_class.oid
7375
LEFT JOIN pg_stat_user_tables pg_stat ON pg_stat.relid = pg_class.oid
74-
WHERE pg_inherits.inhparent = '#{table_name}'::regclass
76+
WHERE pg_inherits.inhparent = #{quoted_table}::regclass
7577
SQL
7678

7779
# Get the maximum estimate (handles both regular and partitioned tables)

0 commit comments

Comments
 (0)