Data access and security · core
SQL injection and mass assignment in Django, DRF and FastAPI
Never let a client choose column names, filter keys or the fields that are saved. Placeholders protect values only. Accept an explicit list of fields and return a separate output model.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Placeholders (%s, :name) work only for values, not column names. A sort parameter that goes into SQL as text can leak data bit by bit. Use an allow-list that maps a choice to a column.
- filter(**request.GET.dict()) lets the client choose fields and lookups such as ?user__password__startswith=... or a __regex lookup on a related table. A crafted _connector key allowed SQL injection through filter() and Q() (CVE-2025-64459, fixed in a Django security release in November 2025). Keep Django patched.
- Scope every query to the user (filter(user=request.user)), limit the page size and select only the needed columns.
- SQLAlchemy: text(f"... ORDER BY {sort}") is the same bug. Bind values with text("... WHERE user_id = :uid").bindparams(uid=...) and map the sort choice to a column.
- Mass assignment: extra="allow" plus a loop that copies every key onto the ORM object lets a client send {"is_admin": true}. Use an explicit input model with only editable fields and extra="forbid", with model_dump(exclude_unset=True) for PATCH.
- Returning the ORM object can expose password_hash and internal flags; use response_model=UserOut. In DRF: an explicit fields list and read_only_fields, separate serializers for admins, ownership set in perform_create.
- Tests: send forbidden fields to every write endpoint and assert 422 or 400, or that nothing changed. Role changes go through a separate admin-only endpoint with an audit log.
- Catch such code in CI: Bandit or Semgrep rules for raw, extra, RawSQL and text(f"...").
The red pen
Common traps: mistake → fix
Mistake: ORM means injection is impossible
Fix: raw(), extra(), RawSQL and text(f"...") can still inject, and filter(**dict) lets clients probe fields.
Mistake: Placeholders protect the ORDER BY column
Fix: they only bind values.
Mistake: The client only sends fields we show in the form
Fix: a client can send any field the model accepts.
Mistake: Hiding the field in the response protects the write
Fix: input and output need separate models.
Easy to hard
Questions interviewers ask, easy to hard
- Review this search endpoint for security problems.
- Why is filter(**request.GET.dict()) dangerous?
- How can a user make themselves admin through a profile update API?
- How do you fix it in FastAPI and in DRF?
- How do you test for it?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.