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
Binding parameters are by far the best way to protect your dynamic SQL query.
Violation Code Sample
CREATE OR REPLACE PROCEDURE testBind(vname IN VARCHAR2) AS
BEGIN
EXECUTE IMMEDIATE 'UPDATE products set price = price-1 where name = ' || vname;
COMMIT;
END;
/
Fixed Code Sample
PL/SQL:
CREATE OR REPLACE PROCEDURE testBind(vname IN VARCHAR2) AS
BEGIN
EXECUTE IMMEDIATE 'UPDATE products set price = price-1 where name = :1' USING vname;
COMMIT;
END;
/
Reference
https://blogs.oracle.com/sql/improve-sql-query-performance-by-using-bind-variables
https://www.sqlinjection.net/defense/pl-sql/
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.