Amazon-Signature4-Lite

 view release on metacpan or  search on metacpan

t/02-s3-path-encoding.t  view on Meta::CPAN

##
## SigV4 encodes the request path segment twice for every service EXCEPT
## S3, which is encoded once. Callers hand us a URL whose path is already
## percent-encoded once; re-encoding it for S3 turns an S3 key like
## '#S3.pm.in#' (already '%23S3.pm.in%23' in the URL) into
## '%2523S3.pm.in%2523' in the canonical request, so the signature no
## longer matches the wire path and S3 returns SignatureDoesNotMatch.
##
## This bites only keys containing reserved characters (#, space, %, ...);
## plain keys are unaffected because encoding is idempotent on the
## unreserved set -- which is why ordinary uploads work and an Emacs
## auto-save file (#name#) fails.

use strict;
use warnings;

use Test::More;

use_ok('Amazon::Signature4::Lite');

my %common = (

t/02-s3-path-encoding.t  view on Meta::CPAN

    headers => { host => 'b.s3.amazonaws.com', 'x-amz-date' => '20260101T000000Z' }, payload => 'p' )->{Authorization};
  my $sig_b = $x->sign( method => 'PUT', url => "https://b.s3.amazonaws.com$path",
    headers => { host => 'b.s3.amazonaws.com', 'x-amz-date' => '20260101T000000Z' }, payload => 'p' )->{Authorization};
  is( $sig_a, $sig_b, 's3 signing is deterministic for the same encoded path' );
}

# --- a plain key is unaffected either way (idempotent encoding) ---
{
  my $plain = '/lib/OrePAN2/S3.pm';
  is( Amazon::Signature4::Lite::_encode_path($plain), $plain,
    'plain keys are unchanged by _encode_path (why ordinary uploads work)' );
}

done_testing;



( run in 1.366 second using v1.01-cache-2.11-cpan-b16cb0d3907 )