AWS-S3

 view release on metacpan or  search on metacpan

lib/AWS/S3/Roles/Request.pm  view on Meta::CPAN

use AWS::S3::Types;
use AWS::S3::Signer::V4;
use Log::Any qw( $LOG );

has 's3' => (
    is       => 'ro',
    isa      => 'AWS::S3',
    required => 1,
);

has 'type' => (
    is       => 'ro',
    isa      => 'Str',
    required => 1,
);

has 'protocol' => (
    is      => 'ro',
    isa     => 'Str',
    lazy    => 1,
    default => sub {
        shift->s3->secure ? 'https' : 'http';
    }
);

has 'endpoint' => (
    is      => 'ro',
    isa     => 'Str',
    lazy    => 1,
    default => sub {
        shift->s3->endpoint;
    }
);

# XXX should be required=>1; https://rt.cpan.org/Ticket/Display.html?id=77863
has "_action" => (
    isa       => 'Str',
    is        => 'ro',
    init_arg  => undef,
    #required  => 1
);

has '_expect_nothing' => ( isa => 'Bool', is => 'ro', init_arg => undef );

has '_uri' => (
    isa     => Uri,
    is      => 'ro',
    lazy    => 1,
    default => sub {
        my $self = shift;
        my $m = $self->meta;

        my $uri = URI->new(
            $self->protocol . '://'
            . ( $m->has_attribute('bucket') ? $self->bucket . '.' : '' )
            . $self->endpoint
            . '/'
        );

		# note we add some extra exceptions to uri_escape to prevent
		# encoding of things like "/", ":", "="
        if ( $m->has_attribute('key') ) {
            my $escaped_path = $self->_escape_key( $self->key );

            # if we have a leading slash in the key name we need to add *another*
            # slash in the call to ->path to ensure it is retained
            if ( $escaped_path =~ m!^/! && $self->s3->honor_leading_slashes ) {
                $uri->path( '/'.$escaped_path )
            } else {
                $uri->path( $escaped_path )
            }
        }

        $uri->query_keywords( $self->_subresource )
          if $m->has_attribute('_subresource');

        $uri;
    }
);

has 'signerv4' => (
    is       => 'ro',
    isa      => 'AWS::S3::Signer::V4',
    lazy     => 1,
    default  => sub {
        my $s = shift;
        AWS::S3::Signer::V4->new(
            -access_key => $s->s3->access_key_id,
            -secret_key => $s->s3->secret_access_key,
        );
    }
);

# uri_escape() croaks on characters above 0xFF, so encode those as UTF-8
# first, which is what URI does too. Byte strings are escaped as they were.
sub _uri_escape {
    my ( $s, $str, $unsafe ) = @_;

    return '' unless defined $str;
    utf8::encode( $str ) if $str =~ /[^\x00-\xFF]/;

    return defined $unsafe ? uri_escape( $str, $unsafe ) : uri_escape( $str );
}

# Escape an object key for use as the path of a URI; note we add some extra
# exceptions to uri_escape to prevent encoding of things like "/", ":", "="
sub _escape_key {
    my ( $s, $key ) = @_;

    return $s->_uri_escape( $key, "^A-Za-z0-9\-\._~\/:=" );
}

sub _send_request {
    my ( $s, $method, $uri, $headers, $content ) = @_;
    $LOG->debug('Making AWS request', {method => $method, uri => "$uri"});

    my $req = HTTP::Request->new( $method => $uri );
    $req->content( $content ) if $content;

    delete($headers->{Authorization}); # we will use a v4 signature
    map { $req->header( $_ => $headers->{$_} ) } keys %$headers;

    $s->_sign($req);
    my $res = $s->s3->ua->request( $req );

    # After creating a bucket and setting its location constraint, we get this
    # strange 'TemporaryRedirect' response.  Deal with it.
    if ( $res->header( 'location' ) && $res->content =~ m{>TemporaryRedirect<}s ) {
        my $location = eval { URI->new_abs( $res->header( 'location' ), $req->uri )->as_string };

        # never replay a signed request (which includes the session token and
        # the body) to somewhere we have no reason to trust, the response may
        # have been tampered with, particularly if we are not using https
        unless ( defined $location && $s->_is_trusted_redirect( $location ) ) {
            ( my $safe = $res->header( 'location' ) ) =~ s/[^\x21-\x7e]/?/g;
            $LOG->error( 'Refusing to follow redirect to untrusted location', { location => $safe } );
            die "Refusing to follow redirect to untrusted location '$safe'";
        }

        # the signature covers the host so the request must be signed again,
        # and the region may differ, a bucket in another region redirects us to
        # the regional endpoint, so take it from the host where we can
        $req->uri( $location );
        $req->remove_header( qw( Authorization Host X-Amz-Date ) );
        my ( undef, $region ) = $s->signerv4->parse_host( $req->uri->host );
        $s->_sign( $req, $region );
        $res = $s->s3->ua->request( $req );
    }
    return $s->parse_response( $res );
}

# A redirect is only trusted if it stays within the configured endpoint (or
# is an S3 hostname when that is an AWS endpoint), keeps to the same port, and
# doesn't downgrade from https.
sub _is_trusted_redirect {
    my ( $s, $location ) = @_;

    my $uri = eval { URI->new( $location ) } or return 0;

    my $scheme = $uri->scheme // '';
    return 0 unless $scheme eq 'http' || $scheme eq 'https';
    return 0 if $s->s3->secure && $scheme ne 'https';
    return 0 if defined $uri->userinfo;

    my $host = lc( $uri->host // '' );
    $host =~ s/\.\z//;



( run in 0.790 second using v1.01-cache-2.11-cpan-39bc3f1cbff )