f-string SQL query
This looks up a user by email for the login form. What's the problem?
import sqlite3 def find_user(conn: sqlite3.Connection, email: str): cur = conn.cursor() cur.execute(f"SELECT id, email, password_hash FROM users WHERE email = '{email}'") return cur.fetchone()Show answer
line 6 · Security / injection
The email is pasted into the SQL text, so input like ' OR '1'='1 changes the query and returns the first user, letting an attacker log in as someone else. Let the driver bind the value instead.
cur.execute("SELECT id, email, password_hash FROM users WHERE email = ?", (email,))Interview follow-up: Expect a follow-up on why escaping quotes by hand is not enough, and how ORMs parameterize queries for you.