Skip to main content

FastAPI CSP Docs logo

FastAPI CSP Docs

Build Status codecov pypi package Supported Python versions License: MIT

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.

Documentation: https://fastapi-csp-docs.matblack81.com/


Features

  • Drop-in setup(): one call replaces the /docs, /redoc, and OAuth2 redirect wiring. See the Quick start.
  • Fails at startup on misconfiguration: setup() raises RuntimeError if 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_path of each request, so a mount or --root-path needs no configuration.
  • Independent paths: docs_url and redoc_url are setup() parameters, and None skips 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

pip install fastapi-csp-docs
# or
uv add fastapi-csp-docs

Quick start

Create the app with its built-in docs disabled, send your Content-Security-Policy, and call setup():

from fastapi import FastAPI, Request
from fastapi.responses import Response

import fastapi_csp_docs

CSP = (
    "default-src 'self'; "
    "script-src 'self' https://cdn.jsdelivr.net; "
    "style-src 'self' https://cdn.jsdelivr.net https://fonts.googleapis.com "
    "'sha256-47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=' "
    "'sha256-QMIg+bpjm3JdElJ388KYke01izlUW0UoNOeKjpMxdgc='; "
    "font-src 'self' https://fonts.gstatic.com; "
    "img-src 'self' data: https://fastapi.tiangolo.com https://cdn.redoc.ly; "
    "connect-src 'self' https://cdn.jsdelivr.net; "
    "worker-src 'self' blob:"
)

app = FastAPI(docs_url=None, redoc_url=None)


@app.middleware("http")
async def add_csp_header(request: Request, call_next) -> Response:
    response = await call_next(request)
    response.headers["Content-Security-Policy"] = CSP
    return response


fastapi_csp_docs.setup(app)


@app.get("/items")
def list_items():
    return {"items": []}

The directives of this policy, self-hosted assets and the other options are in the documentation.


Release Notes

Release notes


License

MIT

Metadata

Release files for fastapi-csp-docs 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-csp-docs 1.1.0
File Size Uploaded Provenance Actions
fastapi_csp_docs-1.1.0.tar.gz 1.7 MB Without provenance Details for fastapi_csp_docs-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-csp-docs 1.1.0
File Interpreter ABI Platform Actions
fastapi_csp_docs-1.1.0-py3-none-any.whl Python 3 none any Details for fastapi_csp_docs-1.1.0-py3-none-any.whl

Total release size: 1.7 MB

Release files / fastapi_csp_docs-1.1.0.tar.gz

Download URL fastapi_csp_docs-1.1.0.tar.gz
Size 1.7 MB
Tags Source
SHA-256 checksum
How to use checksums
75e4e3642751d802f76269a79149859d1d5ede744a7b9ec4e490e69fde4241d7
BLAKE2b-256 checksum
How to use checksums
629f877203074e38a40c273e42b5346dd3d663df2483fe8a091247f4e434cdff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / fastapi_csp_docs-1.1.0-py3-none-any.whl

Download URL fastapi_csp_docs-1.1.0-py3-none-any.whl
Size 10.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69901570d3a3688d84d52d91e6564c64ca3e987bd7892e9cd90e0b9dd2ffe0fc
BLAKE2b-256 checksum
How to use checksums
21dcb8fc09486c3f08d63c8e5b27cae5f3d9bfa059136b1ae4ccc60755b13334
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for fastapi-csp-docs 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page