AWS-S3

 view release on metacpan or  search on metacpan

t/aws/s3/response_parser.t  view on Meta::CPAN

#!perl

use strict;
use warnings;

# Responses must not be able to make us read local files, make network
# requests, or consume unbounded resources by way of entities or a DTD.

use Test::More;
use File::Temp qw/ tempfile /;
use HTTP::Response;

use_ok( 'AWS::S3::ResponseParser' );

sub _parser {
    my ( $body, %args ) = @_;

    my $code = delete $args{code};
    $code = 200 unless defined $code;

    return AWS::S3::ResponseParser->new(
        response => HTTP::Response->new( $code, 'OK', [], $body ),
        type     => 'Test',
        %args,
    );
}

my ( $fh, $secret_file ) = tempfile( UNLINK => 1 );
print {$fh} 'TOP-SECRET-CONTENT-12345';
close $fh;

my $xxe = <<"XML";
<?xml version="1.0"?>
<!DOCTYPE ListAllMyBucketsResult [
  <!ENTITY xxe SYSTEM "file://$secret_file">
]>
<ListAllMyBucketsResult>
  <Owner><ID>&xxe;</ID><DisplayName>x</DisplayName></Owner>
</ListAllMyBucketsResult>
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 &amp; b &lt;c&gt; &#65;</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"    ],



( run in 1.187 second using v1.01-cache-2.11-cpan-0e0213f3164 )