Skip to content

Commit

Permalink
fix(security): CORS Allow-Credentials header applies to ALL CORS requ…
Browse files Browse the repository at this point in the history
…ests (edgexfoundry#4669)

Signed-off-by: Bryon Nevis <[email protected]>
  • Loading branch information
bnevis-i authored Sep 6, 2023
1 parent 5e4c173 commit 2bac5d1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion cmd/security-proxy-setup/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ fi
echo "$(date) CORS settings dump ..."
( set | grep EDGEX_SERVICE_CORSCONFIGURATION ) || true

# See https://github.com/edgexfoundry/edgex-go/issues/4648 as to why CORS is implemented this way.
# Warning: no not simplify add_header redundancy. See https://www.peterbe.com/plog/be-very-careful-with-your-add_header-in-nginx
corssnippet=/etc/nginx/templates/cors.block.$$
touch "${corssnippet}"
if test "${EDGEX_SERVICE_CORSCONFIGURATION_ENABLECORS}" = "true"; then
echo " if (\$request_method = 'OPTIONS') {" >> "${corssnippet}"
echo " add_header 'Access-Control-Allow-Origin' '${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWEDORIGIN}';" >> "${corssnippet}"
echo " add_header 'Access-Control-Allow-Methods' '${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWEDMETHODS}';" >> "${corssnippet}"
echo " add_header 'Access-Control-Allow-Headers' '${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWEDHEADERS}';" >> "${corssnippet}"
# Access-Control-Expose-Headers should not be set on OPTIONS request
if test "${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWCREDENTIALS}" = "true"; then
# CORS specificaiton says that if not true, omit the header entirely
echo " add_header 'Access-Control-Allow-Credentials' '${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWCREDENTIALS}';" >> "${corssnippet}"
Expand All @@ -81,6 +82,10 @@ if test "${EDGEX_SERVICE_CORSCONFIGURATION_ENABLECORS}" = "true"; then
# Always add headers regardless of response code. Omit preflight-related headers (allow-methods, allow-headers, allow-credentials, max-age)
echo " add_header 'Access-Control-Allow-Origin' '${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWEDORIGIN}' always;" >> "${corssnippet}"
echo " add_header 'Access-Control-Expose-Headers' '${EDGEX_SERVICE_CORSCONFIGURATION_CORSEXPOSEHEADERS}' always;" >> "${corssnippet}"
if test "${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWCREDENTIALS}" = "true"; then
# CORS specificaiton says that if not true, omit the header entirely
echo " add_header 'Access-Control-Allow-Credentials' '${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWCREDENTIALS}';" >> "${corssnippet}"
fi
echo " add_header 'Vary' 'origin' always;" >> "${corssnippet}"
echo " }" >> "${corssnippet}"
echo "" >> "${corssnippet}"
Expand Down
7 changes: 6 additions & 1 deletion snap/local/runtime-helpers/bin/security-bootstrapper-nginx
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ fi
echo "$(date) CORS settings dump ..."
( set | grep EDGEX_SERVICE_CORSCONFIGURATION ) || true

# See https://github.com/edgexfoundry/edgex-go/issues/4648 as to why CORS is implemented this way.
# Warning: no not simplify add_header redundancy. See https://www.peterbe.com/plog/be-very-careful-with-your-add_header-in-nginx
corssnippet=/tmp/cors.block.$$
touch "${corssnippet}"
if test "${EDGEX_SERVICE_CORSCONFIGURATION_ENABLECORS}" = "true"; then
echo " if (\$request_method = 'OPTIONS') {" >> "${corssnippet}"
echo " add_header 'Access-Control-Allow-Origin' '${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWEDORIGIN}';" >> "${corssnippet}"
echo " add_header 'Access-Control-Allow-Methods' '${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWEDMETHODS}';" >> "${corssnippet}"
echo " add_header 'Access-Control-Allow-Headers' '${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWEDHEADERS}';" >> "${corssnippet}"
# Access-Control-Expose-Headers should not be set on OPTIONS request
if test "${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWCREDENTIALS}" = "true"; then
# CORS specificaiton says that if not true, omit the header entirely
echo " add_header 'Access-Control-Allow-Credentials' '${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWCREDENTIALS}';" >> "${corssnippet}"
Expand All @@ -53,6 +54,10 @@ if test "${EDGEX_SERVICE_CORSCONFIGURATION_ENABLECORS}" = "true"; then
# Always add headers regardless of response code. Omit preflight-related headers (allow-methods, allow-headers, allow-credentials, max-age)
echo " add_header 'Access-Control-Allow-Origin' '${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWEDORIGIN}' always;" >> "${corssnippet}"
echo " add_header 'Access-Control-Expose-Headers' '${EDGEX_SERVICE_CORSCONFIGURATION_CORSEXPOSEHEADERS}' always;" >> "${corssnippet}"
if test "${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWCREDENTIALS}" = "true"; then
# CORS specificaiton says that if not true, omit the header entirely
echo " add_header 'Access-Control-Allow-Credentials' '${EDGEX_SERVICE_CORSCONFIGURATION_CORSALLOWCREDENTIALS}';" >> "${corssnippet}"
fi
echo " add_header 'Vary' 'origin' always;" >> "${corssnippet}"
echo " }" >> "${corssnippet}"
echo "" >> "${corssnippet}"
Expand Down

0 comments on commit 2bac5d1

Please sign in to comment.