About security-fastapi
security-fastapi is a security-audit skill for FastAPI applications, covering authentication dependencies, CORS, and middleware. It helps developers review API route access controls and production security settings.
Key features
- 01Check authentication dependencies on routes
- 02Review API key schemes and CORS settings
- 03Check host-header protection and HTTPS redirects
- 04Scan route and authentication usage with commands
Use cases
- 01Audit access control on FastAPI routes
- 02Review CORS origins for credentialed requests
- 03Check host and HTTPS protections in production
Install
Copy the command and run it in a terminal.
npx skills add https:/ / github.com/ igorwarzocha/ opencode-workflows --skill security-fastapiAfter installing, type/security-fastapiin Claude Code to confirm it loaded.
Bundled filesscripts/scan.sh
Details
From the project READMESecurity audit patterns for FastAPI applications covering authentication dependencies, CORS configuration, and middleware security.
Core Risks to Check
Missing Auth on Routes
FastAPI expects authentication/authorization via dependencies on routes or routers. If no Depends()/Security() usage exists, review every route for unintended public access.
from fastapi import Depends, Security
@app.get("/private")
async def private_route(user=Depends(get_current_user)):
return {"ok": True}
@app.get("/scoped")
async def scoped_route(user=Security(get_current_user, scopes=["items"])):
return {"ok": True}
API Key Schemes
If using API keys, SHOULD prefer header-based schemes (APIKeyHeader) and validate the key server-side.
from fastapi import Depends, FastAPI
from fastapi.security import APIKeyHeader
api_key = APIKeyHeader(name="x-api-key")
@app.get("/items")
async def read_items(key: str = Depends(api_key)):
return {"key": key}
CORS: Avoid Wildcards with Credentials
Using allow_origins=["*"] excludes credentialed requests (cookies/Authorization). For authenticated browser clients, MUST explicitly list allowed origins.
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=["https://app.example.com"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
Host Header and HTTPS Enforcement
SHOULD use Starlette middleware to prevent host-header attacks and enforce HTTPS in production.
from starlette.middleware.trustedhost import TrustedHostMiddleware
from starlette.middleware.httpsredirect import HTTPSRedirectMiddleware
app.add_middleware(TrustedHostMiddleware, allowed_hosts=["example.com", "*.example.com"])
app.add_middleware(HTTPSRedirectMiddleware)
Quick Audit Commands
# Detect FastAPI usage
rg -n "fastapi" pyproject.toml requirements*.txt
# Find routes
rg -n "@app\.(get|post|put|patch|delete)" . -g "*.py"
# Check for auth dependencies
rg -n "Depends\(|Security\(" . -g "*.py"
# CORS config and wildcards
rg -n "CORSMiddleware|allow_origins|allow_credentials" . -g "*.py"
# TrustedHost/HTTPS middleware
rg -n "TrustedHostMiddleware|HTTPSRedirectMiddleware" . -g "*.py"
Hardening Checklist
- All sensitive routes require
Depends()orSecurity()auth dependencies - API key schemes use headers (
APIKeyHeader), not query params -
allow_originsis explicit whenallow_credentials=True -
TrustedHostMiddlewareconfigured for production domains -
HTTPSRedirectMiddlewareenabled in production (or enforced by proxy)
Scripts
scripts/scan.sh- First-pass FastAPI security scan