0
0
Flaskframework~20 mins

SQL injection prevention in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SQL Injection Prevention Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use parameterized queries in Flask?

Which of the following best explains why parameterized queries help prevent SQL injection attacks in Flask applications?

AThey automatically sanitize HTML output to prevent script injection.
BThey encrypt the database connection to block attackers.
CThey separate SQL code from data, so user input cannot change the query structure.
DThey limit the number of database connections to prevent overload.
Attempts:
2 left
💡 Hint

Think about how user input is handled inside the SQL command.

component_behavior
intermediate
2:00remaining
Output of unsafe query with user input

Consider this Flask code snippet using raw string formatting for SQL:

user_id = "1 OR 1=1"
query = f"SELECT * FROM users WHERE id = {user_id}"
cursor.execute(query)
result = cursor.fetchall()

What will result contain?

AOnly the row with id = 1, because the input is treated as a string.
BAll rows from the users table, because the condition always evaluates to true.
CAn empty list, because the query is invalid.
DA syntax error is raised due to improper query formatting.
Attempts:
2 left
💡 Hint

Look at how the user input changes the WHERE clause.

📝 Syntax
advanced
2:00remaining
Correct parameterized query syntax in Flask

Which of the following Flask code snippets correctly uses parameterized queries to safely select a user by id?

Acursor.execute("SELECT * FROM users WHERE id = :user_id", {'user_id': user_id})
Bcursor.execute(f"SELECT * FROM users WHERE id = {user_id}")
Ccursor.execute("SELECT * FROM users WHERE id = ?", (user_id,))
Dcursor.execute("SELECT * FROM users WHERE id = %s", (user_id,))
Attempts:
2 left
💡 Hint

Check the parameter style and how parameters are passed as a tuple.

🔧 Debug
advanced
2:00remaining
Identify the error in parameterized query usage

What error will this Flask code raise?

user_id = 5
cursor.execute("SELECT * FROM users WHERE id = ?", user_id)
ATypeError because parameters must be in a sequence like a tuple or list.
BSyntaxError due to missing quotes around the placeholder.
COperationalError because the placeholder ? is invalid in this database.
DNo error; the query runs successfully.
Attempts:
2 left
💡 Hint

Look at how parameters are passed to execute.

state_output
expert
2:00remaining
Result of mixed safe and unsafe query parts

Given this Flask code:

user_input = "1; DROP TABLE users;"
query = "SELECT * FROM users WHERE id = " + user_input
cursor.execute(query)
rows = cursor.fetchall()

What will happen when this code runs?

AA syntax error occurs because multiple statements are not allowed in one execute call.
BThe database executes the SELECT and then drops the users table, causing data loss.
COnly the SELECT runs safely; the DROP TABLE is ignored by the database.
DThe query returns no rows but the users table remains intact.
Attempts:
2 left
💡 Hint

Consider how the database driver handles multiple SQL statements in one execute.