Bugzilla – Attachment 191622 Details for
Bug 38365
Add Content-Security-Policy HTTP header to HTML responses
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 38365: Add Koha::Middleware::ContentSecurityPolicy
Bug-38365-Add-KohaMiddlewareContentSecurityPolicy.patch (text/plain), 11.54 KB, created by
Lari Taskula
on 2026-01-19 09:38:26 UTC
(
hide
)
Description:
Bug 38365: Add Koha::Middleware::ContentSecurityPolicy
Filename:
MIME Type:
Creator:
Lari Taskula
Created:
2026-01-19 09:38:26 UTC
Size:
11.54 KB
patch
obsolete
>From 5340d448b6c2a3762eba509c93408738bcc1fb43 Mon Sep 17 00:00:00 2001 >From: Lari Taskula <lari.taskula@hypernova.fi> >Date: Wed, 26 Nov 2025 22:24:37 +0200 >Subject: [PATCH] Bug 38365: Add Koha::Middleware::ContentSecurityPolicy > >This patch adds a new Perl package, Koha::Middleware::ContentSecurityPolicy. > >It is called for each Plack requests, and its job is to add the >Content-Security-Policy[-Report-Only] header with the configured value. > >To test: >1. prove t/Koha/Middleware/ContentSecurityPolicy.t >2. prove t/db_dependent/Koha/Middleware/ContentSecurityPolicy.t >--- > Koha/Middleware/ContentSecurityPolicy.pm | 56 ++++++ > t/Koha/Middleware/ContentSecurityPolicy.t | 96 ++++++++++ > .../Koha/Middleware/ContentSecurityPolicy.t | 165 ++++++++++++++++++ > 3 files changed, 317 insertions(+) > create mode 100644 Koha/Middleware/ContentSecurityPolicy.pm > create mode 100755 t/Koha/Middleware/ContentSecurityPolicy.t > create mode 100755 t/db_dependent/Koha/Middleware/ContentSecurityPolicy.t > >diff --git a/Koha/Middleware/ContentSecurityPolicy.pm b/Koha/Middleware/ContentSecurityPolicy.pm >new file mode 100644 >index 00000000000..2f61747209a >--- /dev/null >+++ b/Koha/Middleware/ContentSecurityPolicy.pm >@@ -0,0 +1,56 @@ >+package Koha::Middleware::ContentSecurityPolicy; >+ >+# 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 <https://www.gnu.org/licenses>. >+ >+use Modern::Perl; >+ >+use parent qw(Plack::Middleware); >+use Plack::Request; >+use Plack::Response; >+ >+use Koha::ContentSecurityPolicy; >+ >+=head1 METHODS >+ >+=head2 call >+ >+This method is called for each request, and adds the Content-Security-Policy or >+the Content-Security-Policy-Report-Only header based on your CSP configuration in >+koha-conf.xml. >+ >+=cut >+ >+sub call { >+ my ( $self, $env ) = @_; >+ >+ my $req = Plack::Request->new($env); >+ >+ my $csp = Koha::ContentSecurityPolicy->new; >+ >+ my $res = $self->app->($env); >+ return $res unless $csp->is_enabled; >+ >+ return Plack::Util::response_cb( >+ $res, >+ sub { >+ my $res = shift; >+ my $headers = $res->[1]; >+ push @$headers, ( $csp->header_name => $csp->header_value ); >+ } >+ ); >+} >+ >+1; >diff --git a/t/Koha/Middleware/ContentSecurityPolicy.t b/t/Koha/Middleware/ContentSecurityPolicy.t >new file mode 100755 >index 00000000000..a03b3a7cdf4 >--- /dev/null >+++ b/t/Koha/Middleware/ContentSecurityPolicy.t >@@ -0,0 +1,96 @@ >+#!/usr/bin/perl >+ >+# >+# Copyright 2025 Hypernova Oy >+# >+# 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 <https://www.gnu.org/licenses>. >+ >+use strict; >+use warnings; >+use Test::NoWarnings; >+use Test::More tests => 3; >+use Test::Warn; >+ >+use HTTP::Request::Common; >+use Plack::Builder; >+use Plack::Util; >+use Plack::Test; >+ >+use t::lib::Mocks; >+ >+use_ok("Koha::Middleware::ContentSecurityPolicy"); >+ >+my $conf_csp_section = 'content_security_policy'; >+ >+subtest 'test Content-Security-Policy header' => sub { >+ plan tests => 3; >+ >+ my $test_nonce = 'TEST_NONCE'; >+ my $csp_header_value = >+ "default-src 'self'; script-src 'self' 'nonce-_CSP_NONCE_'; style-src 'self' 'nonce-_CSP_NONCE_'; img-src 'self' data:; font-src 'self'; object-src 'none'"; >+ >+ t::lib::Mocks::mock_config( >+ $conf_csp_section, >+ { opac => { csp_mode => 'enabled', csp_header_value => $csp_header_value } } >+ ); >+ >+ # Set nonce >+ Koha::ContentSecurityPolicy->new->nonce($test_nonce); >+ >+ my $env = {}; >+ my $app = sub { >+ my $resp = [ >+ 200, >+ [ >+ 'Content-Type', >+ 'text/plain', >+ 'Content-Length', >+ 12 >+ ], >+ [ Koha::ContentSecurityPolicy->new->nonce ] >+ ]; >+ return $resp; >+ }; >+ >+ $app = builder { >+ enable "+Koha::Middleware::ContentSecurityPolicy"; >+ $app; >+ }; >+ >+ my $test = Plack::Test->create($app); >+ my $res = $test->request( GET "/" ); >+ my $expected_csp_header_value = $csp_header_value; >+ $expected_csp_header_value =~ s/_CSP_NONCE_/$test_nonce/g; >+ is( >+ $res->header('content-security-policy'), $expected_csp_header_value, >+ "Response contains Content-Security-Policy header with the expected value" >+ ); >+ is( >+ $res->content, $test_nonce, >+ "Response contains generated nonce in the body" >+ ); >+ >+ t::lib::Mocks::mock_config( >+ $conf_csp_section, >+ { opac => { csp_mode => '', csp_header_value => $csp_header_value } } >+ ); >+ >+ $res = $test->request( GET "/" ); >+ is( >+ $res->header('content-security-policy'), undef, >+ "Response does not contain Content-Security-Policy header when it is disabled in koha-conf.xml" >+ ); >+}; >diff --git a/t/db_dependent/Koha/Middleware/ContentSecurityPolicy.t b/t/db_dependent/Koha/Middleware/ContentSecurityPolicy.t >new file mode 100755 >index 00000000000..be8fc76da36 >--- /dev/null >+++ b/t/db_dependent/Koha/Middleware/ContentSecurityPolicy.t >@@ -0,0 +1,165 @@ >+#!/usr/bin/perl >+ >+# >+# Copyright 2025 Hypernova Oy >+# >+# 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 <https://www.gnu.org/licenses>. >+ >+use strict; >+use warnings; >+use Test::NoWarnings; >+use Test::More tests => 4; >+ >+use HTTP::Request::Common; >+use Plack::App::CGIBin; >+use Plack::Builder; >+use Plack::Test; >+ >+use t::lib::Mocks; >+ >+use_ok("Koha::Middleware::ContentSecurityPolicy"); >+ >+my $conf_csp_section = 'content_security_policy'; >+ >+subtest 'test CSP in OPAC' => sub { >+ plan tests => 5; >+ >+ my $test_nonce = 'TEST_NONCE'; >+ my $csp_header_value = >+ "default-src 'self'; script-src 'self' 'nonce-_CSP_NONCE_'; style-src 'self' 'nonce-_CSP_NONCE_'; img-src 'self' data:; font-src 'self'; object-src 'none'"; >+ >+ t::lib::Mocks::mock_preference( 'OpacPublic', 1 ); >+ t::lib::Mocks::mock_config( >+ $conf_csp_section, >+ { opac => { csp_mode => 'enabled', csp_header_value => $csp_header_value } } >+ ); >+ >+ # Set nonce >+ Koha::ContentSecurityPolicy->new->nonce($test_nonce); >+ >+ my $env = {}; >+ my $home = $ENV{KOHA_HOME}; >+ my $app = Plack::App::CGIBin->new( root => $ENV{GIT_INSTALL} ? "$home/opac" : "$home/opac/cgi-bin/opac" )->to_app; >+ >+ $app = builder { >+ mount '/opac' => builder { >+ enable "+Koha::Middleware::ContentSecurityPolicy"; >+ $app; >+ }; >+ }; >+ >+ my $test = Plack::Test->create($app); >+ my $res = $test->request( GET "/opac/opac-main.pl" ); >+ my $expected_csp_header_value = $csp_header_value; >+ $expected_csp_header_value =~ s/_CSP_NONCE_/$test_nonce/g; >+ is( >+ $res->header('content-security-policy'), $expected_csp_header_value, >+ "Response contains Content-Security-Policy header with the expected value" >+ ); >+ >+ t::lib::Mocks::mock_config( >+ $conf_csp_section, >+ { opac => { csp_mode => '', csp_header_value => $csp_header_value } } >+ ); >+ >+ $res = $test->request( GET "/opac/opac-main.pl" ); >+ is( >+ $res->header('content-security-policy'), undef, >+ "Response does not contain Content-Security-Policy header when it is disabled in koha-conf.xml" >+ ); >+ >+ like( $res->content, qr/<body ID="opac-main"/, 'opac-main.pl looks okay' ); >+ >+ t::lib::Mocks::mock_config( >+ $conf_csp_section, >+ {} >+ ); >+ >+ $res = $test->request( GET "/opac/opac-main.pl" ); >+ is( >+ $res->header('content-security-policy'), undef, >+ "Response does not contain Content-Security-Policy header when it has not been defined in koha-conf.xml" >+ ); >+ >+ like( $res->content, qr/<body ID="opac-main"/, 'opac-main.pl looks okay' ); >+}; >+ >+subtest 'test CSP in staff client' => sub { >+ plan tests => 5; >+ >+ my $test_nonce = 'TEST_NONCE'; >+ my $csp_header_value = >+ "default-src 'self'; script-src 'self' 'nonce-_CSP_NONCE_'; style-src 'self' 'nonce-_CSP_NONCE_'; img-src 'self' data:; font-src 'self'; object-src 'none'"; >+ >+ t::lib::Mocks::mock_config( >+ $conf_csp_section, >+ { opac => { csp_mode => 'enabled', csp_header_value => $csp_header_value } } >+ ); >+ >+ # Set nonce >+ Koha::ContentSecurityPolicy->new->nonce($test_nonce); >+ >+ my $env = {}; >+ my $home = $ENV{KOHA_HOME}; >+ my $app = Plack::App::CGIBin->new( root => $ENV{GIT_INSTALL} ? $home : "$home/intranet/cgi-bin/" )->to_app; >+ >+ $app = builder { >+ mount '/intranet' => builder { >+ enable "+Koha::Middleware::ContentSecurityPolicy"; >+ $app; >+ }; >+ }; >+ >+ my $test = Plack::Test->create($app); >+ my $res = $test->request( GET "/intranet/mainpage.pl" ); >+ my $expected_csp_header_value = $csp_header_value; >+ $expected_csp_header_value =~ s/_CSP_NONCE_/$test_nonce/g; >+ is( >+ $res->header('content-security-policy'), undef, >+ "Response does not Content-Security-Policy header when it has not been defined in koha-conf.xml" >+ ); >+ >+ t::lib::Mocks::mock_config( >+ $conf_csp_section, >+ { >+ intranet => { >+ csp_mode => '', csp_header_value => $csp_header_value, >+ } >+ } >+ ); >+ >+ $res = $test->request( GET "/intranet/mainpage.pl" ); >+ is( >+ $res->header('content-security-policy'), undef, >+ "Response does not contain Content-Security-Policy header when it is explicitly disabled in koha-conf.xml" >+ ); >+ >+ like( $res->content, qr/<body id="main_auth"/, 'mainpage.pl looks okay' ); >+ >+ t::lib::Mocks::mock_config( >+ $conf_csp_section, >+ { intranet => { csp_mode => 'enabled', csp_header_value => $csp_header_value } } >+ ); >+ >+ $res = $test->request( GET "/intranet/mainpage.pl" ); >+ is( >+ $res->header('content-security-policy'), $expected_csp_header_value, >+ "Response contains Content-Security-Policy header when it is enabled in koha-conf.xml" >+ ); >+ >+ like( $res->content, qr/<body id="main_auth"/, 'mainpage.pl looks okay' ); >+}; >+ >-- >2.34.1
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 38365
:
174054
|
188487
|
188488
|
188489
|
188490
|
188491
|
188733
|
188734
|
188735
|
188736
|
188737
|
188881
|
188882
|
189465
|
189466
|
189467
|
189468
|
189469
|
189470
|
189471
|
189472
|
191618
|
191619
|
191620
|
191621
|
191622
|
191623
|
191660
|
191661
|
191662
|
191663
|
191664
|
191665
|
191666
|
191667
|
191668
|
192294
|
192295
|
192296
|
192297
|
192298
|
192299
|
192300
|
192301
|
192302
|
192303
|
192304
|
192305
|
192306
|
192412
|
192413
|
192414
|
192415
|
192416
|
192417
|
192418
|
192419
|
192420
|
192421
|
192422
|
192423
|
192424
|
192425
|
192426
|
192427
|
192428
|
192429
|
192524
|
192535
|
192536
|
192537
|
192553
|
192554
|
192555
|
192556
|
192557
|
192558
|
192559
|
192560
|
192561
|
192562
|
192563
|
192564
|
192565
|
192566
|
192567
|
192568
|
192569
|
192570
|
192571
|
192572
|
192573
|
192574
|
192684
|
192685
|
192686
|
192691
|
192692
|
192693
|
192694
|
192695
|
192696
|
192697
|
192698
|
192699
|
192700
|
192701
|
192702
|
192703
|
192704
|
192705
|
192706
|
192707
|
192708
|
192709
|
192738
|
192843
|
192844
|
192934
|
192935
|
192936
|
192937
|
193288
|
193289
|
193290
|
193291
|
193292
|
193293
|
193294
|
193295
|
193296
|
193297
|
193298
|
193299
|
193300
|
193301
|
193302
|
193303
|
193304
|
193305
|
193306
|
193307
|
193308
|
193309
|
193310
|
193311
|
193312
|
193335
|
193705
|
193706
|
193707
|
193708
|
193709
|
193710
|
193711
|
193712
|
193713
|
193714
|
193941
|
193942
|
193943
|
193944
|
193945
|
193946
|
193947
|
193948
|
193949
|
193950
|
193951
|
193952
|
193953
|
193954
|
193955
|
193956
|
193957
|
193958
|
193959
|
193960
|
193961
|
193962
|
193963
|
193964
|
193965
|
193966
|
193967
|
193968