Looking through our exception tracker the other day, I ran across
a notice from our slow-query logger that caught my eye. I saw a
SELECT … WHERE … LIKE
query with lots of percent
signs in the LIKE
clause. It was pretty obvious that
this term was user-provided and my first thought was SQL
injection.
[3.92 sec] SELECT ... WHERE (profiles.email LIKE '%64%68%6f%6d%65%73@%67%6d%61%69%6c.%63%6f%6d%') LIMIT 10
Looking at the code, it turned out that we were using a
user-provided term directly in the LIKE
clause
without any checks for metacharacters that are interpreted in
this context (%
, _
, \
).
def self.search(term, options = {})
limit = (options[:limit] || 30).to_i
friends = options[:friends] || []
with_orgs = options[:with_orgs].nil? ? false : options[:with_orgs]
if term.to_s.index("@")
users = User.includes(:profile) …
[Read more]