Skip to content

Commit 71b2dd0

Browse files
committed
feat: allow access to forbidden location to specified user agents
1 parent d410259 commit 71b2dd0

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ The entrypoint file contains a list of environment variables that will be replac
104104
- `NGINX_CLIENT_MAX_BODY_SIZE`: the maximum allowed size for the client request body (default: `200M`)
105105
- `NGINX_CORS_ENABLED`: enable cors for `/` path and the caller origin header represented by `$http_origin` nginx variable (<https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin>) (default: `0`)
106106
- `NGINX_CORS_DOMAINS`: a list of CORS enabled domains to activate cors just for the specified ones (no default provided)
107+
- `NGINX_FORBIDDEN_LOCATIONS_ALLOWED_USER_AGENTS`: a regexp, used as `startswith`, to match the valid user agents that can call the forbidden locations and obtain a simple `return 200` (default `(kube-probe)`)
107108

108109
## Rootless feature
109110

Diff for: docker-entrypoint.sh

+9
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,18 @@ if [ -n "${NGINX_BASIC_AUTH_USER}" ] && [ -n "${NGINX_BASIC_AUTH_PASS}" ]; then
7373
fi
7474

7575
# Activate the forbidden locations when the environment is not local
76+
NGINX_FORBIDDEN_LOCATIONS_ALLOWED_USER_AGENTS=${NGINX_FORBIDDEN_LOCATIONS_ALLOWED_USER_AGENTS:-"(kube-probe)"}
77+
export NGINX_FORBIDDEN_LOCATIONS_ALLOWED_USER_AGENTS
7678
if [ "${ENV:-}" != "loc" ]; then
7779
print "Activating the forbidden locations"
7880
cp /templates/fragments/005-forbidden-locations.conf /etc/nginx/conf.d/fragments/005-forbidden-locations.conf
81+
82+
if [ -n "${NGINX_FORBIDDEN_LOCATIONS_ALLOWED_USER_AGENTS}" ]; then
83+
# shellcheck disable=SC2016 # The envsubst command needs to be executed without variable expansion
84+
envsubst '${NGINX_FORBIDDEN_LOCATIONS_ALLOWED_USER_AGENTS}' < /templates/fragments/005-forbidden-locations-user-agents-fragment.conf.tpl > /templates/fragments/005-forbidden-locations-user-agents-fragment.conf
85+
sed -e '/#forbidden-locations-allowed-user-agents/r /templates/fragments/005-forbidden-locations-user-agents-fragment.conf' -i /etc/nginx/conf.d/fragments/005-forbidden-locations.conf
86+
fi
87+
sed -i '/#forbidden-locations-allowed-user-agents/d' /etc/nginx/conf.d/fragments/005-forbidden-locations.conf
7988
fi
8089

8190
# Activate HSTS header (default: off)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if ($http_user_agent ~ ^${NGINX_FORBIDDEN_LOCATIONS_ALLOWED_USER_AGENTS}) {
2+
return 200;
3+
}

Diff for: templates/fragments/005-forbidden-locations.conf

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
location = /core/install.php {
2+
#forbidden-locations-allowed-user-agents
23
return 404;
34
}
45

56
location = /update.php {
7+
#forbidden-locations-allowed-user-agents
68
return 404;
79
}

0 commit comments

Comments
 (0)