|
2 | 2 |
|
3 | 3 | set -euo pipefail |
4 | 4 |
|
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`. |
6 | 15 | # |
7 | 16 | # Inputs (aside from all the individual CSP settings): |
8 | 17 | # - NGINX_CSP_MODE: defaults to 'enforce' |
@@ -41,6 +50,17 @@ map "" \$$1 { |
41 | 50 | EOF |
42 | 51 | } |
43 | 52 |
|
| 53 | +# Load embedded CSP values from file (if it exists) |
| 54 | +EMBEDDED_CSP_PATH=/etc/csp-generator/default |
| 55 | +if [ -f "${EMBEDDED_CSP_PATH}" ]; then |
| 56 | + echo "Nginx: found CSP defaults at '$EMBEDDED_CSP_PATH', processing…" |
| 57 | + PROCESSED_CSP_PATH=$(mktemp) |
| 58 | + sed 's/[^=]\+=/EMBEDDED_CSP_&/' "${EMBEDDED_CSP_PATH}" > "${PROCESSED_CSP_PATH}" |
| 59 | + cat $PROCESSED_CSP_PATH |
| 60 | + source "${PROCESSED_CSP_PATH}" |
| 61 | + rm "${PROCESSED_CSP_PATH}" |
| 62 | +fi |
| 63 | + |
44 | 64 | # nginx frame options header |
45 | 65 | if [ "${NGINX_FRAME_OPTIONS}" = 'disable' ]; then |
46 | 66 | echo "Nginx: configuring frame options as disabled…" |
|
54 | 74 | csp_item() { |
55 | 75 | item="$1" |
56 | 76 |
|
57 | | - # Lookup values if needed, checking `NGINX_CSP_…` |
| 77 | + # Lookup values if needed, checking `EMBEDDED_CSP_…` and `NGINX_CSP_…` |
58 | 78 | if [ -n "${2:-}" ]; then |
59 | 79 | value="$2" |
60 | 80 | else |
61 | 81 | uc_item=$(echo "$item" | tr '[:lower:]-' '[:upper:]_') |
62 | | - value=$(printenv "NGINX_CSP_${uc_item}" || true) |
| 82 | + embedded_val=$(printenv "EMBEDDED_CSP_${uc_item}" || true) |
| 83 | + nginx_val=$(printenv "NGINX_CSP_${uc_item}" || true) |
| 84 | + value="${embedded_val}${embedded_val:+ }${nginx_val}" |
63 | 85 | fi |
64 | 86 |
|
65 | 87 | # Only output if we have a value |
|
0 commit comments