CRITICAL
Rule Definition
To avoid the creation of Injection flaws, the Open Web Application Security Project (OWASP) recommends to check "Injection flaws, such as SQL, OS, and LDAP injection occur when untrusted data is sent to an interpreter as part of a command or query. The attacker’s hostile data can trick the interpreter into executing unintended commands or accessing data without proper authorization."
The Common Weakness Enumeration defines Improper Neutralization of Special Elements used in an SQL Command ( CWE-89 ) as follows:
"Without sufficient removal or quoting of SQL syntax in user-controllable inputs, the generated SQL query can cause those inputs to be interpreted as SQL instead of ordinary user data. This can be used to alter query logic to bypass security checks, or to insert additional statements that modify the back-end database, possibly including execution of system commands.
SQL injection has become a common issue with database-driven web sites. The flaw is easily detected, and easily exploited, and as such, any site or software package with even a minimal user base is likely to be subject to an attempted attack of this kind. This flaw depends on the fact that SQL makes no real distinction between the control and data planes."
Remediation
Common database libraries handling SQL code provide specific API to perform parameterized SQL queries. Use them to assure sanitized SQL queries.
Violation Code Sample
import {PoolConfig, Pool} from "pg";
// pools will use environment variables
// for connection information
const pool = new Pool()
const text = 'INSERT INTO users(name, email) VALUES('+request.form['name']+', '+request.form['email']+') RETURNING *'
// callback
pool.query(text, (err, res) => {
if (err) {
console.log(err.stack)
} else {
console.log(res.rows[0])
}
})
Fixed Code Sample
import {PoolConfig, Pool} from "pg";
// pools will use environment variables
// for connection information
const pool = new Pool()
const text = 'INSERT INTO users(name, email) VALUES($1, $2) RETURNING *'
const values = [request.form['name'], request.form['email']]
// callback
pool.query(text, values, (err, res) => {
if (err) {
console.log(err.stack)
} else {
console.log(res.rows[0])
}
})
Related Technologies
Technical Criterion
CWE-89 - Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.