17, 'is_admin' => true, 'is_super_admin' => true, 'can_manage' => true, 'operation_id' => 'operation_12345678', 'session_id' => 'must-not-cross-host-boundary', ]; $body = '{"name":"测试"}'; $secret = 'test-secret-which-is-at-least-32-bytes'; $gatewaySecret = 'test-gateway-secret-at-least-32-bytes'; $nonce = '00112233445566778899aabbccddeeff'; foreach (["/v1/vms\\escape", '/v1/vms#fragment'] as $invalidTarget) { try { CloudAgentClient::headers('GET', $invalidTarget, '', $actor, $secret, 1800000000, $nonce); throw new RuntimeException('Unsafe Agent request target was accepted.'); } catch (InvalidArgumentException $error) { // Expected. } } $headers = CloudAgentClient::headers('post', '/v1/vms?wait=1', $body, $actor, $secret, 1800000000, $nonce); $actorBase64 = strtr($headers['X-Virt-Actor'], '-_', '+/'); $actorBase64 .= str_repeat('=', (4 - strlen($actorBase64) % 4) % 4); $decodedActor = json_decode(base64_decode($actorBase64, true), true, 32, JSON_THROW_ON_ERROR); $assert($decodedActor === [ 'user_id' => 17, 'is_admin' => true, 'is_super_admin' => true, 'can_manage' => true, 'operation_id' => 'operation_12345678', ], 'Agent actor leaked panel session state or changed its typed contract.'); $digest = hash('sha256', $body); $canonical = "POST\n/v1/vms?wait=1\n{$digest}\n1800000000\n{$nonce}\n" . $headers['X-Virt-Actor']; $assert($headers['X-Virt-Content-SHA256'] === $digest && $headers['X-Virt-Signature'] === hash_hmac('sha256', $canonical, $secret) && $headers['X-Virt-Content-SHA256'] === '827361636cbd9dfdb06cb0fc540ee89605f405bfd3e5dd36e2eec7d10908c600' && $headers['X-Virt-Actor'] === 'eyJ1c2VyX2lkIjoxNywiaXNfYWRtaW4iOnRydWUsImlzX3N1cGVyX2FkbWluIjp0cnVlLCJjYW5fbWFuYWdlIjp0cnVlLCJvcGVyYXRpb25faWQiOiJvcGVyYXRpb25fMTIzNDU2NzgifQ' && $headers['X-Virt-Signature'] === '9a3fd49fcedbe4d79f6bf8efc5c0867efb5b6d0bffdc9bca602d24507205dcd7', 'Agent inner HMAC contract changed.'); $history = []; $handler = new MockHandler([ new Response(200, ['Content-Type' => 'application/json'], json_encode(['status' => 'ok', 'service' => 'virt-host-agent', 'version' => 'test'], JSON_THROW_ON_ERROR)), new Response(401, ['Content-Type' => 'application/json'], json_encode(['error' => [ 'code' => 'unauthorized', 'message' => 'secret details', 'retryable' => false, 'dispatch_state' => 'not_dispatched', ]], JSON_THROW_ON_ERROR)), new Response(401, ['Content-Type' => 'application/json'], json_encode(['error' => [ 'code' => 'replayed_request', 'message' => 'request nonce has already been used', 'retryable' => false, ]], JSON_THROW_ON_ERROR)), new Response(400, ['Content-Type' => 'application/json'], json_encode(['error' => [ 'code' => 'gateway_request_invalid', 'message' => 'Agent gateway request is invalid', 'retryable' => false, 'dispatch_state' => 'not_dispatched', ]], JSON_THROW_ON_ERROR)), new Response(503, ['Content-Type' => 'application/json'], json_encode(['error' => [ 'code' => 'agent_busy', 'message' => 'Agent control request was not dispatched', 'retryable' => true, 'dispatch_state' => 'not_dispatched', ]], JSON_THROW_ON_ERROR)), new Response(403, ['Content-Type' => 'application/json'], json_encode(['error' => [ 'code' => 'gateway_forbidden', 'message' => 'Agent gateway request is forbidden', 'retryable' => false, 'dispatch_state' => 'not_dispatched', ]], JSON_THROW_ON_ERROR)), new Response(403, ['Content-Type' => 'application/json'], json_encode(['error' => [ 'code' => 'forbidden', 'message' => 'administrator privileges are required', 'retryable' => false, 'dispatch_state' => 'not_dispatched', ]], JSON_THROW_ON_ERROR)), new Response(404, ['Content-Type' => 'application/json'], json_encode(['error' => [ 'code' => 'not_found', 'message' => 'endpoint not found', 'retryable' => false, 'dispatch_state' => 'not_dispatched', ]], JSON_THROW_ON_ERROR)), new Response(405, ['Content-Type' => 'application/json'], json_encode(['error' => [ 'code' => 'method_not_allowed', 'message' => 'method is not allowed', 'retryable' => false, 'dispatch_state' => 'not_dispatched', ]], JSON_THROW_ON_ERROR)), new Response(503, ['Content-Type' => 'application/json'], json_encode(['error' => [ 'code' => 'nonce_store_unavailable', 'message' => 'authentication state is unavailable', 'retryable' => true, 'dispatch_state' => 'not_dispatched', ]], JSON_THROW_ON_ERROR)), new Response(422, ['Content-Type' => 'application/json'], json_encode(['error' => [ 'code' => 'invalid_request', 'message' => 'request is invalid', 'retryable' => false, ]], JSON_THROW_ON_ERROR)), new Response(422, ['Content-Type' => 'application/json'], json_encode(['error' => [ 'code' => 'invalid_request', 'message' => 'request is invalid', 'retryable' => false, 'dispatch_state' => 'not_dispatched', 'unexpected' => true, ]], JSON_THROW_ON_ERROR)), new Response(409, ['Content-Type' => 'application/json'], json_encode(['error' => [ 'code' => 'operation_failed', 'message' => 'private upstream detail', 'retryable' => false, 'operation' => [ 'operation_id' => 'operation_12345678', 'status' => 'failed', 'cause_code' => 'storage_clone_failed', 'error' => ['code' => 'creation_absent'], ], ]], JSON_THROW_ON_ERROR)), new Response(409, ['Content-Type' => 'application/json'], json_encode(['error' => [ 'code' => 'operation_failed', 'message' => 'private upstream detail', 'retryable' => false, 'operation' => [ 'operation_id' => 'operation_12345678', 'status' => 'failed', 'cause_code' => 'StorageCloneFailed', 'error' => ['code' => 'creation_absent'], ], ]], JSON_THROW_ON_ERROR)), ]); $stack = HandlerStack::create($handler); $stack->push(Middleware::history($history)); $client = new CloudAgentClient('42', $secret, new Client(['handler' => $stack]), 20.0, $gateway, $gatewaySecret); $health = $client->request('GET', '/v1/health', $actor); $assert($health['service'] === 'virt-host-agent', 'Gateway-transported Agent health response did not round-trip.'); $wireRequest = $history[0]['request']; $wireBody = (string) $wireRequest->getBody(); $envelope = json_decode($wireBody, true, 16, JSON_THROW_ON_ERROR); $gatewayActor = $wireRequest->getHeaderLine('X-Virt-Actor'); $gatewayCanonical = "POST\n/internal/agent/request\n" . hash('sha256', $wireBody) . "\n" . $wireRequest->getHeaderLine('X-Virt-Timestamp') . "\n" . $wireRequest->getHeaderLine('X-Virt-Nonce') . "\n" . $gatewayActor; $assert($wireRequest->getMethod() === 'POST' && $wireRequest->getUri()->getPath() === '/internal/agent/request' && ($envelope['agent_id'] ?? null) === '42' && ($envelope['type'] ?? null) === 'request' && preg_match('/^[a-f0-9]{32}$/D', $envelope['id'] ?? '') === 1 && ($envelope['method'] ?? null) === 'GET' && ($envelope['request_target'] ?? null) === '/v1/health' && ($envelope['body'] ?? null) === '' && isset($envelope['headers']['X-Virt-Signature']) && hash_equals(hash_hmac('sha256', $gatewayCanonical, $gatewaySecret), $wireRequest->getHeaderLine('X-Virt-Signature')), 'PHP did not preserve the signed request inside an authenticated local gateway envelope.'); try { $client->request('GET', '/v1/capabilities', $actor); throw new RuntimeException('Agent authentication failure was accepted.'); } catch (CloudAgentError $error) { $assert($error->agentCode === 'unauthorized' && $error->httpStatus === 502 && !$error->retryable && $error->safeValidationFailure && !$error->safePreDispatchFailure && !str_contains($error->getMessage(), 'secret details'), 'Agent errors lost typed handling or exposed upstream detail.'); } try { $client->request('POST', '/v1/vms', $actor, ['operation_id' => 'operation_12345678']); throw new RuntimeException('Agent replay rejection was accepted.'); } catch (CloudAgentError $error) { $assert($error->agentCode === 'replayed_request' && !$error->safeValidationFailure && !$error->safePreDispatchFailure, 'Agent replay rejection incorrectly claimed the original request was not dispatched.'); } try { $client->request('POST', '/v1/vms', $actor, ['operation_id' => 'operation_12345678']); throw new RuntimeException('Gateway validation failure was accepted.'); } catch (CloudAgentError $error) { $assert($error->agentCode === 'gateway_request_invalid' && !$error->retryable && $error->safeValidationFailure && $error->agentHttpStatus === 400, 'Typed local gateway rejection was treated as an ambiguous dispatch.'); } try { $client->request('POST', '/v1/vms', $actor, ['operation_id' => 'operation_12345678']); throw new RuntimeException('Definitive pre-dispatch busy response was accepted.'); } catch (CloudAgentError $error) { $assert($error->agentCode === 'agent_busy' && $error->retryable && !$error->safeValidationFailure && $error->safePreDispatchFailure && $error->agentHttpStatus === 503, 'Authenticated not-dispatched proof was not preserved for the durable queue.'); } try { $client->request('POST', '/v1/vms', $actor, ['operation_id' => 'operation_12345678']); throw new RuntimeException('Gateway authentication failure was accepted.'); } catch (CloudAgentError $error) { $assert($error->agentCode === 'gateway_forbidden' && !$error->retryable && $error->safeValidationFailure && !$error->safePreDispatchFailure && $error->agentHttpStatus === 403, 'Gateway authentication rejection was not terminal pre-dispatch.'); } foreach ([ ['forbidden', 403], ['not_found', 404], ['method_not_allowed', 405], ] as [$code, $status]) { try { $client->request('POST', '/v1/vms', $actor, ['operation_id' => 'operation_12345678']); throw new RuntimeException("Terminal {$code} response was accepted."); } catch (CloudAgentError $error) { $assert($error->agentCode === $code && $error->agentHttpStatus === $status && $error->safeValidationFailure && !$error->safePreDispatchFailure, "Terminal {$code} response lost its strict no-journal proof."); } } try { $client->request('POST', '/v1/vms', $actor, ['operation_id' => 'operation_12345678']); throw new RuntimeException('Nonce-store outage was accepted.'); } catch (CloudAgentError $error) { $assert($error->agentCode === 'nonce_store_unavailable' && $error->retryable && !$error->safeValidationFailure && $error->safePreDispatchFailure, 'Retryable Agent authentication-state failure lost its no-journal proof.'); } foreach (['missing marker', 'unexpected field'] as $case) { try { $client->request('POST', '/v1/vms', $actor, ['operation_id' => 'operation_12345678']); throw new RuntimeException("Loose {$case} response was accepted."); } catch (CloudAgentError $error) { $assert($error->agentCode === 'invalid_request' && !$error->safeValidationFailure && !$error->safePreDispatchFailure, "A {$case} envelope was accepted as definitive no-journal proof."); } } $journalFailure = $client->request('POST', '/v1/vms', $actor, ['operation_id' => 'operation_12345678']); $assert(($journalFailure['status'] ?? null) === 'failed' && ($journalFailure['cause_code'] ?? null) === 'storage_clone_failed' && ($journalFailure['error']['code'] ?? null) === 'creation_absent' && !isset($journalFailure['message']), 'A valid durable Agent cause code was lost or free-form detail escaped its envelope.'); $malformedCause = $client->request('POST', '/v1/vms', $actor, ['operation_id' => 'operation_12345678']); $assert(($malformedCause['status'] ?? null) === 'failed' && !isset($malformedCause['cause_code']), 'A malformed durable Agent cause code crossed the PHP trust boundary.'); echo "Agent outbound gateway and nested HMAC protocol contract passed.\n";