Skip to content

Commit 157bc99

Browse files
committed
🐛[#68] add CSP headers for DRF spectacular schema
1 parent 9ef90bb commit 157bc99

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

open_api_framework/conf/base.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
"vng_api_common",
214214
"notifications_api_common",
215215
"drf_spectacular",
216+
"drf_spectacular_sidecar",
216217
"rest_framework",
217218
"django_markup",
218219
"solo",
@@ -955,9 +956,7 @@ def init_sentry(before_send: Callable | None = None):
955956
# NOTE: make sure values are a tuple or list, and to quote special values like 'self'
956957

957958
# ideally we'd use BASE_URI but it'd have to be lazy or cause issues
958-
CSP_DEFAULT_SRC = [
959-
"'self'",
960-
] + config(
959+
CSP_DEFAULT_SRC = ["'self'", "'unsafe-inline'"] + config(
961960
"CSP_EXTRA_DEFAULT_SRC",
962961
default=[],
963962
split=True,
@@ -998,12 +997,16 @@ def init_sentry(before_send: Callable | None = None):
998997
+ CORS_ALLOWED_ORIGINS
999998
)
1000999

1001-
CSP_IMG_SRC = CSP_DEFAULT_SRC + config(
1002-
"CSP_EXTRA_IMG_SRC",
1003-
default=[],
1004-
split=True,
1005-
group="Content Security Policy",
1006-
help_text="Extra ``img-src`` sources for CSP other than ``CSP_DEFAULT_SRC``.",
1000+
CSP_IMG_SRC = (
1001+
CSP_DEFAULT_SRC
1002+
+ ["data:", "cdn.redoc.ly", "cdn.jsdelivr.net"] # used by DRF spectacular
1003+
+ config(
1004+
"CSP_EXTRA_IMG_SRC",
1005+
default=[],
1006+
split=True,
1007+
group="Content Security Policy",
1008+
help_text="Extra ``img-src`` sources for CSP other than ``CSP_DEFAULT_SRC``.",
1009+
)
10071010
)
10081011

10091012
# affects <object> and <embed> tags, block everything by default but allow deploy-time
@@ -1018,8 +1021,10 @@ def init_sentry(before_send: Callable | None = None):
10181021

10191022
# we must include this explicitly, otherwise the style-src only includes the nonce because
10201023
# of CSP_INCLUDE_NONCE_IN
1021-
CSP_STYLE_SRC = CSP_DEFAULT_SRC
1024+
CSP_STYLE_SRC = CSP_DEFAULT_SRC + ["fonts.googleapis.com"] # used by DRF spectacular
10221025
CSP_SCRIPT_SRC = CSP_DEFAULT_SRC
1026+
CSP_FONT_SRC = ("'self'", "fonts.gstatic.com")
1027+
CSP_WORKER_SRC = ("'self'", "blob:")
10231028

10241029
# firefox does not get the nonce from default-src, see
10251030
# https://stackoverflow.com/a/63376012
@@ -1035,9 +1040,3 @@ def init_sentry(before_send: Callable | None = None):
10351040
# CSP_SANDBOX # too much
10361041

10371042
CSP_UPGRADE_INSECURE_REQUESTS = False # TODO enable on production?
1038-
1039-
CSP_EXCLUDE_URL_PREFIXES = (
1040-
# ReDoc/Swagger pull in external sources, so don't enforce CSP on API endpoints/documentation.
1041-
"/api/",
1042-
"/admin/",
1043-
)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ dependencies = [
3838
"djangorestframework>=3.15.2",
3939
"djangorestframework-gis>=1.0",
4040
"django-filter>=24.2",
41-
"drf-spectacular>=0.27.2",
41+
"drf-spectacular[sidecar]>=0.27.2",
4242
"django-csp>=3.8",
4343
"djangorestframework-inclusions>=1.2.0",
4444
"commonground-api-common>=1.12.1",

0 commit comments

Comments
 (0)