Sequelize: escape string in a literal string

Viewed 5876

I can use literal in Sequelize to manually build a SQL query part:

sequelize.literal(`"foo".bar ILIKE '%baz%'`)

But if I want to add a var in this literal block, I now introduce SQL injection vulnerability:

sequelize.literal(`"foo".name ILIKE '%${myVar}%'`)

Is there a Sequelize way to protect variables in literal blocks?

3 Answers

You may use replacements and ? to avoid sql injections:

sequelize.query(`"foo".name ILIKE '%?%'`,
  { replacements: [myVar], type: sequelize.QueryTypes.SELECT }
)

try

description = {
    $like: sequelize.literal(`${myVar} ESCAPE '\\'`)
};
Related