Bugzilla – Attachment 182844 Details for
Bug 39789
Add ability to specify an alternative header to X-Forwarded-For for finding the real IP address
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 39789: Check requests for the custom header with fallback to X-Forwarded-For
Bug-39789-Check-requests-for-the-custom-header-wit.patch (text/plain), 3.15 KB, created by
Kyle M Hall (khall)
on 2025-05-29 16:17:11 UTC
(
hide
)
Description:
Bug 39789: Check requests for the custom header with fallback to X-Forwarded-For
Filename:
MIME Type:
Creator:
Kyle M Hall (khall)
Created:
2025-05-29 16:17:11 UTC
Size:
3.15 KB
patch
obsolete
>From 030cf301a574dc4ad4fbdaf3e1fe646c98e80370 Mon Sep 17 00:00:00 2001 >From: Kyle M Hall <kyle@bywatersolutions.com> >Date: Tue, 13 May 2025 17:47:15 +0000 >Subject: [PATCH] Bug 39789: Check requests for the custom header with fallback > to X-Forwarded-For > >This patch updates the feature to allow both the custom header and X-Forwarded-For to work harmoniously > >For example, CloudFlare has it's own custom header. >This patch allows a site to be configured for Cloudflare's proxy before the proxy is enabled. >--- > C4/Context.pm | 13 ++++++------- > Koha/Middleware/RealIP.pm | 18 +++++++++--------- > 2 files changed, 15 insertions(+), 16 deletions(-) > >diff --git a/C4/Context.pm b/C4/Context.pm >index 95a33ae3e3d..589554e7f20 100644 >--- a/C4/Context.pm >+++ b/C4/Context.pm >@@ -880,13 +880,12 @@ variable to be set correctly. > sub set_remote_address { > if ( C4::Context->config('koha_trusted_proxies') ) { > require CGI; >- if ( my $reverse_proxy_ip_header = C4::Context->config('reverse_proxy_ip_header') ) { # Updated here >- my $header = CGI->http($reverse_proxy_ip_header); >- >- if ($header) { >- require Koha::Middleware::RealIP; >- $ENV{REMOTE_ADDR} = Koha::Middleware::RealIP::get_real_ip( $ENV{REMOTE_ADDR}, $header ); >- } >+ my $reverse_proxy_ip_header = C4::Context->config('reverse_proxy_ip_header'); >+ my $header = $reverse_proxy_ip_header ? CGI->http($reverse_proxy_ip_header) : undef; >+ $header ||= CGI->http('HTTP_X_FORWARDED_FOR'); # If custom header is not set, fallback to X-Forwarded-For >+ if ($header) { >+ require Koha::Middleware::RealIP; >+ $ENV{REMOTE_ADDR} = Koha::Middleware::RealIP::get_real_ip( $ENV{REMOTE_ADDR}, $header ); > } > } > } >diff --git a/Koha/Middleware/RealIP.pm b/Koha/Middleware/RealIP.pm >index 97394dffe6c..c4bf85184d3 100644 >--- a/Koha/Middleware/RealIP.pm >+++ b/Koha/Middleware/RealIP.pm >@@ -51,15 +51,15 @@ sub call { > my $self = shift; > my $env = shift; > >- my $reverse_proxy_ip_header = C4::Context->config('reverse_proxy_ip_header'); >- if ( $reverse_proxy_ip_header && $env->{$reverse_proxy_ip_header} ) { >- my @trusted_proxy = $self->trusted_proxy ? @{ $self->trusted_proxy } : undef; >- >- if (@trusted_proxy) { >- my $addr = get_real_ip( $env->{REMOTE_ADDR}, $env->{$reverse_proxy_ip_header}, \@trusted_proxy ); >- $ENV{REMOTE_ADDR} = $addr; >- $env->{REMOTE_ADDR} = $addr; >- } >+ # If no reverse_proxy_ip_header, just use HTTP_X_FORWARDED_FOR as the header to check, simplifies logic and is a bit faster that way >+ my $reverse_proxy_ip_header = C4::Context->config('reverse_proxy_ip_header') || 'HTTP_X_FORWARDED_FOR'; >+ >+ # Check the env for the custom header, if the custom header is not set, fall back to HTTP_X_FORWARDED_FOR >+ my $header = $env->{$reverse_proxy_ip_header} || $env->{'HTTP_X_FORWARDED_FOR'}; >+ if ($header) { >+ my $addr = get_real_ip( $env->{REMOTE_ADDR}, $header ); >+ $ENV{REMOTE_ADDR} = $addr; >+ $env->{REMOTE_ADDR} = $addr; > } > > return $self->app->($env); >-- >2.39.5 (Apple Git-154)
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 39789
:
181718
|
181720
|
181915
|
181916
|
181917
|
181918
|
181919
|
181920
|
181921
|
182299
|
182300
|
182395
|
182396
|
182397
|
182841
|
182842
|
182843
|
182844
|
182846
|
182847
|
182848
|
182849
|
183215