Skip to content

Commit 5a67cb7

Browse files
mesudipclaude
andcommitted
fix(analytics): scope real IP to Umami proxy, default to private ranges, log XFF
- Apply set_real_ip_from/real_ip_header only inside the /x/ Umami proxy location, so $remote_addr for all other routes remains the direct peer and no client-supplied header is trusted outside analytics. - Default TRUSTED_PROXY_CIDRS to the private ranges reverse proxies normally connect from (10/8, 172.16/12, 192.168/16, fc00::/7) so the fix works out of the box behind in-cluster ingresses; internet clients cannot spoof these as source addresses. Set TRUSTED_PROXY_CIDRS=none to disable real IP resolution entirely. - Extend the access log format to append xff="$http_x_forwarded_for", recording the proxy-reported client chain verbatim alongside the direct peer address without trusting it. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent bf9edfd commit 5a67cb7

2 files changed

Lines changed: 22 additions & 11 deletions

File tree

govtool/frontend/docker-entrypoint.sh

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,34 @@ TRUSTED_PROXY_CIDRS_VALUE="${TRUSTED_PROXY_CIDRS:-}"
1313
REAL_IP_HEADER_VALUE="${REAL_IP_HEADER:-X-Forwarded-For}"
1414
REAL_IP_CONFIG=""
1515

16+
# Real IP resolution applies only to the Umami proxy location, so
17+
# $remote_addr stays the direct peer everywhere else. Unset means trust
18+
# the private ranges reverse proxies normally connect from; internet
19+
# clients cannot spoof these as a source address. Set
20+
# TRUSTED_PROXY_CIDRS=none to disable real IP resolution entirely.
21+
if [ -z "$TRUSTED_PROXY_CIDRS_VALUE" ]; then
22+
TRUSTED_PROXY_CIDRS_VALUE="10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 fc00::/7"
23+
elif [ "$(printf '%s' "$TRUSTED_PROXY_CIDRS_VALUE" | tr '[:upper:]' '[:lower:]')" = "none" ]; then
24+
TRUSTED_PROXY_CIDRS_VALUE=""
25+
fi
26+
1627
if [ -n "$TRUSTED_PROXY_CIDRS_VALUE" ]; then
1728
if ! printf '%s' "$REAL_IP_HEADER_VALUE" | grep -Eq '^[A-Za-z0-9-]+$'; then
1829
echo "WARNING: Real IP resolution is disabled because REAL_IP_HEADER contains unsupported characters." >&2
1930
else
2031
SET_REAL_IP_FROM_DIRECTIVES=""
2132
for TRUSTED_PROXY_CIDR in $(printf '%s' "$TRUSTED_PROXY_CIDRS_VALUE" | tr ',' ' '); do
2233
if printf '%s' "$TRUSTED_PROXY_CIDR" | grep -Eq '^[0-9A-Fa-f:.]+(/[0-9]{1,3})?$'; then
23-
SET_REAL_IP_FROM_DIRECTIVES="${SET_REAL_IP_FROM_DIRECTIVES} set_real_ip_from ${TRUSTED_PROXY_CIDR};
34+
SET_REAL_IP_FROM_DIRECTIVES="${SET_REAL_IP_FROM_DIRECTIVES} set_real_ip_from ${TRUSTED_PROXY_CIDR};
2435
"
2536
else
2637
echo "WARNING: Ignoring TRUSTED_PROXY_CIDRS entry with unsupported characters: ${TRUSTED_PROXY_CIDR}" >&2
2738
fi
2839
done
2940

3041
if [ -n "$SET_REAL_IP_FROM_DIRECTIVES" ]; then
31-
REAL_IP_CONFIG="${SET_REAL_IP_FROM_DIRECTIVES} real_ip_header ${REAL_IP_HEADER_VALUE};
32-
real_ip_recursive on;"
42+
REAL_IP_CONFIG="${SET_REAL_IP_FROM_DIRECTIVES} real_ip_header ${REAL_IP_HEADER_VALUE};
43+
real_ip_recursive on;"
3344
fi
3445
fi
3546
fi
@@ -124,6 +135,7 @@ upstream umami_backend {
124135
esac
125136

126137
UMAMI_PROXY_CONFIG=" location /x/ {
138+
${REAL_IP_CONFIG}
127139
proxy_pass ${UMAMI_SCHEME}://umami_backend${UMAMI_BASE_PATH}/;
128140
proxy_set_header Host ${UMAMI_AUTHORITY};
129141
proxy_set_header X-Real-IP \$remote_addr;
@@ -167,19 +179,13 @@ mv /tmp/index.html /usr/share/nginx/html/index.html
167179

168180
rm -f /usr/share/nginx/html/index.html.br /usr/share/nginx/html/index.html.gz
169181

170-
awk -v umami_upstream="$UMAMI_UPSTREAM_CONFIG" -v umami_proxy="$UMAMI_PROXY_CONFIG" -v real_ip="$REAL_IP_CONFIG" '
182+
awk -v umami_upstream="$UMAMI_UPSTREAM_CONFIG" -v umami_proxy="$UMAMI_PROXY_CONFIG" '
171183
/# UMAMI_UPSTREAM_CONFIG/ {
172184
if (umami_upstream != "") {
173185
print umami_upstream
174186
}
175187
next
176188
}
177-
/# REAL_IP_CONFIG/ {
178-
if (real_ip != "") {
179-
print real_ip
180-
}
181-
next
182-
}
183189
/# UMAMI_PROXY_CONFIG/ {
184190
if (umami_proxy != "") {
185191
print umami_proxy

govtool/frontend/nginx.conf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# UMAMI_UPSTREAM_CONFIG
22

3+
log_format proxied_combined '$remote_addr - $remote_user [$time_local] '
4+
'"$request" $status $body_bytes_sent '
5+
'"$http_referer" "$http_user_agent" '
6+
'xff="$http_x_forwarded_for"';
7+
38
server {
49
listen 80;
5-
# REAL_IP_CONFIG
10+
access_log /var/log/nginx/access.log proxied_combined;
611
root /usr/share/nginx/html;
712
error_page 503 @maintenance;
813

0 commit comments

Comments
 (0)