I initially discovered that this was an issue when I tried to search for terms that had been prepended with a hashtag, which it turns out is a comment delimiter in SQL. The search returned nothing, because it ignored the #term that came after the hashtag.
So now I'm having trouble finding the proper way of escaping the user's input. It seems to me that this would both solve the hashtag issue and also address the much larger problem, SQL injection.
Here is the snippet I am working with specifically:
function (term) {
term = term.toLowerCase()
return db('ticket')
.select('*')
.where(db.raw('lower(question)'), 'like', `%${term}%`)
.orWhere(db.raw('lower(note)'), 'like', `%${term}%`)
.orWhere(db.raw('lower(user_name)'), 'like', `%${term}%`)
}
I did find this and this SO article that seemed close, as well as a couple other things. Also, Knex's docs and other sources recommend parameterized binding as a method to safeguard against SQL injection.
I'm just having trouble finding a clear example that can be explained to me in JavaScript or using Knex.