Skip to content

Commit a2dd4e5

Browse files
kkimurakchodorenko
andcommitted
Accept multiple hosts for RACK_ATTACK_WHITELIST
see sameersbn#2828 The current setup also accepts multiple hosts, but the syntax is a bit strange. The leading/trailing double quotes are embedded in the configuration file itself, so users should expect double quotes around the string they set. In other words, when setting two hosts 0.0.0.0 and 1.1.1.1, you will set the strings 0.0.0.0","1.1.1.1 in the environment variables. This is not intuitive. This commit removes double quote around corresponding config and set backward compatibility fallback process to surround whole with [], each host with double quote. Also, validation script (written in ruby) will be executed during configuration. Example docker-compose.yml ````yaml services: gitlab: image: sameersbn/gitlab:latest environment: - RACK_ATTACK_WHITELIST='["127.0.0.1","0.0.0.0"]' ```` Co-authored-by: Mikhail Khadarenka <[email protected]>
1 parent 52e1461 commit a2dd4e5

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2381,7 +2381,19 @@ Enable/disable rack middleware for blocking & throttling abusive requests Defaul
23812381

23822382
##### `RACK_ATTACK_WHITELIST`
23832383

2384-
Always allow requests from whitelisted host. Defaults to `127.0.0.1`
2384+
Always allow requests from whitelisted host.
2385+
This should be a valid yaml sequence of host address. Each host address string must be a valid IP address that can be passed to `IPAddr.new` of ruby. See [ruby-lang reference](https://docs.ruby-lang.org/en/3.0/IPAddr.html#method-c-new) for detail.
2386+
If you need to set multiple hosts, set this parameter like `["1.1.1.1","192.168.0.0/24"]` for example. In docker-compose.yml, you have to quote whole value like below:
2387+
2388+
````yaml
2389+
environment:
2390+
# pattern 1: surround with single quote, double quote each IP address
2391+
- RACK_ATTACK_WHITELIST='["1.1.1.1","192.168.0.0/24"]'
2392+
# pattern 2: surround with double quote, single quote each IP address
2393+
- RACK_ATTACK_WHITELIST="['1.1.1.1','192.168.0.0/24']"
2394+
````
2395+
2396+
Defaults to `["127.0.0.1"]`
23852397

23862398
##### `RACK_ATTACK_MAXRETRY`
23872399

assets/runtime/config/gitlabhq/gitlab.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ production: &base
12311231
enabled: {{RACK_ATTACK_ENABLED}}
12321232
#
12331233
# Whitelist requests from 127.0.0.1 for web proxies (NGINX/Apache) with incorrect headers
1234-
ip_whitelist: ["{{RACK_ATTACK_WHITELIST}}"]
1234+
ip_whitelist: {{RACK_ATTACK_WHITELIST}}
12351235
#
12361236
# Limit the number of Git HTTP authentication attempts per IP
12371237
maxretry: {{RACK_ATTACK_MAXRETRY}}

assets/runtime/env-defaults

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,31 @@ PIWIK_SITE_ID=${PIWIK_SITE_ID:-}
544544

545545
## RACK ATTACK
546546
RACK_ATTACK_ENABLED=${RACK_ATTACK_ENABLED:-true}
547-
RACK_ATTACK_WHITELIST=${RACK_ATTACK_WHITELIST:-"127.0.0.1"}
547+
RACK_ATTACK_WHITELIST=${RACK_ATTACK_WHITELIST:-'["127.0.0.1"]'}
548+
RACK_ATTACK_WHITELIST=${RACK_ATTACK_WHITELIST// /}
549+
# Backward compatibility : See sameersbn/docker-gitlab#2828
550+
# Pre-check: each host is surrounded by single / double quotation
551+
# if not, generated string will be [127.0.0.1] for example and ruby raises error
552+
RACK_ATTACK_WHITELIST_ORIGIN=${RACK_ATTACK_WHITELIST}
553+
# remove [], then iterate entries
554+
RACK_ATTACK_WHITELIST=${RACK_ATTACK_WHITELIST#"["}
555+
RACK_ATTACK_WHITELIST=${RACK_ATTACK_WHITELIST%"]"}
556+
IFS_ORG=${IFS}
557+
IFS=,
558+
for host in ${RACK_ATTACK_WHITELIST}; do
559+
# Both single / double quotation may be used
560+
if ! [[ ${host} =~ ^(\"|\').*(\"|\')$ ]]; then
561+
RACK_ATTACK_WHITELIST=${RACK_ATTACK_WHITELIST/${host}/\"${host//(\'|\")/}\"}
562+
fi
563+
done
564+
IFS=$IFS_ORG
565+
# surround with []
566+
RACK_ATTACK_WHITELIST="[${RACK_ATTACK_WHITELIST}]"
567+
if [[ "${RACK_ATTACK_WHITELIST}" != "${RACK_ATTACK_WHITELIST_ORIGIN}" ]]; then
568+
printf "[warning] RACK_ATTACK_WHITELIST must be a yaml sequence of hosts.\nFixing from %s to %s\n" \
569+
"${RACK_ATTACK_WHITELIST_ORIGIN}" \
570+
"${RACK_ATTACK_WHITELIST}"
571+
fi
548572
RACK_ATTACK_MAXRETRY=${RACK_ATTACK_MAXRETRY:-10}
549573
RACK_ATTACK_FINDTIME=${RACK_ATTACK_FINDTIME:-60}
550574
RACK_ATTACK_BANTIME=${RACK_ATTACK_BANTIME:-3600}

assets/runtime/functions

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,24 @@ gitlab_configure_analytics() {
10681068

10691069
gitlab_configure_rack_attack() {
10701070
echo "Configuring gitlab::rack_attack..."
1071+
1072+
# validity check : RACK_ATTACK_WHITELIST should be an array of valid IP Address string
1073+
echo " Validating RACK_ATTACK_WHITELIST..."
1074+
/usr/bin/env ruby << SCRIPT
1075+
require 'ipaddr'
1076+
${RACK_ATTACK_WHITELIST}.each do |host|
1077+
begin
1078+
printf(" input=%s, to_range=%s\n", host, IPAddr.new(host).to_range)
1079+
rescue IPAddr::InvalidAddressError => e
1080+
p e
1081+
exit 1
1082+
rescue => e
1083+
put "Unexpected error", e
1084+
exit 1
1085+
end
1086+
end
1087+
SCRIPT
1088+
10711089
update_template ${GITLAB_CONFIG} \
10721090
RACK_ATTACK_ENABLED \
10731091
RACK_ATTACK_WHITELIST \

0 commit comments

Comments
 (0)