Skip to content

Commit 940c9c5

Browse files
danielcomptondependabot[bot]4141done
authored
feature: add CORS_ALLOW_PRIVATE_NETWORK_ACCESS env var (#181)
feature: add CORS_ALLOW_PRIVATE_NETWORK_ACCESS env var This is needed to be able to access internal IP ranges from a publicly available website, e.g. sourcemaps. https://developer.chrome.com/blog/private-network-access-preflight/ --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Javier Evans <[email protected]>
1 parent 8eb0529 commit 940c9c5

File tree

8 files changed

+26
-0
lines changed

8 files changed

+26
-0
lines changed

Dockerfile.buildkit.plus

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ENV PROXY_CACHE_VALID_OK "1h"
1212
ENV PROXY_CACHE_VALID_NOTFOUND "1m"
1313
ENV PROXY_CACHE_VALID_FORBIDDEN "30s"
1414
ENV CORS_ENABLED 0
15+
ENV CORS_ALLOW_PRIVATE_NETWORK_ACCESS ""
1516
ENV DIRECTORY_LISTING_PATH_PREFIX ""
1617
ENV STRIP_LEADING_DIRECTORY_PATH ""
1718
ENV PREFIX_LEADING_DIRECTORY_PATH ""

Dockerfile.oss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ ENV PROXY_CACHE_VALID_OK "1h"
1111
ENV PROXY_CACHE_VALID_NOTFOUND "1m"
1212
ENV PROXY_CACHE_VALID_FORBIDDEN "30s"
1313
ENV CORS_ENABLED 0
14+
ENV CORS_ALLOW_PRIVATE_NETWORK_ACCESS ""
1415
ENV DIRECTORY_LISTING_PATH_PREFIX ""
1516
ENV STRIP_LEADING_DIRECTORY_PATH ""
1617
ENV PREFIX_LEADING_DIRECTORY_PATH ""

Dockerfile.plus

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ENV PROXY_CACHE_VALID_OK "1h"
1212
ENV PROXY_CACHE_VALID_NOTFOUND "1m"
1313
ENV PROXY_CACHE_VALID_FORBIDDEN "30s"
1414
ENV CORS_ENABLED 0
15+
ENV CORS_ALLOW_PRIVATE_NETWORK_ACCESS ""
1516
ENV DIRECTORY_LISTING_PATH_PREFIX ""
1617
ENV STRIP_LEADING_DIRECTORY_PATH ""
1718
ENV PREFIX_LEADING_DIRECTORY_PATH ""

common/docker-entrypoint.d/00-check-for-required-env.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,4 @@ echo "Append slash for directory enabled: ${APPEND_SLASH_FOR_POSSIBLE_DIRECTORY}
136136
echo "Stripping the following headers from responses: x-amz-;${HEADER_PREFIXES_TO_STRIP}"
137137
echo "Allow the following headers from responses (these take precendence over the above): ${HEADER_PREFIXES_ALLOWED}"
138138
echo "CORS Enabled: ${CORS_ENABLED}"
139+
echo "CORS Allow Private Network Access: ${CORS_ALLOW_PRIVATE_NETWORK_ACCESS}"

common/docker-entrypoint.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ if [ -z "${CORS_ALLOWED_ORIGIN+x}" ]; then
6868
export CORS_ALLOWED_ORIGIN="*"
6969
fi
7070

71+
# See documentation for this feature. We do not parse this as a boolean
72+
# since "true" and "false" are the required values of the header this populates
73+
if [ "${CORS_ALLOW_PRIVATE_NETWORK_ACCESS}" != "true" ] && [ "${CORS_ALLOW_PRIVATE_NETWORK_ACCESS}" != "false" ]; then
74+
export CORS_ALLOW_PRIVATE_NETWORK_ACCESS=""
75+
fi
76+
7177
# This is the primary logic to determine the s3 host used for the
7278
# upstream (the actual proxying action) as well as the `Host` header
7379
#

common/etc/nginx/templates/gateway/cors.conf.template

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ if ($request_cors = "OPTIONS_1") {
1111
# Tell client that this pre-flight info is valid for 20 days
1212
#
1313
add_header 'Access-Control-Max-Age' 1728000;
14+
#
15+
# Allow/deny Private Network Access CORS requests.
16+
# https://developer.chrome.com/blog/private-network-access-preflight/
17+
#
18+
add_header 'Access-Control-Allow-Private-Network' '${CORS_ALLOW_PRIVATE_NETWORK_ACCESS}';
19+
1420
add_header 'Content-Type' 'text/plain; charset=utf-8';
1521
add_header 'Content-Length' 0;
1622
return 204;

docs/getting_started.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ running as a Container or as a Systemd service.
4747
| `CORS_ALLOWED_ORIGIN` | No | | | Value to set to be returned from the CORS `Access-Control-Allow-Origin` header. This value is only used if CORS is enabled. (default: \*) |
4848
| `STRIP_LEADING_DIRECTORY_PATH` | No | | | Removes a portion of the path in the requested URL (if configured). Useful when deploying to an ALB under a folder (eg. www.mysite.com/somepath). |
4949
| `PREFIX_LEADING_DIRECTORY_PATH` | No | | | Prefix to prepend to all S3 object paths. Useful to serve only a subset of an S3 bucket. When used in combination with `STRIP_LEADING_DIRECTORY_PATH`, this allows the leading path to be replaced, rather than just removed. |
50+
| `CORS_ALLOW_PRIVATE_NETWORK_ACCESS` | No | `true`, `false` | | Flag that enables responding to the CORS OPTIONS pre-flight request header `Access-Control-Request-Private-Network` with the `Access-Control-Allow-Private-Network` header. If the value is "true", responds with "true", if "false" responds with "false". If the environment variable is blank/not set, does not respond with any header. This value is only used if CORS is enabled. See [Private Network Access: introducing preflights](https://developer.chrome.com/blog/private-network-access-preflight/) for more information about this header. |
5051

5152

5253

standalone_ubuntu_oss_install.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ echo "Proxy Caching Time for Valid Response: ${PROXY_CACHE_VALID_OK}"
9797
echo "Proxy Caching Time for Not Found Response: ${PROXY_CACHE_VALID_NOTFOUND}"
9898
echo "Proxy Caching Time for Forbidden Response: ${PROXY_CACHE_VALID_FORBIDDEN}"
9999
echo "CORS Enabled: ${CORS_ENABLED}"
100+
echo "CORS Allow Private Network Access: ${CORS_ALLOW_PRIVATE_NETWORK_ACCESS}"
100101

101102
set -o nounset # abort on unbound variable
102103

@@ -230,12 +231,20 @@ fi
230231

231232
set -o nounset # abort on unbound variable
232233

234+
235+
# CORS related variable setup
233236
if [ -z "${CORS_ALLOWED_ORIGIN+x}" ]; then
234237
CORS_ALLOWED_ORIGIN="*"
235238
fi
236239

240+
if [ "${CORS_ALLOW_PRIVATE_NETWORK_ACCESS:-}" != "true" ] && [ "${CORS_ALLOW_PRIVATE_NETWORK_ACCESS:-}" != "false" ]; then
241+
CORS_ALLOW_PRIVATE_NETWORK_ACCESS=""
242+
fi
243+
244+
237245
cat >> "/etc/nginx/environment" << EOF
238246
CORS_ALLOWED_ORIGIN=${CORS_ALLOWED_ORIGIN}
247+
CORS_ALLOW_PRIVATE_NETWORK_ACCESS=${CORS_ALLOW_PRIVATE_NETWORK_ACCESS}
239248
EOF
240249

241250
# Only include these env vars if we are not using a instance profile credential

0 commit comments

Comments
 (0)