User input concatenated directly into SQL strings lets attackers inject arbitrary SQL.
Concatenating user input directly into SQL strings lets attackers inject arbitrary SQL. The fix isn’t "sanitize the input" — any character can be weaponized. Parameterized queries (prepared statements) are the only real fix: the database driver separates query structure from data, so `‘ OR ‘1’=‘1’` is always just a string, never executable SQL.
// VULNERABLE — string concatenation
app.post('/login', (req, res) => {
const { username, password } = req.body;
const query = `SELECT * FROM users WHERE username = '` + username + `' AND password = '` + password + `'`;
// Attacker: username = "' OR '1'='1'; --"
db.query(query, (err, result) => { /* ... */ });
});
// FIXED — parameterized query
app.post('/login', (req, res) => {
const { username, password } = req.body;
const query = 'SELECT * FROM users WHERE username = $1 AND password = $2';
db.query(query, [username, password], (err, result) => { /* ... */ });
});
pool.query(`SELECT * FROM users WHERE id = ${req.params.id}`)