The Attacks to Expect: Injection and Mass Assignment

An API online gets requests no app of yours would send, so it keeps typed text out of its SQL with placeholders, takes only the fields a person may change with a model that forbids extras, answers errors without their inner details, and checks its packages for known holes with pip-audit.

Journeyman · FastAPI · Web Development · Updated

By

After this lesson you can

  • Stop mass assignment with an update model that leaves out fields such as role and forbids extra fields
  • Recognize SQL injection, error answers that leak details and packages with known holes, and choose each one's fix

Key takeaways

  • SQL injection turns typed text into SQL. A ? placeholder sends the text as a value, so it can't change the query.
  • Mass assignment is a body that sets fields it shouldn't, such as role. An update model that lists only what a person may change is an allow-list.
  • model_config = ConfigDict(extra="forbid") makes Pydantic refuse unknown fields with a 422, instead of quietly ignoring them.
  • An error answer says what went wrong in plain words, never the database's own message, a column's name or a traceback.
  • pip-audit checks a project's packages against a list of known security holes and names the version that fixes each.

Within days of going online, an API starts getting requests that no app of yours ever sent: quotes in an email field, fields nobody asked for, addresses that don't exist. Most of it comes from programs that try the same few tricks on every API they find. This lesson walks through the ones Bean There Coffee Club's API should expect, and the habit that stops each.

Injection: the oldest trick

Placeholders, and Why SQL Is Never Built from Strings showed it on a plant shop. Here it is once more, on a lookup of a member by email, because it is still one of the most common holes on the web:

import sqlite3

conn = sqlite3.connect(":memory:")
conn.execute("CREATE TABLE members (id INTEGER PRIMARY KEY, email TEXT NOT NULL, favorite_roast TEXT NOT NULL)")
conn.executemany(
    "INSERT INTO members (email, favorite_roast) VALUES (?, ?)",
    [("dana@example.com", "Sunrise Blend"), ("marcus@example.com", "Midnight Harbor")],
)

typed = "x' OR '1'='1"

unsafe = conn.execute(f"SELECT email, favorite_roast FROM members WHERE email = '{typed}'").fetchall()
safe = conn.execute("SELECT email, favorite_roast FROM members WHERE email = ?", (typed,)).fetchall()
print(unsafe)
print(safe)
[('dana@example.com', 'Sunrise Blend'), ('marcus@example.com', 'Midnight Harbor')]
[]

The f-string pasted the typed text into the SQL, so its quote ended the email early and OR '1'='1' became part of the query: every member came back. The ? placeholder sent the same text as a value, and no member has that email. The habit: every value goes in through a placeholder, every time.

Mass assignment: a body that sets role

The club's PATCH /members/me lets a member change their own record. Its model was copied from the staff's tool while the API was being built, so it has one field too many:

from pydantic import BaseModel


class MemberChanges(BaseModel):
    name: str | None = None
    favorite_roast: str | None = None
    role: str | None = None

The route saves whatever the model holds, with model_dump(exclude_unset=True) as Create, Update and Delete: POST, PUT, PATCH and DELETE does. Dana, signed in, sends {"favorite_roast": "Midnight Harbor", "role": "staff"}, and the API answers 200 OK:

{"id":1,"email":"dana@example.com","name":"Dana","favorite_roast":"Midnight Harbor","role":"staff"}

Dana is staff now, and GET /members, which shows every member's email, answers her with the whole list. This is mass assignment: a request sets fields it was never meant to, because the code copies the whole body onto the record. Picture a form where members may change their address, and a clerk who types in everything written on it, including "make me a manager" in the margin.

The fix is to decide at the door which fields may come in: an allow-list. A model that lists only what a member may change is one:

from pydantic import BaseModel, ConfigDict


class MemberChanges(BaseModel):
    model_config = ConfigDict(extra="forbid")

    name: str | None = None
    favorite_roast: str | None = None

Without role in the model, Pydantic drops it, and Dana's request changes only her roast. That's safe, but quiet: the caller never learns that role was thrown away. model_config = ConfigDict(extra="forbid") (read "extra equals forbid") turns the quiet drop into a 422, in the API's one error shape:

{"status":422,"title":"Unprocessable Content","detail":"The request isn't valid.","problems":[{"field":"role","message":"Extra inputs are not permitted"}]}

Nothing is saved, and Dana is still a member. The data module's update_member can still change role, because the staff's own tools need it; deciding what each request may change belongs to the route's model.

Errors that say too much

An error answer is read by attackers too. Suppose a route catches a database error and sends its message back:

no such column: favourite_roast

That's SQLite's own message, and it tells a stranger that the API uses SQLite and what one of its columns is called, which is a head start on the next attack. The one error shape from One Error Shape for the Whole API already does the right thing: a crash answers 500 with "Something went wrong on our side.", and the details stay in the terminal. Sign-in does the same with one vague 401 for a wrong email or a wrong password, so nobody can use it to find out who's a member.

Packages with known holes: pip-audit

Your own code isn't the only code that runs. FastAPI, Starlette, PyJWT and the rest are code too, and now and then someone finds a hole in one, which gets fixed in a new version. pip-audit checks your packages against a public list of known holes. Install it in the project's virtual environment with pip install pip-audit==2.10.1, then check the pinned packages in requirements.txt:

pip-audit -r requirements.txt
No known vulnerabilities found

That's the coffee club's requirements, with the same versions as this chapter's exercise. On a project still pinned to an old PyJWT, 2.3.0, the answer starts like this, and goes on for 18 rows, each with the version that fixes it:

Found 18 known vulnerabilities in 1 package
Name  Version ID                  Fix Versions
----- ------- ------------------- ------------
pyjwt 2.3.0   PYSEC-2022-202      2.4.0

The fix is to pin a version at least as new as the highest one in the Fix Versions column, run the tests, and commit the new requirements.txt. Run pip-audit before each release, and whenever you add a package; it ends with an error code when it finds something, so a script or an automatic check can stop on it.

Do it: keep only the fields a member may change

Bean There's API takes a member's changes as a dictionary, the request's body as it arrived. Before it saves anything, it keeps only the fields a member is allowed to change, so a body that tries to set role changes nothing.

  • body: the request's body, a dictionary, such as {"favorite_roast": "Midnight Harbor", "role": "staff", "cups_left": 99}.
  • allowed: a list of the field names a member may change, such as ["name", "favorite_roast"].

Your job: write pick_allowed_fields(body, allowed). It returns a new dictionary with only the keys of body that are in allowed, each with its value from body.

  • A field in allowed that body doesn't have is left out: don't add it.
  • When no field of body is allowed, return an empty dictionary, {}.
  • Don't change body.

Worked through: pick_allowed_fields({"favorite_roast": "Midnight Harbor", "role": "staff", "cups_left": 99}, ["name", "favorite_roast"]). favorite_roast is allowed and kept; role and cups_left aren't, and name isn't in the body. It returns:

{"favorite_roast": "Midnight Harbor"}

Practiced live on the lesson page: write the Python right there and check it.

Check yourself

2 questions to try on the lesson page.

So what

Most attacks on an API are the same few tricks tried everywhere, and four habits answer them: placeholders for every value, a model that lists what may change, errors that say only what the caller needs, and packages checked for known holes.

Key terms

Related