Repository navigation
Description
Symptom
The /api/docs (Swagger UI) and /api/redoc endpoints render as blank pages when accessed behind the bundled nginx reverse proxy, because the default Content-Security-Policy blocks loading external scripts and styles from cdn.jsdelivr.net.
This breaks API exploration for any deployment behind a strict CSP or in an air-gapped environment.
Technical Analysis
FastAPI's built-in get_swagger_ui_html() and get_redoc_html() reference CDN-hosted assets by default:
https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.jshttps://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.csshttps://cdn.jsdelivr.net/npm/redoc@latest/bundles/redoc.standalone.js
The shipped nginx CSP (nginx/nginx.conf:28) restricts script-src and style-src to 'self':
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; ..."This is the correct hardened default. The fix is to remove the CDN dependency, not to weaken the CSP.
Related: src/rackscope/api/app.py:328-335 (FastAPI app configuration).
Proposed Fix
1. src/Dockerfile — fetch assets at build time
ARG SWAGGER_UI_VERSION=5.17.14
ARG REDOC_VERSION=2.1.5
RUN mkdir -p /app/static/openapi-ui && \
curl -fsSL -o /app/static/openapi-ui/swagger-ui-bundle.js \
https://cdn.jsdelivr.net/npm/swagger-ui-dist@${SWAGGER_UI_VERSION}/swagger-ui-bundle.js && \
curl -fsSL -o /app/static/openapi-ui/swagger-ui.css \
https://cdn.jsdelivr.net/npm/swagger-ui-dist@${SWAGGER_UI_VERSION}/swagger-ui.css && \
curl -fsSL -o /app/static/openapi-ui/redoc.standalone.js \
https://cdn.jsdelivr.net/npm/redoc@${REDOC_VERSION}/bundles/redoc.standalone.js2. src/rackscope/api/app.py — serve assets locally, override doc routes
from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html
from fastapi.staticfiles import StaticFiles
app = FastAPI(
title="rackscope",
version=APP_VERSION,
lifespan=lifespan,
docs_url=None, # was /api/docs
redoc_url=None, # was /api/redoc
openapi_url="/api/openapi.json",
)
app.mount(
"/api/static/openapi-ui",
StaticFiles(directory="/app/static/openapi-ui"),
name="openapi-ui",
)
@app.get("/api/docs", include_in_schema=False)
async def swagger_ui_html():
return get_swagger_ui_html(
openapi_url="/api/openapi.json",
title="rackscope — Swagger UI",
swagger_js_url="/api/static/openapi-ui/swagger-ui-bundle.js",
swagger_css_url="/api/static/openapi-ui/swagger-ui.css",
)
@app.get("/api/redoc", include_in_schema=False)
async def redoc_html():
return get_redoc_html(
openapi_url="/api/openapi.json",
title="rackscope — ReDoc",
redoc_js_url="/api/static/openapi-ui/redoc.standalone.js",
)3. Image size impact
Approx. ~3 MB added to the backend image (uncompressed):
- swagger-ui-bundle.js ≈ 1.4 MB
- swagger-ui.css ≈ 150 KB
- redoc.standalone.js ≈ 1.2 MB
Acceptable trade-off for offline self-sufficiency.
Test Checklist
-
make buildsucceeds and the new assets are present in the image at/app/static/openapi-ui/ -
https://localhost/api/docsrenders a fully functional Swagger UI with the strict nginx CSP -
https://localhost/api/redocrenders a fully functional ReDoc with the strict nginx CSP - Browser network tab shows zero requests to
cdn.jsdelivr.netor any external domain - Calling an endpoint from Swagger UI's "Try it out" works
-
/api/openapi.jsonis still served correctly (unchanged contract) - Image build still succeeds without network access to jsdelivr (verifiable by rebuilding offline once cached)
Impact and Severity
- Audience affected: every user running Rackscope behind a strict CSP, behind a corporate proxy, in an air-gapped environment, or on a hardened reverse proxy. This is the majority of HPC / on-prem deployments.
- Severity: medium — degrades developer experience and operability, but does not affect the runtime API or the frontend.
- Workaround attempted (do not ship): temporarily whitelisting
cdn.jsdelivr.netinnginx/nginx.confCSP. Used during a live demo and reverted afterwards.
Breaking Changes
None. Routes /api/docs, /api/redoc, and /api/openapi.json keep the same URL and contract.
Related
- Follow-up: full audit of external runtime dependencies (separate issue).
Activity