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
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user