Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent sql-injection in nodejs and sequelize? [closed]

I want to write custom queries using Sequelize, and as far as possible avoid potential issues with SQL Injection. My question is therefore if there exists a secure way of writing custom queries with inserted variables using Sequelize?

like image 390
Bhargava Narayan K P Avatar asked Feb 27 '16 10:02

Bhargava Narayan K P


People also ask

Does Sequelize protect against SQL injection?

All versions of sequelize lower than 5.8. 11 are vulnerable to SQL Injection (CVE-2019-10748) because they contain JSON path keys that are not being properly escaped for the MySQL and MariaDB dialects.

How can SQL injection be stopped?

Developers can prevent SQL Injection vulnerabilities in web applications by utilizing parameterized database queries with bound, typed parameters and careful use of parameterized stored procedures in the database.

Do I need to close Sequelize connection?

If you don't close the Sequelize connection, the micro-service will still run until the connection got timed out (idle time pool parameter)..


1 Answers

Sequelize escapes replacements, which avoids the problem at the heart of SQL injection attacks: unescaped strings. It also supports binding parameters when using SQLite or PostgreSQL, which alleviates the risk further by sending the parameters to the database separately to the query, as documented here:

Bind parameters are like replacements. Except replacements are escaped and inserted into the query by sequelize before the query is sent to the database, while bind parameters are sent to the database outside the SQL query text. A query can have either bind parameters or replacements.

Only SQLite and PostgreSQL support bind parameters. Other dialects will insert them into the SQL query in the same way it is done for replacements. Bind parameters are referred to by either $1, $2, ... (numeric) or $key (alpha-numeric). This is independent of the dialect.

like image 99
Dan Abrey Avatar answered Sep 16 '22 13:09

Dan Abrey