AWS-S3
view release on metacpan or search on metacpan
t/aws/s3/request/redirect.t view on Meta::CPAN
#!perl
use strict;
use warnings;
# A "TemporaryRedirect" response must not be able to make us replay a signed
# request (which carries the session token and the request body) to a host we
# have no reason to trust. Trusted redirects must be signed again, as the
# signature covers the host.
package Scripted::UA;
use parent 'LWP::UserAgent';
our ( @RESPONSES, @SEEN );
sub request {
my ( $self, $req ) = @_;
# the same request object is mutated and re-sent, so keep a copy
push @SEEN, $req->clone;
return shift( @RESPONSES ) // HTTP::Response->new( 200, 'OK' );
}
package main;
use Test::More;
use_ok( 'AWS::S3' );
sub _redirect_to {
my ( $location ) = @_;
return HTTP::Response->new(
307,
'TEMPORARY REDIRECT',
[ Location => $location ],
'<Error><Code>TemporaryRedirect</Code><Message>Please re-send this request</Message></Error>',
);
}
sub _s3 {
my ( %args ) = @_;
return AWS::S3->new(
access_key_id => 'foo',
secret_access_key => 'bar',
session_token => 'SECRET-SESSION-TOKEN',
region => 'us-east-1',
endpoint => 's3.amazonaws.com',
secure => 1,
ua => Scripted::UA->new,
%args,
);
}
# the request classes all share the same _send_request
sub _create_bucket {
my ( $s3 ) = @_;
return $s3->request( 'CreateBucket', bucket => 'maibucket' )->request;
}
note( "_is_trusted_redirect" );
{
my @cases = (
# endpoint, secure, location, trusted?
[ 's3.amazonaws.com', 1, 'https://maibucket.s3.amazonaws.com/', 1 ],
[ 's3.amazonaws.com', 1, 'https://maibucket.s3-us-west-2.amazonaws.com/', 1 ],
[ 's3.amazonaws.com', 1, 'https://maibucket.s3.us-west-2.amazonaws.com/', 1 ],
[ 's3.amazonaws.com', 1, 'https://s3.eu-west-1.amazonaws.com/maibucket/', 1 ],
[ 's3.amazonaws.com', 1, 'https://s3.dualstack.us-east-1.amazonaws.com/', 1 ],
[ 's3.amazonaws.com', 1, 'https://MAIBUCKET.S3.AMAZONAWS.COM/', 1 ],
[ 's3.eu-west-1.amazonaws.com', 1, 'https://maibucket.s3.amazonaws.com/', 1 ],
[ 's3.amazonaws.com', 0, 'http://maibucket.s3.amazonaws.com/', 1 ],
[ 's3.amazonaws.com', 0, 'https://maibucket.s3.amazonaws.com/', 1 ],
[ 's3.amazonaws.com', 1, 'https://evil.com/', 0 ],
[ 's3.amazonaws.com', 1, 'https://maibucket.s3.amazonaws.com.evil.com/', 0 ],
[ 's3.amazonaws.com', 1, 'https://evilamazonaws.com/', 0 ],
[ 's3.amazonaws.com', 1, 'https://xs3.amazonaws.com/', 0 ],
[ 's3.amazonaws.com', 1, 'https://ec2.us-east-1.amazonaws.com/', 0 ],
[ 's3.amazonaws.com', 1, 'https://abc.execute-api.us-east-1.amazonaws.com/',0 ],
[ 's3.amazonaws.com', 1, 'https://s3.amazonaws.com@evil.com/', 0 ],
[ 's3.amazonaws.com', 1, 'https://user:pw@maibucket.s3.amazonaws.com/', 0 ],
[ 's3.amazonaws.com', 1, 'https://evil.com/maibucket.s3.amazonaws.com', 0 ],
[ 's3.amazonaws.com', 1, 'https://evil.com/?h=s3.amazonaws.com', 0 ],
[ 's3.amazonaws.com', 1, 'http://maibucket.s3.amazonaws.com/', 0 ], # downgrade
[ 's3.amazonaws.com', 1, 'https://maibucket.s3.amazonaws.com:8443/', 0 ], # other port
[ 's3.amazonaws.com', 1, 'ftp://maibucket.s3.amazonaws.com/', 0 ],
[ 's3.amazonaws.com', 1, 'file:///etc/passwd', 0 ],
[ 's3.amazonaws.com', 1, '', 0 ],
# custom endpoint (S3 compatible service): only that domain is trusted
[ 's3.example.com', 1, 'https://s3.example.com/', 1 ],
[ 's3.example.com', 1, 'https://maibucket.s3.example.com/', 1 ],
[ 's3.example.com', 1, 'https://maibucket.s3.amazonaws.com/', 0 ],
[ 's3.example.com', 1, 'https://s3.example.com.evil.com/', 0 ],
[ 's3.example.com', 1, 'https://nots3.example.com/', 0 ],
[ 's3.example.com', 1, 'https://evil.com/', 0 ],
# a trailing dot on the host is the same host
[ 'bad.hostname.', 0, 'http://bar.eu.bad.hostname./', 1 ],
[ 'bad.hostname.', 0, 'http://bad.hostname.evil.com./', 0 ],
# custom endpoints with a port must stay on that port
[ 's3.example.com:9000', 1, 'https://maibucket.s3.example.com:9000/', 1 ],
[ 's3.example.com:9000', 1, 'https://maibucket.s3.example.com/', 0 ],
[ 's3.example.com:9000', 1, 'https://maibucket.s3.example.com:9001/', 0 ],
);
foreach my $case ( @cases ) {
my ( $endpoint, $secure, $location, $trusted ) = @{ $case };
my $req = _s3( endpoint => $endpoint, secure => $secure )
->request( 'CreateBucket', bucket => 'maibucket' );
is(
$req->_is_trusted_redirect( $location ) ? 1 : 0,
$trusted,
( $trusted ? 'trusts' : 'refuses' ) . " '$location' for endpoint '$endpoint'"
. ( $secure ? ' (secure)' : '' ),
);
}
}
note( "an untrusted redirect is not followed" );
foreach my $location (
'https://evil.com/',
'https://maibucket.s3.amazonaws.com.evil.com/',
'https://ec2.us-east-1.amazonaws.com/',
'http://maibucket.s3.amazonaws.com/',
'//evil.com/x',
) {
@Scripted::UA::SEEN = ();
@Scripted::UA::RESPONSES = ( _redirect_to( $location ) );
my $s3 = _s3();
eval { _create_bucket( $s3 ) };
like( $@, qr/Refusing to follow redirect to untrusted location/, "dies for '$location'" );
is( scalar @Scripted::UA::SEEN, 1, '... and the request was not replayed' );
}
note( "a control character in the location can't be used to forge the error message" );
{
@Scripted::UA::SEEN = ();
@Scripted::UA::RESPONSES = ( _redirect_to( "https://evil.com/\x01\x7f\x80" ) );
eval { _create_bucket( _s3() ) };
like( $@, qr/Refusing to follow redirect/, 'dies' );
unlike( $@, qr/[\x00-\x08\x0b-\x1f\x7f-\xff]/, '... without control characters in the message' );
}
note( "a trusted redirect is followed, and signed again for the new host" );
foreach my $case (
# location, region it is signed for (the client is configured for us-east-1)
[ 'https://maibucket.s3-us-west-2.amazonaws.com/', 'us-west-2' ],
[ 'https://maibucket.s3.us-west-2.amazonaws.com/', 'us-west-2' ],
[ 'https://s3.eu-west-1.amazonaws.com/maibucket/', 'eu-west-1' ],
[ 'https://maibucket.s3.amazonaws.com/', 'us-east-1' ],
[ '/relative/path', 'us-east-1' ], # resolved against the original request
) {
my ( $location, $region ) = @{ $case };
@Scripted::UA::SEEN = ();
@Scripted::UA::RESPONSES = ( _redirect_to( $location ) );
my $s3 = _s3();
my $parsed = eval { _create_bucket( $s3 ) };
is( $@, '', "no error for '$location'" );
isa_ok( $parsed, 'AWS::S3::ResponseParser' );
is( scalar @Scripted::UA::SEEN, 2, '... the request was sent twice' );
my ( $first, $second ) = @Scripted::UA::SEEN;
my $expected = URI->new_abs( $location, $first->uri )->as_string;
is( $second->uri->as_string, $expected, '... the second to the redirect location' );
is( $second->method, $first->method, '... with the same method' );
is( $second->content, $first->content, '... and the same body' );
( run in 0.572 second using v1.01-cache-2.11-cpan-eb134014566 )