| 1 |
<?php |
| 2 |
namespace Aws\S3; |
| 3 |
|
| 4 |
use Aws\Api\Service; |
| 5 |
use Aws\Api\Shape; |
| 6 |
use Aws\CommandInterface; |
| 7 |
use Aws\MetricsBuilder; |
| 8 |
use GuzzleHttp\Psr7; |
| 9 |
use InvalidArgumentException; |
| 10 |
use Psr\Http\Message\RequestInterface; |
| 11 |
use Psr\Http\Message\StreamInterface; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class ApplyChecksumMiddleware |
| 21 |
{ |
| 22 |
use CalculatesChecksumTrait; |
| 23 |
|
| 24 |
public const DEFAULT_CALCULATION_MODE = 'when_supported'; |
| 25 |
public const DEFAULT_ALGORITHM = 'crc32'; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public static $sha256 = [ |
| 34 |
'PutObject' => true, |
| 35 |
'UploadPart' => true, |
| 36 |
]; |
| 37 |
|
| 38 |
|
| 39 |
private $api; |
| 40 |
|
| 41 |
|
| 42 |
private $config; |
| 43 |
|
| 44 |
|
| 45 |
private $nextHandler; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static function wrap(Service $api, array $config = []) |
| 54 |
{ |
| 55 |
return function (callable $handler) use ($api, $config) { |
| 56 |
return new self($handler, $api, $config); |
| 57 |
}; |
| 58 |
} |
| 59 |
|
| 60 |
public function __construct( |
| 61 |
callable $nextHandler, |
| 62 |
Service $api, |
| 63 |
array $config = [] |
| 64 |
) |
| 65 |
{ |
| 66 |
$this->api = $api; |
| 67 |
$this->nextHandler = $nextHandler; |
| 68 |
$this->config = $config; |
| 69 |
} |
| 70 |
|
| 71 |
public function __invoke( |
| 72 |
CommandInterface $command, |
| 73 |
RequestInterface $request |
| 74 |
) { |
| 75 |
$next = $this->nextHandler; |
| 76 |
$name = $command->getName(); |
| 77 |
$body = $request->getBody(); |
| 78 |
$operation = $this->api->getOperation($name); |
| 79 |
$mode = $command['@context']['request_checksum_calculation'] |
| 80 |
?? $this->config['request_checksum_calculation'] |
| 81 |
?? self::DEFAULT_CALCULATION_MODE; |
| 82 |
|
| 83 |
$command->getMetricsBuilder()->identifyMetricByValueAndAppend( |
| 84 |
'request_checksum_calculation', |
| 85 |
$mode |
| 86 |
); |
| 87 |
|
| 88 |
|
| 89 |
$this->handleDeprecatedAddContentMD5($command); |
| 90 |
|
| 91 |
$checksumInfo = $operation['httpChecksum'] ?? []; |
| 92 |
$checksumMemberName = $checksumInfo['requestAlgorithmMember'] ?? ''; |
| 93 |
$checksumMember = !empty($checksumMemberName) |
| 94 |
? $operation->getInput()->getMember($checksumMemberName) |
| 95 |
: null; |
| 96 |
$checksumRequired = $checksumInfo['requestChecksumRequired'] ?? false; |
| 97 |
$requestedAlgorithm = $command[$checksumMemberName] ?? null; |
| 98 |
|
| 99 |
$shouldAddChecksum = $this->shouldAddChecksum( |
| 100 |
$mode, |
| 101 |
$checksumRequired, |
| 102 |
$checksumMember, |
| 103 |
$requestedAlgorithm |
| 104 |
); |
| 105 |
if ($shouldAddChecksum) { |
| 106 |
if (!$this->hasAlgorithmHeader($request)) { |
| 107 |
$supportedAlgorithms = array_map('strtolower', $checksumMember['enum'] ?? []); |
| 108 |
$algorithm = $this->determineChecksumAlgorithm( |
| 109 |
$supportedAlgorithms, |
| 110 |
$requestedAlgorithm, |
| 111 |
$checksumMemberName |
| 112 |
); |
| 113 |
$request = $this->addAlgorithmHeader($algorithm, $request, $body); |
| 114 |
|
| 115 |
$command->getMetricsBuilder()->identifyMetricByValueAndAppend( |
| 116 |
'request_checksum', |
| 117 |
$algorithm |
| 118 |
); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
if (isset(self::$sha256[$name]) && $command['ContentSHA256']) { |
| 124 |
$request = $request->withHeader( |
| 125 |
'X-Amz-Content-Sha256', |
| 126 |
$command['ContentSHA256'] |
| 127 |
); |
| 128 |
$command->getMetricsBuilder()->append( |
| 129 |
MetricsBuilder::FLEXIBLE_CHECKSUMS_REQ_SHA256 |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
return $next($command, $request); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
private function handleDeprecatedAddContentMD5(CommandInterface $command): void |
| 142 |
{ |
| 143 |
if (!empty($command['AddContentMD5'])) { |
| 144 |
trigger_error( |
| 145 |
'S3 no longer supports MD5 checksums. ' . |
| 146 |
'A CRC32 checksum will be computed and applied on your behalf.', |
| 147 |
E_USER_DEPRECATED |
| 148 |
); |
| 149 |
$command['ChecksumAlgorithm'] = self::DEFAULT_ALGORITHM; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
private function shouldAddChecksum( |
| 163 |
string $mode, |
| 164 |
bool $checksumRequired, |
| 165 |
?Shape $checksumMember, |
| 166 |
?string $requestedAlgorithm |
| 167 |
): bool |
| 168 |
{ |
| 169 |
return ($mode === 'when_supported' && $checksumMember) |
| 170 |
|| ($mode === 'when_required' |
| 171 |
&& ($checksumRequired || ($checksumMember && $requestedAlgorithm))); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
private function determineChecksumAlgorithm( |
| 182 |
array $supportedAlgorithms, |
| 183 |
?string $requestedAlgorithm, |
| 184 |
?string $checksumMemberName |
| 185 |
): string |
| 186 |
{ |
| 187 |
$algorithm = self::DEFAULT_ALGORITHM; |
| 188 |
|
| 189 |
if ($requestedAlgorithm) { |
| 190 |
$requestedAlgorithm = strtolower($requestedAlgorithm); |
| 191 |
if (!in_array($requestedAlgorithm, $supportedAlgorithms)) { |
| 192 |
throw new InvalidArgumentException( |
| 193 |
"Unsupported algorithm supplied for input variable {$checksumMemberName}. " . |
| 194 |
"Supported checksums for this operation include: " |
| 195 |
. implode(", ", $supportedAlgorithms) . "." |
| 196 |
); |
| 197 |
} |
| 198 |
$algorithm = $requestedAlgorithm; |
| 199 |
} |
| 200 |
|
| 201 |
return $algorithm; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
private function addAlgorithmHeader( |
| 212 |
string $requestedAlgorithm, |
| 213 |
RequestInterface $request, |
| 214 |
StreamInterface $body |
| 215 |
): RequestInterface |
| 216 |
{ |
| 217 |
$headerName = "x-amz-checksum-{$requestedAlgorithm}"; |
| 218 |
if (!$request->hasHeader($headerName)) { |
| 219 |
$encoded = self::getEncodedValue($requestedAlgorithm, $body); |
| 220 |
$request = $request->withHeader($headerName, $encoded); |
| 221 |
} |
| 222 |
|
| 223 |
return $request; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
private function hasAlgorithmHeader(RequestInterface $request): bool |
| 232 |
{ |
| 233 |
$headers = $request->getHeaders(); |
| 234 |
|
| 235 |
foreach ($headers as $name => $values) { |
| 236 |
if (stripos($name, 'x-amz-checksum-') === 0) { |
| 237 |
return true; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
return false; |
| 242 |
} |
| 243 |
} |