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,



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