Skip to content

Commit

Permalink
🐛[#68] add CSP headers for DRF spectacular schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Coperh committed Sep 13, 2024
1 parent 9ef90bb commit 74d7e26
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
24 changes: 13 additions & 11 deletions open_api_framework/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@
"vng_api_common",
"notifications_api_common",
"drf_spectacular",
"drf_spectacular_sidecar",
"rest_framework",
"django_markup",
"solo",
Expand Down Expand Up @@ -955,9 +956,7 @@ def init_sentry(before_send: Callable | None = None):
# NOTE: make sure values are a tuple or list, and to quote special values like 'self'

# ideally we'd use BASE_URI but it'd have to be lazy or cause issues
CSP_DEFAULT_SRC = [
"'self'",
] + config(
CSP_DEFAULT_SRC = ["'self'", "'unsafe-inline'"] + config(
"CSP_EXTRA_DEFAULT_SRC",
default=[],
split=True,
Expand Down Expand Up @@ -998,12 +997,16 @@ def init_sentry(before_send: Callable | None = None):
+ CORS_ALLOWED_ORIGINS
)

CSP_IMG_SRC = CSP_DEFAULT_SRC + config(
"CSP_EXTRA_IMG_SRC",
default=[],
split=True,
group="Content Security Policy",
help_text="Extra ``img-src`` sources for CSP other than ``CSP_DEFAULT_SRC``.",
CSP_IMG_SRC = (
CSP_DEFAULT_SRC
+ ["data:", "cdn.jsdelivr.net"]
+ config(
"CSP_EXTRA_IMG_SRC",
default=[],
split=True,
group="Content Security Policy",
help_text="Extra ``img-src`` sources for CSP other than ``CSP_DEFAULT_SRC``.",
)
)

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

# we must include this explicitly, otherwise the style-src only includes the nonce because
# of CSP_INCLUDE_NONCE_IN
CSP_STYLE_SRC = CSP_DEFAULT_SRC
CSP_STYLE_SRC = CSP_DEFAULT_SRC + ["fonts.googleapis.com"]
CSP_SCRIPT_SRC = CSP_DEFAULT_SRC

# firefox does not get the nonce from default-src, see
Expand All @@ -1039,5 +1042,4 @@ def init_sentry(before_send: Callable | None = None):
CSP_EXCLUDE_URL_PREFIXES = (
# ReDoc/Swagger pull in external sources, so don't enforce CSP on API endpoints/documentation.
"/api/",
"/admin/",
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies = [
"djangorestframework>=3.15.2",
"djangorestframework-gis>=1.0",
"django-filter>=24.2",
"drf-spectacular>=0.27.2",
"drf-spectacular[sidecar]>=0.27.2",
"django-csp>=3.8",
"djangorestframework-inclusions>=1.2.0",
"commonground-api-common>=1.12.1",
Expand Down

0 comments on commit 74d7e26

Please sign in to comment.