 
                            ...
Suppose a system authenticates users by issuing the following query to a SQL database. If the query returns any results, authentication succeeds; otherwise, authentication fails.
| Code Block | ||
|---|---|---|
| 
 | ||
| SELECT * FROM db_user WHERE username='<USERNAME>' AND 
                            password='<PASSWORD>' | 
Suppose an attacker can substitute arbitrary strings for <USERNAME> and <PASSWORD>. In that case, the authentication mechanism can be bypassed by supplying the following <USERNAME> with an arbitrary password:
| Code Block | ||
|---|---|---|
| 
 | ||
| validuser' OR '1'='1 | 
The authentication routine dynamically constructs the following query:
| Code Block | ||
|---|---|---|
| 
 | ||
| SELECT * FROM db_user WHERE username='validuser' OR '1'='1' AND password='<PASSWORD>' | 
...
Similarly, an attacker could supply the following string for <PASSWORD> with an arbitrary username:
| Code Block | ||
|---|---|---|
| 
 | ||
| ' OR '1'='1 | 
producing the following query:
| Code Block | ||
|---|---|---|
| 
 | ||
| SELECT * FROM db_user WHERE username='<USERNAME>' AND password='' OR '1'='1' | 
...
Failure to sanitize user input before processing or storing it can result in injection attacks.
| Rule | Severity | Likelihood | Detectable | Remediation CostRepairable | Priority | Level | 
|---|---|---|---|---|---|---|
| IDS00-J | High | Likely | YesProbable | MediumNo | P12P18 | L1 | 
Automated Detection
| Tool | Version | Checker | Description | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| The Checker Framework | 
 | Tainting Checker | Trust and security errors (see Chapter 8) | |||||||||||
| CodeSonar | FB
 | SECURITY.SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRINGJAVA. | IO.INJ.SQL | SQL injection | ||||||||||
| Coverity | 7.5 | SQLI | Implemented | |||||||||||
| Findbugs | 1.0 | SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE | Implemented | |||||||||||
| Fortify | 1.0 | HTTP_Response_Splitting  | Implemented | |||||||||||
| Klocwork | 
 | 
 | SV.DATA.DB  | Implemented | ||||||||||
| Parasoft Jtest | 
 | BD-SECURITY-CERT.IDS00.TDSQL | ImplementedProtect against SQL injection | |||||||||||
| SonarQube Java Plugin | 
 | |||||||||||||
| SpotBugs | 
 | SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE | Implemented | 
Related Vulnerabilities
CVE-2008-2370 describes a vulnerability in Apache Tomcat 4.1.0 through 4.1.37, 5.5.0 through 5.5.26, and 6.0.0 through 6.0.16. When a RequestDispatcher is used, Tomcat performs path normalization before removing the query string from the URI, which allows remote attackers to conduct directory traversal attacks and read arbitrary files via a .. (dot dot) in a request parameter.
...
| STR02-C. Sanitize data passed to complex subsystemsSEI CERT C++ Coding StandardVOID STR02-CPP. Sanitize data passed to complex subsystems | |
| SEI CERT Perl Coding Standard | IDS33-PL. Sanitize untrusted data passed across a trust boundary | 
| Injection [RST] | |
| CWE-116, Improper Encoding or Escaping of Output | 
...