Bug 16520: Allow per-VirtualHost environment variables with Plack

This allows OVERRIDE_SYSPREF_* and others to work properly.

Test plan:

  1) Add the following line to your plack.psgi (near the bottom, just
     above "mount ..."):
       enable "+Koha::Middleware::Plack";
  2) Load the OPAC advanced search page (under Plack). The title should
    read "Koha online catalog" (or whatever your LibraryName syspref
    contains).
  3) Add the following to your Apache configuration:
       RequestHeader add X-Koha-SetEnv "OVERRIDE_SYSPREF_LibraryName Potato\, Potato"
  4) Restart Apache.
  5) Refresh. The title should now read "Potato, Potato online catalog".

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Brendan Gallagher <brendan@bywatersolutions.com>
This commit is contained in:
Jesse Weaver 2016-05-13 16:27:22 -06:00 committed by Brendan Gallagher
parent 8cb65b367b
commit 1e6df24d6c
3 changed files with 129 additions and 1 deletions

122
Koha/Middleware/SetEnv.pm Normal file
View File

@ -0,0 +1,122 @@
package Koha::Middleware::SetEnv;
# Copyright 2016 ByWater Solutions and the Koha Dev Team
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use Modern::Perl;
use parent qw(Plack::Middleware);
use Plack::Request;
=head1 NAME
Koha::Middleware::SetEnv - Plack middleware to allow SetEnv through proxied headers
=head1 SYNOPSIS
builder {
...
enable "Koha::Middleware::SetEnv";
...
}
SetEnv MEMCACHED_NAMESPACE "localhost:11211"
RequestHeader add X-Koha-SetEnv "MEMCACHED_NAMESPACE localhost:11211"
=head1 DESCRIPTION
This module adds a Plack middleware to convert C<X-Koha-SetEnv> request headers to actual
environment variables.
This is needed because Plackified Koha is currently connected to Apache via an HTTP proxy, and
C<SetEnv>s are not passed through. Koha uses SetEnvs to pass memcached settings and per-virtualhost
styles, search limits and syspref overrides.
=head1 CAVEATS
Due to how HTTP headers are combined, if you want to set a value with an embedded comma, it must be
escaped:
SetEnv OVERRIDE_SYSPREF_LibraryName "The Best, Truly the Best, Koha Library"
RequestHeader add X-Koha-SetEnv "OVERRIDE_SYSPREF_LibraryName The Best\, Truly the Best\, Koha Library"
=head1 NOTES
This system was designed to use a single header for reasons of security. We have no way of knowing
whether a given request header was set by Apache or the original client, so we have to clear any
relevant headers before Apache starts adding them. This is only really practical for a single header
name.
=cut
my $allowed_setenvs = qr/^(
MEMCACHED_SERVERS |
MEMCACHED_NAMESPACE |
OVERRIDE_SYSPREF_(\w+) |
OVERRIDE_SYSPREF_NAMES |
OPAC_CSS_OVERRIDE |
OPAC_SEARCH_LIMIT |
OPAC_LIMIT_OVERRIDE
)\ /x;
sub call {
my ( $self, $env ) = @_;
my $req = Plack::Request->new($env);
# First, we split the result only on unescaped commas.
my @setenv_headers = split /(?<!\\),\s*/, $req->header('X-Koha-SetEnv') || '';
# Then, these need to be mapped to key-value pairs, and commas removed.
my %setenvs = map {
# The syntax of this is a bit awkward because you can't use return inside a map (it
# returns from the enclosing function).
if (!/$allowed_setenvs/) {
warn "Forbidden/incorrect X-Koha-SetEnv: $_";
();
} else {
my ( $key, $value ) = /(\w+) (.*)/;
$value =~ s/\\,/,/g;
( $key, $value );
}
} @setenv_headers;
# Finally, everything is shoved into the $env.
$env = {
%$env,
%setenvs
};
# We also add the MEMCACHED_ settings to the actual environment, to make sure any early
# initialization of Koha::Cache correctly sets up a memcached connection.
foreach my $special_var ( qw( MEMCACHED_SERVERS MEMCACHED_NAMESPACE ) ) {
$ENV{$special_var} = $setenvs{$special_var} if defined $setenvs{$special_var};
}
return $self->app->($env);
}
=head1 AUTHOR
Jesse Weaver, E<lt>jweaver@bywatersolutions.comE<gt>
=cut
1;

View File

@ -24,6 +24,7 @@ use Plack::Builder;
use Plack::App::CGIBin;
use Plack::App::Directory;
use Plack::App::URLMap;
use Plack::Request;
use Mojo::Server::PSGI;
@ -67,11 +68,13 @@ my $apiv1 = builder {
};
builder {
enable "ReverseProxy";
enable "Plack::Middleware::Static";
# + is required so Plack doesn't try to prefix Plack::Middleware::
enable "+Koha::Middleware::SetEnv";
mount '/opac' => $opac;
mount '/intranet' => $intranet;
mount '/api/v1/app.pl' => $apiv1;
};

View File

@ -95,6 +95,9 @@ builder {
path => qr{^/(intranet|opac)-tmpl/},
root => "$ENV{INTRANETDIR}/koha-tmpl/";
# + is required so Plack doesn't try to prefix Plack::Middleware::
enable "+Koha::Middleware::SetEnv";
mount "/cgi-bin/koha" => $app;
mount "/" => $home;