From a6cccf5951f00fdb2ceab88bce2af9ad7447e999 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 29 Sep 2026 06:48:14 +0800 Subject: [PATCH] fix: preserve cloud image upload responses --- web/app/Services/Cloud/CloudEngineClient.php | 7 +++- web/tests/CloudTemplateErrorContractTest.php | 42 ++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/web/app/Services/Cloud/CloudEngineClient.php b/web/app/Services/Cloud/CloudEngineClient.php index 086cbe4d..bd361887 100644 --- a/web/app/Services/Cloud/CloudEngineClient.php +++ b/web/app/Services/Cloud/CloudEngineClient.php @@ -272,7 +272,12 @@ final class CloudEngineClient try { return $this->send('POST', $base . $path, ['headers' => $headers, 'body' => $stream, 'timeout' => 900], null, $path); } finally { - fclose($stream); + // Guzzle's PSR-7 stream owns and may close the supplied resource + // before request() returns. Avoid replacing the real engine + // response with a PHP 8 TypeError from closing it a second time. + if (is_resource($stream)) { + fclose($stream); + } } } diff --git a/web/tests/CloudTemplateErrorContractTest.php b/web/tests/CloudTemplateErrorContractTest.php index 7deb3520..bd1d4851 100644 --- a/web/tests/CloudTemplateErrorContractTest.php +++ b/web/tests/CloudTemplateErrorContractTest.php @@ -40,12 +40,20 @@ namespace GuzzleHttp { class Client { + public int $closedUploadBodies = 0; + public function __construct(private array $responses = []) { } public function request(string $method, string $url, array $options): TestResponse { + // Match Guzzle PSR-7 ownership: a resource supplied as the body + // may already be closed by the time request() returns. + if (is_resource($options['body'] ?? null)) { + fclose($options['body']); + $this->closedUploadBodies++; + } $response = array_shift($this->responses); if (!$response instanceof TestResponse) { throw new \RuntimeException('No fake engine response was queued.'); @@ -151,5 +159,39 @@ try { 'An arbitrary engine error escaped instead of the generic failure.'); } +$uploadFile = tempnam(sys_get_temp_dir(), 'cloud-upload-contract-'); +if ($uploadFile === false) { + throw new RuntimeException('Unable to prepare the upload resource contract fixture.'); +} +if (file_put_contents($uploadFile, 'qcow2-contract') === false) { + @unlink($uploadFile); + throw new RuntimeException('Unable to write the upload resource contract fixture.'); +} +try { + $uploadTransport = new Client([ + new TestResponse(202, '{"job_id":"upload-contract"}'), + ]); + $uploadClient = new CloudEngineClient($uploadTransport); + $uploadResult = $uploadClient->upload($uploadPath, $actor, $uploadFile); + $assert(($uploadResult['job_id'] ?? null) === 'upload-contract' + && $uploadTransport->closedUploadBodies === 1, + 'A client-owned upload stream replaced a successful engine response with TypeError.'); + + $failedUploadTransport = new Client([ + new TestResponse(422, json_encode(['error' => $uploadMessage], JSON_UNESCAPED_UNICODE)), + ]); + $failedUploadClient = new CloudEngineClient($failedUploadTransport); + try { + $failedUploadClient->upload($uploadPath, $actor, $uploadFile); + throw new RuntimeException('A rejected cloud-template upload did not raise CloudError.'); + } catch (CloudError $error) { + $assert($error->getMessage() === $uploadMessage && $error->httpStatus === 422 + && $failedUploadTransport->closedUploadBodies === 1, + 'Closing a client-owned upload stream masked the reviewed engine error.'); + } +} finally { + @unlink($uploadFile); +} + echo "Cloud-template engine error allowlist contract passed.\n"; }