Files

51 lines
2.4 KiB
Bash
Raw Permalink Normal View History

#!/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