Building SQL by joining strings? Close SQL injection with parameters
SQL injection happens when a user's value is pasted into the query text and changes its shape. The fix is a parameterized query: the query's shape stays fixed in code, and the values reach the database separately.

A single quote, ', typed into the email box on a login page, and back comes a database error. That small mark tells the person who typed it that your site pastes their text into the query as it is, and the next step could be reading the whole users table. This article shows how it happens, the line that closes it, and what to do with the parts that can't be a parameter.
What is SQL injection?
SQL injection is a flaw that happens when a value from the user is pasted into the text of an SQL query, so it can change the shape of the query itself instead of staying a value.
The database reads the whole query text as commands. If your code builds that text by joining pieces, nothing tells the database where your words end and the user's words begin. So any quote mark or SQL keyword in the value is read as part of the command.
How does joining strings become a hole?
Joining strings becomes a hole when the value holds a mark that closes the string and carries on with a new condition, so the query that runs is not the one you wrote.
This is the shape that keeps showing up in Node.js projects:
const sql = "SELECT * FROM users WHERE email = '" + req.body.email + "'";
const { rows } = await client.query(sql);If the user types a normal email, all is fine. If they type ' OR '1'='1, the query becomes a condition that is always true, and it returns the whole table. No hacking tool is needed: a form field, or a request sent straight to the server.
The node-postgres documentation on queries says it plainly: if you are passing parameters to your queries, avoid string concatenating them into the query text directly, because this can, and often does, lead to SQL injection.
What is a parameterized query?
A parameterized query is a query whose shape is fixed in code, with placeholders for the values, and the values reach the database separately instead of being pasted into the text.
In node-postgres the placeholders are $1 and $2, and the values go in an array next to the query:
const { rows } = await client.query(
'SELECT * FROM users WHERE email = $1',
[req.body.email]
);Now if the user types ' OR '1'='1, the database looks for a user whose email is literally that text, and finds no one. The OWASP SQL Injection Prevention Cheat Sheet explains why: you define the SQL structure separately from its data values and bind those values through the database API, and parameter binding prevents the value from changing SQL syntax. The same sheet lists parameterized queries as the first defense, and says that manually escaping values is error-prone and strongly discouraged.
So you don't have to chase every dangerous character in every value. Once the command and the value travel apart, the user's text stays data whatever they type.
What about column names and sort order?
Column names and sort direction can't be parameters, so you pick them from a fixed list of values in your code and never paste in what the user sent.
The OWASP sheet says parameter markers represent data values, not table names, column names, or SQL keywords such as ASC and DESC. The node-postgres documentation says the same thing from the database side: PostgreSQL does not support parameters for identifiers. The fix the sheet gives: map the user's choice to a small set of values the application owns, and map the sort direction to the literal ASC or DESC.
const sortColumns = { name: 'name', newest: 'created_at' };
const column = sortColumns[req.query.sort] ?? 'created_at';
const direction = req.query.dir === 'asc' ? 'ASC' : 'DESC';
const { rows } = await client.query(
`SELECT * FROM products WHERE category = $1 ORDER BY ${column} ${direction}`,
[req.query.category]
);Here the column name can only come from sortColumns, so any odd value falls back to the default order, and the category is still a parameter.
How do you find the exposed queries in your project?
The fastest way is to search the code for every query that joins strings, or uses a template string with a value from the request.
Search the project for
query(,execute(andraw(, and open each one.If you see
+or${inside the query text, ask where that value comes from. If it comes from the request, or from data a user wrote into the database, turn it into a parameter.If the value is a column name or a sort direction, put it in a fixed list like the example above.
Then try every search box and filter with a
': you should get an empty result or a normal message, not a database error.
Where do you start today?
Start with the login page and the search box, because that's where someone tries the quote mark first. Turn any query behind them into a parameterized one today, then work through the rest of the project with the search above.
If the users table is what worries you, also see how to store passwords with Argon2id, so that even a leaked table doesn't give away passwords. Once the query is closed, make sure every record belongs to its owner with the IDOR check. And if you want someone to try every field on your platform the way an attacker would, that's the job of a penetration test with Muhassan.
In your project, how many queries with a + inside the text has nobody opened yet?
FAQ
If I escape the quotes myself, is that enough?
No. OWASP says manually escaping values is error-prone and strongly discouraged, and recommends parameterized queries instead.
Do parameters work with INSERT too, not only SELECT?
Yes. The node-postgres example itself is an INSERT with two values, $1 for the name and $2 for the email, with the values in an array next to the query.
Can I pass a table or column name as a parameter?
No. A parameter stands for a value only. Pick the column name or the sort direction from a fixed list of values in your code.
My form already has validation, why worry?
Because an attacker skips the form. They send the request straight to the server, so the protection has to live in the code that builds the query.


