Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible sql injection

I'm using squeel gem in my project, and I have code something like this :

def self.search(query)
    return self.scoped if query.blank?

    self.joins(:supplier).where{lower(supplier.supplier_name).like_any(["%#{query}%"])}
  end

My questions is this code vulnerable to SQL injection? And how do I fix it? I tried to do sanitize(query) but it just adds extra set of quotes and the SQL statement doesn't get generated appropriately

like image 435
Gandalf StormCrow Avatar asked Mar 06 '26 03:03

Gandalf StormCrow


1 Answers

UPDATED:

Squeel will automatically escape strings, so your query is fine and won't open you up to injection. See question about sql injection - Squeel - Github

OLD (INCORRECT) ANSWER: This is the active record version

Someone correct me if i'm wrong, but since you are passing in #{query} as a STRING and not an argument, then you are opening yourself up to injection. See the docs for passing in arguments

Using arguments will escape the 'query' STRING

Your query using arguments:

self.joins(:supplier).where{lower(supplier.supplier_name).like_any(["%"+?+"%"], query)}
like image 187
dannypaz Avatar answered Mar 07 '26 17:03

dannypaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!