FastAPI CSP Docs¶
FastAPI's built-in /docs, /redoc, and OAuth2 redirect pages embed inline <script>/<style> tags, so they break under a Content-Security-Policy without 'unsafe-inline'. Following FastAPI's own "Custom Docs UI Static Assets" recipe only swaps the CDN URLs for local ones; the inline content stays. fastapi-csp-docs replaces all three pages with versions that load every script and stylesheet from a separate endpoint, with no inline content anywhere.
Features¶
- Drop-in
setup(): one call replaces the/docs,/redoc, and OAuth2 redirect wiring. See the Quick start. - Fails at startup on misconfiguration:
setup()raisesRuntimeErrorif the app's built-in docs are still enabled. - CDN or self-hosted: the default asset URLs point at jsdelivr, and your own static files replace them with
script-src 'self'. - Reverse proxy and sub-app support: URLs include the ASGI
root_pathof each request, so a mount or--root-pathneeds no configuration. - Independent paths:
docs_urlandredoc_urlaresetup()parameters, andNoneskips either page. - Public content generators: the functions
setup()uses are exported for routes you write yourself.
Requirements¶
- Python >= 3.10
- FastAPI >= 0.120.0
Installation¶
Continue with the Quick start.