Skip to content

Commit 4e6786b

Browse files
committed
Nginx: provide mechanism to switch CSP mode
1 parent 46fd189 commit 4e6786b

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
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/config/nginx/snippets/headers/security-web-content.conf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
include /etc/nginx/snippets/headers/security-base.conf;
22

3-
# $content_security_policy, $x_frame_options and $x_robots_tag are set in the
4-
# `http` block, generated via a script.
3+
# $content_security_policy (and …_report_only), $x_frame_options and
4+
# $x_robots_tag are set in the `http` block, generated via a script.
55

66
# Which content is allowed to load (CSP)
77
add_header 'Content-Security-Policy' $content_security_policy always;
8+
add_header 'Content-Security-Policy-Report-Only' $content_security_policy_report_only always;
89

910
# Disable embedding
1011
# TODO: only works if resource has CORP header

common/scripts/startup/50-env-configure-nginx-csp.sh

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,27 @@ 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
1213
NGINX_CONFIG_FILE='/etc/nginx/snippets/vars/csp-and-robots.conf'
1314
NGINX_CSP_ITEMS='child-src connect-src font-src form-action frame-ancestors frame-src img-src manifest-src media-src object-src require-trusted-types-for script-src style-src trusted-types worker-src'
15+
NGINX_CSP_MODE="${NGINX_CSP_MODE:-enforce}"
1416
NGINX_CSP_REPORT_URI="${NGINX_CSP_REPORT_URI:-}"
1517
NGINX_FRAME_OPTIONS="${NGINX_FRAME_OPTIONS:-deny}"
1618

19+
# Validate input
20+
if [ "${NGINX_CSP_MODE}" = 'enforce' ]; then
21+
NGINX_CSP_VAR_NAME='content_security_policy'
22+
elif [ "${NGINX_CSP_MODE}" = 'report-only' ]; then
23+
NGINX_CSP_VAR_NAME='content_security_policy_report_only'
24+
else
25+
echo "Nginx: invalid CSP mode ${NGINX_CSP_MODE}"
26+
exit 1
27+
fi
28+
1729
# Check nginx structure
1830
if [ ! -f "${NGINX_CONFIG_FILE}" ]; then
1931
echo "Nginx: var-csp-and-robots.conf file is missing, skipping configuring it…"
@@ -60,15 +72,20 @@ csp_item() {
6072
nginx_csp_definition() {
6173
name="$1"
6274

63-
nginx_var_definition "$name" "$(
64-
printf "default-src 'self';"
65-
for item in $NGINX_CSP_ITEMS; do
66-
csp_item "$item"
67-
done
68-
csp_item 'report-uri' "$NGINX_CSP_REPORT_URI"
69-
)"
75+
if [ "$name" = "$NGINX_CSP_VAR_NAME" ]; then
76+
nginx_var_definition "$name" "$(
77+
printf "default-src 'self';"
78+
for item in $NGINX_CSP_ITEMS; do
79+
csp_item "$item"
80+
done
81+
csp_item 'report-uri' "$NGINX_CSP_REPORT_URI"
82+
)"
83+
else
84+
nginx_var_definition "$name" ''
85+
fi
7086
}
7187

7288
# nginx content policy
7389
echo "Nginx: configuring content security policy…"
7490
nginx_csp_definition content_security_policy >> "${NGINX_CONFIG_FILE}"
91+
nginx_csp_definition content_security_policy_report_only >> "${NGINX_CONFIG_FILE}"

0 commit comments

Comments
 (0)