What is SQL injection and how do parameterized queries prevent it?
What is SQL injection and how do parameterized queries prevent it?
SQL injection happens when user input is concatenated into a SQL string, so the input changes the structure of the query instead of being treated as a value.
email = request.args["email"] # never build SQL like this
cur.execute("SELECT id FROM users WHERE email = '" + email + "'")
Input ' OR '1'='1 produces WHERE email = '' OR '1'='1', which returns every user. Input '; DROP TABLE users; -- can stack a second statement where the driver allows it.
The SQL text and the values travel separately, so the value can never become SQL.
cur.execute("SELECT id FROM users WHERE email = %s", (email,)) # psycopg
await client.query("SELECT id FROM users WHERE email = $1", [email]); // node-postgres
PreparedStatement ps = conn.prepareStatement("SELECT id FROM users WHERE email = ?");
Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.