Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Serve Swagger UI (/api/v1/docs) from vendored assets instead of unpkg CDN so it renders under the default CSP without relaxation

Murtaza Nasir0db80db1fa20
diff --git a/scripts/download_offline_deps.py b/scripts/download_offline_deps.py
index 44b2aecc..f7fa8e8e 100644
--- a/scripts/download_offline_deps.py
+++ b/scripts/download_offline_deps.py
@@ -15,6 +15,7 @@ DEPENDENCIES = {
"css": {
"fontawesome.min.css": "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css",
"easymde.min.css": "https://cdn.jsdelivr.net/npm/easymde/dist/easymde.min.css",
+ "swagger-ui.css": "https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui.css",
},
"js": {
"tailwind.min.js": "https://cdn.tailwindcss.com/3.4.0",
@@ -24,6 +25,7 @@ DEPENDENCIES = {
"axios.min.js": "https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js",
"chart.umd.min.js": "https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js",
"purify.min.js": "https://cdn.jsdelivr.net/npm/[email protected]/dist/purify.min.js",
+ "swagger-ui-bundle.js": "https://cdn.jsdelivr.net/npm/[email protected]/swagger-ui-bundle.js",
}
}
diff --git a/src/api/api_v1.py b/src/api/api_v1.py
index 17227360..eae76cff 100644
--- a/src/api/api_v1.py
+++ b/src/api/api_v1.py
@@ -449,25 +449,32 @@ def get_openapi_spec():
@api_v1_bp.route('/docs', methods=['GET'])
def get_docs():
- """Serve Swagger UI documentation."""
- from flask import Response
- html = '''<!DOCTYPE html>
+ """Serve Swagger UI documentation.
+
+ Assets are served from Speakr's own vendored copy (static/vendor) rather than
+ a public CDN so the page works under the default Content-Security-Policy
+ (script-src/style-src 'self') without any per-route CSP relaxation.
+ """
+ from flask import Response, url_for
+ css_url = url_for('static', filename='vendor/css/swagger-ui.css')
+ bundle_url = url_for('static', filename='vendor/js/swagger-ui-bundle.js')
+ html = f'''<!DOCTYPE html>
<html>
<head>
<title>Speakr API v1 Documentation</title>
- <link rel="stylesheet" href="https://unpkg.com/swagger-ui-dist@5/swagger-ui.css" />
+ <link rel="stylesheet" href="{css_url}" />
</head>
<body>
<div id="swagger-ui"></div>
- <script src="https://unpkg.com/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
+ <script src="{bundle_url}"></script>
<script>
- SwaggerUIBundle({
+ SwaggerUIBundle({{
url: "/api/v1/openapi.json",
dom_id: '#swagger-ui',
presets: [SwaggerUIBundle.presets.apis, SwaggerUIBundle.SwaggerUIStandalonePreset],
layout: "BaseLayout",
persistAuthorization: true
- });
+ }});
</script>
</body>
</html>'''