: Ensure the database user account used by the application does not have permission to execute administrative functions like pg_sleep() or access system tables like pg_user . 🔍 Understanding the Payload
To protect a PostgreSQL-backed application from injection, you must move away from building queries with string concatenation.
: This is the most effective defense. It separates the SQL command from the data, ensuring input is never executed as code.
// UNSAFE: Vulnerable to the injection provided const query = "SELECT * FROM articles WHERE topic = '" + userInput + "'"; // SAFE: Parameterized query const query = "SELECT * FROM articles WHERE topic = $1"; const values = [userInput]; db.query(query, values, (err, res) => { // The database treats $1 strictly as data, even if it contains "SELECT PG_SLEEP(5)" }); Use code with caution. Copied to clipboard
Instead of concatenating strings, use placeholders ( $1 , $2 ) to safely handle user input. javascript
The statement separator used to "stack" a new command after the first one. SELECT PG_SLEEP(5)
The SQL comment syntax used to ignore the rest of the original, legitimate query so it doesn't cause a syntax error. 🛠️ Secure Implementation Example (Node.js/pg)