UI delivery, stage B: single CSP with optional connect-src origins (OVPMON_CSP_CONNECT_EXTRA)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d38acd74de
commit
d5e83eaff5
13 files changed
+211
-17
No files matched your search
@@ -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)"
|
||||
|
||||
+52
-4
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
+15
-2
@@ -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 (<web root>/.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)"
|
||||
|
||||
Reference in new issue
Block a user