AI pentesting guides / By tech stack

Stack guide: FastAPI

FastAPI security testing

FastAPI security testing focuses on dependency-injected authentication and on what the Pydantic models accept and return: FastAPI makes it easy to declare a security dependency, and just as easy to forget it on one router or to return the database model instead of a trimmed response model.

Every FastAPI app also publishes an OpenAPI document at /openapi.json by default, with interactive docs at /docs and /redoc. For a tester that is a complete map of the API on the first request.

Authentication through dependencies

  • Auth is usually a Depends(get_current_user) parameter. A route or router without it is public, and nothing warns you.
  • Dependencies that decode a JWT without verifying the signature or expiry, or that accept the none algorithm through a misconfigured library call.
  • OAuth2PasswordBearer only extracts the token; it does not validate it. Validation is your code.
  • Ownership checks are often missing: get_current_user proves who you are, not that item_id belongs to you.

Pydantic models in both directions

If an endpoint returns an ORM object without a response_model, or with a response model that includes hashed_password, internal flags or other users' data, the API overshares. On input, reusing one model for create and update lets clients set fields such as is_superuser or owner_id. Separate input schemas per operation and explicit response models avoid both.

Async-specific and deployment issues

  • Raw SQL through text() or driver cursors with f-strings is injectable just as in any Python app.
  • Blocking calls inside async endpoints can make the service easy to stall; rate limiting is not built in.
  • Multipart parsing has had denial-of-service advisories in python-multipart; keep it current.
  • CORSMiddleware with allow_origins=['*'] and allow_credentials=True should be treated as a finding.
With Pentest-Swarm

Testing FastAPI with Pentest-Swarm

FastAPI's self-published schema makes black-box testing efficient: Pentest-Swarm's recon probes /openapi.json and turns every documented path and method into an endpoint to test.

  • Run the api-security playbook against your staging base URL to test BOLA, BFLA and mass assignment across the documented routes.
  • Provide a test user's bearer token with --auth; run again with a second user to look for cross-user access.
  • Use --verify so injection findings are confirmed by differential or timing oracles.
  • Wire the ci-cd-security playbook into your pipeline with SARIF output so new routes get tested as they ship.

Where it falls short

The OpenAPI document shows what is declared, not what is enforced, and undocumented routes (include_in_schema=False) will only be found if recon reaches them another way. A black-box test also cannot see which dependency is missing in code; static checks or a code review of router definitions fill that gap.

Questions

Should I disable /docs in production FastAPI?

For private APIs, set docs_url and openapi_url to None or protect them. Testers should still be given the schema so they can test thoroughly.

Does Pydantic validation prevent injection?

Pydantic validates types and shapes. A valid string can still carry SQL or template injection if your code builds queries or templates from it.

Can Pentest-Swarm use my FastAPI OpenAPI spec?

Yes. Recon auto-discovers a spec served at /openapi.json or similar common paths and maps every documented endpoint into the test.