SQL Injection
Also known as: SQLi
Web application vulnerability in which untrusted input is interpreted as part of a database query, letting attackers read, change or delete data.
SQL injection is a class of vulnerability that arises when an application builds a database query by combining fixed SQL code with data supplied by a user, without keeping the two strictly separate. If the input contains characters that the database interprets as code, the meaning of the query changes. Depending on the application, the result can be disclosure of entire databases, bypassed logins, altered or deleted records and, in some configurations, control of the underlying server. It belongs to a broader family of injection flaws.
The flaw was publicly discussed in hacker publications by the late 1990s and remained among the most common and damaging web vulnerabilities for two decades. Injection has featured in every edition of the OWASP Top 10 list of web application risks, and SQL injection was behind numerous large data breaches in the 2000s and 2010s. The underlying cause is conceptual rather than specific to any product: whenever code and data share the same channel, carefully crafted data can be mistaken for instructions, the same pattern seen in prompt injection against AI systems.
SQL injection is almost entirely preventable. The primary defense is parameterized queries, also called prepared statements, which send the query structure and the user's data to the database separately so input can never change the command. Object-relational mappers and stored procedures help when used correctly. Supporting measures include validating input, running database accounts with the least privilege necessary, web application firewalls and static analysis during development. Its persistence despite well-known fixes shows how legacy code, rushed development and gaps in training keep old vulnerabilities alive.
Key points
- SQL injection occurs when user input is treated as part of a database command.
- Consequences range from data theft and altered records to bypassed logins and server compromise.
- Parameterized queries, which keep code and data separate, are the primary and highly effective defense.
- The flaw shares its root cause with other injection attacks, including prompt injection against AI models.
Related entries
SQL
Declarative language for defining, querying and modifying data in relational databases, standardized by ANSI and ISO since the 1980s.
OWASP Foundation
Nonprofit foundation that produces free, community-built resources for improving software security, including the widely used OWASP Top 10.
Cross-Site Scripting
Web vulnerability that lets attackers inject malicious scripts into pages viewed by other users, which then run with the trust of the affected site.
Prompt Injection
Security vulnerability in which crafted text causes an AI model to follow an attacker's instructions instead of those of its developer or user.
Vulnerability
A weakness in software, hardware, configuration or process that an attacker could exploit to compromise confidentiality, integrity or availability.
Database
Organized collection of structured data stored electronically and managed by software that lets users store, query and update it reliably.
Static Analysis
Examination of source code or binaries without executing them, using automated tools to find bugs, security flaws and quality issues.
Penetration Testing
An authorized, simulated attack on a system, network or application, performed to find and fix security weaknesses before real attackers do.
Featured in guides
Referenced by
Other entries that discuss or point to SQL Injection.