Skip to content

Commit 96fb6e1

Browse files
committed
Nginx: provide mechanism to switch CSP mode
1 parent 2ba2d4c commit 96fb6e1

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ Images with `nginx` can be configured using the following environment variables.
1414

1515
### Content Security Policy
1616

17+
You can control the CSP behaviour with the `NGINX_CSP_MODE` key:
18+
- `enforce` (default): Configure the `Content-Security-Policy` header.
19+
- `report-only`: Instead configure the `Content-Security-Policy-Report-Only` header.
20+
1721
Fetch:
1822

1923
| Environment Key | Applied | Description | Default |

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set -euo pipefail
1414
# - NGINX_CSP_IMG_SRC: defaults to 'https:'
1515
# - NGINX_CSP_MANIFEST_SRC: defaults to ''
1616
# - NGINX_CSP_MEDIA_SRC: defaults to 'https:'
17+
# - NGINX_CSP_MODE: defaults to 'enforce'
1718
# - NGINX_CSP_OBJECT_SRC: defaults to ''
1819
# - NGINX_CSP_REPORT_URI: defaults to ''
1920
# - NGINX_CSP_SCRIPT_SRC: defaults to ''
@@ -31,13 +32,24 @@ NGINX_CSP_FRAME_SRC="${NGINX_CSP_FRAME_SRC:-https://youtube.com https://www.yout
3132
NGINX_CSP_IMG_SRC="${NGINX_CSP_IMG_SRC:-https:}"
3233
NGINX_CSP_MANIFEST_SRC="${NGINX_CSP_MANIFEST_SRC:-}"
3334
NGINX_CSP_MEDIA_SRC="${NGINX_CSP_MEDIA_SRC:-https:}"
35+
NGINX_CSP_MODE="${NGINX_CSP_MODE:-enforce}"
3436
NGINX_CSP_OBJECT_SRC="${NGINX_CSP_OBJECT_SRC:-}"
3537
NGINX_CSP_REPORT_URI="${NGINX_CSP_REPORT_URI:-}"
3638
NGINX_CSP_SCRIPT_SRC="${NGINX_CSP_SCRIPT_SRC:-}"
3739
NGINX_CSP_STYLE_SRC="${NGINX_CSP_STYLE_SRC:-https://fonts.googleapis.com}"
3840
NGINX_CSP_WORKER_SRC="${NGINX_CSP_WORKER_SRC:-}"
3941
NGINX_FRAME_OPTIONS="${NGINX_FRAME_OPTIONS:-deny}"
4042

43+
# Validate input
44+
if [ "${NGINX_CSP_MODE}" = 'enforce' ]; then
45+
NGINX_CSP_HEADER_NAME='Content-Security-Policy'
46+
elif [ "${NGINX_CSP_MODE}" = 'report-only' ]; then
47+
NGINX_CSP_HEADER_NAME='Content-Security-Policy-Report-Only'
48+
else
49+
echo "Nginx: invalid CSP mode ${NGINX_CSP_MODE}"
50+
exit 1
51+
fi
52+
4153
if [ -d /etc/nginx/site-mods-enabled.d/ ]; then
4254
# nginx frame options header
4355
if [ "${NGINX_FRAME_OPTIONS}" != 'disable' ]; then
@@ -50,7 +62,7 @@ EOF
5062
# nginx content policy
5163
echo "Nginx: configuring content security policy…"
5264
cat <<EOF >> /etc/nginx/site-mods-enabled.d/00-generated-security.conf
53-
add_header 'Content-Security-Policy' "\
65+
add_header '${NGINX_CSP_HEADER_NAME}' "\
5466
default-src 'self'; \
5567
child-src 'self' data: blob: ${NGINX_CSP_CHILD_SRC}; \
5668
connect-src 'self' data: blob: ${NGINX_CSP_CONNECT_SRC}; \

0 commit comments

Comments
 (0)