AWS-S3
view release on metacpan or search on metacpan
t/aws/s3/request/uri_injection.t view on Meta::CPAN
#!perl
use strict;
use warnings;
# Bucket names, object keys and query values are caller supplied and end up in
# the URL of the requests we send. They must not be able to change the shape of
# that URL: send the (signed) request to another host, turn an upload into a
# different S3 operation, or inject extra query parameters.
package Capturing::UA;
use parent 'LWP::UserAgent';
our @REQUESTS;
sub request {
my ( $self, $req ) = @_;
push @REQUESTS, $req;
return HTTP::Response->new( 200, 'OK', [ ETag => '"abc"' ] );
}
package main;
use Test::More;
use_ok( 'AWS::S3' );
use_ok( 'AWS::S3::Bucket' );
use_ok( 'AWS::S3::File' );
use_ok( 'AWS::S3::Types' );
my $s3 = AWS::S3->new(
access_key_id => 'foo',
secret_access_key => 'bar',
endpoint => 's3.baz.com',
ua => Capturing::UA->new,
);
my $bucket = AWS::S3::Bucket->new( s3 => $s3, name => 'maibucket' );
# no contents passed so nothing is fetched or sent when the object is built
my $file = AWS::S3::File->new(
key => 'k',
bucket => $bucket,
is_encrypted => 0,
size => 1,
);
# every request type that takes a bucket, and any other required arguments
my %request_args = (
CreateBucket => {},
DeleteBucket => {},
DeleteFile => { key => 'k' },
DeleteMulti => { keys => ['k'] },
GetBucketAccessControl => {},
GetBucketLocationConstraint => {},
GetBucketPolicy => {},
GetFileContents => { key => 'k' },
GetFileInfo => { key => 'k' },
GetPreSignedUrl => { key => 'k', expires => 60 },
ListBucket => { max_keys => 1 },
SetBucketAccessControl => { acl_short => 'private' },
SetBucketPolicy => { policy => '{}' },
SetFileContents => { file => $file },
);
my @good_names = (
'maibucket',
'foo',
'my.dotted-bucket.name',
'Legacy_Bucket_Name', # legacy buckets could have upper case/underscores
'0123',
'a' x 255,
);
my @bad_names = (
'evil.com/x', # would send the request to evil.com
'evil.com#',
'evil.com?',
'user@evil.com',
'evil.com:8080',
'a/b',
'a\\b',
'a b',
'a%2Fb',
"a\nb",
"a\n", # trailing newline, \z not $
"a\r\nHost: evil.com",
'',
'.leadingdot',
'-leadinghyphen',
'_leadingunderscore',
"caf\x{e9}",
'a' x 256,
);
sub _label {
my ( $str ) = @_;
$str =~ s/\r/\\r/g;
$str =~ s/\n/\\n/g;
$str =~ s/[^\x20-\x7e]/?/g;
return length( $str ) > 40 ? substr( $str, 0, 40 ) . '...' : $str;
}
note( "bucket name type" );
{
my $type = Moose::Util::TypeConstraints::find_type_constraint( 'AWS::S3::BucketName' );
ok( $type->check( $_ ), "accepts '" . _label( $_ ) . "'" ) for @good_names;
ok( !$type->check( $_ ), "rejects '" . _label( $_ ) . "'" ) for @bad_names;
ok( !$type->check( undef ), 'rejects undef' );
}
note( "every request class validates the bucket" );
foreach my $class ( sort keys %request_args ) {
my %args = %{ $request_args{$class} };
foreach my $name ( @good_names ) {
my $req = eval { $s3->request( $class, bucket => $name, %args ) };
ok( $req, "$class accepts '" . _label( $name ) . "'" ) or diag $@;
}
foreach my $name ( @bad_names ) {
@Capturing::UA::REQUESTS = ();
eval { $s3->request( $class, bucket => $name, %args ) };
like( $@, qr/Invalid S3 bucket name/, "$class rejects '" . _label( $name ) . "'" );
is( scalar @Capturing::UA::REQUESTS, 0, '... and nothing was sent' );
}
my $attr = "AWS::S3::Request::$class"->meta->get_attribute( 'bucket' );
is( $attr->type_constraint->name, 'AWS::S3::BucketName', "$class bucket attribute uses the type" );
}
note( "add_bucket does not send a request for an invalid name" );
{
@Capturing::UA::REQUESTS = ();
eval { $s3->add_bucket( name => 'evil.com/x' ) };
like( $@, qr/Invalid S3 bucket name/, 'add_bucket rejects the name' );
is( scalar @Capturing::UA::REQUESTS, 0, '... and nothing was sent' );
}
note( "object keys are escaped when uploading" );
{
my %cases = (
'foo?acl' => '/foo%3Facl', # not the ?acl subresource
'foo?policy=1&x' => '/foo%3Fpolicy=1%26x',
'a#b' => '/a%23b', # not a fragment
'a%41' => '/a%2541', # a literal %, not "A"
'dir/sub/file name.txt' => '/dir/sub/file%20name.txt',# "/" kept as a separator
"caf\x{e9}\x{263a}.txt" => '/caf%C3%A9%E2%98%BA.txt', # wide characters, no croak
);
foreach my $key ( sort keys %cases ) {
@Capturing::UA::REQUESTS = ();
eval { $bucket->add_file( key => $key, contents => \'data', is_encrypted => 0, size => 4 ) };
is( $@, '', "add_file lives for '" . _label( $key ) . "'" );
( run in 1.569 second using v1.01-cache-2.11-cpan-0e0213f3164 )