CWE-89: SQL injection (SQLi) | Causes and Fixes Skip to content

Security weaknessCWE Top 25 · #2

CWE-89 SQL injection (SQLi)

Untrusted input changes your database queries, letting attackers read, alter or delete the data your business runs on.

  • Senior security engineers
  • 540+ projects delivered
  • Reply within one business day

Quick answer

What is CWE-89?

Improper Neutralization of Special Elements used in an SQL Command: 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.

Business impact

What CWE-89 could cost your business

MITRE documents what this weakness can do. Here is what that means for a website, store or app.

Confidentiality

Private data exposed

Customer records, credentials, orders or internal files can be read by people who should never see them.

  • Execute Unauthorized Code or Commands
  • Read Application Data

Integrity

Data, pages or code changed

Prices, orders, content or the code your site runs can be altered without your approval.

  • Execute Unauthorized Code or Commands
  • Modify Application Data

Availability

Downtime and lost sales

Crashes, slowdowns or exhausted resources can take your site, store or app offline.

  • Execute Unauthorized Code or Commands

Access Control

Accounts and admin areas exposed

Logins and permission checks can be bypassed, putting admin and customer accounts at risk.

  • Bypass Protection Mechanism

Authentication

Sign-in that does not protect

Weak or bypassable authentication lets the wrong people sign in as someone else.

  • Gain Privileges or Assume Identity
  • Bypass Protection Mechanism

In plain English

What SQL injection (SQLi) means for your business

  • 540+projects delivered
  • 9+years in business
  • 23+certified experts
Get a free security check

A senior engineer replies within one business day.

SQL injection happens when your code builds a database query by pasting user input into the query text. The database cannot tell which part you wrote and which part a visitor typed. If the input contains SQL syntax, the database runs it as part of the command. A login form, a search box, a filter in a URL or an API field can each become a way to rewrite the query.

Your database usually holds the most valuable things you own: customer records, orders, password hashes and content. A successful attack can expose that data, change it or erase it. It can also let someone sign in as another user without knowing the password. For a store or a membership site, that means privacy notifications, lost customer trust and downtime while you restore and investigate. The fix is well understood, which makes this weakness avoidable.

Where it shows up

  • Search boxes, category filters and sort options whose values are added to a query string in PHP, Node.js or Python code without placeholders.
  • WordPress plugins that call the database directly and build queries with string concatenation instead of the prepare method provided by the platform.
  • Login, password reset and coupon forms that look up a record using whatever text the visitor submitted, with no parameter binding.
  • REST and GraphQL API endpoints that pass JSON fields such as an ID, a column name or a sort direction straight into a query.

Free security check

Is CWE-89 hiding in your site?

A senior engineer checks your site, store or app for this weakness and the rest of the CWE Top 25, then sends a prioritized fix plan within one business day. No obligation.

Official definition

How MITRE defines CWE-89

Improper Neutralization of Special Elements used in an SQL Command

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.

Also known as: SQL Injection, SQLi

Weakness ID
CWE-89
Type
Base
Status
Stable
Likelihood of exploit
High
Applies to
Go, SQL, Python, Database Server
Introduced during
Implementation
2025 CWE Top 25
Rank 2

Fix it

How to fix SQL injection (SQLi)

Practical steps in priority order. Share them with your developer or agency.

  1. Use parameterized queries everywhere

    Send the query text and the values separately using prepared statements or bound parameters. In WordPress, use $wpdb->prepare. In PHP, use PDO with placeholders. The database then treats input as data only, never as SQL.

  2. Use an ORM or query builder

    Build queries through a maintained ORM or query builder such as Eloquent, Prisma, Sequelize or SQLAlchemy. These bind values for you. Review any raw query methods separately, because they bypass that protection.

  3. Allowlist identifiers you cannot bind

    Table names, column names and sort directions cannot be bound as parameters. Map the user’s choice to a fixed list of allowed values in code, and reject anything that is not on that list.

  4. Limit database account privileges

    Connect with a database user that has only the permissions the application needs. Do not use a root or owner account. Separate read-only and write accounts where you can, so one flaw exposes less.

  5. Hide database errors and monitor

    Show visitors a generic error page and log the details on the server. Turn off debug output in production. Add a web application firewall as a second layer, and alert on repeated query errors.

Signs you may be affected

  • Pages show raw database error messages when a form or URL receives unusual characters.
  • Your code builds query strings with concatenation or string interpolation instead of bound parameters.
  • Logs show long, strange values in URL parameters or form fields that normally hold short text.

Questions to ask your developer

  • Does every database query in our code use prepared statements or bound parameters?
  • Which plugins or custom modules run raw SQL, and who reviewed them?
  • What permissions does the database account used by our website have?

Prevention

What MITRE recommends, phase by phase

