Stack

How to security test a Django app

Updated

Django and DRF are safe by default and unsafe the moment a view sets permission_classes = [AllowAny] or a queryset looks up by pk without scoping to the request user. Test each DRF view anonymously, then with a low-privilege user against another tenant's object id.

Where this class of bug actually lives in a Django app

  • DRF views with no permission class. APIView defaults to fully open; only settings.DEFAULT_PERMISSION_CLASSES saves you, and a single view can override it.
  • Object lookup by pk. get_object_or_404(Invoice, pk=pk) returns any tenant's invoice. The correct form filters on request.user.organization first.
  • Admin at a guessed path. /admin/ is a login form and a staff-only surface; left at the default URL it is a public, well-known target.
  • Serializer fields. A ModelSerializer with fields = '__all__' exposes and accepts every column, including is_staff.

What to try first

  1. List DRF views and their permission_classes; request each one anonymously and confirm a 401 or 403.
  2. Take a pk from account A, request it with account B's token, and compare the body with a nonexistent pk.
  3. POST to a profile update with is_staff and organization in the body.
  4. Check /admin/ in production and whether DEBUG leaks settings on a 500.

A reproduction you can keep

The unscoped pk lookup is the classic Django finding. Two accounts, one id swap:

Cross-tenant object read
# account A's object id, requested with account B's token
curl -sS -i https://staging.example.com/api/invoices/1003/ \
  -H 'Authorization: Bearer ***'

# control: an id that does not exist
curl -sS -i https://staging.example.com/api/invoices/99999999/ \
  -H 'Authorization: Bearer ***'

# vulnerable: 200 for 1003, 404 for 99999999 — the id is an oracle
# fixed: both 404, with identical bodies

What "fixed" has to look like

Scope the queryset to the request user's organization before the lookup, so a foreign id and a missing id are indistinguishable. The test is exactly the pair above: if the two responses differ, the record leaked.

Where people get this wrong

  • DEBUG=True in production turns any 500 into a settings dump with your database password.
  • DRF's browsable API renders forms for every field it exposes, which makes a serializer misconfiguration obvious to anyone who opens the URL.
  • Object-level permissions are a separate class from view permissions. A view can be correctly authenticated and still return another tenant's row.

What Offense does here

Offense runs an agent swarm against your app in a disposable sandbox, keeps the request, the identity and the vulnerable response as evidence, and replays the same attack after the fix so the retest is a control comparison rather than a green check. It does not read your source and it does not patch your code: the finding goes to your coding agent with the reproduction attached. The hosted workspace is invite-only — join the waitlist — and the free dependency audit in /install needs no account.

Questions this page answers

Are Django views authenticated by default?

Plain Django views are not; you add @login_required. DRF views use DEFAULT_PERMISSION_CLASSES, and a view that sets its own permission_classes overrides it — sometimes to AllowAny.

What is the fastest way to find an IDOR in Django?

Two accounts, one object id. Request the same URL with both tokens; if the second returns the first account's data, it is an IDOR.

Written by Offense Research, reviewed December 2, 2026. Correct something? Email imran@offenseresearch.com.