|
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,16 @@ 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/[^=]\+=/export EMBEDDED_CSP_&/' "${EMBEDDED_CSP_PATH}" > "${PROCESSED_CSP_PATH}" |
| 59 | + . "${PROCESSED_CSP_PATH}" |
| 60 | + rm "${PROCESSED_CSP_PATH}" |
| 61 | +fi |
| 62 | + |
44 | 63 | # nginx frame options header |
45 | 64 | if [ "${NGINX_FRAME_OPTIONS}" = 'disable' ]; then |
46 | 65 | echo "Nginx: configuring frame options as disabled…" |
|
54 | 73 | csp_item() { |
55 | 74 | item="$1" |
56 | 75 |
|
57 | | - # Lookup values if needed, checking `NGINX_CSP_…` |
| 76 | + # Lookup values if needed, checking `EMBEDDED_CSP_…` and `NGINX_CSP_…` |
58 | 77 | if [ -n "${2:-}" ]; then |
59 | 78 | value="$2" |
60 | 79 | else |
61 | 80 | uc_item=$(echo "$item" | tr '[:lower:]-' '[:upper:]_') |
62 | | - value=$(printenv "NGINX_CSP_${uc_item}" || true) |
| 81 | + embedded_val=$( (printenv "EMBEDDED_CSP_${uc_item}" || true) | sed 's/^"//; s/"$//') |
| 82 | + nginx_val=$( (printenv "NGINX_CSP_${uc_item}" || true) | sed 's/^"//; s/"$//') |
| 83 | + value="${embedded_val}${embedded_val:+${nginx_val:+ }}${nginx_val}" |
63 | 84 | fi |
64 | 85 |
|
65 | 86 | # Only output if we have a value |
|
0 commit comments