22
33set -euo pipefail
44
5- # Configure nginx security based on ENV vars.
5+ # Configure nginx security based on ENV vars, and if available the defaults
6+ # located at `/etc/csp-generator/default`.
7+ #
8+ # The defaults file should be a list of variable declarations, such as
9+ # `CHILD_SRC="…"`. Essentially 1 variable for each option that exists. Be
10+ # careful about using quotes though! Keywords such as `none` need to be
11+ # surrounded by single `'` quotes, so the value would be `"'none'"`.
12+ #
13+ # Equivalent settings can be set via ENV, just prefix the variables with
14+ # `NGINX_CSP_…`, like `NGINX_CSP_CHILD_SRC`.
615#
716# Inputs (aside from all the individual CSP settings):
817# - NGINX_CSP_MODE: defaults to 'enforce'
@@ -30,6 +39,17 @@ if [ ! -d /etc/nginx/site-mods-enabled.d/ ]; then
3039 exit 0
3140fi
3241
42+ # Load embedded CSP values from file (if it exists)
43+ EMBEDDED_CSP_PATH=/etc/csp-generator/default
44+ if [ -f " ${EMBEDDED_CSP_PATH} " ]; then
45+ echo " Nginx: found CSP defaults at '$EMBEDDED_CSP_PATH ', processing…"
46+ PROCESSED_CSP_PATH=$( mktemp)
47+ sed ' s/[^=]\+=/EMBEDDED_CSP_&/' " ${EMBEDDED_CSP_PATH} " > " ${PROCESSED_CSP_PATH} "
48+ cat $PROCESSED_CSP_PATH
49+ source " ${PROCESSED_CSP_PATH} "
50+ rm " ${PROCESSED_CSP_PATH} "
51+ fi
52+
3353# nginx frame options header
3454if [ " ${NGINX_FRAME_OPTIONS} " != ' disable' ]; then
3555 echo " Nginx: configuring frame options with '${NGINX_FRAME_OPTIONS} '…"
@@ -43,21 +63,21 @@ echo "Nginx: configuring content security policy…"
4363cat << EOF >> /etc/nginx/site-mods-enabled.d/00-generated-security.conf
4464add_header '${NGINX_CSP_HEADER_NAME} ' "\
4565default-src 'self'; \
46- child-src ${NGINX_CSP_CHILD_SRC:- } ; \
47- connect-src ${NGINX_CSP_CONNECT_SRC:- } ; \
48- font-src ${NGINX_CSP_FONT_SRC:- } ; \
49- form-action ${NGINX_CSP_FORM_ACTION:- } ; \
50- frame-ancestors ${NGINX_CSP_FRAME_ANCESTORS:- } ; \
51- frame-src ${NGINX_CSP_FRAME_SRC:- } ; \
52- img-src ${NGINX_CSP_IMG_SRC:- } ; \
53- manifest-src ${NGINX_CSP_MANIFEST_SRC:- } ; \
54- media-src ${NGINX_CSP_MEDIA_SRC:- } ; \
55- object-src ${NGINX_CSP_OBJECT_SRC:- } ; \
56- require-trusted-types-for ${NGINX_CSP_REQUIRE_TRUSTED_TYPES_FOR:- } ; \
57- script-src ${NGINX_CSP_SCRIPT_SRC:- } ; \
58- style-src ${NGINX_CSP_STYLE_SRC:- } ; \
59- trusted-types ${NGINX_CSP_TRUSTED_TYPES:- } ; \
60- worker-src ${NGINX_CSP_WORKER_SRC:- } ; \
66+ child-src ${EMBEDDED_CSP_CHILD_SRC :- } ${ NGINX_CSP_CHILD_SRC:- } ; \
67+ connect-src ${EMBEDDED_CSP_CONNECT_SRC :- } ${ NGINX_CSP_CONNECT_SRC:- } ; \
68+ font-src ${EMBEDDED_CSP_FONT_SRC :- } ${ NGINX_CSP_FONT_SRC:- } ; \
69+ form-action ${EMBEDDED_CSP_FORM_ACTION :- } ${ NGINX_CSP_FORM_ACTION:- } ; \
70+ frame-ancestors ${EMBEDDED_CSP_FRAME_ANCESTORS :- } ${ NGINX_CSP_FRAME_ANCESTORS:- } ; \
71+ frame-src ${EMBEDDED_CSP_FRAME_SRC :- } ${ NGINX_CSP_FRAME_SRC:- } ; \
72+ img-src ${EMBEDDED_CSP_IMG_SRC :- } ${ NGINX_CSP_IMG_SRC:- } ; \
73+ manifest-src ${EMBEDDED_CSP_MANIFEST_SRC :- } ${ NGINX_CSP_MANIFEST_SRC:- } ; \
74+ media-src ${EMBEDDED_CSP_MEDIA_SRC :- } ${ NGINX_CSP_MEDIA_SRC:- } ; \
75+ object-src ${EMBEDDED_CSP_OBJECT_SRC :- } ${ NGINX_CSP_OBJECT_SRC:- } ; \
76+ require-trusted-types-for ${EMBEDDED_CSP_REQUIRE_TRUSTED_TYPES_FOR :- } ${ NGINX_CSP_REQUIRE_TRUSTED_TYPES_FOR:- } ; \
77+ script-src ${EMBEDDED_CSP_SCRIPT_SRC :- } ${ NGINX_CSP_SCRIPT_SRC:- } ; \
78+ style-src ${EMBEDDED_CSP_STYLE_SRC :- } ${ NGINX_CSP_STYLE_SRC:- } ; \
79+ trusted-types ${EMBEDDED_CSP_TRUSTED_TYPES :- } ${ NGINX_CSP_TRUSTED_TYPES:- } ; \
80+ worker-src ${EMBEDDED_CSP_WORKER_SRC :- } ${ NGINX_CSP_WORKER_SRC:- } ; \
6181report-uri ${NGINX_CSP_REPORT_URI} ; \
6282";
6383EOF
0 commit comments