diff --git a/APP_UI/Dockerfile b/APP_UI/Dockerfile index d6e3856..f36c782 100644 --- a/APP_UI/Dockerfile +++ b/APP_UI/Dockerfile @@ -12,5 +12,10 @@ RUN npm run build FROM nginx:alpine COPY --from=build-stage /app/dist /usr/share/nginx/html COPY default.conf.template /etc/nginx/templates/default.conf.template +# Validates OVP_CSP_CONNECT_EXTRA before the template is rendered (20-envsubst-on-templates.sh) +COPY check-csp-connect-extra.sh /docker-entrypoint.d/15-check-csp-connect-extra.sh +# Extra connect-src origins of the CSP, separated by spaces (empty: same origin only). envsubst replaces +# only variables that are defined, so the default must exist +ENV OVP_CSP_CONNECT_EXTRA="" EXPOSE 80 CMD ["nginx", "-g", "daemon off;"] diff --git a/APP_UI/check-csp-connect-extra.sh b/APP_UI/check-csp-connect-extra.sh new file mode 100755 index 0000000..ea16e09 --- /dev/null +++ b/APP_UI/check-csp-connect-extra.sh @@ -0,0 +1,51 @@ +#!/bin/sh +# Validates OVP_CSP_CONNECT_EXTRA: extra origins for the connect-src directive of the Content-Security-Policy +# (compose: OVPMON_CSP_CONNECT_EXTRA). The value ends up in a response header, so only origins are accepted: +# empty, or https://host[:port] and http://host[:port] separated by spaces (host: letters, digits, dots, hyphens; +# no path, quotes, ; or commas). Anything else could inject another CSP directive. +# +# Runs as /docker-entrypoint.d/15-check-csp-connect-extra.sh in the app-ui image (before the template is +# rendered by 20-envsubst-on-templates.sh) and from the all-in-one entrypoint. Exit code 1 stops the container. +# The same rule is in deploy/docker/lib.sh and deploy/native/lib.sh (csp_extra_problem): keep them in step. +set -eu + +csp_extra_problem() { + local _t _rest _host _port + [ -n "$1" ] || return 0 + case "$1" in + *[!A-Za-z0-9.:/\ -]*) + printf 'contains characters that are not allowed (only letters, digits, . - : / and spaces; no quotes, ; or commas)'; return 1 ;; + " "*|*" ") + printf 'starts or ends with a space'; return 1 ;; + esac + set -f + for _t in $1; do + case "$_t" in + https://*) _rest=${_t#https://} ;; + http://*) _rest=${_t#http://} ;; + *) set +f; printf "'%s' is not an origin (use https://host[:port] or http://host[:port])" "$_t"; return 1 ;; + esac + _host=${_rest%%:*} + _port="" + case "$_rest" in *:*) _port=${_rest#*:} ;; esac + case "$_host" in + ""|*[!A-Za-z0-9.-]*) set +f; printf "'%s' has an invalid host (letters, digits, dots and hyphens; no path)" "$_t"; return 1 ;; + esac + if [ -n "$_port" ]; then + case "$_port" in + *[!0-9]*) set +f; printf "'%s' has an invalid port (digits only, no path)" "$_t"; return 1 ;; + esac + if [ "${#_port}" -gt 5 ] || [ "$_port" -lt 1 ] || [ "$_port" -gt 65535 ]; then + set +f; printf "'%s' has a port out of range" "$_t"; return 1 + fi + fi + case "$_rest" in *:) set +f; printf "'%s' has an empty port" "$_t"; return 1 ;; esac + done + set +f + return 0 +} + +if ! _reason=$(csp_extra_problem "${OVP_CSP_CONNECT_EXTRA:-}"); then + echo "check-csp-connect-extra: OVPMON_CSP_CONNECT_EXTRA is invalid: $_reason. Use origins https://host[:port] or http://host[:port] separated by spaces, or leave it empty" >&2 + exit 1 +fi diff --git a/APP_UI/default.conf.template b/APP_UI/default.conf.template index d6c5513..cf6fa71 100644 --- a/APP_UI/default.conf.template +++ b/APP_UI/default.conf.template @@ -8,12 +8,16 @@ server { # Security headers. add_header in a location replaces the ones inherited from the server block, # so the /assets/ location below repeats them. CSP: the bundle is served from this origin only; - # style-src 'unsafe-inline' is needed by Bootstrap and Chart.js (inline style attributes); - # connect-src 'self' means config.json must point to proxied paths (the defaults /api/v1 and /profiles-api). + # style-src 'unsafe-inline' is needed by Bootstrap and Chart.js (inline style attributes). + # connect-src is 'self' plus OVP_CSP_CONNECT_EXTRA (origins separated by spaces, empty by default): set it + # only when config.json points the UI to an API on another origin (the defaults /api/v1 and /profiles-api are + # proxied below, same origin). The value is validated by the deploy scripts and by the all-in-one entrypoint. + # One policy ($csp) for both places. + set $csp "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self' ${OVP_CSP_CONNECT_EXTRA}; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'"; add_header X-Content-Type-Options "nosniff" always; add_header X-Frame-Options "DENY" always; add_header Referrer-Policy "no-referrer" always; - add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'" always; + add_header Content-Security-Policy $csp always; gzip on; gzip_vary on; @@ -31,7 +35,7 @@ server { add_header X-Content-Type-Options "nosniff" always; add_header X-Frame-Options "DENY" always; add_header Referrer-Policy "no-referrer" always; - add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'" always; + add_header Content-Security-Policy $csp always; } # The entry page and the runtime configuration must be revalidated after an update diff --git a/deploy/allinone/Dockerfile b/deploy/allinone/Dockerfile index 2118b51..38e48e9 100644 --- a/deploy/allinone/Dockerfile +++ b/deploy/allinone/Dockerfile @@ -47,6 +47,7 @@ COPY APP_PROFILER /app/APP_PROFILER # UI and nginx: the template is rendered at container start (entrypoint.sh) COPY --from=ui-build /app/dist /usr/share/nginx/html COPY APP_UI/default.conf.template /etc/ovpmon/default.conf.template +COPY APP_UI/check-csp-connect-extra.sh /usr/local/bin/ovpmon-check-csp.sh RUN rm -f /etc/nginx/http.d/default.conf \ && mkdir -p /run/nginx /var/log/nginx \ && ln -sf /dev/stdout /var/log/nginx/access.log \ @@ -56,7 +57,7 @@ COPY deploy/allinone/supervisord.conf /etc/supervisord.conf COPY deploy/allinone/entrypoint.sh /usr/local/bin/ovpmon-entrypoint.sh # Mount points of the five data volumes and of the backup packages volume (ovp_backups) -RUN chmod +x /usr/local/bin/ovpmon-entrypoint.sh \ +RUN chmod +x /usr/local/bin/ovpmon-entrypoint.sh /usr/local/bin/ovpmon-check-csp.sh \ && mkdir -p /app/db /app/easy-rsa /app/client-config /app/backups /etc/openvpn /var/log/openvpn # Default database locations (the compose file sets the same values) diff --git a/deploy/allinone/entrypoint.sh b/deploy/allinone/entrypoint.sh index 93b89a3..8c843b2 100755 --- a/deploy/allinone/entrypoint.sh +++ b/deploy/allinone/entrypoint.sh @@ -71,11 +71,20 @@ ensure_rule mangle FORWARD -o "$NAT_IFACE" -p tcp --tcp-flags SYN,RST SYN -j TCP ensure_rule mangle FORWARD -i "$NAT_IFACE" -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu # --- 5. Nginx configuration --------------------------------------------------------------- -# The same template as the UI image of variant B; all upstreams are local. Only the four -# OVP_* variables are substituted, nginx's own $variables stay intact. +# The same template as the UI image of variant B; all upstreams are local. Only the OVP_* variables +# are substituted, nginx's own $variables stay intact. +# OVP_CSP_CONNECT_EXTRA (compose: OVPMON_CSP_CONNECT_EXTRA): extra connect-src origins of the CSP. It goes into +# a response header, so only origins are accepted (APP_UI/check-csp-connect-extra.sh, copied into the image): +# anything else could inject another CSP directive. +# Empty value: same origin only. +OVP_CSP_CONNECT_EXTRA="${OVP_CSP_CONNECT_EXTRA:-}" +/usr/local/bin/ovpmon-check-csp.sh || die "invalid OVPMON_CSP_CONNECT_EXTRA (see above)" +# one space between origins (the template has the value after 'self' and a space) +OVP_CSP_CONNECT_EXTRA=$(printf '%s' "$OVP_CSP_CONNECT_EXTRA" | tr -s ' ') +export OVP_CSP_CONNECT_EXTRA export OVP_API_HOST=127.0.0.1 OVP_API_PORT=5001 export OVP_PROFILER_HOST=127.0.0.1 OVP_PROFILER_PORT=8000 -envsubst '${OVP_API_HOST} ${OVP_API_PORT} ${OVP_PROFILER_HOST} ${OVP_PROFILER_PORT}' \ +envsubst '${OVP_API_HOST} ${OVP_API_PORT} ${OVP_PROFILER_HOST} ${OVP_PROFILER_PORT} ${OVP_CSP_CONNECT_EXTRA}' \ < /etc/ovpmon/default.conf.template > /etc/nginx/http.d/ovpmon.conf nginx -t || die "generated nginx configuration is invalid (see above)" diff --git a/deploy/docker/install.sh b/deploy/docker/install.sh index a98d395..773e515 100755 --- a/deploy/docker/install.sh +++ b/deploy/docker/install.sh @@ -105,6 +105,7 @@ else if [ -n "$ARG_HTTP_PORT" ]; then echo "OVPMON_HTTP_PORT=$ARG_HTTP_PORT"; else echo "# OVPMON_HTTP_PORT=80"; fi if [ -n "$ARG_VPN_PORT" ]; then echo "OVPMON_VPN_PORT=$ARG_VPN_PORT"; else echo "# OVPMON_VPN_PORT=1194"; fi echo "# OVPMON_CORS_ORIGINS=" + echo "# OVPMON_CSP_CONNECT_EXTRA=" } > "$ENV_FILE" ) chmod 600 "$ENV_FILE" diff --git a/deploy/docker/lib.sh b/deploy/docker/lib.sh index 8ea7880..11ac2c9 100644 --- a/deploy/docker/lib.sh +++ b/deploy/docker/lib.sh @@ -310,6 +310,47 @@ secret_problem() { return 0 } +# csp_extra_problem VALUE: extra origins for the connect-src directive of the Content-Security-Policy +# (OVPMON_CSP_CONNECT_EXTRA in .env). Allowed: empty, or origins https://host[:port] and +# http://host[:port] separated by spaces (host: letters, digits, dots, hyphens; no path, quotes, ; or commas). +# Anything else would let a value inject its own CSP directive. Prints the reason and returns 1 when the +# value is not acceptable. Use it as: if reason=$(csp_extra_problem "$v"); then ...ok...; else ...; fi +csp_extra_problem() { + local _t _rest _host _port + [ -n "$1" ] || return 0 + case "$1" in + *[!A-Za-z0-9.:/\ -]*) + printf 'contains characters that are not allowed (only letters, digits, . - : / and spaces; no quotes, ; or commas)'; return 1 ;; + " "*|*" ") + printf 'starts or ends with a space'; return 1 ;; + esac + set -f + for _t in $1; do + case "$_t" in + https://*) _rest=${_t#https://} ;; + http://*) _rest=${_t#http://} ;; + *) set +f; printf "'%s' is not an origin (use https://host[:port] or http://host[:port])" "$_t"; return 1 ;; + esac + _host=${_rest%%:*} + _port="" + case "$_rest" in *:*) _port=${_rest#*:} ;; esac + case "$_host" in + ""|*[!A-Za-z0-9.-]*) set +f; printf "'%s' has an invalid host (letters, digits, dots and hyphens; no path)" "$_t"; return 1 ;; + esac + if [ -n "$_port" ]; then + case "$_port" in + *[!0-9]*) set +f; printf "'%s' has an invalid port (digits only, no path)" "$_t"; return 1 ;; + esac + if [ "${#_port}" -gt 5 ] || [ "$_port" -lt 1 ] || [ "$_port" -gt 65535 ]; then + set +f; printf "'%s' has a port out of range" "$_t"; return 1 + fi + fi + case "$_rest" in *:) set +f; printf "'%s' has an empty port" "$_t"; return 1 ;; esac + done + set +f + return 0 +} + check_env_file() { local _reason [ -f "$ENV_FILE" ] || die ".env not found at $ENV_FILE (install.sh creates it)" @@ -317,6 +358,9 @@ check_env_file() { if ! _reason=$(secret_problem "$(env_get JWT_SECRET)"); then die "JWT_SECRET in $ENV_FILE is $_reason: the API refuses to start with a weak signing key. Set a random value of at least 32 characters (for example: openssl rand -hex 32); note that a changed key signs out all users" fi + if ! _reason=$(csp_extra_problem "$(env_get OVPMON_CSP_CONNECT_EXTRA)"); then + die "OVPMON_CSP_CONNECT_EXTRA in $ENV_FILE is invalid: $_reason. Use origins https://host[:port] or http://host[:port] separated by spaces, or leave it empty" + fi if [ -n "$(find "$ENV_FILE" \( -perm -040 -o -perm -004 \) 2>/dev/null)" ]; then warn ".env is readable by other users; fix with: chmod 600 $ENV_FILE" fi diff --git a/deploy/native/install.sh b/deploy/native/install.sh index abf2f89..c440dd5 100755 --- a/deploy/native/install.sh +++ b/deploy/native/install.sh @@ -2,7 +2,7 @@ # Install OpenVPN Monitor & Profiler as system services (no containers): Alpine (OpenRC), Debian, Ubuntu (systemd). # # deploy/native/install.sh [--prefix DIR] [--ui-dist FILE|DIR | --build-ui [--node-download] | --no-ui] -# [--https-port N] [--host-name NAME] [--admin-user NAME] [--force] +# [--https-port N] [--csp-connect-extra ORIGINS] [--host-name NAME] [--admin-user NAME] [--force] # # --prefix install directory (APP_CORE, APP_PROFILER, venvs). REQUIRED: without the option the script # asks for it (no default is assumed). It must be a new or empty directory, different from @@ -18,6 +18,9 @@ # automatically (a warning is printed when it is older than APP_UI); when there is no UI # source at all the install stops # --https-port HTTPS port of the web UI (default 8088) +# --csp-connect-extra extra origins the UI may call (CSP connect-src), for an API on another host: origins +# https://host[:port] or http://host[:port] separated by spaces, in quotes (default: none, same +# origin only). Kept in /etc/ovpmon/deploy.state as OVPMON_CSP_CONNECT_EXTRA # --host-name DNS name or IP of this host: certificate name and CORS origin (default: detected address) # --admin-user login of the initial administrator (default: admin); the password is generated # --force continue on an untested OS version / Debian derivative @@ -41,6 +44,7 @@ UI_BUILD=0 UI_NODE_DL=0 NO_UI=0 HTTPS_PORT=8088 +CSP_EXTRA="" HOST_NAME="" ADMIN_USER="admin" FORCE=0 @@ -54,6 +58,7 @@ while [ $# -gt 0 ]; do --node-download) UI_NODE_DL=1; shift ;; --no-ui) NO_UI=1; shift ;; --https-port) [ $# -ge 2 ] || die "--https-port needs a value"; HTTPS_PORT="$2"; shift 2 ;; + --csp-connect-extra) [ $# -ge 2 ] || die "--csp-connect-extra needs a value"; CSP_EXTRA="$2"; shift 2 ;; --host-name) [ $# -ge 2 ] || die "--host-name needs a value"; HOST_NAME="$2"; shift 2 ;; --admin-user) [ $# -ge 2 ] || die "--admin-user needs a value"; ADMIN_USER="$2"; shift 2 ;; --force) FORCE=1; shift ;; @@ -125,6 +130,7 @@ else [ -z "$_problem" ] || die "invalid --prefix '$PREFIX': $_problem" fi valid_port "$HTTPS_PORT" || die "invalid --https-port '$HTTPS_PORT'" +if ! _csp_reason=$(csp_extra_problem "$CSP_EXTRA"); then die "invalid --csp-connect-extra '$CSP_EXTRA': $_csp_reason"; fi case "$ADMIN_USER" in ""|*[!A-Za-z0-9._-]*) die "invalid --admin-user (letters, digits, . _ - only)" ;; esac @@ -434,7 +440,7 @@ fi # nginx site _tmp="$TMPD/ovpmon.conf" mkdir -p "$NGINX_CONF_DIR" -render_nginx "$SRC" "$HTTPS_PORT" "$_tmp" +render_nginx "$SRC" "$HTTPS_PORT" "$_tmp" "$CSP_EXTRA" rc=0 install_managed "$_tmp" "$NGINX_SITE" 644 "deploy/native/nginx/ovpmon.conf.template" || rc=$? case "$rc" in @@ -497,6 +503,7 @@ fi CURRENT_PHASE="finish" state_set OVPMON_PREFIX "$PREFIX" state_set OVPMON_HTTPS_PORT "$HTTPS_PORT" +state_set OVPMON_CSP_CONNECT_EXTRA "$CSP_EXTRA" state_set OVPMON_OS "$OS_ID $OS_VERSION_ID" state_set OVPMON_VERSION "$(cat "$PREFIX/VERSION")" [ -n "$(state_get OVPMON_INSTALLED_AT)" ] || state_set OVPMON_INSTALLED_AT "$(now_iso)" diff --git a/deploy/native/lib.sh b/deploy/native/lib.sh index fdf158f..6661328 100755 --- a/deploy/native/lib.sh +++ b/deploy/native/lib.sh @@ -383,6 +383,47 @@ secret_problem() { fi return 0 } + +# csp_extra_problem VALUE: extra origins for the connect-src directive of the Content-Security-Policy +# (OVPMON_CSP_CONNECT_EXTRA / --csp-connect-extra). Allowed: empty, or origins https://host[:port] and +# http://host[:port] separated by spaces (host: letters, digits, dots, hyphens; no path, quotes, ; or commas). +# Anything else would let a value inject its own CSP directive. Prints the reason and returns 1 when the +# value is not acceptable. Use it as: if reason=$(csp_extra_problem "$v"); then ...ok...; else ...; fi +csp_extra_problem() { + local _t _rest _host _port + [ -n "$1" ] || return 0 + case "$1" in + *[!A-Za-z0-9.:/\ -]*) + printf 'contains characters that are not allowed (only letters, digits, . - : / and spaces; no quotes, ; or commas)'; return 1 ;; + " "*|*" ") + printf 'starts or ends with a space'; return 1 ;; + esac + set -f + for _t in $1; do + case "$_t" in + https://*) _rest=${_t#https://} ;; + http://*) _rest=${_t#http://} ;; + *) set +f; printf "'%s' is not an origin (use https://host[:port] or http://host[:port])" "$_t"; return 1 ;; + esac + _host=${_rest%%:*} + _port="" + case "$_rest" in *:*) _port=${_rest#*:} ;; esac + case "$_host" in + ""|*[!A-Za-z0-9.-]*) set +f; printf "'%s' has an invalid host (letters, digits, dots and hyphens; no path)" "$_t"; return 1 ;; + esac + if [ -n "$_port" ]; then + case "$_port" in + *[!0-9]*) set +f; printf "'%s' has an invalid port (digits only, no path)" "$_t"; return 1 ;; + esac + if [ "${#_port}" -gt 5 ] || [ "$_port" -lt 1 ] || [ "$_port" -gt 65535 ]; then + set +f; printf "'%s' has a port out of range" "$_t"; return 1 + fi + fi + case "$_rest" in *:) set +f; printf "'%s' has an empty port" "$_t"; return 1 ;; esac + done + set +f + return 0 +} state_get() { kv_get "$STATE_FILE" "$1"; } state_set() { kv_set "$STATE_FILE" "$1" "$2" 644; } @@ -510,13 +551,20 @@ render_unit() { sed -e "s#$DEFAULT_PREFIX#$3#g" "$src" > "$4" } -# render_nginx SRC_DIR HTTPS_PORT OUT +# render_nginx SRC_DIR HTTPS_PORT OUT [CSP_CONNECT_EXTRA] +# CSP_CONNECT_EXTRA: validated origins for connect-src (see csp_extra_problem); empty = same origin only. +# The template has no space before @CSP_CONNECT_EXTRA@: it is filled in with a leading space or with nothing. render_nginx() { - local src + local src csp reason src="$1/deploy/native/nginx/ovpmon.conf.template" [ -f "$src" ] || die "nginx template not found: $src" - sed -e "s#@HTTPS_PORT@#$2#g" -e "s#@WEB_ROOT@#$WEB_ROOT#g" \ - -e "s#@TLS_CRT@#$TLS_DIR/ovpmon.crt#g" -e "s#@TLS_KEY@#$TLS_DIR/ovpmon.key#g" "$1/deploy/native/nginx/ovpmon.conf.template" > "$3" + if ! reason=$(csp_extra_problem "${4:-}"); then + die "invalid CSP connect-src origins '${4:-}': $reason" + fi + csp="" + [ -z "${4:-}" ] || csp=" $(printf '%s' "$4" | tr -s ' ')" + sed -e "s#@HTTPS_PORT@#$2#g" -e "s#@WEB_ROOT@#$WEB_ROOT#g" -e "s#@CSP_CONNECT_EXTRA@#$csp#g" \ + -e "s#@TLS_CRT@#$TLS_DIR/ovpmon.crt#g" -e "s#@TLS_KEY@#$TLS_DIR/ovpmon.key#g" "$src" > "$3" } # helper_conf_text PREFIX: content of /etc/ovpmon/helper.conf for this OS diff --git a/deploy/native/nginx/ovpmon.conf.template b/deploy/native/nginx/ovpmon.conf.template index 3428aef..24b9900 100644 --- a/deploy/native/nginx/ovpmon.conf.template +++ b/deploy/native/nginx/ovpmon.conf.template @@ -1,7 +1,7 @@ # managed by ovpmon deploy/native (install.sh / update.sh replace this file only while this marker line is present) # # HTTPS site of OpenVPN Monitor & Profiler. Rendered by install.sh / update.sh: they fill in the HTTPS port -# (default 8088), the UI directory (/var/www/ovpmon) and the certificate and key paths (/etc/ovpmon/tls/ovpmon.crt +# (default 8088), the extra CSP connect-src origins (default none), the UI directory (/var/www/ovpmon) and the certificate and key paths (/etc/ovpmon/tls/ovpmon.crt # and .key: self-signed by install.sh; replace them with a real certificate under the same names, or edit this # file and remove the marker line above so that updates leave it alone). # Installed to the nginx include directory (Alpine /etc/nginx/http.d, Debian/Ubuntu /etc/nginx/conf.d), @@ -32,6 +32,11 @@ server { add_header X-Frame-Options "DENY" always; add_header Referrer-Policy "no-referrer" always; add_header Cache-Control "no-store" always; + # CSP: the bundle is served from this origin only; style-src 'unsafe-inline' is needed by Bootstrap and Chart.js + # (inline style attributes). connect-src: same origin plus the optional origins of another API host + # (install.sh / update.sh --csp-connect-extra, state OVPMON_CSP_CONNECT_EXTRA; the value is filled in + # with its own leading space, so there is none before the placeholder). Same policy as APP_UI/default.conf.template. + add_header Content-Security-Policy "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'@CSP_CONNECT_EXTRA@; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'" always; gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml image/svg+xml; diff --git a/deploy/native/update.sh b/deploy/native/update.sh index 5a44a58..99d7be5 100755 --- a/deploy/native/update.sh +++ b/deploy/native/update.sh @@ -2,7 +2,7 @@ # Update OpenVPN Monitor & Profiler (system services) without touching the data. # # deploy/native/update.sh [--prefix DIR] [--ui-dist FILE|DIR | --build-ui [--node-download]] [--source DIR] -# [--no-pull] [--yes] [--rollback] +# [--csp-connect-extra ORIGINS] [--no-pull] [--yes] [--rollback] # # --prefix install directory (default: from /etc/ovpmon/deploy.state, else /opt/OpenVPN-Monitoring-Simple) # --ui-dist UI tarball from build-ui.sh, or a directory with index.html: always installed (highest priority) @@ -12,6 +12,9 @@ # Without these the committed build deploy/ui-dist of the source is installed automatically when # it differs from the installed UI (/.ui-source-hash); no UI source: the UI stays # --source checkout with the new version (default: the repository this script is in) +# --csp-connect-extra extra origins the UI may call (CSP connect-src): https://host[:port] or http://host[:port] +# separated by spaces, in quotes; "" removes them. Without the option the value kept in +# /etc/ovpmon/deploy.state (OVPMON_CSP_CONNECT_EXTRA) is applied # --no-pull no "git pull --ff-only" of the source checkout (offline hosts, sources already in place) # --yes no confirmation questions # --rollback restore code, databases, configuration, PKI and client profiles from the newest backup @@ -51,6 +54,8 @@ STAMP=$(stamp) RESTORE_STAMP="$STAMP" STAGING="" HTTPS_PORT="" +CSP_EXTRA="" +CSP_EXTRA_SET=0 OLD_VERSION="unknown" NEW_VERSION="unknown" UI_STAGED=0 @@ -217,6 +222,7 @@ main() { --ui-dist) [ $# -ge 2 ] || die "--ui-dist needs a value"; UI_DIST="$2"; shift 2 ;; --build-ui) UI_BUILD=1; shift ;; --node-download) UI_NODE_DL=1; shift ;; + --csp-connect-extra) [ $# -ge 2 ] || die "--csp-connect-extra needs a value"; CSP_EXTRA="$2"; CSP_EXTRA_SET=1; shift 2 ;; --source) [ $# -ge 2 ] || die "--source needs a value"; SOURCE="$2"; shift 2 ;; --no-pull) NO_PULL=1; shift ;; --yes|-y) ASSUME_YES=1; shift ;; @@ -243,6 +249,7 @@ main() { valid_prefix "$PREFIX" || die "invalid install directory '$PREFIX'" HTTPS_PORT=$(state_get OVPMON_HTTPS_PORT) [ -n "$HTTPS_PORT" ] || HTTPS_PORT=$(detect_https_port || true) + if [ "$CSP_EXTRA_SET" = 0 ]; then CSP_EXTRA=$(state_get OVPMON_CSP_CONNECT_EXTRA); fi SOURCE="${SOURCE:-$ROOT_DIR}" case "$UI_DIST" in ""|/*) ;; *) UI_DIST="$PWD/$UI_DIST" ;; esac @@ -252,6 +259,11 @@ main() { return 0 fi + if ! _csp_reason=$(csp_extra_problem "$CSP_EXTRA"); then + if [ "$CSP_EXTRA_SET" = 1 ]; then _csp_src="--csp-connect-extra"; else _csp_src="OVPMON_CSP_CONNECT_EXTRA in $STATE_FILE"; fi + die "invalid CSP connect-src origins '$CSP_EXTRA' ($_csp_src): $_csp_reason" + fi + # ----- Phase 0 ------------------------------------------------------------------------- CURRENT_PHASE="0 preflight" phase 0 "preflight" @@ -480,7 +492,7 @@ main() { [ -d /etc/openvpn/scripts ] || ensure_dir /etc/openvpn/scripts root:root 755 # nginx site (only when it is ours and the port is known) if [ -n "$HTTPS_PORT" ]; then - render_nginx "$SOURCE" "$HTTPS_PORT" "$TMPD/ovpmon.conf" + render_nginx "$SOURCE" "$HTTPS_PORT" "$TMPD/ovpmon.conf" "$CSP_EXTRA" rc=0; install_managed "$TMPD/ovpmon.conf" "$NGINX_SITE" 644 "deploy/native/nginx/ovpmon.conf.template" || rc=$? report "$rc" "$NGINX_SITE" if [ "$rc" = 0 ]; then @@ -510,6 +522,7 @@ main() { rm -rf "$WEB_ROOT.old" state_set OVPMON_PREFIX "$PREFIX" [ -z "$HTTPS_PORT" ] || state_set OVPMON_HTTPS_PORT "$HTTPS_PORT" + state_set OVPMON_CSP_CONNECT_EXTRA "$CSP_EXTRA" state_set OVPMON_OS "$OS_ID $OS_VERSION_ID" state_set OVPMON_VERSION "$NEW_VERSION" [ -n "$(state_get OVPMON_INSTALLED_AT)" ] || state_set OVPMON_INSTALLED_AT "adopted $(now_iso)" diff --git a/docker-compose.allinone.yml b/docker-compose.allinone.yml index f97a120..895cb2c 100644 --- a/docker-compose.allinone.yml +++ b/docker-compose.allinone.yml @@ -3,6 +3,7 @@ # First start only: OVPMON_INITIAL_ADMIN_USER / OVPMON_INITIAL_ADMIN_PASSWORD in .env # Optional: OVPMON_CORS_ORIGINS, OVPMON_HTTP_PORT (host port of the UI, default 80), # OVPMON_VPN_PORT (host UDP port of OpenVPN, default 1194) +# Optional: OVPMON_CSP_CONNECT_EXTRA (origins the UI may call besides its own, e.g. https://api.example.com; empty by default) # Install/update with the scripts in deploy/docker (see DOCS/Deployment/Containers/README.md) # # Variants A (this file) and B (docker-compose.yml) publish the same host ports and use fixed @@ -48,6 +49,8 @@ services: OVPMON_INITIAL_ADMIN_USER: ${OVPMON_INITIAL_ADMIN_USER:-} OVPMON_INITIAL_ADMIN_PASSWORD: ${OVPMON_INITIAL_ADMIN_PASSWORD:-} OVPMON_CORS_ORIGINS: ${OVPMON_CORS_ORIGINS:-} + # Extra connect-src origins of the CSP (API on another host), separated by spaces; validated at start + OVP_CSP_CONNECT_EXTRA: ${OVPMON_CSP_CONNECT_EXTRA:-} healthcheck: # UI through Nginx, profiler root (FastAPI) and TCP connect to the Flask API test: diff --git a/docker-compose.yml b/docker-compose.yml index 155b6ed..ee91068 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,6 +3,7 @@ # First start only: OVPMON_INITIAL_ADMIN_USER / OVPMON_INITIAL_ADMIN_PASSWORD in .env # Optional: OVPMON_CORS_ORIGINS (comma-separated origins; off by default, UI is same-origin via Nginx) # Optional: OVPMON_HTTP_PORT (host port of the UI, default 80), OVPMON_VPN_PORT (host UDP port of OpenVPN, default 1194) +# Optional: OVPMON_CSP_CONNECT_EXTRA (origins the UI may call besides its own, e.g. https://api.example.com; empty by default) # Install/update with the scripts in deploy/docker (see DOCS/Deployment/Containers/README.md) x-secret: &jwt-secret @@ -25,6 +26,8 @@ services: OVP_API_PORT: 5001 OVP_PROFILER_HOST: ovp-profiler OVP_PROFILER_PORT: 8000 + # Extra connect-src origins of the CSP (API on another host), separated by spaces; empty = same origin only + OVP_CSP_CONNECT_EXTRA: ${OVPMON_CSP_CONNECT_EXTRA:-} healthcheck: # busybox wget exists in nginx:alpine test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1/"]