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

+5
View File
@@ -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;"]
+51
View File
@@ -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
+8 -4
View File
@@ -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