fix(dev): proxy local API and restrict frontend upstream

This commit is contained in:
2026-08-04 15:58:13 +02:00
parent 8ffcaf3db9
commit 87b0fda3f6
7 changed files with 114 additions and 9 deletions
+4 -7
View File
@@ -3,13 +3,10 @@ set -eu
THT_FRONTEND_API_UPSTREAM=${THT_FRONTEND_API_UPSTREAM:-http://core:8787}
THT_FRONTEND_API_UPSTREAM=${THT_FRONTEND_API_UPSTREAM%/}
case "$THT_FRONTEND_API_UPSTREAM" in
http://*|https://*) ;;
*)
echo "Invalid THT_FRONTEND_API_UPSTREAM: use an internal http(s) upstream" >&2
exit 2
;;
esac
if ! /usr/local/bin/validate-frontend-api-upstream "$THT_FRONTEND_API_UPSTREAM"; then
echo "Invalid THT_FRONTEND_API_UPSTREAM: expected internal http://core:8787" >&2
exit 2
fi
export THT_FRONTEND_API_UPSTREAM
if ! envsubst '${THT_FRONTEND_API_UPSTREAM}' \
+1
View File
@@ -12,6 +12,7 @@ FROM nginxinc/nginx-unprivileged:1.27-alpine AS runtime
COPY --from=build /src/dist /usr/share/nginx/html
COPY docker/nginx.conf.template /etc/nginx/templates/default.conf.template
COPY --chmod=755 docker/frontend-entrypoint.sh /usr/local/bin/frontend-entrypoint
COPY --chmod=755 docker/validate-frontend-api-upstream.sh /usr/local/bin/validate-frontend-api-upstream
ENTRYPOINT ["/usr/local/bin/frontend-entrypoint"]
CMD ["nginx", "-g", "daemon off;"]
EXPOSE 8080
+23
View File
@@ -31,4 +31,27 @@ if rg -n 'BACKEND_BASE_URL|VITE_BACKEND_URL' docker/frontend-entrypoint.sh docke
exit 1
fi
upstream_validator=docker/validate-frontend-api-upstream.sh
for upstream in http://core:8787 http://core:8787/; do
if ! "$upstream_validator" "$upstream"; then
echo "frontend upstream validator rejected $upstream" >&2
exit 1
fi
done
for upstream in \
https://core:8787 \
http://core:8080 \
http://core:8787/api \
http://user:pass@core:8787 \
'http://core:8787?next=evil' \
'http://core:8787#fragment' \
'http://core:8787 injected' \
'http://core:8787;proxy_pass http://evil'; do
if "$upstream_validator" "$upstream" >/dev/null 2>&1; then
echo "frontend upstream validator accepted unsafe upstream: $upstream" >&2
exit 1
fi
done
echo "frontend same-origin proxy policy: ok"
+7
View File
@@ -0,0 +1,7 @@
#!/bin/sh
set -eu
case "${1-}" in
http://core:8787|http://core:8787/) exit 0 ;;
*) exit 2 ;;
esac