Every mitigation from the CWE entry, in the order a project runs: plan, build, test, operate.

  1. 01

    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 protection against SQL injection if used properly.
    • 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 generated. Process SQL queries using prepared statements, parameterized.
    • 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 software or its environment. For example, database applications rarely need to.
    Show 2 more recommendations
    • 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-side checks entirely. Then, these modified values would be submitted to the.
    • When the set of acceptable objects, such as filenames or URLs, is limited or known, create a mapping from a set of fixed input values (such as numeric IDs) to the actual filenames or URLs, and reject all other inputs.
  2. 02

    Implementation

    • While it is risky to use dynamically-generated query strings, code, or commands that mix control and data together, sometimes it may be unavoidable. Properly quote arguments and escape any special characters within those arguments. The most conservative approach is to escape or filter all characters that do not pass an extremely strict allowlist (such as everything that is not.
    • Assume all input is malicious. Use an "accept known good" input validation strategy, i.e., use a list of acceptable inputs that strictly conform to specifications. Reject any input that does not strictly conform to specifications, or transform it into something that does. When performing input validation, consider all potentially relevant properties, including length, type of.
    • Ensure that error messages only contain minimal details that are useful to the intended audience and no one else. The messages need to strike the balance between being too cryptic (which can confuse users) or being too detailed (which may reveal more than intended). The messages should not reveal the methods that were used to determine the error. Attackers can use detailed.
    Show 1 more recommendation
    • When using PHP, configure the application so that it does not use register_globals. During implementation, develop the application so that it does not rely on this feature, but be wary of implementing a register_globals emulation that is subject to weaknesses such as CWE-95, CWE-621, and similar issues.
  3. 03

    Operation

    • Use an application firewall that can detect attacks against this weakness. It can be beneficial in cases in which the code cannot be fixed (because it is controlled by a third party), as an emergency prevention measure while more comprehensive software assurance measures are applied, or to provide defense in depth [REF-1481.

Detection and evidence

How CWE-89 is found, and where it has hit

The testing methods MITRE recommends, and real vulnerabilities this weakness has caused.

How it is detected

  • Automated Static AnalysisThis 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 static analysis might not be able to recognize when proper input validation is being performed.
  • Automated Dynamic AnalysisThis 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, and fault injection. The software’s operation may slow down, but it should not become unstable.
  • Manual AnalysisManual 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 for all inputs, since the attack surface can be too large.
  • Automated Static Analysis – Binary or BytecodeAccording to SOAR [REF-1479], the following detection techniques may be useful: Bytecode Weakness Analysis – including disassembler + source code weakness analysis Binary Weakness Analysis – including disassembler + source code weakness analysis
  • Dynamic Analysis with Automated Results InterpretationAccording to SOAR [REF-1479], the following detection techniques may be useful: Database Scanners Web Application Scanner Web Services Scanner

Real vulnerabilities it caused

  • CVE-2024-6847SQL injection in AI chatbot via a conversation message
  • CVE-2025-26794SQL injection in e-mail agent through SQLite integration
  • CVE-2023-32530SQL injection in security product dashboard using crafted certificate fields
  • CVE-2021-42258SQL injection in time and billing software, as exploited in the wild per CISA KEV.
  • CVE-2021-27101SQL injection in file-transfer system via a crafted Host header, as exploited in the wild per CISA KEV.
  • CVE-2020-12271SQL injection in firewall product’s admin interface or user portal, as exploited in the wild per CISA KEV.

How we fix it

From finding to fixed: how Codefreex closes CWE-89

  1. Scan and confirm

    We test your site, store or app for this weakness and the rest of the CWE Top 25 with automated scanning and manual review, and confirm what is actually exploitable.

  2. Fix at the source

    We change the code, configuration or dependency that causes it, not just the symptom, and plan the changes to avoid downtime.

  3. Verify the fix

    We retest every finding, check that nothing else broke and hand you a short report you can share with your team, client or auditor.

  4. Keep it fixed

    Updates, monitoring and maintenance so the weakness does not return with the next plugin, theme, app or code change.

FAQ

CWE-89: FAQs

Short answers for owners and developers. Not sure whether your site is affected? Ask us to check.

How dangerous is SQL injection?

It is among the most damaging web weaknesses because it reaches the database directly. Depending on the query and the account permissions, an attacker may read customer data, change or delete records, or sign in as another user. In some setups it can lead to running commands on the server. MITRE rates the likelihood of exploitation as high.

Can a web application firewall stop SQL injection?

A firewall can block many known attack patterns and buys you time, but it is not a fix. It inspects requests from outside and can be bypassed by input it does not recognize. The flaw stays in your code until queries are parameterized. Treat the firewall as a second layer and fix the queries themselves.

How do I prevent SQL injection in a WordPress or custom site?

Use prepared statements for every query that includes outside data. In WordPress that means $wpdb->prepare or the built-in query APIs. Validate input types, such as casting IDs to integers. Allowlist any column or sort value. Give the database user minimal privileges, and keep plugins updated, since third-party code is a common source of unsafe queries.

What is CWE-89?

CWE-89, “Improper Neutralization of Special Elements used in an SQL Command”, is a software weakness catalogued by MITRE in the Common Weakness Enumeration. 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.

How serious is CWE-89?

MITRE rates the likelihood of exploit as High. It can affect confidentiality, integrity, availability, authentication, with impacts such as execute unauthorized code or commands, read application data, gain privileges or assume identity. It ranks number 2 on the 2025 CWE Top 25 Most Dangerous Software Weaknesses. The real risk depends on where the weakness sits and what data or functions it exposes.

How do you prevent CWE-89?

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. 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.

Can Codefreex check my site for CWE-89?

Yes. A senior engineer reviews your site, store or app for CWE-89 and the rest of the CWE Top 25, confirms what is exploitable and sends a prioritized fix plan within one business day. Start with the free security check form on this page.

Free security check

Find out if CWE-89 affects your site

Send your URL. A senior engineer checks for this weakness and the rest of the CWE Top 25, and replies within one business day with what to fix first.

  • A senior strategist reviews your request, not a sales bot
  • Clear scope, timeline and estimate within one business day
  • Free 30-minute discovery call, no obligation
  • NDA available on request

Weakness data from the Common Weakness Enumeration (CWE®) version 5.0, Copyright © 2006–2026, The MITRE Corporation. CWE is free to use by any organization or individual for any research, development, and/or commercial purposes under the CWE Terms of Use. CWE is a registered trademark of The MITRE Corporation. The business impact, risk profile and plain-English guidance on this page are written by Codefreex and are general information, not a security assessment of your system.