@@ -304,10 +304,29 @@ data:
304304 return(pipe);
305305 }
306306
307+ # Match upstream allowlist to supply headers containing real ip
308+ if (client.ip ~ upstream_proxy && req.http.X-Envoy-External-Address) {
309+ set req.http.X-Forwarded-For = req.http.X-Envoy-External-Address;
310+ set req.http.X-Real-IP = req.http.X-Envoy-External-Address;
311+ }
312+ elseif (client.ip ~ upstream_proxy && req.http.X-Forwarded-For) {
313+ set req.http.X-Real-IP = req.http.X-Forwarded-For;
314+ }
315+ elseif (client.ip ~ upstream_proxy && req.http.X-Real-IP) {
316+ set req.http.X-Forwarded-For = req.http.X-Real-IP;
317+ }
318+ else {
319+ set req.http.X-Forwarded-For = client.ip;
320+ set req.http.X-Real-IP = client.ip;
321+ }
322+
323+ # Use first ip from X-Real-IP as parsed ip
324+ set req.http.X-Parsed-IP = regsub(req.http.X-Real-IP, "[, ].*$", "");
325+
307326 # Only allow BAN requests from IP addressees in the 'internal' ACL.
308327 if (req.method == "BAN") {
309328 # Admin port is only exposed to internal network
310- if (!client .ip ~ purge) {
329+ if (!std .ip(req.http.X-Parsed-IP, "0.0.0.0") ~ purge || std.ip(req.http.X-Parsed-IP, "0.0.0.0") ~ upstream_proxy ) {
311330 return (synth(403, "Not allowed."));
312331 }
313332
@@ -331,7 +350,7 @@ data:
331350 # Only allow URIBAN requests from IP addressees in the 'internal' ACL.
332351 if (req.method == "URIBAN") {
333352 # Admin port is only exposed to internal network
334- if (!client .ip ~ purge) {
353+ if (!std .ip(req.http.X-Parsed-IP, "0.0.0.0") ~ purge || std.ip(req.http.X-Parsed-IP, "0.0.0.0") ~ upstream_proxy ) {
335354 return (synth(403, "Not allowed."));
336355 }
337356
@@ -348,24 +367,6 @@ data:
348367 return (synth(200, "Ban added."));
349368 }
350369
351- # Match upstream allowlist to supply headers containing real ip
352- if (client.ip ~ upstream_proxy && req.http.X-Envoy-External-Address) {
353- set req.http.X-Forwarded-For = req.http.X-Envoy-External-Address;
354- set req.http.X-Real-IP = req.http.X-Envoy-External-Address;
355- }
356- elseif (client.ip ~ upstream_proxy && req.http.X-Forwarded-For) {
357- set req.http.X-Forwarded-For = req.http.X-Forwarded-For;
358- set req.http.X-Real-IP = req.http.X-Forwarded-For;
359- }
360- elseif (client.ip ~ upstream_proxy && req.http.X-Real-IP) {
361- set req.http.X-Forwarded-For = req.http.X-Real-IP;
362- set req.http.X-Real-IP = req.http.X-Real-IP;
363- }
364- else {
365- set req.http.X-Forwarded-For = client.ip;
366- set req.http.X-Real-IP = client.ip;
367- }
368-
369370 # Only deal with "normal" types
370371 if (req.method != "GET"
371372 && req.method != "HEAD"
@@ -434,7 +435,7 @@ data:
434435 return (synth( 403, "Forbidden"));
435436 }
436437
437- if (req.http.Authorization || client .ip ~ internal) {
438+ if (req.http.Authorization || ( std .ip(req.http.X-Parsed-IP, "0.0.0.0") ~ internal && std.ip(req.http.X-Parsed-IP, "0.0.0.0") !~ upstream_proxy ) ) {
438439 # Not cacheable by default
439440 return (pass);
440441 }
@@ -460,7 +461,7 @@ data:
460461 }
461462
462463 # Do not allow public access to cron.php , update.php or install.php.
463- if (req.url ~ "^(?:/core)?/(?:cron|install|update)\.php$" && !client .ip ~ internal) {
464+ if (req.url ~ "^(?:/core)?/(?:cron|install|update)\.php$" && (!std .ip(req.http.X-Parsed-IP, "0.0.0.0") ~ internal || std.ip(req.http.X-Parsed-IP, "0.0.0.0") ~ upstream_proxy) ) {
464465 # Have Varnish throw the error directly.
465466 return (synth( 404, "Page not found."));
466467 }
0 commit comments