1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
use strict;
use Test;
BEGIN { plan tests => 26 }
use UNIVERSAL qw(isa);
use XML::LibXSLT;
use XML::LibXML 1.59;
use Data::Dumper;
use Devel::Peek;
my $parser = XML::LibXML->new();
print "# parser\n";
ok($parser);
my $xslt = XML::LibXSLT->new();
print "# xslt\n";
ok($xslt);
my $stylsheetstring = <<'EOT';
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:template match="/">
<html>
<head><title>Know Your Dromedaries</title></head>
<body>
<h1><xsl:apply-templates/></h1>
<xsl:choose>
<xsl:when test="file">
<p>foo: <xsl:apply-templates select="document(file)/*" /></p>
</xsl:when>
<xsl:when test="write">
<exsl:document href="{write}">
<outfile><xsl:value-of select="write"/></outfile>
</exsl:document>
<p>wrote: <xsl:value-of select="write"/></p>
</xsl:when>
<xsl:otherwise>
No file given
</xsl:otherwise>
</xsl:choose>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
EOT
# We're using input callbacks so that we don't actually need real files while
# testing the security callbacks
my $icb = XML::LibXML::InputCallback->new();
ok($icb);
print "# registering input callbacks\n";
$icb->register_callbacks( [ \&match_cb, \&open_cb,
\&read_cb, \&close_cb ] );
$xslt->input_callbacks($icb);
my $scb = XML::LibXSLT::Security->new();
ok($scb);
print "# registering security callbacks\n";
$scb->register_callback( read_file => \&read_file );
$scb->register_callback( write_file => \&write_file );
$scb->register_callback( create_dir => \&create_dir );
$scb->register_callback( read_net => \&read_net );
$scb->register_callback( write_net => \&write_net );
$xslt->security_callbacks($scb);
my $stylesheet = $xslt->parse_stylesheet($parser->parse_string($stylsheetstring));
print "# stylesheet\n";
ok($stylesheet);
# test local read
# ---------------------------------------------------------------------------
# - test allowed
my $doc = $parser->parse_string('<file>allow.xml</file>');
my $results = $stylesheet->transform($doc);
print "# local read results\n";
ok($results);
my $output = $stylesheet->output_string($results);
#warn "output: $output\n";
print "# local read output\n";
ok($output =~ /foo: Text here/);
# - test denied
$doc = $parser->parse_string('<file>deny.xml</file>');
eval {
$results = $stylesheet->transform($doc);
};
print "# local read denied\n";
ok($@ =~ /read for deny\.xml refused/);
# test local write & create dir
# ---------------------------------------------------------------------------
# - test allowed (no create dir)
my $file = 't/allow.xml';
$doc = $parser->parse_string("<write>$file</write>");
$results = $stylesheet->transform($doc);
print "# local write (no create dir) results\n";
ok($results);
$output = $stylesheet->output_string($results);
#warn "output: $output\n";
print "# local write (no create dir) output\n";
ok($output =~ /wrote: \Q$file\E/);
print "# local write (no create dir) file exists\n";
ok(-s $file);
unlink $file;
# - test allowed (create dir)
$file = 't/newdir/allow.xml';
$doc = $parser->parse_string("<write>$file</write>");
$results = $stylesheet->transform($doc);
print "# local write (create dir) results\n";
ok($results);
$output = $stylesheet->output_string($results);
#warn "output: $output\n";
print "# local write (create dir) output\n";
ok($output =~ /wrote: \Q$file\E/);
print "# local write (create dir) file exists\n";
ok(-s $file);
unlink $file;
rmdir 't/newdir';
# - test denied (no create dir)
$file = 't/deny.xml';
$doc = $parser->parse_string("<write>$file</write>");
eval {
$results = $stylesheet->transform($doc);
};
print "# local write (no create dir) denied\n";
ok($@ =~ /write for \Q$file\E refused/);
ok(!-e $file);
# - test denied (create dir)
$file = 't/baddir/allow.xml';
$doc = $parser->parse_string("<write>$file</write>");
eval {
$results = $stylesheet->transform($doc);
};
print "# local write (create dir) denied\n";
ok($@ =~ /creation for \Q$file\E refused/);
ok(!-e $file);
# test net read
# ---------------------------------------------------------------------------
# - test allowed
$doc = $parser->parse_string('<file>http://localhost/allow.xml</file>');
$results = $stylesheet->transform($doc);
print "# net read results\n";
ok($results);
$output = $stylesheet->output_string($results);
#warn "output: $output\n";
print "# net read output\n";
ok($output =~ /foo: Text here/);
# - test denied
$doc = $parser->parse_string('<file>http://localhost/deny.xml</file>');
eval {
$results = $stylesheet->transform($doc);
};
print "# net read denied\n";
ok($@ =~ m|read for http://localhost/deny\.xml refused|);
# test net write
# ---------------------------------------------------------------------------
# - test allowed
$file = 'http://localhost/allow.xml';
$doc = $parser->parse_string("<write>$file</write>");
eval {
$results = $stylesheet->transform($doc);
};
print "# net write allowed\n";
ok($@ =~ /unable to save to \Q$file\E/);
# - test denied
$file = 'http://localhost/deny.xml';
$doc = $parser->parse_string("<write>$file</write>");
eval {
$results = $stylesheet->transform($doc);
};
print "# net write denied\n";
ok($@ =~ /write for \Q$file\E refused/);
# test a dying security callback (and resetting the callback object through
# the stylesheet interface).
# ---------------------------------------------------------------------------
my $scb2 = XML::LibXSLT::Security->new();
$scb2->register_callback( read_file => \&read_file_die );
$stylesheet->security_callbacks($scb2);
# check if transform throws an exception
$doc = $parser->parse_string('<file>allow.xml</file>');
print "# dying callback test\n";
eval {
$stylesheet->transform($doc);
};
ok($@ =~ /Test die from security callback/);
#
# Security preference callbacks
#
sub read_file {
my ($tctxt, $value) = @_;
print "# security read_file: $value\n";
if ($value eq 'allow.xml') {
print "# transform context\n";
ok( isa($tctxt, "XML::LibXSLT::TransformContext") );
print "# stylesheet from transform context\n";
ok( isa($tctxt->stylesheet, "XML::LibXSLT::StylesheetWrapper") );
return 1;
}
else {
return 0;
}
}
sub read_file_die {
my ($tctxt, $value) = @_;
print "# security read_file_die: $value\n";
die "Test die from security callback";
}
sub write_file {
my ($tctxt, $value) = @_;
print "# security write_file: $value\n";
if ($value =~ /allow\.xml|newdir|baddir/) {
return 1;
}
else {
return 0;
}
}
sub create_dir {
my ($tctxt, $value) = @_;
print "# security create_dir: $value\n";
if ($value =~ /newdir/) {
return 1;
}
else {
return 0;
}
}
sub read_net {
my ($tctxt, $value) = @_;
print "# security read_net: $value\n";
if ($value =~ /allow\.xml/) {
return 1;
}
else {
return 0;
}
}
sub write_net {
my ($tctxt, $value) = @_;
print "# security write_net: $value\n";
if ($value =~ /allow\.xml/) {
return 1;
}
else {
return 0;
}
}
#
# input callback functions (used so we don't have to have an actual file)
#
sub match_cb {
my $uri = shift;
print "# input match_cb: $uri\n";
if ($uri =~ /(allow|deny)\.xml/) {
return 1;
}
return 0;
}
sub open_cb {
my $uri = shift;
print "# input open_cb: $uri\n";
my $str ="<foo>Text here</foo>";
return \$str;
}
sub close_cb {
print "# input close_cb\n";
}
sub read_cb {
print "# input read_cb\n";
return substr(${$_[0]}, 0, $_[1], "");
}
|