|
Line 0
Link Here
|
|
|
1 |
package Koha::Middleware::SetEnv; |
| 2 |
|
| 3 |
# Copyright 2016 ByWater Solutions and the Koha Dev Team |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use parent qw(Plack::Middleware); |
| 23 |
|
| 24 |
use Plack::Request; |
| 25 |
|
| 26 |
=head1 NAME |
| 27 |
|
| 28 |
Koha::Middleware::SetEnv - Plack middleware to allow SetEnv through proxied headers |
| 29 |
|
| 30 |
=head1 SYNOPSIS |
| 31 |
|
| 32 |
builder { |
| 33 |
... |
| 34 |
enable "Koha::Middleware::SetEnv"; |
| 35 |
... |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
SetEnv MEMCACHED_NAMESPACE "localhost:11211" |
| 40 |
RequestHeader add X-Koha-SetEnv "MEMCACHED_NAMESPACE localhost:11211" |
| 41 |
|
| 42 |
=head1 DESCRIPTION |
| 43 |
|
| 44 |
This module adds a Plack middleware to convert C<X-Koha-SetEnv> request headers to actual |
| 45 |
environment variables. |
| 46 |
|
| 47 |
This is needed because Plackified Koha is currently connected to Apache via an HTTP proxy, and |
| 48 |
C<SetEnv>s are not passed through. Koha uses SetEnvs to pass memcached settings and per-virtualhost |
| 49 |
styles, search limits and syspref overrides. |
| 50 |
|
| 51 |
=head1 CAVEATS |
| 52 |
|
| 53 |
Due to how HTTP headers are combined, if you want to set a value with an embedded comma, it must be |
| 54 |
escaped: |
| 55 |
|
| 56 |
SetEnv OVERRIDE_SYSPREF_LibraryName "The Best, Truly the Best, Koha Library" |
| 57 |
RequestHeader add X-Koha-SetEnv "OVERRIDE_SYSPREF_LibraryName The Best\, Truly the Best\, Koha Library" |
| 58 |
|
| 59 |
=head1 NOTES |
| 60 |
|
| 61 |
This system was designed to use a single header for reasons of security. We have no way of knowing |
| 62 |
whether a given request header was set by Apache or the original client, so we have to clear any |
| 63 |
relevant headers before Apache starts adding them. This is only really practical for a single header |
| 64 |
name. |
| 65 |
|
| 66 |
=cut |
| 67 |
|
| 68 |
my $allowed_setenvs = qr/^( |
| 69 |
MEMCACHED_SERVERS | |
| 70 |
MEMCACHED_NAMESPACE | |
| 71 |
OVERRIDE_SYSPREF_(\w+) | |
| 72 |
OVERRIDE_SYSPREF_NAMES | |
| 73 |
OPAC_CSS_OVERRIDE | |
| 74 |
OPAC_SEARCH_LIMIT | |
| 75 |
OPAC_LIMIT_OVERRIDE |
| 76 |
)\ /x; |
| 77 |
|
| 78 |
sub call { |
| 79 |
my ( $self, $env ) = @_; |
| 80 |
my $req = Plack::Request->new($env); |
| 81 |
|
| 82 |
# First, we split the result only on unescaped commas. |
| 83 |
my @setenv_headers = split /(?<!\\),\s*/, $req->header('X-Koha-SetEnv') || ''; |
| 84 |
|
| 85 |
# Then, these need to be mapped to key-value pairs, and commas removed. |
| 86 |
my %setenvs = map { |
| 87 |
# The syntax of this is a bit awkward because you can't use return inside a map (it |
| 88 |
# returns from the enclosing function). |
| 89 |
if (!/$allowed_setenvs/) { |
| 90 |
warn "Forbidden/incorrect X-Koha-SetEnv: $_"; |
| 91 |
|
| 92 |
(); |
| 93 |
} else { |
| 94 |
my ( $key, $value ) = /(\w+) (.*)/; |
| 95 |
$value =~ s/\\,/,/g; |
| 96 |
|
| 97 |
( $key, $value ); |
| 98 |
} |
| 99 |
} @setenv_headers; |
| 100 |
|
| 101 |
# Finally, everything is shoved into the $env. |
| 102 |
$env = { |
| 103 |
%$env, |
| 104 |
%setenvs |
| 105 |
}; |
| 106 |
|
| 107 |
# We also add the MEMCACHED_ settings to the actual environment, to make sure any early |
| 108 |
# initialization of Koha::Cache correctly sets up a memcached connection. |
| 109 |
foreach my $special_var ( qw( MEMCACHED_SERVERS MEMCACHED_NAMESPACE ) ) { |
| 110 |
$ENV{$special_var} = $setenvs{$special_var} if defined $setenvs{$special_var}; |
| 111 |
} |
| 112 |
|
| 113 |
return $self->app->($env); |
| 114 |
} |
| 115 |
|
| 116 |
=head1 AUTHOR |
| 117 |
|
| 118 |
Jesse Weaver, E<lt>jweaver@bywatersolutions.comE<gt> |
| 119 |
|
| 120 |
=cut |
| 121 |
|
| 122 |
1; |