CWE-89: Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')

Description:
The product constructs all or part of an SQL command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended SQL command when it is sent to a downstream component. 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.

Affected languages: Not Language-Specific, SQL
Affected technologies: Database Server
Parent weaknesses: CWE-943, CWE-74

Consequences:
  - Confidentiality: Execute Unauthorized Code or Commands
  - Confidentiality: Read Application Data
  - Authentication: Gain Privileges or Assume Identity
  - Access Control: Bypass Protection Mechanism
  - Integrity: Modify Application Data

Detection methods:
  - Automated Static Analysis: This weakness can often be detected using automated static analysis tools. Many modern tools use data flow analysis or constraint-based techniques to minimize the number of false positives. 
         
  - Automated Dynamic Analysis: This weakness can be detected using dynamic tools and techniques that interact with the software using large test suites with many diverse inputs, such as fuzz testing (fuzzing), robustness testing, a
  - Manual Analysis: Manual analysis can be useful for finding this weakness, but it might not achieve desired code coverage within limited time constraints. This becomes difficult for weaknesses that must be considered f
  - Automated Static Analysis - Binary or Bytecode: According to SOAR [REF-1479], the following detection techniques may be useful: 
                   
                      Highly cost effective: 
                         
                           

Mitigations:
  - [Architecture and Design] Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid [REF-1482]. 
                   For example, consider using persistence layers such as Hibernate or Enterprise Java Beans, which can provide significant protect
  - [Architecture and Design] If available, use structured mechanisms that automatically enforce the separation between data and code. These mechanisms may be able to provide the relevant quoting, encoding, and validation automatically, instead of relying on the developer to provide this capability at every point where output is
  - [Architecture and Design] Run your code using the lowest privileges that are required to accomplish the necessary tasks [REF-76]. If possible, create isolated accounts with limited privileges that are only used for a single task. That way, a successful attack will not immediately give the attacker access to the rest of the s
  - [Architecture and Design] For any security checks that are performed on the client side, ensure that these checks are duplicated on the server side, in order to avoid CWE-602. Attackers can bypass the client-side checks by modifying values after the checks have been performed, or by changing the client to remove the client-s

Demonstrative examples:
In 2008, a large number of web servers were compromised using the same SQL injection attack string. This single string worked against many different programs. The SQL injection was then used to modify the web sites to serve malicious code.

The following code dynamically constructs and executes a SQL query that searches for items matching a specified name. The query restricts the items displayed to those where owner matches the user name of the currently-authenticated user.
The query that this code intends to execute follows:
... string userName = ctx.getAuthenticatedUserName(); string query = "SELECT * FROM items WHERE owner = '" + userName + "' AND itemname = '" + ItemName.Text + "'"; sda = new SqlDataAdapter(query, conn); DataTable dt = new DataTable(); sda.Fill(dt); ...
SELECT * FROM items WHERE owner = <userName> AND itemname = <itemName>;

Real-world examples:
  - CVE-2024-6847: SQL injection in AI chatbot via a conversation message
  - CVE-2025-26794: SQL injection in e-mail agent through SQLite integration
  - CVE-2023-32530: SQL injection in security product dashboard using crafted certificate fields
  - CVE-2021-42258: SQL injection in time and billing software, as exploited in the wild per CISA KEV.
  - CVE-2021-27101: SQL injection in file-transfer system via a crafted Host header, as exploited in the wild per CISA KEV.