Skip to content

Commit 3ebb570

Browse files
committed
Nginx: add support for a CSP defaults config file
1 parent cce9aa3 commit 3ebb570

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ You can control the CSP behaviour with the `NGINX_CSP_MODE` key:
1818
- `enforce` (default): Configure the `Content-Security-Policy` header.
1919
- `report-only`: Instead configure the `Content-Security-Policy-Report-Only` header.
2020

21+
Note: the following fetch & navigation CSP keys can also be set via an embedded file located at `/etc/csp-generator/default`.
22+
2123
Fetch:
2224

2325
| Environment Key | Applied | Description | Default |

common/scripts/startup/env-configure-security.sh

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22

33
set -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
3140
fi
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
3454
if [ "${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…"
4363
cat <<EOF >> /etc/nginx/site-mods-enabled.d/00-generated-security.conf
4464
add_header '${NGINX_CSP_HEADER_NAME}' "\
4565
default-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:-}; \
6181
report-uri ${NGINX_CSP_REPORT_URI}; \
6282
";
6383
EOF

0 commit comments

Comments
 (0)