Skip to content

Commit bd97500

Browse files
committed
Nginx: add NGINX_CORS_RESOURCE_POLICY support
1 parent f67b16c commit bd97500

File tree

14 files changed

+70
-3
lines changed

14 files changed

+70
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Images with `nginx` can be configured using the following environment variables.
1717
| Environment Key | Applied | Description | Default |
1818
|-----------------|---------|-------------|---------|
1919
| NGINX_CORS_ORIGINS | Every run | Comma separated list of hostnames (without `https://`) | `*` |
20+
| NGINX_CORS_RESOURCE_POLICY | Every run | `Cross-Origin-Resource-Policy` value | `same-origin` |
2021

2122
### Paths
2223

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
include /etc/nginx/snippets/headers/security-base.conf;
22

33
# Prevent loading resources from other pages
4-
add_header 'Cross-Origin-Resource-Policy' 'same-origin';
4+
add_header 'Cross-Origin-Resource-Policy' $cors_resource_policy;
55

66
# Prevent indexing
77
add_header 'X-Robots-Tag' 'none';

common/config/nginx/snippets/headers/security-web-content.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ include /etc/nginx/snippets/headers/security-base.conf;
1414
add_header 'Cross-Origin-Opener-Policy' 'same-origin';
1515

1616
# Prevent loading resources from other pages
17-
add_header 'Cross-Origin-Resource-Policy' 'same-origin';
17+
add_header 'Cross-Origin-Resource-Policy' $cors_resource_policy;
1818

1919
# No tracking
2020
add_header 'Permissions-Policy' 'interest-cohort=()';

common/scripts/startup/50-env-configure-nginx-cors.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ set -euo pipefail
66
#
77
# Inputs:
88
# - NGINX_CORS_ORIGINS: defaults to '*'
9+
# - NGINX_CORS_RESOURCE_POLICY: defaults to 'same-origin'
910

1011
# Set defaults & clean up (normalize, trim, …)
1112
NGINX_CONFIG_FILE=/etc/nginx/snippets/vars/cors-origin.conf
1213
NGINX_CORS_ORIGINS=$(echo "${NGINX_CORS_ORIGINS:-*}" \
1314
| sed 's/,/ /g; s/^ *//; s/ *$//; s/ */ /g')
15+
NGINX_CORS_RESOURCE_POLICY="${NGINX_CORS_RESOURCE_POLICY:-same-origin}"
1416

1517
# Check nginx structure
1618
if [ ! -f "${NGINX_CONFIG_FILE}" ]; then
@@ -22,7 +24,7 @@ if [ "$NGINX_CORS_ORIGINS" = "*" ]; then
2224
echo "Nginx: configuring CORS origin with wildcard (allow all)…"
2325
cat <<EOF > "${NGINX_CONFIG_FILE}"
2426
# Allow all origins
25-
map "\$http_origin" \$cors_origin {
27+
map "" \$cors_origin {
2628
default "*";
2729
}
2830
EOF
@@ -53,3 +55,12 @@ map "\$request_method:\$cors_origin" \$cors_preflight {
5355
default "";
5456
}
5557
EOF
58+
59+
# Variables used for resource `Cross-Origin-Resource-Policy`
60+
echo "Nginx: configuring CORS resource policy with '${NGINX_CORS_RESOURCE_POLICY}'…"
61+
cat <<EOF >> "${NGINX_CONFIG_FILE}"
62+
# Resource policy
63+
map "" \$cors_resource_policy {
64+
default "${NGINX_CORS_RESOURCE_POLICY}";
65+
}
66+
EOF
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
NGINX_CORS_ORIGINS=apple.com,google.com
2+
NGINX_CORS_RESOURCE_POLICY=cross-origin

tests/nuxt-base/secure/cors-origins/goss.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ snippets:
2020
url: http://localhost:8080/style.css
2121

2222
http:
23+
cors index:
24+
body:
25+
- Hey from index.mjs
26+
headers:
27+
- 'Cross-Origin-Resource-Policy: cross-origin'
28+
status: 200
29+
url: http://localhost:8080/
30+
2331
# Normal requests
2432
cors api apple:
2533
<<: *cors-api
@@ -40,13 +48,15 @@ http:
4048
headers:
4149
- 'Access-Control-Allow-Origin: https://apple.com'
4250
- 'Access-Control-Allow-Credentials: true'
51+
- 'Cross-Origin-Resource-Policy: cross-origin'
4352
request-headers:
4453
- 'Origin: https://apple.com'
4554
cors resource google:
4655
<<: *cors-resource
4756
headers:
4857
- 'Access-Control-Allow-Origin: https://google.com'
4958
- 'Access-Control-Allow-Credentials: true'
59+
- 'Cross-Origin-Resource-Policy: cross-origin'
5060
request-headers:
5161
- 'Origin: https://google.com'
5262

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
NGINX_CORS_ORIGINS=apple.com,google.com
2+
NGINX_CORS_RESOURCE_POLICY=cross-origin

tests/nuxt-base/unsecured/cors-origins/goss.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ snippets:
2020
url: http://localhost:8080/style.css
2121

2222
http:
23+
cors index:
24+
body:
25+
- Hey from index.mjs
26+
headers:
27+
- 'Cross-Origin-Resource-Policy: cross-origin'
28+
status: 200
29+
url: http://localhost:8080/
30+
2331
# Normal requests
2432
cors api apple:
2533
<<: *cors-api
@@ -40,13 +48,15 @@ http:
4048
headers:
4149
- 'Access-Control-Allow-Origin: https://apple.com'
4250
- 'Access-Control-Allow-Credentials: true'
51+
- 'Cross-Origin-Resource-Policy: cross-origin'
4352
request-headers:
4453
- 'Origin: https://apple.com'
4554
cors resource google:
4655
<<: *cors-resource
4756
headers:
4857
- 'Access-Control-Allow-Origin: https://google.com'
4958
- 'Access-Control-Allow-Credentials: true'
59+
- 'Cross-Origin-Resource-Policy: cross-origin'
5060
request-headers:
5161
- 'Origin: https://google.com'
5262

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
NGINX_CORS_ORIGINS=apple.com,google.com
2+
NGINX_CORS_RESOURCE_POLICY=cross-origin

tests/payload-base/final/cors-origins/goss.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ snippets:
2020
url: http://localhost:8080/style.css
2121

2222
http:
23+
cors index:
24+
body:
25+
- Hey from server.js
26+
headers:
27+
- 'Cross-Origin-Resource-Policy: cross-origin'
28+
status: 200
29+
url: http://localhost:8080/
30+
2331
# Normal requests
2432
cors api apple:
2533
<<: *cors-api
@@ -40,13 +48,15 @@ http:
4048
headers:
4149
- 'Access-Control-Allow-Origin: https://apple.com'
4250
- 'Access-Control-Allow-Credentials: true'
51+
- 'Cross-Origin-Resource-Policy: cross-origin'
4352
request-headers:
4453
- 'Origin: https://apple.com'
4554
cors resource google:
4655
<<: *cors-resource
4756
headers:
4857
- 'Access-Control-Allow-Origin: https://google.com'
4958
- 'Access-Control-Allow-Credentials: true'
59+
- 'Cross-Origin-Resource-Policy: cross-origin'
5060
request-headers:
5161
- 'Origin: https://google.com'
5262

0 commit comments

Comments
 (0)