Skip to content

Commit cce9aa3

Browse files
committed
Nginx: provide mechanism to switch CSP mode
1 parent 298642d commit cce9aa3

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
@@ -5,13 +5,25 @@ set -euo pipefail
55
# Configure nginx security based on ENV vars.
66
#
77
# Inputs (aside from all the individual CSP settings):
8+
# - NGINX_CSP_MODE: defaults to 'enforce'
89
# - NGINX_CSP_REPORT_URI: defaults to ''
910
# - NGINX_FRAME_OPTIONS: defaults to 'deny', note that setting to `disable` removes the header completely.
1011

1112
# Set defaults
13+
NGINX_CSP_MODE="${NGINX_CSP_MODE:-enforce}"
1214
NGINX_CSP_REPORT_URI="${NGINX_CSP_REPORT_URI:-}"
1315
NGINX_FRAME_OPTIONS="${NGINX_FRAME_OPTIONS:-deny}"
1416

17+
# Validate input
18+
if [ "${NGINX_CSP_MODE}" = 'enforce' ]; then
19+
NGINX_CSP_HEADER_NAME='Content-Security-Policy'
20+
elif [ "${NGINX_CSP_MODE}" = 'report-only' ]; then
21+
NGINX_CSP_HEADER_NAME='Content-Security-Policy-Report-Only'
22+
else
23+
echo "Nginx: invalid CSP mode ${NGINX_CSP_MODE}"
24+
exit 1
25+
fi
26+
1527
# Check nginx structure
1628
if [ ! -d /etc/nginx/site-mods-enabled.d/ ]; then
1729
echo "Nginx: site-mods-enabled folder is missing, skipping security…"
@@ -29,7 +41,7 @@ fi
2941
# nginx content policy
3042
echo "Nginx: configuring content security policy…"
3143
cat <<EOF >> /etc/nginx/site-mods-enabled.d/00-generated-security.conf
32-
add_header 'Content-Security-Policy' "\
44+
add_header '${NGINX_CSP_HEADER_NAME}' "\
3345
default-src 'self'; \
3446
child-src ${NGINX_CSP_CHILD_SRC:-}; \
3547
connect-src ${NGINX_CSP_CONNECT_SRC:-}; \

0 commit comments

Comments
 (0)