diff --git a/web/app/Services/Cloud/CloudObjectStorageS3Client.php b/web/app/Services/Cloud/CloudObjectStorageS3Client.php index a6db3e0a..0ce9484a 100644 --- a/web/app/Services/Cloud/CloudObjectStorageS3Client.php +++ b/web/app/Services/Cloud/CloudObjectStorageS3Client.php @@ -685,7 +685,7 @@ final class CloudObjectStorageS3Client * * @param array $config * @param array{access_key_id:string,secret_access_key:string} $credentials - * @return array{size_bytes:int,etag:string,version_id:string} + * @return array{size_bytes:int,sha256:string,etag:string,version_id:string} */ public function putImmutableFile( array $config, @@ -719,7 +719,7 @@ final class CloudObjectStorageS3Client $headers = CloudS3Signer::sign( 'PUT', $url, (string) $config['region'], $credentials['access_key_id'], $credentials['secret_access_key'], - time(), $sha256 + time(), $sha256, ['virt-sha256' => $sha256] ) + [ 'Content-Length' => (string) $sizeBytes, 'Content-Type' => 'application/octet-stream', @@ -786,6 +786,7 @@ final class CloudObjectStorageS3Client } return [ 'size_bytes' => $sizeBytes, + 'sha256' => $sha256, 'etag' => $etag, 'version_id' => $versionId, ]; @@ -855,7 +856,7 @@ final class CloudObjectStorageS3Client * * @param array $config * @param array{access_key_id:string,secret_access_key:string} $credentials - * @return array{size_bytes:int,etag:string,version_id:string} + * @return array{size_bytes:int,sha256:string,etag:string,version_id:string} */ public function headObject( array $config, @@ -880,7 +881,7 @@ final class CloudObjectStorageS3Client * * @param array $config * @param array{access_key_id:string,secret_access_key:string} $credentials - * @return array{size_bytes:int,etag:string,version_id:string}|null + * @return array{size_bytes:int,sha256:string,etag:string,version_id:string}|null */ public function inspectObject( array $config, @@ -930,8 +931,22 @@ final class CloudObjectStorageS3Client } $reportedVersion = trim($response->getHeaderLine('x-amz-version-id')); self::assertVersionId($reportedVersion); + $sha256 = strtolower(trim( + $response->getHeaderLine('x-amz-meta-virt-sha256') + )); + if ($sha256 !== '' + && preg_match('/^[a-f0-9]{64}$/D', $sha256) !== 1) { + throw new CloudObjectStorageTransferError( + '备份对象缺少可信的 SHA-256 元数据。', + 'object_head_invalid' + ); + } return [ 'size_bytes' => (int) $length, + // Empty means a legacy/untrusted object. Callers reconciling an + // unknown PUT must require an exact match, never infer identity + // from ETag (multipart and provider ETags are not content hashes). + 'sha256' => $sha256, 'etag' => $etag, 'version_id' => $reportedVersion, ]; diff --git a/web/app/Services/Cloud/CloudS3Signer.php b/web/app/Services/Cloud/CloudS3Signer.php index d90fcf1c..8437b4c1 100644 --- a/web/app/Services/Cloud/CloudS3Signer.php +++ b/web/app/Services/Cloud/CloudS3Signer.php @@ -12,6 +12,8 @@ final class CloudS3Signer /** * Return the request headers required for an AWS Signature Version 4 call. * + * @param array $metadata S3 user metadata without the + * x-amz-meta- prefix. Every entry is included in SignedHeaders. * @return array */ public static function sign( @@ -21,7 +23,8 @@ final class CloudS3Signer string $accessKeyId, string $secretAccessKey, int $timestamp, - ?string $payloadHash = null + ?string $payloadHash = null, + array $metadata = [] ): array { $parts = parse_url($url); if (!is_array($parts) || !isset($parts['scheme'], $parts['host'])) { @@ -37,10 +40,33 @@ final class CloudS3Signer $amzDate = gmdate('Ymd\THis\Z', $timestamp); $date = gmdate('Ymd', $timestamp); $host = self::hostHeader($parts); - $canonicalHeaders = 'host:' . $host . "\n" - . 'x-amz-content-sha256:' . $payloadHash . "\n" - . 'x-amz-date:' . $amzDate . "\n"; - $signedHeaders = 'host;x-amz-content-sha256;x-amz-date'; + $headers = [ + 'host' => $host, + 'x-amz-content-sha256' => $payloadHash, + 'x-amz-date' => $amzDate, + ]; + foreach ($metadata as $name => $value) { + if (!is_string($name) || !is_string($value)) { + throw new \InvalidArgumentException('Invalid S3 metadata'); + } + $name = strtolower(trim($name)); + if (preg_match('/^[a-z0-9][a-z0-9-]{0,63}$/D', $name) !== 1 + || $value === '' || strlen($value) > 2048 + || preg_match('/[\x00-\x1f\x7f]/', $value) === 1) { + throw new \InvalidArgumentException('Invalid S3 metadata'); + } + $header = 'x-amz-meta-' . $name; + if (isset($headers[$header])) { + throw new \InvalidArgumentException('Duplicate S3 metadata'); + } + $headers[$header] = trim($value); + } + ksort($headers, SORT_STRING); + $canonicalHeaders = ''; + foreach ($headers as $name => $value) { + $canonicalHeaders .= $name . ':' . $value . "\n"; + } + $signedHeaders = implode(';', array_keys($headers)); $canonicalRequest = $method . "\n" . self::canonicalUri((string) ($parts['path'] ?? '/')) . "\n" . self::canonicalQuery((string) ($parts['query'] ?? '')) . "\n" @@ -56,7 +82,7 @@ final class CloudS3Signer $signingKey = hash_hmac('sha256', 'aws4_request', $serviceKey, true); $signature = hash_hmac('sha256', $stringToSign, $signingKey); - return [ + $result = [ 'Host' => $host, 'X-Amz-Content-Sha256' => $payloadHash, 'X-Amz-Date' => $amzDate, @@ -64,6 +90,11 @@ final class CloudS3Signer . '/' . $scope . ',SignedHeaders=' . $signedHeaders . ',Signature=' . $signature, ]; + foreach ($metadata as $name => $value) { + $result['x-amz-meta-' . strtolower(trim((string) $name))] = + trim((string) $value); + } + return $result; } /** diff --git a/web/tests/CloudObjectStorageImmutablePutTest.php b/web/tests/CloudObjectStorageImmutablePutTest.php index ffcbee10..dbcbc328 100644 --- a/web/tests/CloudObjectStorageImmutablePutTest.php +++ b/web/tests/CloudObjectStorageImmutablePutTest.php @@ -26,7 +26,12 @@ $sha256 = hash_file('sha256', $path); // A real HTTP handler may release its request body before request() returns. // Closing the request body here deterministically reproduces that ownership // boundary without crossing the network. -$handler = static function (RequestInterface $request, array $options) { +$requestHeaders = []; +$handler = static function ( + RequestInterface $request, + array $options +) use (&$requestHeaders) { + $requestHeaders = $request->getHeaders(); $request->getBody()->close(); return Create::promiseFor(new Response(200, [ 'ETag' => '"closed-body-etag"', @@ -61,8 +66,14 @@ try { $assert($identity === [ 'size_bytes' => $bytes, + 'sha256' => $sha256, 'etag' => '"closed-body-etag"', 'version_id' => 'closed-body-version', ], 'Immutable PUT double-closed an HTTP-owned body stream.'); +$assert(($requestHeaders['x-amz-meta-virt-sha256'][0] ?? '') === $sha256 + && str_contains( + (string) ($requestHeaders['Authorization'][0] ?? ''), + 'x-amz-meta-virt-sha256' + ), 'Immutable PUT did not sign its frozen SHA-256 metadata.'); echo "Immutable object PUT stream ownership passed.\n"; diff --git a/web/tests/CloudS3SignerTest.php b/web/tests/CloudS3SignerTest.php index 78313ec7..2b374261 100644 --- a/web/tests/CloudS3SignerTest.php +++ b/web/tests/CloudS3SignerTest.php @@ -45,6 +45,35 @@ $reordered = CloudS3Signer::sign( $assert($reordered['Authorization'] === $expected, 'S3 query parameters were not canonicalized before signing.'); +$signedMetadata = CloudS3Signer::sign( + 'PUT', + 'https://examplebucket.s3.amazonaws.com/frozen.qcow2', + 'us-east-1', + 'AKIAIOSFODNN7EXAMPLE', + $secret, + $timestamp, + str_repeat('a', 64), + ['virt-sha256' => str_repeat('b', 64)] +); +$assert(($signedMetadata['x-amz-meta-virt-sha256'] ?? '') + === str_repeat('b', 64) + && str_contains( + $signedMetadata['Authorization'], + 'SignedHeaders=host;x-amz-content-sha256;x-amz-date;' + . 'x-amz-meta-virt-sha256' + ), 'Immutable object SHA metadata was not bound into SigV4.'); +try { + CloudS3Signer::sign( + 'PUT', 'https://examplebucket.s3.amazonaws.com/frozen.qcow2', + 'us-east-1', 'AKIAIOSFODNN7EXAMPLE', $secret, $timestamp, + str_repeat('a', 64), ['virt-sha256' => "forged\nvalue"] + ); + throw new RuntimeException('Unsafe S3 metadata was signed.'); +} catch (InvalidArgumentException $error) { + $assert(str_contains($error->getMessage(), 'metadata'), + 'Unsafe S3 metadata failed with the wrong error.'); +} + $presigned = CloudS3Signer::presign( 'GET', 'https://examplebucket.s3.amazonaws.com/test.txt?versionId=3%2FL4kqtJlcpXroDTDmJ%2B3Dc4Rk5w2wTZ', @@ -188,6 +217,7 @@ $head = $multipartClient->headObject( ); $assert($head === [ 'size_bytes' => 123456789, + 'sha256' => '', 'etag' => '"multipart-etag"', 'version_id' => 'version-1', ], 'HeadObject did not return the immutable object identity.');