From d8cb07d6658850a84f125956d6f058a1fa72de74 Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 27 Feb 2020 00:28:48 +0000 Subject: [PATCH] Bug 24719: Remove use of CGI object in C4::Context::set_remote_address() This patch replaces the CGI "http" object method with its equivalent class method, which doesn't require object instantiation and thus skips global initialization and premature handling of the incoming HTTP request. Test plan: 0. Disable Plack if it is enabled 1. Set koha_trusted_proxies in koha-conf.xml to 1.1.1.1 2. Clear Memcached 3. Try to upload MARCXML file to /cgi-bin/koha/tools/stage-marc-import.pl 4. Note that form below "Upload progress" never appears and errors show in browser console 5. Apply the patch 6. Try to upload MARCXML file to /cgi-bin/koha/tools/stage-marc-import.pl 7. Note that form appears below "Upload progress" Signed-off-by: Nick Clemens Signed-off-by: Marcel de Rooy Signed-off-by: Martin Renvoize Signed-off-by: Joy Nelson (cherry picked from commit 74e3ef126c5992ac049a35c75a7d3879669d7a49) Signed-off-by: Lucas Gass --- C4/Context.pm | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/C4/Context.pm b/C4/Context.pm index fa612472f7..9eea861c34 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -1078,6 +1078,25 @@ sub temporary_directory { return C4::Context->config('tmp_path') || File::Spec->tmpdir; } +=head3 set_remote_address + +set_remote_address should be called at the beginning of every script +that is *not* running under plack in order to the REMOTE_ADDR environment +variable to be set correctly. + +=cut + +sub set_remote_address { + if ( C4::Context->config('koha_trusted_proxies') ) { + require CGI; + my $header = CGI->http('HTTP_X_FORWARDED_FOR'); + + if ($header) { + require Koha::Middleware::RealIP; + $ENV{REMOTE_ADDR} = Koha::Middleware::RealIP::get_real_ip( $ENV{REMOTE_ADDR}, $header ); + } + } +} 1; -- 2.39.5