Skip to content

Commit b6d11ca

Browse files
authored
Merge pull request #740 from aledbf/fix-allow-deny
Refactoring whitelist source IP verification
2 parents c5e3097 + 07cdee5 commit b6d11ca

File tree

1 file changed

+22
-4
lines changed
  • controllers/nginx/rootfs/etc/nginx/template

1 file changed

+22
-4
lines changed

controllers/nginx/rootfs/etc/nginx/template/nginx.tmpl

+22-4
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,24 @@ http {
231231
}
232232
{{ end }}
233233

234+
{{/* build the maps that will be use to validate the Whitelist */}}
235+
{{ range $index, $server := .Servers }}
236+
{{ range $location := $server.Locations }}
237+
{{ $path := buildLocation $location }}
238+
239+
{{ if isLocationAllowed $location }}
240+
{{ if gt (len $location.Whitelist.CIDR) 0 }}
241+
geo $the_real_ip $deny_{{ $server.Hostname }}_{{ $path }} {
242+
default 1;
243+
244+
{{ range $ip := $location.Whitelist.CIDR }}
245+
{{ $ip }} 0;{{ end }}
246+
}
247+
{{ end }}
248+
{{ end }}
249+
{{ end }}
250+
{{ end }}
251+
234252
{{/* build all the required rate limit zones. Each annotation requires a dedicated zone */}}
235253
{{/* 1MB -> 16 thousand 64-byte states or about 8 thousand 128-byte states */}}
236254
{{ range $zone := (buildRateLimitZones .Servers) }}
@@ -312,9 +330,9 @@ http {
312330

313331
{{ if isLocationAllowed $location }}
314332
{{ if gt (len $location.Whitelist.CIDR) 0 }}
315-
{{ range $ip := $location.Whitelist.CIDR }}
316-
allow {{ $ip }};{{ end }}
317-
deny all;
333+
if ($deny_{{ $server.Hostname }}_{{ $path }}) {
334+
return 403;
335+
}
318336
{{ end }}
319337

320338
port_in_redirect {{ if $location.UsePortInRedirects }}on{{ else }}off{{ end }};
@@ -362,7 +380,7 @@ http {
362380
{{ end }}
363381

364382
# Pass Real IP
365-
proxy_set_header X-Real-IP $remote_addr;
383+
proxy_set_header X-Real-IP $the_real_ip;
366384

367385
# Allow websocket connections
368386
proxy_set_header Upgrade $http_upgrade;

0 commit comments

Comments
 (0)