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)
SQL injection testing in software testing?' OR '1'='1 try to change query logic to always true.SELECT * FROM users WHERE username = '{input}' AND password = '{input}';
admin and for password is ' OR '1'='1, what will happen?SELECT * FROM users WHERE username = 'admin' AND password = '' OR '1'='1';query = "SELECT * FROM products WHERE id = '" + user_input + "'";
SELECT * FROM users WHERE name LIKE '%{input}%';Pass the test and this pattern moves unknown