|
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::More tests => 5; |
| 22 |
use Test::Exception; |
| 23 |
|
| 24 |
BEGIN { use_ok('Koha::ContentSecurityPolicy') } |
| 25 |
|
| 26 |
use C4::Context; |
| 27 |
use Koha::Cache::Memory::Lite; |
| 28 |
|
| 29 |
use t::lib::Mocks; |
| 30 |
|
| 31 |
my $conf_csp_section = 'content_security_policy'; |
| 32 |
|
| 33 |
subtest 'is_enabled() tests' => sub { |
| 34 |
|
| 35 |
plan tests => 4; |
| 36 |
|
| 37 |
foreach my $interface ( 'opac', 'intranet' ) { |
| 38 |
subtest "interface $interface" => sub { |
| 39 |
t::lib::Mocks::mock_config( $conf_csp_section, {} ); |
| 40 |
C4::Context->interface($interface); |
| 41 |
|
| 42 |
my $csp = Koha::ContentSecurityPolicy->new; |
| 43 |
|
| 44 |
is( $csp->is_enabled, 0, 'CSP is not enabled when koha-conf.xml has not set the entire section' ); |
| 45 |
|
| 46 |
t::lib::Mocks::mock_config( |
| 47 |
$conf_csp_section, |
| 48 |
{ $interface => { csp_mode => '', csp_header_value => 'test' } } |
| 49 |
); |
| 50 |
is( $csp->is_enabled, 0, 'CSP is not enabled when csp_mode is empty' ); |
| 51 |
|
| 52 |
t::lib::Mocks::mock_config( |
| 53 |
$conf_csp_section, |
| 54 |
{ $interface => { csp_mode => 'wrong', csp_header_value => 'test' } } |
| 55 |
); |
| 56 |
is( $csp->is_enabled, 0, 'CSP is not enabled when csp_mode hasnt got a valid csp_header_value' ); |
| 57 |
|
| 58 |
t::lib::Mocks::mock_config( |
| 59 |
$conf_csp_section, |
| 60 |
{ $interface => { csp_mode => 'report-only', csp_header_value => 'test' } } |
| 61 |
); |
| 62 |
is( $csp->is_enabled, 1, 'CSP is enabled when csp_mode is report-only' ); |
| 63 |
|
| 64 |
t::lib::Mocks::mock_config( |
| 65 |
$conf_csp_section, |
| 66 |
{ $interface => { csp_mode => 'enabled', csp_header_value => 'test' } } |
| 67 |
); |
| 68 |
is( $csp->is_enabled, 1, 'CSP is enabled when csp_mode is enabled' ); |
| 69 |
}; |
| 70 |
} |
| 71 |
|
| 72 |
t::lib::Mocks::mock_config( |
| 73 |
$conf_csp_section, { opac => { csp_mode => 'enabled', csp_header_value => 'test' } }, |
| 74 |
intranet => { csp_mode => '', csp_header_value => 'test' } |
| 75 |
); |
| 76 |
C4::Context->interface('intranet'); |
| 77 |
my $csp = Koha::ContentSecurityPolicy->new; |
| 78 |
is( $csp->is_enabled, 0, 'CSP is disabled when other interface is enabled, but not this one' ); |
| 79 |
is( $csp->is_enabled('opac'), 1, 'CSP is enabled when explicitly defining interface' ); |
| 80 |
}; |
| 81 |
|
| 82 |
subtest 'header_name tests' => sub { |
| 83 |
plan tests => 6; |
| 84 |
|
| 85 |
t::lib::Mocks::mock_config( $conf_csp_section, {} ); |
| 86 |
C4::Context->interface('opac'); |
| 87 |
|
| 88 |
my $csp = Koha::ContentSecurityPolicy->new; |
| 89 |
|
| 90 |
throws_ok { $csp->header_name } 'Koha::Exceptions::Config::MissingEntry', 'Exception thrown when missing csp_mode'; |
| 91 |
|
| 92 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => {} } ); |
| 93 |
throws_ok { $csp->header_name } 'Koha::Exceptions::Config::MissingEntry', 'Exception thrown when missing csp_mode'; |
| 94 |
|
| 95 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => { csp_mode => '' } } ); |
| 96 |
throws_ok { $csp->header_name } 'Koha::Exceptions::Config::MissingEntry', 'Exception thrown when missing csp_mode'; |
| 97 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => { csp_mode => 'invalid' } } ); |
| 98 |
throws_ok { $csp->header_name } 'Koha::Exceptions::Config::MissingEntry', 'Exception thrown when invalid csp_mode'; |
| 99 |
|
| 100 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => { csp_mode => 'report-only' } } ); |
| 101 |
is( |
| 102 |
$csp->header_name, 'Content-Security-Policy-Report-Only', |
| 103 |
'report-only => Content-Security-Policy-Report-Only' |
| 104 |
); |
| 105 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => { csp_mode => 'enabled' } } ); |
| 106 |
is( $csp->header_name, 'Content-Security-Policy', 'enabled => Content-Security-Policy' ); |
| 107 |
}; |
| 108 |
|
| 109 |
subtest 'header_value tests' => sub { |
| 110 |
plan tests => 6; |
| 111 |
|
| 112 |
t::lib::Mocks::mock_config( $conf_csp_section, {} ); |
| 113 |
C4::Context->interface('opac'); |
| 114 |
|
| 115 |
my $csp = Koha::ContentSecurityPolicy->new; |
| 116 |
|
| 117 |
throws_ok { $csp->header_value } 'Koha::Exceptions::Config::MissingEntry', |
| 118 |
'Exception thrown when missing csp_header_value'; |
| 119 |
|
| 120 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => {} } ); |
| 121 |
throws_ok { $csp->header_value } 'Koha::Exceptions::Config::MissingEntry', |
| 122 |
'Exception thrown when missing csp_header_value'; |
| 123 |
|
| 124 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => { csp_header_value => '' } } ); |
| 125 |
is( $csp->header_value, '', 'even an empty csp_header_value is retrieved form koha-conf.xml' ); |
| 126 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => { csp_header_value => 'some value' } } ); |
| 127 |
is( $csp->header_value, 'some value', 'csp_header_value is retrieved from koha-conf.xml' ); |
| 128 |
|
| 129 |
t::lib::Mocks::mock_config( $conf_csp_section, { opac => { csp_header_value => 'begin _CSP_NONCE_ end' } } ); |
| 130 |
my $cache = Koha::Cache::Memory::Lite->get_instance(); |
| 131 |
$cache->set_in_cache( 'csp_nonce', 'cached value' ); |
| 132 |
is( $csp->header_value, 'begin nonce-cached value end', 'cached csp_header_value' ); |
| 133 |
|
| 134 |
$csp = Koha::ContentSecurityPolicy->new( { nonce => 'forced value' } ); |
| 135 |
is( $csp->header_value, 'begin nonce-forced value end', 'forced csp_header_value' ); |
| 136 |
}; |
| 137 |
|
| 138 |
1; |