Skip to content

Conversation

@heyjosephme
Copy link

Fixes #1581

Problem

SQLite and non-MySQL/PostgreSQL databases weren't escaping SQL wildcards (%, _, ) in LIKE queries, causing unintended matches.

Solution

Replace database-specific escaping with Rails' sanitize_sql_like method which works for all databases.

Changes

  • Simplified escape_wildcards method in lib/ransack/constants.rb
  • Updated test expectations for SQLite in spec/ransack/predicate_spec.rb
  • Changed from 13 lines of regex to 1 line using Rails built-in

Testing

  • ✅ All tests pass (8 examples, 0 failures)
  • ✅ Wildcard escaping now works correctly for SQLite

This is my first contribution to Ransack. Looking forward to feedback!

Previously, escape_wildcards only escaped special characters (%, _, \)
for MySQL and PostgreSQL, leaving SQLite and other databases vulnerable
to unintended wildcard matches in LIKE queries.

This commit replaces the database-specific escaping logic with Rails'
sanitize_sql_like method, which works consistently across all database
adapters.

Changes:
- Replaced database-specific regex with sanitize_sql_like
- Updated test expectations for SQLite to match correctly escaped output
- Simplified code from 13 lines to 1 line

Fixes activerecord-hackery#1581
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LIKE predicates do not escape values in SQLite and other non-MySQL/PostgreSQL RDBMS

1 participant