
Security Fastapi
- 111 installs
- 125 repo stars
- Updated February 4, 2026
- igorwarzocha/opencode-workflows
Audit FastAPI apps for missing route auth dependencies, weak CORS, TrustedHost/HTTPS middleware, and API keys in query params.
About
A security-audit skill for FastAPI dependencies and middleware. A developer uses it to verify Depends/Security on routes, CORS with credentials, and TrustedHost/HTTPSRedirect config.
- Flags routes lacking Depends()/Security() authentication
- Covers CORS origins, TrustedHost middleware, and moving API keys to headers
Security Fastapi by the numbers
- 111 all-time installs (skills.sh)
- +5 installs in the week ending Aug 2, 2026 (Skillselion tracking)
- Ranked #978 of 2,203 Security skills by installs in the Skillselion catalog
- Data as of Aug 2, 2026 (Skillselion catalog sync)
npx skills add https://github.com/igorwarzocha/opencode-workflows --skill security-fastapiAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 111 |
|---|---|
| repo stars | ★ 125 |
| Last updated | February 4, 2026 |
| Repository | igorwarzocha/opencode-workflows ↗ |
What it does
Audit FastAPI apps for missing route auth dependencies, weak CORS, TrustedHost/HTTPS middleware, and API keys in query params.
Files
<overview>
Security audit patterns for FastAPI applications covering authentication dependencies, CORS configuration, and middleware security.
</overview>
<vulnerabilities>
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)</vulnerabilities>
<commands>
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"</commands>
<checklist>
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)
</checklist>
<scripts>
Scripts
scripts/scan.sh- First-pass FastAPI security scan
</scripts>
#!/usr/bin/env bash
# FastAPI Security Scanner - First-pass automated detection
# Usage: ./scan.sh [directory]
set -euo pipefail
DIR="${1:-.}"
FOUND=0
echo "=== FASTAPI SECURITY SCAN ==="
echo "Directory: $DIR"
echo "Timestamp: $(date -Iseconds)"
echo ""
if ! command -v rg &> /dev/null; then
echo "[ERROR] ripgrep (rg) required"
exit 1
fi
# Detect FastAPI
if ! rg -q "fastapi" "$DIR/pyproject.toml" "$DIR/requirements.txt" "$DIR/requirements-dev.txt" "$DIR/requirements*.txt" 2>/dev/null; then
if ! rg -q "from fastapi|import fastapi" "$DIR" -g "*.py" 2>/dev/null; then
echo "[INFO] FastAPI not detected. Skipping."
exit 0
fi
fi
report() {
local severity="$1"
local title="$2"
local details="${3:-}"
echo "[$severity] $title"
[[ -n "$details" ]] && echo " $details"
echo ""
FOUND=$((FOUND + 1))
}
echo "=== HIGH: Missing Auth Dependencies ==="
echo ""
ROUTE_COUNT=$(rg -c "@app\.(get|post|put|patch|delete)" "$DIR" -g "*.py" 2>/dev/null | awk -F: '{sum+=$2} END {print sum+0}')
HAS_AUTH=$(rg -q "Depends\(|Security\(" "$DIR" -g "*.py" 2>/dev/null && echo "yes" || echo "no")
if [[ "$ROUTE_COUNT" -gt 0 && "$HAS_AUTH" == "no" ]]; then
report "HIGH" "Routes detected but no Depends()/Security() usage found" "Review route auth dependencies"
fi
echo "=== HIGH: CORS Wildcard with Credentials ==="
echo ""
# Warn on allow_origins=["*"] with allow_credentials=True
if rg -q "allow_origins\s*=\s*\[\s*['\"]\*['\"]\s*\]" "$DIR" -g "*.py" 2>/dev/null; then
if rg -q "allow_credentials\s*=\s*True" "$DIR" -g "*.py" 2>/dev/null; then
report "HIGH" "CORS wildcard with credentials" "Set explicit allow_origins when allow_credentials=True"
fi
fi
echo "=== MEDIUM: TrustedHostMiddleware Missing ==="
echo ""
if ! rg -q "TrustedHostMiddleware" "$DIR" -g "*.py" 2>/dev/null; then
report "MEDIUM" "TrustedHostMiddleware not found" "Consider restricting allowed hosts in production"
fi
echo "=== LOW: HTTPSRedirectMiddleware Missing ==="
echo ""
if ! rg -q "HTTPSRedirectMiddleware" "$DIR" -g "*.py" 2>/dev/null; then
report "LOW" "HTTPSRedirectMiddleware not found" "Ensure HTTPS enforced by proxy or middleware"
fi
echo "=== SUMMARY ==="
if [[ $FOUND -gt 0 ]]; then
echo "[!] Found $FOUND potential issues. Review above."
exit 1
else
echo "[✓] No obvious FastAPI security issues detected"
exit 0
fi