Skip to content

Commit 7a42f28

Browse files
authored
refactor: add nginx security headers (#1029)
* refactor: add nginx security headers * chore: update conf * refactor: configurable security headers * refactor: security script changes * refactor: use API_HOST
1 parent c7a2477 commit 7a42f28

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ COPY --from=build /app/packages/web/build /app/build
3131

3232
COPY ./packages/web/scripts/env.sh /app/init/
3333
COPY ./packages/web/scripts/inject-base-href.sh /app/init/
34+
COPY ./packages/web/scripts/security.sh /app/init/
3435

3536
RUN chmod +x /app/init/env.sh /app/init/inject-base-href.sh && \
3637
chmod a+w /etc/nginx/nginx.conf /app/build/index.html && \
@@ -49,6 +50,7 @@ CMD [ \
4950
cp -R /app/nginx/. /etc/nginx && \
5051
sh /app/init/env.sh env-config.js && \
5152
sh /app/init/inject-base-href.sh && \
53+
sh /app/init/security.sh && \
5254
export DISABLE_IPV6=\"$([[ \"$ENABLE_IPV6\" = \"true\" ]] && echo \"false\" || echo \"true\")\" && \
5355
envsubst '$DISABLE_IPV6' < /etc/nginx/nginx.conf.tmpl | sed -e '1h;2,$H;$!d;g' -e 's/# cut true.*# end//g' > /etc/nginx/nginx.conf && \
5456
nginx -g \"daemon off;\"" ]

nginx/nginx.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,8 @@ http {
4343
gzip_http_version 1.1;
4444
gzip_min_length 0;
4545
gzip_types text/plain application/javascript text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype;
46+
47+
#SecurityHeaders
48+
4649
}
4750
}

packages/web/scripts/security.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
3+
API_HOST=$(echo $REACT_APP_API_SERVER_ENDPOINT | sed -e 's|http://||g' -e 's|https://||g')
4+
5+
tempFile=$(mktemp /etc/nginx/tempfile.XXXXXXXX)
6+
7+
cat > "${tempFile}" <<EOF
8+
9+
add_header X-Frame-Options "SAMEORIGIN";
10+
add_header X-Content-Type-Options "nosniff";
11+
add_header Referrer-Policy "strict-origin-when-cross-origin";
12+
add_header Content-Security-Policy "default-src 'self'; style-src 'self' 'unsafe-inline'; connect-src 'self' http://${API_HOST} https://${API_HOST} ws://${API_HOST} wss://${API_HOST} blob:;";
13+
EOF
14+
15+
if [ "${ENABLE_SECURITY_HEADERS}" = "true" ]; then
16+
sed -i "/#SecurityHeaders/r ${tempFile}" /etc/nginx/nginx.conf.tmpl
17+
fi
18+
19+
rm "${tempFile}"
20+
21+
cat /etc/nginx/nginx.conf.tmpl

0 commit comments

Comments
 (0)