Lesson 13: Web Security: Injection and Safe Coding
The most common way websites are broken into is not a clever piece of hacking but a simple mistake: the site treats data from a visitor as if it were instructions. That mistake is called injection, and it comes in many forms, all sharing the same root cause: user input is mixed into code without being separated from it. This lesson explains the two most famous kinds, SQL injection and command injection, shows what they look like from the defender's side, and teaches the single principle that prevents them all, which is to keep data and code apart. The examples here are deliberately small and use a fictional practice site, because the goal is to recognise and prevent these attacks, never to use them against a real system.
SQL injection: data that becomes code
Imagine a fictional library site with a search box. A careless server builds its database query by pasting the visitor's words directly into the SQL text, so a search for “poetry” becomes SELECT * FROM books WHERE title LIKE '%poetry%';. Now imagine a visitor who types something that closes the quote and adds their own SQL, such as a search that ends the text and adds OR 1=1, which is always true. The database then returns every book, including ones the visitor should not see, and with more crafted input, an attacker could read other tables or delete data. The site never intended any of that: it simply pasted untrusted words into code, and the database obeyed. The fix is a parameterised query, which you met in the web development course: the input is passed as a value, never as part of the SQL text, so the database treats it as data and nothing else, and the whole class of attack disappears.
Command injection and other flavours
The same mistake appears wherever input meets a command. If a server runs a system command that includes a visitor's input, such as a tool that checks whether a hostname is reachable, an attacker can add a second command after a separator and have the server run it, which is called command injection. If a page shows a visitor's name without escaping it, and the name contains script tags, the browser may run that script, which is cross-site scripting, covered in the next lesson. The pattern is always the same: a boundary between code and data was not enforced, and input crossed it. The defence is also always the same: use the safe tool for the job, such as a parameterised query or a library that escapes output, and never build code by pasting strings together, because a string built from input is a string an attacker can control.
Validation and the principle of least privilege
Two more habits make injection far less dangerous. The first is validation: check input on the server before you use it, such as confirming a number is a number, a date is a date, and text is not absurdly long, and reject anything that does not fit, rather than trying to clean it up, because cleaning is error-prone and attackers are creative. The second is the principle of least privilege: give each part of your system only the access it needs, so the database account a website uses can read the books table but cannot delete the users table, and a compromise of one part does not become a compromise of everything. Together, validation and least privilege mean that even if an attacker finds a crack, the damage is limited, which is the difference between an incident and a disaster, and it is the subject of Lesson 18.
Safe coding as a habit
Safe coding is less about memorising attacks than about building habits that prevent them by default. Use parameterised queries for every database call, without exception. Use a templating library that escapes output automatically, so visitor text is shown as text, never run as code. Keep your libraries and frameworks updated, because injection flaws in popular software are fixed quickly, and the fix only helps if you install it. And test your own code with unexpected input: what happens if a name contains a quote, a search contains a semicolon, or a number field contains letters? If the answer is an error message rather than a breach, your defences are working. The best time to learn these habits is now, while your projects are small, because the habits you build on small projects are the ones you will carry into large ones.
Key Terms
- Injection
- An attack where visitor input is treated as code, because data and code were not kept apart.
- SQL injection
- Injecting SQL into a database query by typing SQL into a field the server pastes into its query.
- Command injection
- Injecting a system command by adding it to input that a server uses in a command.
- Parameterised query
- A query where input is passed as a value, never pasted into the SQL text, which prevents SQL injection.
- Validation
- Checking input on the server before using it, and rejecting anything that does not fit.
- Least privilege
- Giving each part of a system only the access it needs, so a compromise is limited in scope.
- Escaping
- Converting special characters in output, such as < to <, so text is shown as text and never run as code.
Discussion Questions
- Why does a parameterised query stop SQL injection, in one sentence?
- A website validates input in JavaScript in the browser. Why is that not enough, and what must happen on the server?
- If an attacker does get in, how does least privilege limit the damage?
Practice Questions
- What is the root cause of injection attacks?
- How does a parameterised query prevent SQL injection?
- In the library example, what does adding OR 1=1 to a search do?
- What is the principle of least privilege?
- Why is validating input in the browser not enough?
Show answers
- Visitor input is treated as code. Data and code were mixed together, so input the attacker controls is run as instructions.
- It passes input as a value, never as part of the SQL text. The database treats the input as data, so typed-in SQL cannot be executed.
- It makes the condition always true, so the query returns every row. The attacker has turned a search into a command to show everything.
- Give each part of a system only the access it needs. A website's database account should be able to read the tables it needs and nothing more.
- Because an attacker can bypass the browser entirely. Validation must happen on the server, which is the part the attacker cannot control.