diff --git a/web/app/Services/Cloud/CloudActivePublicNetworkClaimSet.php b/web/app/Services/Cloud/CloudActivePublicNetworkClaimSet.php index 33d47863..4731dd29 100644 --- a/web/app/Services/Cloud/CloudActivePublicNetworkClaimSet.php +++ b/web/app/Services/Cloud/CloudActivePublicNetworkClaimSet.php @@ -13,6 +13,7 @@ final class CloudActivePublicNetworkClaimSet { private function __construct( private readonly int $reservationId, + private readonly int $hostId, private readonly array $claims, private readonly CloudPublicNetworkSnapshot $publicNetwork, private readonly string $digest @@ -27,10 +28,11 @@ final class CloudActivePublicNetworkClaimSet * @param array $claims * @param array $addresses keyed by public IP id */ - public static function fromLedger(int $reservationId, array $claims, - array $addresses, CloudPublicNetworkSnapshot $publicNetwork): self + public static function fromLedger(int $reservationId, int $hostId, + array $claims, array $addresses, + CloudPublicNetworkSnapshot $publicNetwork): self { - if ($reservationId < 1 || count($claims) !== 2) { + if ($reservationId < 1 || $hostId < 1 || count($claims) !== 2) { throw new CloudError('活动双栈公网地址账本不完整', 409); } $canonical = []; @@ -60,6 +62,7 @@ final class CloudActivePublicNetworkClaimSet || preg_match('/^[a-f0-9]{32}$/D', $operationId) !== 1 || !is_object($ip) || (int) ($ip->id ?? 0) !== $publicIpId + || (int) ($ip->host_id ?? 0) !== $hostId || (int) ($ip->address_family ?? 0) !== $family || (int) ($ip->reservation_id ?? 0) !== $reservationId || (int) ($ip->reservation_ip_claim_id ?? 0) !== $claimId @@ -101,7 +104,13 @@ final class CloudActivePublicNetworkClaimSet $addresses, $publicNetwork )->digest(); - return new self($reservationId, $canonical, $publicNetwork, $digest); + return new self( + $reservationId, + $hostId, + $canonical, + $publicNetwork, + $digest + ); } public function reservationId(): int @@ -109,6 +118,11 @@ final class CloudActivePublicNetworkClaimSet return $this->reservationId; } + public function hostId(): int + { + return $this->hostId; + } + public function claimId(int $family): int { if (!isset($this->claims[$family])) { diff --git a/web/app/Services/Cloud/CloudFrozenPublicNetworkIdentity.php b/web/app/Services/Cloud/CloudFrozenPublicNetworkIdentity.php index ed203989..fbb1860f 100644 --- a/web/app/Services/Cloud/CloudFrozenPublicNetworkIdentity.php +++ b/web/app/Services/Cloud/CloudFrozenPublicNetworkIdentity.php @@ -20,10 +20,11 @@ final class CloudFrozenPublicNetworkIdentity ) { } - public static function fromClaimSet(int $hostId, string $uuid, + public static function fromClaimSet(string $uuid, CloudActivePublicNetworkClaimSet $claimSet): self { $reservationId = $claimSet->reservationId(); + $hostId = $claimSet->hostId(); $networkAttachmentId = $claimSet->networkAttachmentId(); self::assertResourceIdentity( $reservationId, @@ -50,9 +51,8 @@ final class CloudFrozenPublicNetworkIdentity /** * Rebuild a frozen identity from its row and the current authoritative - * claim-set projection. The host id must come from the same locked - * reservation used to obtain the claim set. Top-level row columns remain - * separate so a self-consistent cross-target JSON replacement is rejected. + * claim-set projection. Top-level row columns remain separate so a + * self-consistent cross-target JSON replacement is rejected. */ public static function fromStored(int $reservationId, int $hostId, string $uuid, int $networkAttachmentId, string $identityJson, @@ -139,8 +139,9 @@ final class CloudFrozenPublicNetworkIdentity 'Frozen public network identity digest does not match.' ); } - $authoritative = self::fromClaimSet($hostId, $uuid, $claimSet); + $authoritative = self::fromClaimSet($uuid, $claimSet); if ($claimSet->reservationId() !== $reservationId + || $claimSet->hostId() !== $hostId || $claimSet->networkAttachmentId() !== $networkAttachmentId || !hash_equals( $authoritative->claimSetSha256(), diff --git a/web/app/Services/Cloud/CloudPublicNetworkAllocationService.php b/web/app/Services/Cloud/CloudPublicNetworkAllocationService.php index 23c21b60..b37d8534 100644 --- a/web/app/Services/Cloud/CloudPublicNetworkAllocationService.php +++ b/web/app/Services/Cloud/CloudPublicNetworkAllocationService.php @@ -1306,9 +1306,13 @@ final class CloudPublicNetworkAllocationService } $attachment = DB::table('cloud_host_network_attachments') ->where('id', $attachmentIds[0])->first(); - if ($attachment === null + if ($attachment === null) { + throw new CloudError('公网网络接入不存在', 409); + } + $attachmentHostId = (int) $attachment->host_id; + if ($attachmentHostId < 1 || ($expectedHostId !== null - && (int) $attachment->host_id !== $expectedHostId) + && $attachmentHostId !== $expectedHostId) || ($expectedLineId !== null && (int) $attachment->line_id !== $expectedLineId)) { throw new CloudError('公网网络接入不存在', 409); @@ -1326,9 +1330,8 @@ final class CloudPublicNetworkAllocationService if ($pool === null || $ip === null || (int) $pool->address_family !== $family || (int) $ip->address_family !== $family - || ($expectedHostId !== null - && ((int) $pool->host_id !== $expectedHostId - || (int) $ip->host_id !== $expectedHostId)) + || (int) $pool->host_id !== $attachmentHostId + || (int) $ip->host_id !== $attachmentHostId || (int) $pool->network_attachment_id !== $attachmentIds[0] || (int) $ip->public_ip_pool_id !== (int) $pool->id) { throw new CloudError('双栈公网地址引用损坏', 409); @@ -1367,6 +1370,7 @@ final class CloudPublicNetworkAllocationService ]); return CloudActivePublicNetworkClaimSet::fromLedger( (int) array_key_first($reservationIds), + $attachmentHostId, $claims, $addresses, $snapshot diff --git a/web/app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php b/web/app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php index 03f23c15..31c08e55 100644 --- a/web/app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php +++ b/web/app/Services/Cloud/CloudPublicNetworkTransitionWorkflow.php @@ -498,6 +498,7 @@ final class CloudPublicNetworkTransitionWorkflow ); $sourceSet = CloudActivePublicNetworkClaimSet::fromLedger( $reservationId, + (int) $sourceAttachment->host_id, array_values($activeClaims), [ (int) $sourceIps[4]->id => $sourceIps[4], diff --git a/web/tests/CloudPublicNetworkAllocationServiceTest.php b/web/tests/CloudPublicNetworkAllocationServiceTest.php index 5330fa9e..c3b7072b 100644 --- a/web/tests/CloudPublicNetworkAllocationServiceTest.php +++ b/web/tests/CloudPublicNetworkAllocationServiceTest.php @@ -486,7 +486,6 @@ $assert($mixedGeneration->generation(4) === 1 'A legal active pair required both families to share generation or operation.'); $frozenUuid = '123e4567-e89b-42d3-a456-426614174000'; $frozen = CloudFrozenPublicNetworkIdentity::fromClaimSet( - 1, $frozenUuid, $mixedGeneration ); @@ -533,6 +532,29 @@ $rejects(static fn () => CloudFrozenPublicNetworkIdentity::fromStored( $frozen->claimSetSha256(), $mixedGeneration ), 'A self-consistent attachment rewrite bypassed the current claim set.'); +$tamperedHost = json_decode( + $frozen->identityJson(), + true, + 16, + JSON_THROW_ON_ERROR +); +$tamperedHost['host_id'] = 2; +$tamperedHostJson = json_encode( + $tamperedHost, + JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES +); +$rejects(static fn () => CloudFrozenPublicNetworkIdentity::fromStored( + 500, + 2, + $frozenUuid, + 10, + $tamperedHostJson, + hash('sha256', $tamperedHostJson), + $frozen->publicNetworkJson(), + $frozen->publicNetworkSha256(), + $frozen->claimSetSha256(), + $mixedGeneration +), 'A self-consistent host rewrite bypassed the current claim set.'); $tamperedNetwork = $mixedGeneration->publicNetwork()->toArray(); $tamperedNetwork['network_selector'] = 'bridge:br1'; $tamperedNetwork = CloudPublicNetworkSnapshot::fromArray($tamperedNetwork); @@ -590,10 +612,18 @@ foreach ($activeRows as $activeRow) { ->where('id', (int) $activeRow->public_ip_id)->first(); $activeAddresses[(int) $activeRow->public_ip_id] = $address; } +$rejects(static fn () => CloudActivePublicNetworkClaimSet::fromLedger( + 500, + 2, + $activeRows, + $activeAddresses, + $mixedGeneration->publicNetwork() +), 'A wrong-host claim set could be constructed for frozen identity input.'); $mismatchedSnapshot = $mixedGeneration->publicNetwork()->toArray(); $mismatchedSnapshot['ipv4']['address_cidr'] = '8.8.8.11/24'; $rejects(static fn () => CloudActivePublicNetworkClaimSet::fromLedger( 500, + 1, $activeRows, $activeAddresses, CloudPublicNetworkSnapshot::fromArray($mismatchedSnapshot) diff --git a/web/tests/CloudPublicNetworkTransitionWorkflowTest.php b/web/tests/CloudPublicNetworkTransitionWorkflowTest.php index ffce3bf0..b0d0eb87 100644 --- a/web/tests/CloudPublicNetworkTransitionWorkflowTest.php +++ b/web/tests/CloudPublicNetworkTransitionWorkflowTest.php @@ -657,6 +657,7 @@ namespace { )); $activeDigest = CloudActivePublicNetworkClaimSet::fromLedger( 101, + 1, $sourceClaims, $sourceAddresses, $sourceNetwork diff --git a/web/tests/CloudRestoreMigrationLedgerContractTest.php b/web/tests/CloudRestoreMigrationLedgerContractTest.php index f69d2496..3c98ee52 100644 --- a/web/tests/CloudRestoreMigrationLedgerContractTest.php +++ b/web/tests/CloudRestoreMigrationLedgerContractTest.php @@ -204,9 +204,12 @@ $frozenIdentity = file_get_contents( ); $assert(is_string($frozenIdentity) && str_contains($frozenIdentity, - 'public static function fromClaimSet(int $hostId, string $uuid,') + 'public static function fromClaimSet(string $uuid,') && str_contains($frozenIdentity, 'CloudActivePublicNetworkClaimSet $claimSet') + && str_contains($frozenIdentity, '$hostId = $claimSet->hostId();') + && str_contains($frozenIdentity, + '$claimSet->hostId() !== $hostId') && str_contains($frozenIdentity, 'CloudPublicNetworkAllocationService::currentClaimSet()') && !str_contains($frozenIdentity,