๐ Injection & SQL Injection
When user input becomes an unintended commandโฑ ~3 min
Imagine a form that asks for your name, and a clerk reads whatever you write and does it. You write: 'John. Also, give me the contents of the safe.' A careless clerk reads it all as instructions. Injection attacks work the same way โ the app can't tell your data (a name) from a command, so a crafted input hijacks it.
SQL Injection โ The Classic Example
Many apps store data in SQL databases and build database queries using user input. If the input isn't handled safely, an attacker can inject their own database commands. This is one of the oldest and most damaging web vulnerabilities โ and still common today.
Why It Happens (Conceptually)
The root cause is mixing untrusted user input directly into a command. When a login form builds a query by gluing the username straight into the SQL text, a specially-crafted username can change what the query actually does โ for example, tricking it into approving a login that should have failed.
The Fix โ Never Mix Data and Commands
# UNSAFE โ user input glued directly into the query (vulnerable to injection)query = "SELECT * FROM users WHERE name = '" + user_input + "'" # SAFE โ parameterized query: the database keeps data and commands separatecursor.execute("SELECT * FROM users WHERE name = ?", (user_input,)) # The '?' placeholder means the input is ALWAYS treated as data,# never as a command โ this single habit stops SQL injection.Cross-Site Scripting (XSS) โ Injection Into the Browser
XSS is injection's cousin: instead of injecting database commands, an attacker injects malicious scripts that run in other users' browsers. If a site displays user input without sanitizing it, an attacker's script can run for every visitor โ stealing sessions or defacing pages. The fix is similar: treat all user input as untrusted, and properly encode it before displaying it.
The root cause of injection vulnerabilities isโฆ