Skip to content

Rework CSP config#13

Merged
djbe merged 6 commits intomainfrom
feature/rework-csp-config
Oct 24, 2025
Merged

Rework CSP config#13
djbe merged 6 commits intomainfrom
feature/rework-csp-config

Conversation

@djbe
Copy link
Contributor

@djbe djbe commented Mar 1, 2025

Rework some systems to allow for ENV based configuration:

  • Frame options
  • Content security policy

This lets us control such things using ENV injection via the Infra repo (so Sysops will control this). I've also added support for configuring part of the CSP via a settings file embedded in the Docker image.

⚠️ Yes this IS a breaking change in the sense that the -unsecured images will disappear, or at least no longer be updated. We can pre-emptively add the vars we want as mentioned below, and change the repositories that use unsecured (there are a bunch of them)

Config via ENV

See the Readme for the list of supported ENV keys. Those of note are:

  • NGINX_CSP_MODE: Defaults to report-only, set it to enforce to enable enforcement.
  • NGINX_CSP_REPORT_URI: send reports to (e.g.) Sentry.
  • NGINX_FRAME_OPTIONS: Defaults to deny, controls the old X-Frame-Options header. Set to disable to remove it completely.

Config via defaults

The same keys (well, most of) can be set via a defaults file located at /etc/csp-generator/default. The contents of it should look like (note the quotes where needed):

SCRIPT_SRC="'self' 'unsafe-inline' https:://mysite.com"
STYLE_SRC="'self' https://css.mysite.com"

Such a file could be placed there with the following in a Dockerfile (TBD, not final filenames):

COPY --from=build /app/.csp-config /etc/csp-generator/default

Default configuration

The provided defaults are VERY limited. The generated headers will be (equivalent to):

X-Frame-Options: deny
Content-Security-Policy: default-src 'self';

This CSP would barely allow anything, only connecting to itself, loading from itself, etc…. Existing applications WILL break with this setup.

Quick unsecure setup

Values for easy "allow everything" (i.e. any HTTPS url, choose what you actually need):

NGINX_FRAME_OPTIONS=disable
NGINX_CSP_MODE=report-only

# optional
NGINX_CSP_CHILD_SRC="'self' data: blob: https:"
NGINX_CSP_CONNECT_SRC="'self' data: blob: https:"
NGINX_CSP_FONT_SRC="'self' https:"
NGINX_CSP_FRAME_ANCESTORS="'self' https:"
NGINX_CSP_FRAME_SRC="'self' https:"
NGINX_CSP_IMG_SRC="'self' data: blob: https:"
NGINX_CSP_MANIFEST_SRC="'self' data: blob: https:"
NGINX_CSP_MEDIA_SRC="'self' https:"
NGINX_CSP_OBJECT_SRC="'self' https:"
NGINX_CSP_SCRIPT_SRC="'self' 'unsafe-inline' 'unsafe-eval' data: blob: https:"
NGINX_CSP_STYLE_SRC="'self' 'unsafe-inline' https:"
NGINX_CSP_WORKER_SRC="'self' data: blob: https:"

@djbe djbe force-pushed the feature/rework-csp-config branch from 96fb6e1 to 1ce52b3 Compare March 1, 2025 00:31
@ernest-app ernest-app bot added 📃 S PR and removed 📃 M PR labels Mar 1, 2025
@djbe djbe force-pushed the feature/rework-csp-config branch from 1ce52b3 to 882fd4d Compare March 1, 2025 01:27
@djbe djbe force-pushed the feature/rework-csp-config branch from 882fd4d to 69a16eb Compare March 1, 2025 03:29
@djbe djbe marked this pull request as ready for review March 1, 2025 03:44
@djbe djbe force-pushed the feature/rework-csp-config branch from 69a16eb to 76c96e3 Compare March 3, 2025 01:24
@djbe djbe force-pushed the feature/rework-csp-config branch from 76c96e3 to ad9deb9 Compare March 19, 2025 11:18
@djbe djbe force-pushed the feature/rework-csp-config branch 2 times, most recently from 43d59e8 to 38313a4 Compare March 19, 2025 11:59
@djbe
Copy link
Contributor Author

djbe commented Apr 9, 2025

Initial intention would be to set the CSP:

  • To secure (default) on dev & test
  • Report only on staging & production
  • Try to link it with Sentry using the report-url (see project settings > sdk setup > security headers > CSP, looks like https://sentry.appwi.se/api/347/security/?sentry_key=…)

This way we don't break anything for clients/users, but DO force our developers to deal with CSP issues ASAP.

@djbe djbe force-pushed the feature/rework-csp-config branch from 38313a4 to 0fed1c9 Compare April 14, 2025 00:52
@djbe djbe force-pushed the feature/rework-csp-config branch 2 times, most recently from 642e2f9 to 3ebb570 Compare August 28, 2025 22:24
@djbe djbe force-pushed the feature/rework-csp-config branch from 3ebb570 to f6943f6 Compare September 8, 2025 14:47
@djbe djbe force-pushed the main branch 6 times, most recently from 9c2dfe9 to 4a9999c Compare September 9, 2025 20:33
@djbe djbe force-pushed the feature/rework-csp-config branch 2 times, most recently from dec5833 to f661f4c Compare September 17, 2025 13:19
@djbe djbe force-pushed the feature/rework-csp-config branch from 04dad33 to f218d6b Compare October 24, 2025 13:09
@ernest-app ernest-app bot added 📃 XL PR and removed 📃 L PR labels Oct 24, 2025
@djbe
Copy link
Contributor Author

djbe commented Oct 24, 2025

I've switched the default mode to report-only for now, to ease deployment. We'll switch this to "enforce" by default at a later date, once it's been set in all infra repo's

@djbe djbe merged commit 7f3556d into main Oct 24, 2025
26 checks passed
@djbe djbe deleted the feature/rework-csp-config branch October 24, 2025 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant