SQL injection
An attack where untrusted input is joined into a database query so that part of it runs as SQL, letting an attacker read or change data.
It can expose or destroy the whole database.
In more detail
It happens when code builds a query by joining strings, such as a name that closes the quote and adds its own SQL. The fix is parameterised queries, which send values separately from the SQL text, never escaping by hand. Client choices such as sort keys should map to fixed server expressions rather than raw text.
Examples
Web logs show it as quotes, OR 1=1, comments or UNION SELECT in request parameters, often URL-encoded, so decode before matching."SELECT body FROM doc WHERE owner = '" + owner + "'" with owner = "bob' OR '1'='1"In the course, the name alice' OR '1'='1 closes the quotation early and returns every row.Related terms
- parameterised query — A database query written with placeholders, such as WHERE id = ?, whose values are sent separately so they can never be read as SQL.
- input validation — Checking that data from a user, file or caller is acceptable before using it, and rejecting or asking again if not.
- sort key — The field a collection is ordered by, chosen by the client from a small documented list, such as sort=title or sort=-id for descending.
- SQL — The standard language for defining, querying and changing data in a relational database.
- prepared statement — A SQL statement with placeholders, such as ?, whose values are sent separately from the SQL text instead of pasted into it.
- static application security testing — Analysing source code without running it to find insecure patterns, such as SQL built from user input.
- CWE — Common Weakness Enumeration: MITRE's numbered catalogue of software weakness types, such as CWE-89 for SQL injection.
- path traversal — An attack that uses ../ sequences in a request to reach files outside the folder a web application meant to serve.
- web access log — The log a web server writes with one line per request: client address, time, request line, status code, size and often user agent.
- URL encoding — Writing special characters in a web address as % followed by two hexadecimal digits, such as %20 for a space.
- tautology — A condition that is always true, such as '1'='1', which attackers add to a query to make it match every row.
- injection — A bug where untrusted data reaches an interpreter and part of it is read as code instead of as a value.
- string literal — A fixed text value written directly in a query between single quotes, such as 'Lagos'.
Taught in
- Filtering And Searching — Modern API Architecture: REST, GraphQL & gRPC
- Sorting Results — Modern API Architecture: REST, GraphQL & gRPC
- Security Best Practices — Modern API Architecture: REST, GraphQL & gRPC
- Twelve Alerts, Ten Services — AWS Certified Cloud Practitioner (CLF-C02) Exam Prep
- Security Sprint: Fifteen Questions' Worth — AWS Certified Cloud Practitioner (CLF-C02) Exam Prep
- Confusable Services Sprint: One Clue Each — AWS Certified Cloud Practitioner (CLF-C02) Exam Prep
- The Edge: WAF, Shield, CloudFront And GuardDuty — AWS Certified Solutions Architect – Associate (SAA-C03) Exam Prep
- The Full Mock And Your Review Plan — AWS Certified Solutions Architect – Associate (SAA-C03) Exam Prep
- Static Application Security Testing with Semgrep — Cloud Security & DevSecOps Engineering
- Detection as Code: Authoring & Testing Sigma Rules — Security Operations & Threat Hunting (SOC)
- Adversary Tactics, Techniques & Procedures (TTP) Mapping — Security Operations & Threat Hunting (SOC)
- Injection In All Its Forms — Application Security & Threat Modeling for Engineers
- SQL Injection: The Exploit — Application Security & Threat Modeling for Engineers
- SQL Injection: The Fix — Application Security & Threat Modeling for Engineers
- Inserting Data Safely — PostgreSQL, Indexing & Relational Data Modeling
References
- OWASP Cheat Sheet Series: SQL injection prevention
- OWASP Top 10: A03 Injection
- OWASP: SQL injection
- CWE-89: SQL injection
- MITRE ATT&CK: Exploit Public-Facing Application