Introduction
Imagine a website that lets you search for products. What if someone could trick the website into showing or changing data they shouldn't see? SQL injection testing helps find these weak spots before bad people do.
Imagine a restaurant where customers can write special requests on their order slips. If the kitchen blindly follows every word, a customer might write a harmful instruction like 'add poison'. Testing is like checking if the kitchen safely handles these requests without causing harm.
┌───────────────┐ ┌───────────────┐ ┌───────────────┐
│ User Input │──────▶│ Application │──────▶│ Database │
│ (possibly │ │ (builds query) │ │ (stores data) │
│ harmful) │ │ │ │ │
└───────────────┘ └───────────────┘ └───────────────┘
▲ │ ▲
│ │ │
└─────────────────────┴────────────────────────┘
If input is not checked,
harmful commands reach databaseimport sqlite3 conn = sqlite3.connect(':memory:') cur = conn.cursor() cur.execute('CREATE TABLE users (id INTEGER, name TEXT)') cur.execute("INSERT INTO users VALUES (1, 'Alice')") # Unsafe query vulnerable to SQL injection user_input = "1 OR 1=1" query = f"SELECT * FROM users WHERE id = {user_input}" cur.execute(query) results = cur.fetchall() for row in results: print(row)