Lesson 1 · Unit 02
Injection
When user input is run as SQL, shell or a file path
What you practise
- SQL built by string concatenation,
' OR '1'='1, parameterized queries as the fix, the same shape in shell commands and file paths
Try a question from this lesson
Pick one
A login builds its SQL by pasting your input between quotes. In SQL, -- starts a comment. You enter the name ana' -- . What happens to the password check?
query = ("SELECT * FROM users "
"WHERE name = '" + name + "' "
"AND pw = '" + pw + "'")Correct answer
It is commented out: only the name is checked
Why
Your quote closes the name early, and -- turns the rest of the line, password check included, into a comment. The query now asks only for the user ana. This is SQL injection.
The idea
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Data became code
The bug is not the quote. It is building a command by gluing strings, so the database cannot tell your SQL from the user's text.
A parameterized query keeps them apart. You send the SQL with ? placeholders and the values separately. The values are never parsed as SQL, whatever they contain.
# wrong: input is parsed as SQL
q = "... WHERE name = '" + name + "'"
db.execute(q)
# right: input can only be a value
db.execute("... WHERE name = ?", (name,))11 more exercises in the app
- 2× type the answer
- 5× multiple choice
- 1× fill the blanks
- 1× true or false
- 1× order the lines
- 1× match the pairs
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.