|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <https://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::NoWarnings; |
| 21 |
use Test::MockModule; |
| 22 |
use Test::More tests => 6; |
| 23 |
use Test::Exception; |
| 24 |
|
| 25 |
BEGIN { use_ok('Koha::ContentSecurityPolicy') } |
| 26 |
|
| 27 |
use C4::Context; |
| 28 |
use Koha::Cache::Memory::Lite; |
| 29 |
|
| 30 |
use t::lib::Mocks; |
| 31 |
|
| 32 |
my $conf_csp_section = 'content_security_policy'; |
| 33 |
|
| 34 |
subtest 'is_enabled() tests' => sub { |
| 35 |
|
| 36 |
plan tests => 4; |
| 37 |
|
| 38 |
foreach my $interface ( 'opac', 'intranet' ) { |
| 39 |
subtest "interface $interface" => sub { |
| 40 |
t::lib::Mocks::mock_config( $conf_csp_section, {} ); |
| 41 |
C4::Context->interface($interface); |
| 42 |
|
| 43 |
my $csp = Koha::ContentSecurityPolicy->new; |
| 44 |
|
| 45 |
is( $csp->is_enabled, 0, 'CSP is not enabled when koha-conf.xml has not set the entire section' ); |
| 46 |
|
| 47 |
t::lib::Mocks::mock_config( |
| 48 |
$conf_csp_section, |
| 49 |
{ $interface => { csp_mode => '', csp_header_value => 'test' } } |
| 50 |
); |
| 51 |
is( $csp->is_enabled, 0, 'CSP is not enabled when csp_mode is empty' ); |
| 52 |
|
| 53 |
t::lib::Mocks::mock_config( |
| 54 |
$conf_csp_section, |
| 55 |
{ $interface => { csp_mode => 'wrong', csp_header_value => 'test' } } |
| 56 |
); |
| 57 |
is( $csp->is_enabled, 0, 'CSP is not enabled when csp_mode has not got a valid csp_header_value' ); |
| 58 |
|
| 59 |
t::lib::Mocks::mock_config( |
| 60 |
$conf_csp_section, |
| 61 |
{ $interface => { csp_mode => 'report-only', csp_header_value => 'test' } } |
| 62 |
); |
| 63 |
is( $csp->is_enabled, 1, 'CSP is enabled when csp_mode is report-only' ); |
| 64 |
|
| 65 |
t::lib::Mocks::mock_config( |
| 66 |
$conf_csp_section, |
| 67 |
{ $interface => { csp_mode => 'enabled', csp_header_value => 'test' } } |
| 68 |
); |
| 69 |
is( $csp->is_enabled, 1, 'CSP is enabled when csp_mode is enabled' ); |
| 70 |
|
| 71 |
t::lib::Mocks::mock_config( |
| 72 |
$conf_csp_section, |
| 73 |
{ |
| 74 |
$interface => { |
| 75 |
csp_mode => 'enabled', csp_header_value => 'test', |
| 76 |
} |
| 77 |
} |
| 78 |
); |
| 79 |
}; |
| 80 |
} |
| 81 |
|
| 82 |
t::lib::Mocks::mock_config( |
| 83 |
$conf_csp_section, { opac => { csp_mode => 'enabled', csp_header_value => 'test' } }, |
| 84 |
intranet => { csp_mode => '', csp_header_value => 'test' } |
| 85 |
); |
| 86 |
C4::Context->interface('intranet'); |
| 87 |
my $csp = Koha::ContentSecurityPolicy->new; |
| 88 |
is( $csp->is_enabled, 0, 'CSP is disabled when other interface is enabled, but not this one' ); |
| 89 |
is( $csp->is_enabled( { interface => 'opac' } ), 1, 'CSP is enabled when explicitly defining interface' ); |
| 90 |
}; |
| 91 |
|
| 92 |
subtest 'header_name tests' => sub { |
| 93 |
plan tests => 6; |
| 94 |
|
| 95 |
t::lib::Mocks::mock_config( $conf_csp_section, {} ); |
| 96 |
C4::Context->interface('opac'); |
| 97 |
|
| 98 |
my $csp = Koha::ContentSecurityPolicy->new; |
| 99 |
|
| 100 |
throws_ok { $csp->header_name } 'Koha::Exceptions::Config::MissingEntry', 'Exception thrown when missing csp_mode'; |
| 101 |
|
| 102 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => {} } ); |
| 103 |
throws_ok { $csp->header_name } 'Koha::Exceptions::Config::MissingEntry', 'Exception thrown when missing csp_mode'; |
| 104 |
|
| 105 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => { csp_mode => '' } } ); |
| 106 |
throws_ok { $csp->header_name } 'Koha::Exceptions::Config::MissingEntry', 'Exception thrown when missing csp_mode'; |
| 107 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => { csp_mode => 'invalid' } } ); |
| 108 |
throws_ok { $csp->header_name } 'Koha::Exceptions::Config::MissingEntry', 'Exception thrown when invalid csp_mode'; |
| 109 |
|
| 110 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => { csp_mode => 'report-only' } } ); |
| 111 |
is( |
| 112 |
$csp->header_name, 'Content-Security-Policy-Report-Only', |
| 113 |
'report-only => Content-Security-Policy-Report-Only' |
| 114 |
); |
| 115 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => { csp_mode => 'enabled' } } ); |
| 116 |
is( $csp->header_name, 'Content-Security-Policy', 'enabled => Content-Security-Policy' ); |
| 117 |
}; |
| 118 |
|
| 119 |
subtest 'header_value tests' => sub { |
| 120 |
plan tests => 6; |
| 121 |
|
| 122 |
t::lib::Mocks::mock_config( $conf_csp_section, {} ); |
| 123 |
C4::Context->interface('opac'); |
| 124 |
|
| 125 |
my $csp = Koha::ContentSecurityPolicy->new; |
| 126 |
|
| 127 |
throws_ok { $csp->header_value } 'Koha::Exceptions::Config::MissingEntry', |
| 128 |
'Exception thrown when missing csp_header_value'; |
| 129 |
|
| 130 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => {} } ); |
| 131 |
throws_ok { $csp->header_value } 'Koha::Exceptions::Config::MissingEntry', |
| 132 |
'Exception thrown when missing csp_header_value'; |
| 133 |
|
| 134 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => { csp_header_value => '' } } ); |
| 135 |
is( $csp->header_value, '', 'Even an empty csp_header_value is retrieved form koha-conf.xml' ); |
| 136 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => { csp_header_value => 'some value' } } ); |
| 137 |
is( $csp->header_value, 'some value', 'csp_header_value is retrieved from koha-conf.xml' ); |
| 138 |
|
| 139 |
t::lib::Mocks::mock_config( |
| 140 |
$conf_csp_section, |
| 141 |
{ opac => { csp_header_value => 'begin nonce-_CSP_NONCE_ end' } } |
| 142 |
); |
| 143 |
my $cache = Koha::Cache::Memory::Lite->get_instance(); |
| 144 |
$cache->set_in_cache( 'CSP-NONCE', 'cached value' ); |
| 145 |
is( $csp->header_value, 'begin nonce-cached value end', 'Cached csp_header_value' ); |
| 146 |
$cache->flush; |
| 147 |
|
| 148 |
$csp = Koha::ContentSecurityPolicy->new( { nonce => 'forced value' } ); |
| 149 |
is( $csp->header_value, 'begin nonce-forced value end', 'Forced csp_header_value' ); |
| 150 |
}; |
| 151 |
|
| 152 |
subtest 'nonce tests' => sub { |
| 153 |
plan tests => 5; |
| 154 |
|
| 155 |
t::lib::Mocks::mock_config( $conf_csp_section, {} ); |
| 156 |
C4::Context->interface('opac'); |
| 157 |
|
| 158 |
my $csp = Koha::ContentSecurityPolicy->new; |
| 159 |
|
| 160 |
$csp = Koha::ContentSecurityPolicy->new( { nonce => 'forced value' } ); |
| 161 |
is( $csp->nonce, 'forced value', 'nonce is not re-generated as it was passed to new()' ); |
| 162 |
|
| 163 |
$csp = Koha::ContentSecurityPolicy->new( { nonce => 'forced value' } ); |
| 164 |
is( $csp->nonce, 'forced value', 'nonce is not re-generated as was cached in memory' ); |
| 165 |
|
| 166 |
my $cache = Koha::Cache::Memory::Lite->get_instance(); |
| 167 |
$cache->flush; |
| 168 |
|
| 169 |
$csp = Koha::ContentSecurityPolicy->new(); |
| 170 |
my $nonce = $csp->nonce; |
| 171 |
like( $nonce, qr/\w{10}/, 'nonce is a random string of 10 characters' ); |
| 172 |
is( $nonce, $csp->nonce, 're-calling nonce() returns the same randomly generated cached value' ); |
| 173 |
|
| 174 |
$cache->set_in_cache( 'CSP-NONCE', 'cached value' ); |
| 175 |
is( $csp->nonce, 'cached value', 'nonce is not re-generated as it was previously cached' ); |
| 176 |
}; |
| 177 |
|
| 178 |
1; |