Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Security Fundamentals

    Injection

    Injection — attacker-controlled input is interpreted as part of a command or query.

    Chapter 7 of 135 min readOften asked in interviews

    The Plain Picture#

    You hand a clerk a form: "Please fetch the file for student: ______". Someone writes in the blank: "Priya. Also, hand me the keys to the building." A clerk who reads the whole blank as instructions will do both.

    The flaw: the data (the name) was mixed into the instructions, and the clerk couldn't tell where one ended and the other began.

    The Real Names#

    • Injection — attacker-controlled input is interpreted as part of a command or query.

    • SQL injection — the query language of databases (SQL, SQL Basics (coming soon)) receives input glued into the query string. Part 0's problem 3.

    • Command injection — input glued into a shell command (The Linux Command Line).

    • The fix for all of them: keep data and code separate — parameterized queries (also called prepared statements) for SQL; argument lists instead of shell strings for commands.

    Under the Hood — SQL injection step by step#

    Vulnerable login code builds the query by gluing strings:

    Python
    query = "SELECT * FROM users WHERE email = '" + email + "' AND password_hash = '" + h + "'"

    The attacker types this as the email:

    Output
    ' OR '1'='1' --

    The database receives:

    SQL
    SELECT * FROM users WHERE email = '' OR '1'='1' -- ' AND password_hash = '...'
    • '1'='1' is always true, so OR makes the whole condition true — every user matches;

    • -- starts a comment in SQL, so the password check is ignored;

    • the app logs the attacker in as the first user, often the admin.

    Worse inputs can read other tables (UNION SELECT ...) or delete data.

    The fix — parameterized query:

    Python
    db.execute("SELECT * FROM users WHERE email = %s", (email,))

    The query's structure is sent to the database first; the values are sent separately and are always treated as data, never as SQL. An email of ' OR '1'='1' -- is just a strange email that matches nobody. Every database library supports this, and ORMs (Object-Relational Mappers — libraries that let you work with database rows as normal objects in your code, writing the SQL for you) like SQLAlchemy use it automatically (FastAPI with Databases (coming soon)) — until someone builds a raw query with an f-string.

    Command injection:

    Python
    os.system("convert " + filename + " out.png")     # filename = "a.jpg; rm -rf ~"  → runs rm!
    subprocess.run(["convert", filename, "out.png"])  # safe: filename is one argument, never parsed by a shell

    Gotchas

    • Escaping input by hand ("just remove the quotes") is fragile — there are many encodings and edge cases. Use parameters.

    • Table and column names can't be parameters. If users choose a sort column, check it against an allow-list of known column names.

    • Injection exists everywhere data meets an interpreter: NoSQL queries, LDAP, HTML (that's XSS, next chapter), and LLM prompts (Ch 12).

    Check yourself

    4 questions
    1

    What is the root cause of every injection attack?

    Show answer

    Untrusted data is mixed into code/commands, so the interpreter can't tell data from instructions.

    2

    Explain how ' OR '1'='1' -- bypasses a login.

    Show answer

    It closes the string, adds an always-true condition with OR, and comments out the rest (the password check).

    3

    What's the fix for SQL injection, and why does it work?

    Show answer

    Parameterized queries: the query structure and values travel separately, so values are always data, never SQL.

    4

    A user can choose which column to sort by. Why doesn't a parameter help, and what do you do?

    Show answer

    Identifiers like column names can't be parameters; validate against an allow-list of permitted columns.