diff --git a/docker/Dockerfile b/docker/Dockerfile index affd0ca82..db52dc298 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -13,6 +13,8 @@ COPY --from=npm-install /app/node_modules /app/node_modules COPY ./ /app/ RUN npm run ng build --no-progress --configuration=production +RUN ./docker/generate-csp-hash.mjs dist/edc-demo-client/*.js > script-src.txt + # Stage 3: Serve app with nginx FROM docker.io/nginxinc/nginx-unprivileged:1.25-alpine3.18 @@ -28,6 +30,13 @@ COPY docker/default.conf.template etc/nginx/templates/default.conf.template # so that the automatic envsubst templating is not disabled. COPY docker/99-generate-app-config.sh /docker-entrypoint.d/99-generate-app-config.sh +# Mount the template from the build context and the hash list from the previous stage +# instead of copying them, as they are not needed in the final image. +RUN --mount=type=bind,from=build,source=/app/script-src.txt,target=/tmp/script-src.txt \ + --mount=type=bind,source=/docker/headers.include.template,target=/tmp/headers.include.template \ + env SCRIPT_SRC_EXTRA="$(cat /tmp/script-src.txt)" \ + envsubst '$$SCRIPT_SRC_EXTRA' < /tmp/headers.include.template > /etc/nginx/headers.include + RUN ln -sf /tmp/app-config.json /usr/share/nginx/html/assets/config/app-config.json \ # Nginx is configured to reject symlinks that point to a file owned by a different user, for security reasons && chown --no-dereference nginx:root /usr/share/nginx/html/assets/config/app-config.json diff --git a/docker/default.conf.template b/docker/default.conf.template index 68979ad2c..c6ef210ff 100644 --- a/docker/default.conf.template +++ b/docker/default.conf.template @@ -10,12 +10,16 @@ server { root /usr/share/nginx/html; location / { + add_header Cache-Control "public, immutable, max-age=604800"; + include headers.include; + index do-not-use-me.html; try_files $uri @index; } location @index { - add_header Cache-Control no-cache; + add_header Cache-Control "no-cache"; + include headers.include; expires 0; index index.html; try_files /index.html =404; diff --git a/docker/headers.include.template b/docker/headers.include.template new file mode 100644 index 000000000..797bb8003 --- /dev/null +++ b/docker/headers.include.template @@ -0,0 +1,38 @@ +# Documentation of individual directives: +# - default-src: fallback if a more specific directive is not given; 'self' allows resources from the same origin +# - base-uri: restricts the URLs that can be used in a document's element; 'self' allows resources from the same origin +# - script-src: controls