Full answer
Injection means attacker-controlled text changes the meaning of a command.
SQL injection. If you build SQL with a template string, input such as ' OR '1'='1 turns a filter into "always true" and returns every row. The fix is a parameterized query (prepared statement): the SQL text has placeholders (?, $1), and the driver sends values separately. The database never parses the values as SQL. This works the same in sqlite, pg, mysql2 and ORMs. Identifiers (table and column names) cannot be parameters, so choose them from a fixed allow-list.
Command injection. child_process.exec(ls ${folder}) runs the whole string in a shell. A value such as x; rm -rf ~ runs a second command. Use execFile('ls', [folder]) or spawn, which pass each argument directly with no shell. Even then, check that a value cannot start with - and be read as an option; the -- marker helps.
Other injection places: NoSQL (an object like { "$ne": null } where a string was expected), template engines, regular expressions built from input, log lines (new line characters), and HTTP headers.
Defence in depth: validate type, length and format; use least privilege database users (no DROP rights for the web app); and never show raw database errors to users.
const { DatabaseSync } = require('node:sqlite');
const { execFile } = require('node:child_process');
const { promisify } = require('node:util');
const db = new DatabaseSync(':memory:');
db.exec(`
CREATE TABLE students (name TEXT, marks INTEGER, private_note TEXT);
INSERT INTO students VALUES ('Asha', 78, 'scholarship file'), ('Ravi', 91, 'counselling file');
`);
const attack = "x' OR '1'='1";
// Unsafe: the input becomes part of the SQL text.
const unsafe = db.prepare(`SELECT name FROM students WHERE name = '${attack}'`).all();
console.log('unsafe query returned rows:', unsafe.length);
// Safe: the input is sent as a value.
const safe = db.prepare('SELECT name FROM students WHERE name = ?').all(attack);
console.log('parameterized query returned rows:', safe.length);
// Command injection: exec-style strings vs argument arrays.
(async () => {
const hostile = 'hello; echo INJECTED';
const { stdout } = await promisify(execFile)(process.execPath, ['-p', 'process.argv[1]', hostile]);
console.log('execFile treats the whole text as one argument:', JSON.stringify(stdout.trim()));
})();This example uses the sqlite module that ships with Node.js 22 and later, so it needs no install. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): unsafe query returned rows: 2 parameterized query returned rows: 0 execFile treats the whole text as one argument: "hello; echo INJECTED"
Key points
- Keep data and commands separate.
- Parameterized queries for SQL.
- execFile or spawn with an array, not exec with a string.
- Validate input and limit database privileges.
Common mistakes
- Building SQL with template strings.
- Escaping quotes by hand instead of using placeholders.
- Using exec with user input.
- Using the database admin account for the web app.
Follow-ups
- Why can table names not be parameters?
- How does NoSQL injection work in a MongoDB filter?
- What is the purpose of the -- argument separator?
Tips
- Say 'data and code stay separate' in one sentence.
- Show the attack string and what it does.
- Mention least privilege to show defence in depth.