Breaking and Fixing a Tiny Login Form: SQL Injection Hands-On
SQL injection is the classic security bug in database code. The best way to understand it is to watch it happen in a tiny program on your own computer, then fix it. Only ever try this on your own practice code, never on systems you do not own.
Step 1: the unsafe version
Here is a deliberately bad Python function using the built-in sqlite3 module:
import sqlite3
db = sqlite3.connect("practice.db")
db.execute("CREATE TABLE IF NOT EXISTS users (name TEXT, password TEXT)")
db.execute("INSERT INTO users VALUES ('ana', 'secret1')")
def login_unsafe(name, password):
sql = "SELECT * FROM users WHERE name = '" + name + "' AND password = '" + password + "'"
return db.execute(sql).fetchone() is not None
It glues the user's input straight into the SQL text. (Real apps should never store plain passwords either; this is only a toy.)
Step 2: watch it break
Call it with normal input and it works: login_unsafe("ana", "wrong") returns False. Now try this password:
' OR '1'='1
The SQL the function builds becomes:
SELECT * FROM users WHERE name = 'ana' AND password = '' OR '1'='1'
The quote in the input closed the string early, and the rest was read as SQL. Because '1'='1' is always true, the query returns a row and the login succeeds without the password. That is SQL injection: user input changed the meaning of the query.
Step 3: the fix
Use a placeholder (also called a parameter). The SQL text stays fixed, and the values travel separately:
def login_safe(name, password):
sql = "SELECT * FROM users WHERE name = ? AND password = ?"
return db.execute(sql, (name, password)).fetchone() is not None
Try the same tricky password again. This time the database treats it as plain text to compare, finds no user with that exact password, and returns False.
Step 4: why the fix works
With placeholders, the database receives the query shape first and the values second. A value can never become part of the SQL itself, no matter what characters it contains. You do not need to escape quotes by hand, which is easy to get wrong.
Common mistakes
- Using string formatting instead of placeholders.
f"... WHERE name = '{name}'"is just as unsafe as+. - Thinking "my users are friendly". Injection is usually found by automated scanners, not friends.
- Trying to parameterise table or column names. Placeholders work for values only. If a user chooses a sort column, check it against a fixed list of allowed names in your code.
- Forgetting other languages. Every database library has placeholders, though the symbol differs:
?in SQLite and MySQL drivers,$1in the Node.jspglibrary,%sin Python's psycopg. Our Node.js placeholder how-to shows another example.
Clean up
Delete practice.db when you are done. Then keep one habit for life: whenever you see SQL built by joining strings with user input, stop and use a placeholder.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.
This page is a free outline. No downloadable files, paid content, courses or certificates are offered on this site.