Skip to content

Commit 2be05e6

Browse files
authored
Merge pull request #69 from maykinmedia/feature/68-CSP-for-drf-spectacular
🐛[#68] add CSP headers for DRF spectacular schema
2 parents e39541b + 45bef6b commit 2be05e6

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

open_api_framework/conf/base.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
"vng_api_common",
211211
"notifications_api_common",
212212
"drf_spectacular",
213+
"drf_spectacular_sidecar",
213214
"rest_framework",
214215
"django_markup",
215216
"solo",
@@ -953,9 +954,7 @@ def init_sentry(before_send: Callable | None = None):
953954
# NOTE: make sure values are a tuple or list, and to quote special values like 'self'
954955

955956
# ideally we'd use BASE_URI but it'd have to be lazy or cause issues
956-
CSP_DEFAULT_SRC = [
957-
"'self'",
958-
] + config(
957+
CSP_DEFAULT_SRC = ["'self'"] + config(
959958
"CSP_EXTRA_DEFAULT_SRC",
960959
default=[],
961960
split=True,
@@ -996,12 +995,16 @@ def init_sentry(before_send: Callable | None = None):
996995
+ CORS_ALLOWED_ORIGINS
997996
)
998997

999-
CSP_IMG_SRC = CSP_DEFAULT_SRC + config(
1000-
"CSP_EXTRA_IMG_SRC",
1001-
default=[],
1002-
split=True,
1003-
group="Content Security Policy",
1004-
help_text="Extra ``img-src`` sources for CSP other than ``CSP_DEFAULT_SRC``.",
998+
CSP_IMG_SRC = (
999+
CSP_DEFAULT_SRC
1000+
+ ["data:", "cdn.redoc.ly"] # used by DRF spectacular
1001+
+ 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``.",
1007+
)
10051008
)
10061009

10071010
# affects <object> and <embed> tags, block everything by default but allow deploy-time
@@ -1016,8 +1019,13 @@ def init_sentry(before_send: Callable | None = None):
10161019

10171020
# we must include this explicitly, otherwise the style-src only includes the nonce because
10181021
# of CSP_INCLUDE_NONCE_IN
1019-
CSP_STYLE_SRC = CSP_DEFAULT_SRC
1020-
CSP_SCRIPT_SRC = CSP_DEFAULT_SRC
1022+
CSP_STYLE_SRC = CSP_DEFAULT_SRC + [
1023+
"'unsafe-inline'",
1024+
"fonts.googleapis.com",
1025+
] # used by DRF spectacular
1026+
CSP_SCRIPT_SRC = CSP_DEFAULT_SRC + ["'unsafe-inline'"]
1027+
CSP_FONT_SRC = ("'self'", "fonts.gstatic.com")
1028+
CSP_WORKER_SRC = ("'self'", "blob:")
10211029

10221030
# firefox does not get the nonce from default-src, see
10231031
# https://stackoverflow.com/a/63376012
@@ -1033,9 +1041,3 @@ def init_sentry(before_send: Callable | None = None):
10331041
# CSP_SANDBOX # too much
10341042

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

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)