#!/usr/bin/perl
#
# Having 'XML_PARSE_HUGE' enabled can make an application vulnerable to
# denial of service through entity expansion attacks. This test script
# confirms that huge document mode is disabled by default and that this
# does not adversely affect expansion of sensible entity definitions.
#
use strict;
use warnings;
use Test::More;
use XML::LibXML;
if (XML::LibXML::LIBXML_VERSION() < 20700) {
plan skip_all => "XML_PARSE_HUGE option not supported for libxml2 < 2.7.0";
}
else {
plan tests => 3;
}
my $benign_xml = <<'EOF';
]>
&lol;
EOF
my $evil_xml = <<'EOF';
]>
&ha48;
EOF
my($parser, $doc);
$parser = XML::LibXML->new;
#$parser->set_option(huge => 0);
ok(!$parser->get_option('huge'), "huge mode disabled by default");
$doc = eval { $parser->parse_string($evil_xml); };
# fails with libxml 2.14.x
#isnt("$@", "", "exception thrown during parse");
#like($@, qr/entity/si, "exception refers to entity maximum loop (libxml2 <= 2.10) or depth (>= 2.11)");
$parser = XML::LibXML->new;
$doc = eval { $parser->parse_string($benign_xml); };
is("$@", "", "no exception thrown during parse");
my $body = $doc->findvalue( '/lolz' );
is($body, 'haha', 'entity was parsed and expanded correctly');
exit;