AWS-S3
view release on metacpan or search on metacpan
t/aws/s3/response_parser.t view on Meta::CPAN
XML
my $bomb = <<'XML';
<?xml version="1.0"?>
<!DOCTYPE lolz [
<!ENTITY lol "lol">
<!ENTITY lol1 "&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;&lol;">
<!ENTITY lol2 "&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;&lol1;">
<!ENTITY lol3 "&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;&lol2;">
<!ENTITY lol4 "&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;&lol3;">
]>
<lolz>&lol4;</lolz>
XML
my $external_dtd = <<'XML';
<?xml version="1.0"?>
<!DOCTYPE r SYSTEM "http://127.0.0.1:1/evil.dtd">
<r/>
XML
note( "normal responses still parse" );
{
my $parser = _parser( '<r><Name>a & b <c> A</Name></r>' );
my $doc = $parser->xml;
isa_ok( $doc, 'XML::LibXML::Document' );
is( $doc->findvalue( '/r/Name' ), 'a & b <c> A', 'predefined entities and character references are decoded' );
}
note( "responses with a DOCTYPE are refused" );
foreach my $case (
[ 'external entity (XXE)', $xxe ],
[ 'entity expansion bomb', $bomb ],
[ 'external DTD', $external_dtd ],
[ 'lower case doctype', "<?xml version=\"1.0\"?>\n<!doctype r [<!ENTITY a \"b\">]>\n<r>&a;</r>" ],
[ 'leading whitespace', " \n$xxe" ],
) {
my ( $name, $body ) = @{ $case };
my $parser = _parser( $body );
my $doc = eval { $parser->xml };
like( $@, qr/DOCTYPE/, "$name: ->xml dies" );
ok( !$doc, "$name: nothing returned" );
unlike( $@, qr/TOP-SECRET/, "$name: and the file was not read" );
eval { _parser( $body )->xpc };
like( $@, qr/DOCTYPE/, "$name: ->xpc dies too" );
}
note( "a response with a DOCTYPE is refused when checking for errors" );
{
eval { _parser( $xxe, code => 400, expect_nothing => 1 ) };
like( $@, qr/DOCTYPE/, 'dies rather than parse it' );
unlike( $@, qr/TOP-SECRET/, '... and the file was not read' );
}
note( "error responses still work" );
{
my $error = <<'XML';
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>TooManyBuckets</Code>
<Message>You have attempted to create more buckets than allowed.</Message>
</Error>
XML
my $parser = _parser( $error, code => 400, expect_nothing => 1 );
is( $parser->error_code, 'TooManyBuckets', 'error code parsed' );
like( $parser->friendly_error, qr/TooManyBuckets.*more buckets than allowed/, 'friendly_error' );
}
note( "errors that are not xml" );
{
# a leading number is the code
my $parser = _parser( '503 Service Unavailable', code => 503, expect_nothing => 1 );
is( $parser->error_code, '503', 'number at the start of the body is the code' );
is( $parser->error_message, '503 Service Unavailable', '... and the body is the message' );
# anything else (e.g. from a proxy) uses the status, not the whole body
$parser = _parser( 'Bad Gateway', code => 502, expect_nothing => 1 );
is( $parser->error_code, '502', 'otherwise the code is the status of the response' );
is( $parser->error_message, 'Bad Gateway', '... and the body is the message' );
like( $parser->friendly_error, qr/\[502\] Bad Gateway/, '... friendly_error' );
# no body at all must not look like success
$parser = _parser( '', code => 500, expect_nothing => 1 );
is( $parser->error_code, '500', 'an empty body with an error status is an error' );
ok( $parser->friendly_error, '... with a friendly_error' );
# and a success with no body is still fine
$parser = _parser( '', code => 200, expect_nothing => 1 );
ok( !$parser->error_code, 'an empty successful response is not an error' );
ok( !$parser->friendly_error, '... and has no friendly_error' );
}
# the DOCTYPE check is the first line of defence, but the parser must also be
# safe on its own (e.g. if a caller supplies a ->libxml, or the check is
# bypassed), so feed it the payloads directly
note( "the default parser does not expand entities or read files itself" );
{
my $libxml = _parser( '<r/>' )->libxml;
isa_ok( $libxml, 'XML::LibXML' );
my $doc = eval { $libxml->parse_string( $xxe ) };
my $out = $doc ? $doc->toString : '';
unlike( $out, qr/TOP-SECRET/, 'external entity content is not included' );
unlike( $@ // '', qr/TOP-SECRET/, '... or leaked in an error' );
}
done_testing();
( run in 0.660 second using v1.01-cache-2.11-cpan-39bc3f1cbff )