Parameterized Queries JavaScript Guide: How to Prevent SQL Injection with Parameterized Queries

SQL Injection Prevention for JavaScript Developers: Parameterized Queries Explained

Ajay Monga
3 min readMay 9, 2024

As we know, Javascript doesn’t directly interact with databases in the same way that languages like Java, C++ or PHP do. It can not execute SQL queries directly to the database.
It can be used to make requests to a server-side script (like PHP or Node.js) that then interacts with the database.

In this article, we will learn about SQL injection and parameterized queries in Node.js.

SQL injection (SQLi) remains one of the most dangerous web application security vulnerabilities.

There is no doubt that SQL Injection was introduced two decades ago as also mitigation of SQL Injection but still, it is a common vulnerability found in applications. For the last 25 years, it is always in OWASP Top 10.

Decades Later: Why SQL Injection Remains a Top Security Concern💉

Little intro of SQLi:

SQL injection (SQLi) occurs when malicious user input is directly inserted into an SQL query. Attackers can manipulate input fields on websites or applications to inject harmful SQL code and can extract sensitive data (usernames, passwords, financial data, etc.), modify or delete existing data, run system commands on the database server, potentially compromise the entire server.

s

Understanding SQL Injection

Let’s take a simple example of how SQL injection works. example of a login form:

let query = `SELECT * FROM users WHERE username = '${userInput}' AND password = '${passwordInput}'`;
// Simulate sending the query to the server
db.query(query);

If a user enters “admin” for username and “password” for password, the query executes as intended.

However, if an attacker enters “‘ OR ‘1’=’1” as the username, the query transforms into:

SELECT * FROM users WHERE username = '' OR '1' = '1' AND password = 'password';

Since ‘1’=’1' is always true, the attacker gains access, bypassing authentication.

One of the most popular remediations of SQL injection is parameterized query

What Are Parameterized queries?

Parameterized queries (also known as prepared statements) involve predefining an SQL query template with placeholders for user-supplied values. These placeholders are replaced with actual data at execution time, but the database engine treats them as data, not as executable code.

This technique enforces a clear separation between the SQL query structure and the dynamic data elements. Even if an attacker somehow includes malicious code within the input, the database engine will not interpret it as part of the SQL command.

How Parameterized Queries Work (Example in JavaScript)

  • Query with Placeholders: Instead of directly embedding user input into SQL queries, and create a query template with placeholders:
let query = "SELECT * FROM customers WHERE username = ? AND password = ?";
  • Separate Parameters: Pass the actual user input as separate parameters alongside the SQL query:
let username = "ajay";
let password = "Whdu83uK";

let query = "SELECT * FROM customers WHERE username = ? AND password = ?";

// Assuming a database connection object 'connection' is established
connection.query(query, [username, password], function (error, results, fields) {
if (error) throw error;
// Process results
});
  • The placeholder in the query is replaced with the parameter value, and the safely constructed query is executed.

Advantages of Parameterized Queries

  • Prevention of SQL Injection: The primary advantage; it’s exceptionally difficult to execute successful SQLi attacks using parameterized queries.
  • Improved Performance: The database can cache and reuse the precompiled query structure, potentially optimizing query execution times.
  • Code Readability: Parameterized queries often make SQL code cleaner and easier to understand by separating logic and data.

Parameterized queries are the most reliable shield against common SQL injection attacks. By understanding how they work and consistently applying them in JS code, and significantly enhances the security of applications.

Let me know if you want more in-depth explanations or examples in additional programming languages!

For Java: https://medium.com/@ajay.monga73/parameterized-queries-java-guide-how-to-prevent-sql-injection-with-parameterized-queries-10e250996df9

For C++: https://medium.com/@ajay.monga73/parameterized-queries-c-guide-how-to-prevent-sql-injection-with-parameterized-queries-94b8105cacbd

For Python: https://medium.com/@ajay.monga73/sql-injection-prevention-for-c-developers-parameterized-queries-explained-b5a4cb1b6207

For PHP: https://medium.com/@ajay.monga73/parameterized-queries-php-guide-how-to-prevent-sql-injection-with-parameterized-queries-9899e77f9609

Follow me on LinkedIn: https://www.linkedin.com/in/ajay-monga2/

--

--

Ajay Monga

Security @ ADP | AI Security | SAST | Shift Left | My writing style is clear and concise, making complex security concepts understandable to a broad audience