httpStatus === $status, $message . ' Unexpected status: ' . $error->httpStatus); return; } throw new RuntimeException($message); }; $request = static function (string $body, string $contentType, ?string $declaredLength = null): object { return new class($body, $contentType, $declaredLength) { private string $body; private string $contentType; private ?string $declaredLength; public function __construct(string $body, string $contentType, ?string $declaredLength) { $this->body = $body; $this->contentType = $contentType; $this->declaredLength = $declaredLength; } public function getHeaderLine(string $name): string { if (strcasecmp($name, 'Content-Type') === 0) { return $this->contentType; } if (strcasecmp($name, 'Content-Length') === 0) { return $this->declaredLength ?? ''; } return ''; } public function getBody(): object { return new class($this->body) { private string $body; private int $offset = 0; public function __construct(string $body) { $this->body = $body; } public function isSeekable(): bool { return true; } public function rewind(): void { $this->offset = 0; } public function eof(): bool { return $this->offset >= strlen($this->body); } public function read(int $length): string { $chunk = substr($this->body, $this->offset, $length); $this->offset += strlen($chunk); return $chunk; } }; } }; }; $capsule = new Capsule(); $capsule->addConnection([ 'driver' => 'sqlite', 'database' => ':memory:', ], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $capsule->schema('default')->create('cloud_upload_staging', static function (Blueprint $table): void { $table->string('id')->primary(); $table->integer('host_id'); $table->string('purpose'); $table->string('file_key'); $table->integer('size_bytes'); $table->string('sha256'); $table->string('token_hash'); $table->integer('expires_at'); $table->integer('consumed_at')->nullable(); $table->integer('created_at'); }); $payload = "qcow2\0test-image"; $capture = CloudUploadStagingService::capture( $request($payload, 'application/octet-stream; charset=binary', (string) strlen($payload)), 'qcow2', 1, 1024 ); $assert(is_file($capture['path']) && (int) $capture['size_bytes'] === strlen($payload) && hash_file('sha256', $capture['path']) === $capture['sha256'] && preg_match('/^[a-f0-9]{64}\.qcow2$/D', $capture['file_key']) === 1, 'Upload capture did not create a bounded, content-addressed private file.'); $first = CloudUploadStagingService::issue($capture, 11, 'cloud-template'); $second = CloudUploadStagingService::issue($capture, 12, 'cloud-template'); $assert($first['id'] !== $second['id'] && $first['token'] !== $second['token'] && str_starts_with($first['url'], 'https://panel.example.test/base/api/internal/cloud/upload-staging/') && !str_contains($first['url'], $first['token']), 'Fan-out did not issue independent bearer capabilities per Agent host.'); $firstRow = CloudUploadStaging::find($first['id']); $assert($firstRow !== null && (string) $firstRow->token_hash === hash('sha256', $first['token']) && !str_contains((string) $firstRow->token_hash, $first['token']) && (int) $firstRow->host_id === 11, 'Staging persisted a plaintext token or lost its target host binding.'); $expect(static fn () => CloudUploadStagingService::consume( $first['id'], str_repeat('x', 64) ), 401, 'A staging capability accepted the wrong bearer token.'); $assert(CloudUploadStaging::find($first['id'])->consumed_at === null, 'A failed bearer check consumed the one-use capability.'); $tamperPayload = 'same-length-original'; $tamperCapture = CloudUploadStagingService::capture( $request($tamperPayload, 'application/octet-stream', (string) strlen($tamperPayload)), 'qcow2', 1, 1024 ); $tamper = CloudUploadStagingService::issue($tamperCapture, 14, 'cloud-template'); file_put_contents($tamperCapture['path'], str_repeat('x', strlen($tamperPayload))); $expect(static fn () => CloudUploadStagingService::consume( $tamper['id'], $tamper['token'] ), 503, 'Same-length staging content tampering bypassed SHA-256 verification.'); $tamperRow = CloudUploadStaging::find($tamper['id']); $assert($tamperRow !== null && $tamperRow->consumed_at === null, 'A failed staging digest check consumed the bearer capability.'); $tamperRow->expires_at = time() - 1; $tamperRow->save(); CloudUploadStagingService::cleanupExpired(); $assert(CloudUploadStaging::find($tamper['id']) === null && !is_file($tamperCapture['path']), 'Digest-failed staging content did not remain eligible for expiry cleanup.'); $consumed = CloudUploadStagingService::consume( $first['id'], $first['token'] ); $assert($consumed['path'] === $capture['path'] && $consumed['purpose'] === 'cloud-template' && CloudUploadStaging::find($first['id'])->consumed_at !== null, 'A valid bearer capability did not atomically consume its staging row.'); $expect(static fn () => CloudUploadStagingService::consume( $first['id'], $first['token'] ), 410, 'A consumed bearer capability was replayable.'); $assert(is_file($capture['path']), 'Shared staging content was removed before every host consumed it.'); CloudUploadStagingService::consume($second['id'], $second['token']); $expiredPayload = 'expired-image'; $expiredCapture = CloudUploadStagingService::capture( $request($expiredPayload, 'application/octet-stream', (string) strlen($expiredPayload)), 'iso', 1, 1024 ); $expired = CloudUploadStagingService::issue($expiredCapture, 13, 'rescue-template'); CloudUploadStaging::query()->where('id', $expired['id'])->update([ 'expires_at' => time() - 1, ]); CloudUploadStagingService::cleanupExpired(); $assert(CloudUploadStaging::find($expired['id']) === null && !is_file($expiredCapture['path']), 'Expired staging capability or its unreferenced file was not removed.'); $discardPayload = 'dispatch-failed'; $discardCapture = CloudUploadStagingService::capture( $request($discardPayload, 'application/octet-stream', (string) strlen($discardPayload)), 'qcow2', 1, 1024 ); CloudUploadStagingService::discard($discardCapture); $assert(!is_file($discardCapture['path']), 'A capture with no issued Agent capability was not discarded.'); $expect(static fn () => CloudUploadStagingService::capture( $request('invalid', 'text/plain', '7'), 'qcow2', 1, 1024 ), 415, 'Upload staging accepted a non-binary content type.'); $expect(static fn () => CloudUploadStagingService::capture( $request('mismatch', 'application/octet-stream', '7'), 'qcow2', 1, 1024 ), 400, 'Upload staging accepted a false Content-Length.'); $controller = file_get_contents(dirname(__DIR__) . '/app/Controllers/CloudUploadStagingController.php'); $routes = file_get_contents(dirname(__DIR__) . '/config/routes_cloud.php'); $assert(is_string($controller) && is_string($routes) && str_contains($routes, '/api/internal/cloud/upload-staging/{id:') && str_contains($controller, "preg_match('/^Bearer ([A-Za-z0-9_-]{32,512})$/D'") && str_contains($controller, 'CloudUploadStagingService::consume(') && str_contains($controller, "->withHeader('Digest'") && str_contains($controller, "->withHeader('Cache-Control', 'private, no-store, max-age=0')"), 'Private staging route lost strict Bearer authentication or download hardening.'); register_shutdown_function(static function () use ( $assert, $capture ): void { $assert(CloudUploadStaging::query()->count() === 0 && !is_file($capture['path']), 'Consumed staging rows or their final shared file survived response cleanup.'); $directory = dirname($capture['path']); @rmdir($directory); @rmdir(dirname($directory)); @rmdir(BASE_PATH); }); echo "Cloud upload staging bearer, one-use and cleanup contracts passed.\n"; }