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:
ayurishchevandClaude Sonnet 5.5 committed 2026-10-07 13:28:30 +03:00
1 parent d38acd74de
commit d5e83eaff5
13 files changed
+211 -17

No files matched your search

+52 -4
View File
@@ -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