fix: restore instance metadata after SSR

dev
admin 2 months ago
parent ea84d9519e
commit 352ac4bdb8

@ -128,6 +128,40 @@ class FakeVMService:
for vm in [self.vm, *self.other_vms]
]
def list_vm_metadata(self, vm_ids):
self.calls.append(("list_vm_metadata", tuple(vm_ids)))
if self.list_error:
raise VMError("libvirt API 不可用", 502)
by_id = {vm["uuid"]: vm for vm in [self.vm, *self.other_vms]}
rows = []
for vm_id in vm_ids:
if vm_id not in by_id:
continue
vm = by_id[vm_id]
source = vm.get("os_source", "unknown")
disks = [
{"capacity_gib": disk.get("capacity_gib")}
for disk in vm.get("disks", [])
]
capacities = [
disk["capacity_gib"] for disk in disks
if isinstance(disk.get("capacity_gib"), (int, float))
and disk["capacity_gib"] > 0
]
rows.append({
"uuid": vm["uuid"], "name": vm["name"],
"vcpu": vm.get("vcpu"), "memory_mib": vm.get("memory_mib"),
"disks": disks,
"disk_gib": round(sum(capacities), 2) if capacities else None,
"autostart": vm.get("autostart", False),
"managed": vm.get("managed", False),
"public_ip_cidr": vm.get("public_ip_cidr"),
"created_at": vm.get("created_at"), "os_name": vm.get("os_name"),
"os_source": source, "install_iso": vm.get("install_iso"),
"login_name": "root" if source == "cloud_template" else None,
})
return rows
def get_vm(self, vm_id):
self.calls.append(("get_vm", vm_id))
if vm_id != self.vm["uuid"]:
@ -878,6 +912,42 @@ def test_vm_crud_actions_and_error_mapping(web):
assert all("command=/fake/ssh-wrapper" in p["libvirt_uri"] for p in seen)
def test_vm_metadata_reads_only_selected_ids_and_has_no_credentials(web):
client, _, token, service, _, _ = web
csrf = setup_admin(client, token)
configure_host(client, csrf)
service.calls.clear()
service.vm.update({
"managed": True, "os_source": "cloud_template", "os_name": "AlmaLinux 10",
"public_ip_cidr": "192.0.2.10/24", "created_at": "2026-09-28T00:00:00Z",
})
response = client.get(f"/api/v1/vm-metadata?ids={VM_ID}")
assert response.status_code == 200, response.text
assert response.json() == {"items": [{
"uuid": VM_ID, "name": "alma-test", "vcpu": 2, "memory_mib": 2048,
"disks": [], "disk_gib": None, "autostart": False, "managed": True,
"public_ip_cidr": "192.0.2.10/24", "created_at": "2026-09-28T00:00:00Z",
"os_name": "AlmaLinux 10", "os_source": "cloud_template",
"install_iso": None, "login_name": "root",
}]}
assert service.calls == [("list_vm_metadata", (VM_ID,))]
assert "credential" not in response.text and "password" not in response.text
assert "console" not in response.text
assert all(set(disk) == {"capacity_gib"}
for disk in response.json()["items"][0]["disks"])
stale_id = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"
mixed = client.get(f"/api/v1/vm-metadata?ids={stale_id},{VM_ID}")
assert mixed.status_code == 200, mixed.text
assert [item["uuid"] for item in mixed.json()["items"]] == [VM_ID]
invalid = client.get("/api/v1/vm-metadata?ids=not-a-uuid")
assert invalid.status_code == 400
too_many = client.get("/api/v1/vm-metadata?ids=" + ",".join([VM_ID] * 101))
assert too_many.status_code == 400
def test_vm_create_uses_vcpu_without_a_separate_nic_queue_field(web):
client, _, token, service, _, _ = web
csrf = setup_admin(client, token)

@ -1069,6 +1069,29 @@ def test_lightweight_vm_state_reads_are_allowlisted(boundary):
assert ("get_vm_state", VM_ID) in services["hk"].calls
def test_selected_vm_metadata_is_allowlisted_and_owner_scoped(boundary):
client, engine, _store, _logs, services = boundary
path = f"/internal/v1/hosts/hk/vm-metadata?ids={VM_ID}"
admin = call(client, "GET", path, SUPER)
assert admin.status_code == 200, admin.text
assert admin.json()["items"][0]["uuid"] == VM_ID
assert services["hk"].calls[-1] == ("list_vm_metadata", (VM_ID,))
component = engine.registry.components["hk"]
scope = host_scope(component.manager.profile)
component.state.users.assign_vm(scope, VM_ID, USER["user_id"])
owner = call(client, "GET", path, USER)
assert owner.status_code == 200, owner.text
calls_after_owner = services["hk"].calls.count(("list_vm_metadata", (VM_ID,)))
assert calls_after_owner == 2
stranger = call(client, "GET", path, {**USER, "user_id": 42})
assert stranger.status_code == 404
assert stranger.json()["error"] == "虚拟机不存在"
assert services["hk"].calls.count(("list_vm_metadata", (VM_ID,))) == calls_after_owner
def test_only_paid_operations_can_create_and_retry_never_duplicates(boundary):
client, engine, store, _, services = boundary
path = "/internal/v1/hosts/hk/vms"

@ -404,6 +404,12 @@ class FakeConnection:
assert flags == 0
return list(self.backend.domains)
def lookupByUUIDString(self, vm_uuid):
for domain in self.backend.domains:
if domain.UUIDString() == vm_uuid:
return domain
raise FakeLibvirtError("domain not found")
def getInfo(self):
return ("x86_64", 0, 0, 0, 0, 0, 0, 0)
@ -805,6 +811,30 @@ class VMServiceTests(unittest.TestCase):
self.assertEqual(self.service.get_vm_state(vm["uuid"]), {
"uuid": vm["uuid"], "name": "alma-test", "state": "running",
})
metadata = self.service.list_vm_metadata([vm["uuid"]])
self.assertEqual(len(metadata), 1)
self.assertEqual(metadata[0]["uuid"], vm["uuid"])
self.assertEqual(metadata[0]["os_source"], "iso")
self.assertEqual(metadata[0]["install_iso"], "AlmaLinux-10.iso")
self.assertIsNone(metadata[0]["login_name"])
self.assertEqual(metadata[0]["disk_gib"], 20)
self.assertEqual(metadata[0]["disks"], [{"capacity_gib": 20}])
self.assertNotIn("source", metadata[0]["disks"][0])
self.assertNotIn("state", metadata[0])
self.assertNotIn("console_port", metadata[0])
self.assertNotIn("credential_generation", metadata[0])
with self.assertRaises(self.service_module.VMError) as empty:
self.service.list_vm_metadata([])
self.assertEqual(empty.exception.status, 400)
with self.assertRaises(self.service_module.VMError) as excessive:
self.service.list_vm_metadata([vm["uuid"]] * 101)
self.assertEqual(excessive.exception.status, 400)
missing_id = str(uuid.uuid4())
self.assertEqual(self.service.list_vm_metadata([missing_id]), [])
self.assertEqual(
self.service.list_vm_metadata([missing_id, vm["uuid"]]),
metadata,
)
disk_name = "alma-test.qcow2"
self.assertIn(disk_name, self.backend.pools["images"].volumes)
root = self.backend.domains[0].root
@ -1630,6 +1660,13 @@ class VMServiceTests(unittest.TestCase):
self.assertRegex(created["created_at"], r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$")
self.assertEqual(created["os_name"], "AlmaLinux 10")
self.assertEqual(created["os_source"], "cloud_template")
metadata = service.list_vm_metadata([created["uuid"]])[0]
self.assertEqual(metadata["public_ip_cidr"], "8.8.8.18/24")
self.assertEqual(metadata["os_name"], "AlmaLinux 10")
self.assertEqual(metadata["os_source"], "cloud_template")
self.assertEqual(metadata["login_name"], "root")
self.assertEqual(metadata["disks"], [{"capacity_gib": 20}])
self.assertNotIn("source", metadata["disks"][0])
self.assertEqual(self.backend.opened, self.backend.closed)
def test_cloud_vm_delete_disks_removes_clone_and_seed_but_keeps_template(self):

@ -2184,6 +2184,38 @@ def create_app(
vms = [vm for vm in vms if vm["uuid"] in owned_ids]
return {"items": vms}
@app.get("/api/v1/vm-metadata")
def list_vm_metadata(
ids: str = Query(..., min_length=1, max_length=4096),
_session: Session = Depends(require_approved),
):
raw_ids = ids.split(",")
if not 1 <= len(raw_ids) <= 100 or any(not value for value in raw_ids):
raise VMError("虚拟机元数据查询数量必须为 1 到 100", 400)
canonical_ids: list[str] = []
seen: set[str] = set()
for value in raw_ids:
try:
canonical = str(uuid.UUID(value))
except (TypeError, ValueError, AttributeError) as exc:
raise VMError("虚拟机 UUID 无效", 400) from exc
if canonical not in seen:
canonical_ids.append(canonical)
seen.add(canonical)
profile = current_profile()
if database_mode:
account = account_for_session(_session)
if not account["admin"]:
owned_ids = state.users.list_owned_vm_ids(
host_scope(profile), account["id"]
)
if any(vm_id not in owned_ids for vm_id in canonical_ids):
# Reject the complete request rather than disclosing which
# UUID in a mixed-owner batch exists on this host.
raise VMError("虚拟机不存在", 404)
return {"items": new_service(profile).list_vm_metadata(canonical_ids)}
@app.get("/api/v1/vms/{vm_id}/state")
def get_vm_state(vm_id: str, _session: Session = Depends(require_approved)):
profile = current_profile()

@ -42,7 +42,7 @@ VM_ID = r"[0-9a-fA-F-]{36}"
JSON_LIMIT = 1024 * 1024
READ_PATHS = (
r"host", r"resources", r"vm-templates", r"rescue-templates", r"user-isos", r"vms",
r"vm-states",
r"vm-states", r"vm-metadata",
r"user-isos/[0-9a-f]{32}",
rf"vms/{VM_ID}(?:/state|/metrics|/traffic/daily|/rescue)?",
r"(?:reinstall-jobs|rescue-jobs|vm-templates/import-jobs|rescue-templates/import-jobs|user-isos/import-jobs)/[A-Za-z0-9_-]+",

@ -508,6 +508,75 @@ class VMService:
"state": STATES.get(info[0], "unknown"),
}
def _vm_metadata_dict(self, domain: Any) -> dict[str, Any]:
"""Return the static fields needed to repair the PHP metadata cache.
Unlike :meth:`_vm_dict`, this deliberately omits live state, console
details, rescue state and credential-generation identifiers. It does
parse the selected domain's XML and inspect its disks, but callers can
select a bounded UUID set instead of loading the complete host
inventory.
"""
info = domain.info()
root = self._vm_xml(domain)
disks = self._disk_rows(root)
for disk in disks:
identifier = disk["target"] or disk["source"]
if not identifier:
continue
try:
capacity, _allocation, _physical = domain.blockInfo(identifier, 0)
except libvirt.libvirtError:
continue
if isinstance(capacity, int) and capacity > 0:
disk["capacity_gib"] = round(capacity / GIB, 2)
safe_disks = [
{"capacity_gib": disk.get("capacity_gib")}
for disk in disks
]
known_capacities = [
disk["capacity_gib"] for disk in safe_disks
if isinstance(disk.get("capacity_gib"), (int, float))
and not isinstance(disk.get("capacity_gib"), bool)
and disk["capacity_gib"] > 0
]
managed = root.find(f"./metadata/{{{PANEL_NS}}}managed")
os_source = managed.get("os-source") if managed is not None else None
if os_source not in {"cloud_template", "iso"}:
os_source = "unknown"
try:
autostart = bool(domain.autostart())
except libvirt.libvirtError:
autostart = False
return {
"uuid": domain.UUIDString(),
"name": domain.name(),
"vcpu": info[3],
"memory_mib": info[1] // 1024,
# Storage paths and volume names are host-internal details. The
# PHP boundary only needs capacities for the instance summary.
"disks": safe_disks,
"disk_gib": round(sum(known_capacities), 2) if known_capacities else None,
"autostart": autostart,
"managed": managed is not None,
"public_ip_cidr": (
managed.get("public-ip-cidr") if managed is not None else None
),
"created_at": managed.get("created-at") if managed is not None else None,
"os_name": (
managed.get("os-name")
if managed is not None and os_source == "cloud_template" else None
),
"os_source": os_source,
"install_iso": (
managed.get("install-iso")
if managed is not None and os_source == "iso" else None
),
"login_name": "root" if os_source == "cloud_template" else None,
}
def list_vm_states(self) -> list[dict[str, Any]]:
"""Fetch lightweight power state for every persistent domain."""
with self._connect() as conn:
@ -521,6 +590,34 @@ class VMService:
continue
return sorted(rows, key=lambda row: row["name"].casefold())
def list_vm_metadata(self, vm_ids: list[str]) -> list[dict[str, Any]]:
"""Fetch static metadata for at most 100 explicitly selected domains."""
if not isinstance(vm_ids, list) or not 1 <= len(vm_ids) <= 100:
raise VMError("虚拟机元数据查询数量必须为 1 到 100", 400)
canonical_ids: list[str] = []
seen: set[str] = set()
for value in vm_ids:
canonical = self._uuid(value)
if canonical not in seen:
canonical_ids.append(canonical)
seen.add(canonical)
with self._connect() as conn:
rows: list[dict[str, Any]] = []
for vm_id in canonical_ids:
try:
# Resolve only the domains requested by the visible page.
# This avoids enumerating every guest on a large host.
domain = conn.lookupByUUIDString(vm_id)
rows.append(self._vm_metadata_dict(domain))
except (libvirt.libvirtError, VMError):
# Ownership is resolved by the API before this host query.
# A database row can nevertheless become stale after an
# operator deletes the domain directly, or disappear while
# its XML is being read. Do not let that one row prevent the
# rest of this bounded batch from refreshing its metadata.
continue
return rows
def provisioning_absent(self, vm_uuid: str, name: str) -> bool:
"""Only certify refund safety after a fresh host/volume inventory.

@ -17,6 +17,7 @@ use App\Services\Cloud\CloudAccess;
use App\Services\Cloud\CloudEngineClient;
use App\Services\Cloud\CloudError;
use App\Services\Cloud\CloudInput;
use App\Services\Cloud\CloudInstanceMetadata;
use App\Services\Cloud\CloudInstancePageService;
use App\Services\Cloud\CloudJobService;
use App\Services\Cloud\CloudInvoicePageService;
@ -519,6 +520,87 @@ final class CloudApiController
));
}
/**
* Load immutable libvirt metadata for only the actor-visible rows on the
* current page. This is intentionally separate from the frequent power
* state poll: XML and disk inspection happens once after SSR, then the
* sanitized result is cached in cloud_instances.spec_json.
*/
public function vmMetadata($request, $response)
{
$actor = $this->actor($request);
$raw = $request->getQueryParams()['ids'] ?? '';
if (is_array($raw) || preg_match('/^[1-9][0-9]*(?:,[1-9][0-9]*){0,19}$/D',
(string) $raw) !== 1) {
throw new CloudError('实例编号列表无效');
}
$ids = array_values(array_unique(array_map('intval', explode(',', (string) $raw))));
return $this->json($response, ['vms' => $this->vmMetadataRows($actor, $ids)]);
}
/** Load one detail page's static metadata without returning credentials. */
public function vmMetadataOne($request, $response, $args)
{
$actor = $this->actor($request);
$id = filter_var($args['id'] ?? null, FILTER_VALIDATE_INT,
['options' => ['min_range' => 1]]);
if ($id === false) {
throw new CloudError('实例编号无效');
}
$rows = $this->vmMetadataRows($actor, [(int) $id]);
if ($rows === []) {
// The actor-scoped database lookup deliberately does not disclose
// whether a foreign instance exists.
throw new CloudError('虚拟机不存在或无权访问', 404);
}
return $this->json($response, $rows[0]);
}
private function vmMetadataRows(array $actor, array $ids): array
{
$snapshots = CloudInstancePageService::forActorIds($actor, $ids);
$live = [];
$engine = new CloudEngineClient();
foreach ($this->vmSnapshotsByHost($snapshots) as $hostId => $group) {
$allowed = [];
$hostLive = [];
foreach ($group as $snapshot) {
$uuid = strtolower((string) $snapshot['uuid']);
$allowed[$uuid] = true;
}
try {
$payload = $engine->host((int) $hostId, 'GET', 'vm-metadata?ids=' .
rawurlencode(implode(',', array_keys($allowed))), $actor);
foreach (is_array($payload['items'] ?? null) ? $payload['items'] : [] as $item) {
if (!is_array($item) || !is_string($item['uuid'] ?? null)) {
continue;
}
$uuid = strtolower(trim($item['uuid']));
if (isset($allowed[$uuid])) {
$hostLive[$uuid] = $item;
$live[(int) $hostId . ':' . $uuid] = $item;
}
}
} catch (CloudError $error) {
// Static database fields stay usable when one host is offline;
// another host's metadata can still be returned and cached.
}
CloudInstanceMetadata::persistBatch($group, $hostLive);
}
$rows = [];
foreach ($snapshots as $snapshot) {
$uuid = strtolower((string) $snapshot['uuid']);
$key = (int) $snapshot['host_id'] . ':' . $uuid;
// Return the actor-visible identity even while its host is
// offline. The browser keeps the SSR fields and can distinguish
// temporary metadata unavailability from a missing instance.
$rows[] = CloudInstanceMetadata::publicView(
$snapshot, $live[$key] ?? []
);
}
return $rows;
}
/**
* Lightweight live state for the visible list page. Database ownership is
* resolved before any engine response is considered, and engine UUIDs are
@ -549,7 +631,7 @@ final class CloudApiController
}
$uuid = strtolower(trim($item['uuid']));
if (isset($allowed[$uuid])) {
$live[$uuid] = $item;
$live[(int) $hostId . ':' . $uuid] = $item;
}
}
} catch (CloudError $error) {
@ -560,8 +642,9 @@ final class CloudApiController
$statuses = [];
foreach ($snapshots as $snapshot) {
$uuid = strtolower((string) $snapshot['uuid']);
$key = (int) $snapshot['host_id'] . ':' . $uuid;
$statuses[] = CloudInstancePageService::powerStatus(
$snapshot, $live[$uuid] ?? []
$snapshot, $live[$key] ?? []
);
}
return $this->json($response, ['vms' => $statuses]);

@ -0,0 +1,333 @@
<?php
namespace App\Services\Cloud;
use App\Models\CloudInstance;
/**
* Validate and persist the non-secret, slow-changing part of an engine VM.
*
* The engine response also contains mutable power/console/rescue fields and
* storage source names. Those values must never become an authoritative
* business record or be embedded into an SSR bootstrap. This class is the
* single allowlist used by provisioning and by an actor-scoped backfill.
*/
final class CloudInstanceMetadata
{
private const VERSION = 1;
private const STATIC_KEYS = [
'vcpu', 'memory_mib', 'disk_gib', 'disks', 'autostart', 'managed',
'public_ip_cidr', 'os_source', 'os_name', 'install_iso', 'login_name',
'engine_metadata_version',
];
private const FORBIDDEN_ENGINE_KEYS = [
'state', 'console_port', 'credential_generation', 'rescue_mode',
'rescue_staged', 'rescue_volume_name', 'auth_mode',
'password_available', 'generated_password',
];
/**
* Canonicalize cached engine fields without touching order/billing keys.
* Safe to call while building an SSR payload from an older database row.
*/
public static function sanitizeSpec(array $spec): array
{
$spec = CloudInput::safeResult($spec);
$existing = self::metadata($spec);
$version = self::integer($spec['engine_metadata_version'] ?? null,
1, self::VERSION);
foreach (array_merge(self::STATIC_KEYS, self::FORBIDDEN_ENGINE_KEYS) as $key) {
unset($spec[$key]);
}
$spec = array_merge($spec, $existing);
if ($version !== null) {
$spec['engine_metadata_version'] = $version;
}
return $spec;
}
/** Merge a trusted engine snapshot into an existing business spec. */
public static function merge(array $spec, array $live): array
{
// Re-validate an older cached copy as well. Early discovery releases
// stored raw disk source paths in `disks`; canonicalizing before the
// overlay removes them during the first successful backfill.
$spec = self::sanitizeSpec($spec);
$metadata = self::metadata($live);
if ($metadata === []) {
return $spec;
}
unset($spec['engine_metadata_version']);
if (($metadata['os_source'] ?? null) === 'cloud_template') {
unset($spec['install_iso']);
} elseif (($metadata['os_source'] ?? null) === 'iso') {
unset($spec['os_name'], $spec['login_name']);
} elseif (($metadata['os_source'] ?? null) === 'unknown') {
unset($spec['os_name'], $spec['install_iso'], $spec['login_name']);
}
return array_merge($spec, $metadata, [
'engine_metadata_version' => self::VERSION,
]);
}
/**
* Return only identity and public static fields after an exact UUID check.
*
* The snapshot is already actor-scoped by the caller. A mismatched live
* record is ignored instead of allowing one libvirt row to decorate a
* different tenant's database record.
*/
public static function publicView(array $snapshot, array $live): array
{
$view = self::identity($snapshot);
$view = array_merge($view, self::metadata($snapshot));
if (self::sameUuid($snapshot['uuid'] ?? null, $live['uuid'] ?? null)) {
$metadata = self::metadata($live);
$view = array_merge($view, $metadata);
if ($metadata !== []) {
$view['engine_metadata_version'] = self::VERSION;
}
$name = self::name($live['name'] ?? null);
if ($name !== null) {
$view['name'] = $name;
}
}
return $view;
}
/**
* Persist engine metadata for only the caller-provided, actor-visible rows.
*
* $liveByUuid is expected to be keyed by canonical UUID, although the UUID
* inside every live row remains authoritative and must match as well. The
* database rows are locked and re-read before merge so a concurrent worker
* cannot lose an updated order/reinstall specification.
*/
public static function persistBatch(array $snapshots, array $liveByUuid): void
{
$allowed = [];
foreach ($snapshots as $snapshot) {
if (!is_array($snapshot)) {
continue;
}
$id = filter_var($snapshot['id'] ?? $snapshot['api_id'] ?? null,
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
$uuid = self::uuid($snapshot['uuid'] ?? null);
if ($id !== false && $uuid !== null) {
$allowed[(int) $id] = $uuid;
}
}
if ($allowed === []) {
return;
}
$live = [];
foreach ($liveByUuid as $key => $value) {
if (!is_array($value)) {
continue;
}
$rowUuid = self::uuid($value['uuid'] ?? null);
$keyUuid = self::uuid(is_string($key) ? $key : null);
if ($rowUuid !== null && $keyUuid !== null
&& hash_equals($keyUuid, $rowUuid)) {
$live[$rowUuid] = $value;
}
}
if ($live === []) {
return;
}
$connection = (new CloudInstance())->getConnection();
$connection->transaction(static function () use ($allowed, $live): void {
$instances = CloudInstance::whereIn('id', array_keys($allowed))
->where('status', '<>', 'deleted')
->lockForUpdate()->get(['id', 'uuid', 'spec_json', 'updated_at']);
foreach ($instances as $instance) {
$uuid = self::uuid($instance->uuid);
if ($uuid === null
|| !isset($allowed[(int) $instance->id])
|| !hash_equals($allowed[(int) $instance->id], $uuid)
|| !isset($live[$uuid])) {
continue;
}
$decoded = json_decode((string) $instance->spec_json, true);
if (!is_array($decoded) || array_is_list($decoded)) {
// A damaged business snapshot may still contain the only
// plan/template evidence. Never replace it with a partial
// engine view; publicView can serve the live fields for
// this response while an administrator repairs the row.
continue;
}
$spec = $decoded;
$merged = self::merge($spec, $live[$uuid]);
if ($merged === $spec) {
continue;
}
$instance->spec_json = json_encode($merged,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
$instance->updated_at = time();
$instance->save();
}
}, 3);
}
private static function identity(array $snapshot): array
{
$id = filter_var($snapshot['id'] ?? $snapshot['api_id'] ?? null,
FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
$uuid = self::uuid($snapshot['uuid'] ?? null);
return [
'id' => $id === false ? '' : (string) $id,
'api_id' => $id === false ? '' : (string) $id,
'uuid' => $uuid ?? '',
'name' => self::name($snapshot['name'] ?? null) ?? '',
];
}
/** Extract the strict public/static allowlist from one engine row. */
private static function metadata(array $value): array
{
$result = [];
$integerBounds = [
'vcpu' => [1, 64],
'memory_mib' => [1, 262144],
'disk_gib' => [1, 1048576],
];
foreach ($integerBounds as $key => [$minimum, $maximum]) {
$integer = self::integer($value[$key] ?? null, $minimum, $maximum);
if ($integer !== null) {
$result[$key] = $integer;
}
}
foreach (['autostart', 'managed'] as $key) {
if (is_bool($value[$key] ?? null)) {
$result[$key] = $value[$key];
}
}
$cidr = self::cidr($value['public_ip_cidr'] ?? null);
if ($cidr !== null) {
$result['public_ip_cidr'] = $cidr;
}
$source = $value['os_source'] ?? null;
if (is_string($source) && in_array($source,
['cloud_template', 'iso', 'unknown'], true)) {
$result['os_source'] = $source;
}
$osName = self::text($value['os_name'] ?? null, 64);
if ($osName !== null && $source === 'cloud_template') {
$result['os_name'] = $osName;
}
$installIso = self::text($value['install_iso'] ?? null, 255);
if ($installIso !== null && $source === 'iso') {
$result['install_iso'] = $installIso;
}
$login = $value['login_name'] ?? null;
if ($source === 'cloud_template' && is_string($login)
&& preg_match('/^[A-Za-z_][A-Za-z0-9_.-]{0,31}$/D', $login) === 1) {
$result['login_name'] = $login;
} elseif (($result['managed'] ?? false) === true
&& ($result['os_source'] ?? null) === 'cloud_template') {
// Discovery returns raw libvirt data rather than decorate_vm().
$result['login_name'] = 'root';
}
$disks = self::disks($value['disks'] ?? null);
if ($disks !== []) {
$result['disks'] = $disks;
$capacity = array_sum(array_column($disks, 'capacity_gib'));
if ($capacity > 0) {
$result['disk_gib'] = round($capacity, 2);
}
}
return $result;
}
private static function disks($value): array
{
if (!is_array($value) || !array_is_list($value)) {
return [];
}
$result = [];
foreach (array_slice($value, 0, 32) as $disk) {
if (!is_array($disk)) {
continue;
}
$capacity = $disk['capacity_gib'] ?? $disk['size_gib'] ?? null;
if (is_int($capacity) || is_float($capacity)
|| (is_string($capacity) && is_numeric($capacity))) {
$capacity = (float) $capacity;
if (is_finite($capacity) && $capacity > 0 && $capacity <= 1048576) {
// Capacity is all the UI needs. Never persist a host path,
// pool/volume name, target name or disk format here.
$result[] = ['capacity_gib' => round($capacity, 2)];
}
}
}
return $result;
}
private static function integer($value, int $minimum, int $maximum): ?int
{
if (is_bool($value) || (!is_int($value)
&& !(is_string($value) && preg_match('/^[0-9]+$/D', $value) === 1))) {
return null;
}
$value = (int) $value;
return $value >= $minimum && $value <= $maximum ? $value : null;
}
private static function cidr($value): ?string
{
if (!is_string($value) || strlen($value) > 64 || trim($value) !== $value) {
return null;
}
$parts = explode('/', $value, 2);
if (count($parts) !== 2 || filter_var($parts[0], FILTER_VALIDATE_IP) === false
|| preg_match('/^[0-9]{1,3}$/D', $parts[1]) !== 1) {
return null;
}
$maximum = filter_var($parts[0], FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)
!== false ? 32 : 128;
$prefix = (int) $parts[1];
return $prefix <= $maximum ? $parts[0] . '/' . $prefix : null;
}
private static function text($value, int $maximum): ?string
{
if (!is_string($value) || $value === '' || trim($value) !== $value) {
return null;
}
$controls = preg_match('/[\x00-\x1F\x7F]/u', $value);
if ($controls !== 0 || mb_strlen($value) > $maximum) {
return null;
}
return $value;
}
private static function name($value): ?string
{
return self::text($value, 63);
}
private static function uuid($value): ?string
{
if (!is_string($value)) {
return null;
}
$value = strtolower(trim($value));
// libvirt also accepts operator-supplied UUIDs whose version/variant
// nibbles do not follow RFC 4122. Match the existing ownership and
// discovery boundary while still requiring the canonical shape.
return preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/D',
$value) === 1 ? $value : null;
}
private static function sameUuid($left, $right): bool
{
$left = self::uuid($left);
$right = self::uuid($right);
return $left !== null && $right !== null && hash_equals($left, $right);
}
}

@ -226,7 +226,8 @@ final class CloudInstancePageService
$keys = [
'id', 'api_id', 'uuid', 'name', 'state', 'region', 'location',
'os_source', 'os_name', 'install_iso', 'public_ip_cidr', 'public_ip',
'vcpu', 'cpu_cores', 'memory_mib', 'ram_mib', 'disks', 'disk_gib',
'login_name', 'engine_metadata_version', 'vcpu', 'cpu_cores',
'memory_mib', 'ram_mib', 'disks', 'disk_gib',
];
return array_intersect_key($snapshot, array_flip($keys));
}
@ -240,7 +241,9 @@ final class CloudInstancePageService
$result = [];
foreach ($instances as $instance) {
$decoded = json_decode((string) $instance->spec_json, true);
$spec = CloudInput::safeResult(is_array($decoded) ? $decoded : []);
$spec = CloudInstanceMetadata::sanitizeSpec(CloudInput::safeResult(
is_array($decoded) && !array_is_list($decoded) ? $decoded : []
));
$host = $hosts[(int) $instance->host_id] ?? null;
$region = trim((string) ($host?->region ?? ''));
if ($region === '') {
@ -322,7 +325,9 @@ final class CloudInstancePageService
?CloudHost $host, ?CloudServicePeriod $period, int $futurePeriods): array
{
$decoded = json_decode((string) $instance->spec_json, true);
$spec = CloudInput::safeResult(is_array($decoded) ? $decoded : []);
$spec = CloudInstanceMetadata::sanitizeSpec(CloudInput::safeResult(
is_array($decoded) && !array_is_list($decoded) ? $decoded : []
));
$owner = (int) $instance->user_id === (int) $actor['user_id'];
$global = ($actor['is_admin'] ?? false)
&& ($actor['is_super_admin'] ?? false)

@ -369,7 +369,16 @@ final class CloudWorkerService
$instance->lifetime_download_bytes = 0;
$instance->lifetime_upload_bytes = 0;
$instance->traffic_synced_at = 0;
$instance->spec_json = $order->spec_json;
// Persist only the engine's slow-changing public metadata.
// SSR pages can then render IP, operating system, login name
// and actual capacity without issuing a full libvirt/XML read
// on every request. Billing/order fields remain authoritative
// because the allowlist cannot overwrite them.
$instance->spec_json = json_encode(
CloudInstanceMetadata::merge($spec, $vm),
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE
);
$instance->credential_cipher = isset($payload['credential']) ? CloudSecrets::encrypt($payload['credential']) : null;
// The paid service begins when provisioning actually succeeds,
// not while an opening invoice is waiting for payment. A
@ -490,8 +499,34 @@ final class CloudWorkerService
if (in_array((string) $job->kind, ['delete', 'expiry_delete'], true)) {
$instance->status = 'deleted';
$instance->credential_cipher = null;
} elseif ($job->kind === 'reinstall' && isset($payload['credential'])) {
$instance->credential_cipher = CloudSecrets::encrypt($payload['credential']);
} elseif ($job->kind === 'reinstall') {
if (isset($payload['credential'])
&& is_array($payload['credential'])) {
$instance->credential_cipher = CloudSecrets::encrypt(
$payload['credential']
);
}
// The replacement template changes the static OS
// metadata even though IP and disk capacity remain
// stable. Invalidate only those cached fields so the
// next detail render performs one bounded engine
// metadata read and persists the new system label.
$decoded = json_decode((string) $instance->spec_json, true);
if (is_array($decoded) && !array_is_list($decoded)) {
$templateId = is_array($payload['body'] ?? null)
? ($payload['body']['template_id'] ?? null) : null;
if (is_string($templateId)
&& preg_match('/^[A-Za-z0-9][A-Za-z0-9_.-]{0,79}$/D',
$templateId) === 1) {
$decoded['template_id'] = $templateId;
}
unset($decoded['os_source'], $decoded['os_name'],
$decoded['install_iso'], $decoded['login_name'],
$decoded['engine_metadata_version']);
$instance->spec_json = json_encode($decoded,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES
| JSON_UNESCAPED_UNICODE);
}
} elseif ($job->kind === 'service_suspend'
&& in_array((string) $instance->service_status,
['suspending_expired', 'resume_pending', 'resuming',

@ -28,7 +28,9 @@ $app->group('/api/v1', function () {
// Fixed lightweight read routes must be registered before the historical
// dynamic /vms/{id} compatibility endpoint.
$this->get('/vms/page', 'App\Controllers\CloudApiController:vmPage');
$this->get('/vms/metadata', 'App\Controllers\CloudApiController:vmMetadata');
$this->get('/vms/statuses', 'App\Controllers\CloudApiController:vmStatuses');
$this->get('/vms/{id:[0-9]+}/metadata', 'App\Controllers\CloudApiController:vmMetadataOne');
$this->get('/vms/{id:[0-9]+}/status', 'App\Controllers\CloudApiController:vmStatus');
$this->get('/vms', 'App\Controllers\CloudApiController:vms');
$this->get('/vms/{id}', 'App\Controllers\CloudApiController:vm');

@ -2,7 +2,7 @@ const runtimeUrl = new URL('/cloud/runtime.js', location.origin);
const assetVersion = new URL(import.meta.url).searchParams.get('v');
if (assetVersion) runtimeUrl.searchParams.set('v', assetVersion);
const {
VM_DETAIL_TABS, bindPageCleanup, byId, clearCredential,
VM_DETAIL_TABS, api, bindPageCleanup, byId, clearCredential,
clearVmMetrics, clearVmTrafficDaily, closeConsole, copyText, credential,
drawVmTrafficDaily, hydrateVmDetailBootstrap, loadVmMetrics, loadVmStatus,
loadVmTrafficDaily, renderVmDetail, renderVmMetrics,
@ -10,6 +10,82 @@ const {
stopVmTrafficDaily, toast, trafficDailyViewVisible, vmIp,
} = await import(runtimeUrl.href);
const VM_METADATA_FIELDS = Object.freeze([
'name', 'region', 'location', 'created_at', 'os_source', 'os_name',
'install_iso', 'public_ip_cidr', 'public_ip', 'login_name', 'vcpu',
'cpu_cores', 'memory_mib', 'ram_mib', 'disks', 'disk_gib',
'engine_metadata_version',
]);
let metadataRequest = null;
function hasMetadataText(value) {
return value !== null && value !== undefined && String(value).trim() !== '';
}
function needsVmMetadata(vm) {
if (Number(vm?.engine_metadata_version) >= 1) return false;
const source = hasMetadataText(vm?.os_source) ? String(vm.os_source).trim() : '';
if (!source) return true;
if (source === 'cloud_template') {
return !hasMetadataText(vm.public_ip_cidr || vm.public_ip)
|| !hasMetadataText(vm.os_name) || !hasMetadataText(vm.login_name);
}
if (source === 'iso') return !hasMetadataText(vm.install_iso);
return true;
}
/**
* The SSR snapshot deliberately avoids libvirt. Fetch the immutable host
* metadata once after first paint, while keeping the recurring poll on the
* much smaller /status endpoint. Identity and permissions always remain
* authoritative from the actor-scoped PHP bootstrap.
*/
async function loadVmMetadata(vmId, force = false) {
if (!force && !needsVmMetadata(selectedVm())) return selectedVm();
if (metadataRequest) return metadataRequest;
metadataRequest = (async () => {
const payload = await api(`/api/v1/vms/${encodeURIComponent(vmId)}/metadata`);
const update = payload?.vm && typeof payload.vm === 'object' ? payload.vm : payload;
const current = selectedVm();
if (!current || !update || typeof update !== 'object') {
throw new Error('实例静态信息格式无效。');
}
const currentUuid = String(current.uuid || '').toLowerCase();
const updateUuid = String(update.uuid || '').toLowerCase();
if (!updateUuid || updateUuid !== currentUuid) {
throw new Error('实例静态信息标识不匹配。');
}
const merged = { ...current };
for (const field of VM_METADATA_FIELDS) {
if (Object.prototype.hasOwnProperty.call(update, field)) merged[field] = update[field];
}
state.vms = [merged];
state.selectedVmId = current.uuid;
renderVmDetail(merged);
return merged;
})();
try {
return await metadataRequest;
} finally {
metadataRequest = null;
}
}
async function refreshMetadataAndStatus(vmId, notifyFailure = false, forceMetadata = false) {
let metadataError = null;
// These requests intentionally run independently. Metadata does not share
// runtime.js's loadingVMs latch, and each merge starts from selectedVm(), so
// a slow host XML read cannot delay the lightweight state refresh.
await Promise.all([
loadVmMetadata(vmId, forceMetadata).catch(error => { metadataError = error; }),
loadVmStatus(vmId),
]);
if (metadataError && notifyFailure) {
toast(`${metadataError.message || '实例静态信息刷新失败。'} 实时状态已继续更新。`, 'error');
}
}
function detailBootstrap() {
const source = byId('vm-detail-bootstrap');
if (!source) throw new Error('实例详情初始数据缺失,请刷新后重试。');
@ -73,7 +149,7 @@ export async function initialize() {
byId('logout-button')?.addEventListener('click', () => location.assign('/user/logout'));
byId('vm-detail-refresh-button')?.addEventListener('click', async () => {
await loadVmStatus(vmId);
await refreshMetadataAndStatus(vmId, true, true);
if (trafficDailyViewVisible()) await loadVmTrafficDaily(true);
});
byId('vm-detail-ip-copy')?.addEventListener('click', async () => {
@ -121,7 +197,7 @@ export async function initialize() {
timer = null;
};
startPolling();
void loadVmStatus(vmId);
void refreshMetadataAndStatus(vmId);
window.addEventListener('pagehide', () => {
stopPolling();
});

@ -12,6 +12,7 @@ const state = {
},
user: {},
loading: false,
metadataLoading: false,
statusLoading: false,
liveStatsReady: false,
pageActive: true,
@ -305,19 +306,20 @@ function renderPagination() {
if (!navigation) return;
const { page, total_pages: totalPages, total, has_previous: previous, has_next: next } = state.pagination;
navigation.hidden = total === 0;
navigation.setAttribute('aria-busy', String(state.loading));
const busy = state.loading || state.metadataLoading || state.statusLoading;
navigation.setAttribute('aria-busy', String(busy));
if (byId('instances-page-label')) {
byId('instances-page-label').textContent = `第 ${page} / ${totalPages} 页 · 共 ${total} 台`;
}
if (byId('instances-previous')) {
byId('instances-previous').disabled = state.loading || state.statusLoading || !previous;
byId('instances-previous').disabled = busy || !previous;
}
if (byId('instances-next')) {
byId('instances-next').disabled = state.loading || state.statusLoading || !next;
byId('instances-next').disabled = busy || !next;
}
if (byId('refresh-button')) {
byId('refresh-button').disabled = state.loading || state.statusLoading;
byId('refresh-button').setAttribute('aria-busy', String(state.loading || state.statusLoading));
byId('refresh-button').disabled = busy;
byId('refresh-button').setAttribute('aria-busy', String(busy));
}
}
@ -364,6 +366,64 @@ function vmMatchesUpdate(vm, update) {
return candidates.includes(vm.api_id) || candidates.includes(vm.uuid);
}
const VM_METADATA_FIELDS = Object.freeze([
'name', 'region', 'location', 'os_source', 'os_name', 'install_iso',
'public_ip_cidr', 'public_ip', 'login_name', 'vcpu', 'cpu_cores', 'memory_mib',
'ram_mib', 'disks', 'disk_gib', 'engine_metadata_version',
]);
function needsVmMetadata(vm) {
if (Number(vm?.engine_metadata_version) >= 1) return false;
const source = firstText(vm?.os_source);
if (!source) return true;
if (source === 'cloud_template') {
return !firstText(vm.public_ip_cidr, vm.public_ip)
|| !firstText(vm.os_name) || !firstText(vm.login_name);
}
if (source === 'iso') return !firstText(vm.install_iso);
return true;
}
function metadataItems(payload) {
if (Array.isArray(payload?.vms)) return payload.vms;
if (Array.isArray(payload?.items)) return payload.items;
return [];
}
function applyMetadata(payload) {
for (const update of metadataItems(payload)) {
if (!update || typeof update !== 'object') continue;
const vm = state.vms.find(item => vmMatchesUpdate(item, update));
if (!vm) continue;
const updateUuid = String(update.uuid || '').toLowerCase();
if (!updateUuid || updateUuid !== String(vm.uuid || '').toLowerCase()) continue;
for (const field of VM_METADATA_FIELDS) {
if (Object.prototype.hasOwnProperty.call(update, field)) vm[field] = update[field];
}
Object.assign(vm, normalizeVm(vm));
}
renderVms();
}
/** Fetch host-derived static fields only once for the currently visible page. */
async function refreshMetadata() {
if (!state.pageActive || state.metadataLoading || !state.vms.length) return;
const ids = state.vms.filter(needsVmMetadata).map(vm => vm.api_id).filter(Boolean);
if (!ids.length) return;
state.metadataLoading = true;
renderPagination();
try {
const payload = await api(`/api/v1/vms/metadata?ids=${encodeURIComponent(ids.join(','))}`);
applyMetadata(payload);
} catch {
// Keep the database-backed first paint usable. The independent status
// request below still runs, and a manual page refresh can retry metadata.
} finally {
state.metadataLoading = false;
renderPagination();
}
}
function applyStatuses(payload) {
const updates = statusItems(payload);
let statusFilterChanged = false;
@ -425,7 +485,7 @@ async function refreshStatuses(scheduleNext = true) {
}
async function loadPage(page = state.pagination.page, updateUrl = true) {
if (state.loading) return;
if (state.loading || state.metadataLoading) return;
const requestedPage = Math.max(1, Math.trunc(Number(page) || 1));
state.loading = true;
state.liveStatsReady = false;
@ -451,7 +511,9 @@ async function loadPage(page = state.pagination.page, updateUrl = true) {
state.loading = false;
renderPagination();
}
if (pageLoaded) await refreshStatuses(true);
if (pageLoaded) {
await Promise.all([refreshMetadata(), refreshStatuses(true)]);
}
}
function hydrate() {
@ -513,5 +575,7 @@ export async function initialize() {
const hydrated = hydrate();
bindEvents();
if (!hydrated) await loadPage(1, false);
else void refreshStatuses(true);
else {
await Promise.all([refreshMetadata(), refreshStatuses(true)]);
}
}

@ -1 +1 @@
{assign var=venix_asset_version value='2026092805' scope='global'}
{assign var=venix_asset_version value='2026092806' scope='global'}

@ -146,7 +146,26 @@ namespace {
&& CloudOrder::find($first->id)->payload_cipher === null,
'Opening invoice payment was not an atomic, exactly-once transition into the provisioning queue.');
$history = [];
$mock = new MockHandler([new Response(200, [], json_encode(['status' => 'succeeded', 'uuid' => '11111111-1111-4111-8111-111111111111', 'generated_password' => 'must-not-persist']))]);
$mock = new MockHandler([new Response(200, [], json_encode([
'status' => 'succeeded',
'uuid' => '11111111-1111-4111-8111-111111111111',
'name' => 'vm-one',
'vcpu' => 2,
'memory_mib' => 2048,
'disks' => [[
'capacity_gib' => 20,
'source' => '/home/kvm/vm-one.qcow2',
'target' => 'vda',
]],
'autostart' => true,
'managed' => true,
'public_ip_cidr' => '203.0.113.18/28',
'os_source' => 'cloud_template',
'os_name' => 'AlmaLinux 10',
'login_name' => 'root',
'console_port' => 5908,
'generated_password' => 'must-not-persist',
]))]);
$stack = HandlerStack::create($mock); $stack->push(Middleware::history($history));
$worker = new CloudWorkerService(new CloudEngineClient(new Client(['handler' => $stack])));
$worker->tick();
@ -175,6 +194,18 @@ namespace {
&& !array_key_exists('root_password', $terminalPayload['body'] ?? []),
'Terminal provisioning job retained a recoverable root password.');
$instance = CloudInstance::first();
$instanceSpec = json_decode((string) $instance->spec_json, true,
flags: JSON_THROW_ON_ERROR);
$assert(($instanceSpec['public_ip_cidr'] ?? null) === '203.0.113.18/28'
&& ($instanceSpec['os_source'] ?? null) === 'cloud_template'
&& ($instanceSpec['os_name'] ?? null) === 'AlmaLinux 10'
&& ($instanceSpec['login_name'] ?? null) === 'root'
&& ($instanceSpec['managed'] ?? null) === true
&& ($instanceSpec['engine_metadata_version'] ?? null) === 1
&& ($instanceSpec['disks'] ?? null) === [['capacity_gib' => 20]]
&& !isset($instanceSpec['console_port'])
&& !isset($instanceSpec['disks'][0]['source']),
'Successful provisioning did not persist the safe engine metadata snapshot.');
// One active instance plus one unpaid reservation must exhaust a limit of
// two. Expiry releases finite stock and the encrypted credential exactly
@ -673,8 +704,18 @@ namespace {
$assert(CloudJob::find($reinstall->id)->status === 'running' && $instance->fresh()->credential_cipher === $oldCredential, 'Pending reinstall replaced password prematurely.');
CloudJob::where('id', $reinstall->id)->update(['available_at' => 0]);
$mock->append(new Response(200, [], '{"status":"succeeded","result":{"job_id":"remote-reinstall-id","generated_password":"never-public"}}')); $worker->tick();
$assert(CloudJob::find($reinstall->id)->status === 'succeeded' && $instance->fresh()->credential_cipher !== $oldCredential
&& !str_contains(CloudJob::find($reinstall->id)->result_json, 'never-public'), 'Completed reinstall password or sanitization failed.');
$reinstalled = $instance->fresh();
$reinstalledSpec = json_decode((string) $reinstalled->spec_json, true,
flags: JSON_THROW_ON_ERROR);
$assert(CloudJob::find($reinstall->id)->status === 'succeeded'
&& $reinstalled->credential_cipher !== $oldCredential
&& !str_contains(CloudJob::find($reinstall->id)->result_json, 'never-public')
&& ($reinstalledSpec['template_id'] ?? null) === $input['template_id']
&& ($reinstalledSpec['public_ip_cidr'] ?? null) === '203.0.113.18/28'
&& !isset($reinstalledSpec['os_source'], $reinstalledSpec['os_name'],
$reinstalledSpec['install_iso'], $reinstalledSpec['login_name'],
$reinstalledSpec['engine_metadata_version']),
'Completed reinstall password, sanitization or metadata invalidation failed.');
$delete = CloudJobService::enqueue($instance, $actor, 'delete', 'DELETE', '?delete_disks=true', []);
$mock->append(new Response(200, [], '{"status":"succeeded","uuid":"11111111-1111-4111-8111-111111111111"}')); $worker->tick();
$last = end($history)['request']; $assert((string) $last->getBody() === '{}' && $last->getUri()->getQuery() === 'delete_disks=true'

@ -0,0 +1,176 @@
<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use App\Models\CloudInstance;
use App\Services\Cloud\CloudInstanceMetadata;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Schema\Blueprint;
$assert = static function ($condition, string $message): void {
if (!$condition) {
throw new RuntimeException($message);
}
};
$capsule = new Capsule();
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
$capsule->setAsGlobal();
$capsule->bootEloquent();
$schema = $capsule->schema('default');
$schema->create('cloud_instances', static function (Blueprint $table): void {
$table->increments('id');
$table->string('uuid');
$table->integer('host_id');
$table->integer('user_id');
$table->string('name');
$table->string('status');
$table->text('spec_json');
$table->integer('updated_at');
});
$firstUuid = '11111111-1111-4111-8111-111111111111';
$secondUuid = '22222222-2222-4222-8222-222222222222';
foreach ([1 => $firstUuid, 2 => $secondUuid] as $id => $uuid) {
Capsule::table('cloud_instances')->insert([
'id' => $id,
'uuid' => $uuid,
'host_id' => 7,
'user_id' => $id,
'name' => 'vm-' . $id,
'status' => 'active',
'spec_json' => json_encode([
'plan_id' => 91,
'validity_days' => 30,
'traffic_quota_bytes' => 1099511627776,
'template_id' => 'alma10-template',
'state' => 'running',
'console_port' => 5999,
'password_available' => true,
], JSON_THROW_ON_ERROR),
'updated_at' => 1,
]);
}
$damagedUuid = '33333333-3333-4333-8333-333333333333';
Capsule::table('cloud_instances')->insert([
'id' => 3, 'uuid' => $damagedUuid, 'host_id' => 7, 'user_id' => 1,
'name' => 'damaged-vm', 'status' => 'active',
'spec_json' => '{damaged-business-snapshot', 'updated_at' => 1,
]);
$live = [
'uuid' => $firstUuid,
'name' => 'engine-name',
'state' => 'running',
'vcpu' => 4,
'memory_mib' => 4096,
'disks' => [
['capacity_gib' => 40, 'source' => '/home/kvm/private.qcow2',
'target' => 'vda', 'format' => 'qcow2'],
['size_gib' => '2.5', 'source' => 'secret-pool-volume'],
],
'autostart' => true,
'managed' => true,
'public_ip_cidr' => '203.0.113.18/28',
'os_source' => 'cloud_template',
'os_name' => 'AlmaLinux 10',
'install_iso' => 'must-not-win.iso',
'login_name' => 'root',
'console_port' => 5908,
'credential_generation' => 'panel-seed-secret.iso',
'password_available' => true,
'unexpected' => ['private_key' => 'must-not-persist'],
];
$view = CloudInstanceMetadata::publicView([
'id' => '1', 'api_id' => '1', 'uuid' => $firstUuid,
'name' => 'database-name', 'disk_gib' => 20,
], $live);
$assert($view['id'] === '1' && $view['uuid'] === $firstUuid
&& $view['name'] === 'engine-name'
&& $view['public_ip_cidr'] === '203.0.113.18/28'
&& $view['os_source'] === 'cloud_template'
&& $view['os_name'] === 'AlmaLinux 10'
&& $view['login_name'] === 'root'
&& $view['engine_metadata_version'] === 1
&& $view['disk_gib'] === 42.5,
'Matching engine metadata was not exposed through the public allowlist.');
$assert(array_keys($view['disks']) === [0, 1]
&& array_keys($view['disks'][0]) === ['capacity_gib']
&& !array_key_exists('state', $view)
&& !array_key_exists('console_port', $view)
&& !array_key_exists('credential_generation', $view)
&& !array_key_exists('password_available', $view)
&& !array_key_exists('install_iso', $view),
'The public VM metadata view leaked dynamic, storage or credential fields.');
$mismatch = CloudInstanceMetadata::publicView([
'id' => '1', 'uuid' => $firstUuid, 'name' => 'database-name',
'public_ip_cidr' => '198.51.100.2/24',
], array_replace($live, ['uuid' => $secondUuid]));
$assert($mismatch['name'] === 'database-name'
&& $mismatch['public_ip_cidr'] === '198.51.100.2/24'
&& !isset($mismatch['os_name'], $mismatch['engine_metadata_version']),
'A mismatched live UUID decorated another database instance.');
$operatorUuid = 'aaaaaaaa-aaaa-0aaa-0aaa-aaaaaaaaaaaa';
$operatorView = CloudInstanceMetadata::publicView([
'id' => '9', 'uuid' => $operatorUuid, 'name' => 'operator-vm',
], array_replace($live, ['uuid' => $operatorUuid]));
$assert(($operatorView['os_name'] ?? null) === 'AlmaLinux 10',
'A canonical libvirt UUID was rejected solely for non-RFC version nibbles.');
$emptyMerge = CloudInstanceMetadata::merge([
'plan_id' => 91,
'state' => 'running',
'engine_metadata_version' => 1,
], []);
$assert(($emptyMerge['plan_id'] ?? null) === 91
&& ($emptyMerge['engine_metadata_version'] ?? null) === 1
&& !isset($emptyMerge['state']),
'An empty live result discarded a valid metadata marker or skipped sanitization.');
// Only the actor-scoped snapshot is eligible even when the live inventory
// contains another tenant's UUID.
CloudInstanceMetadata::persistBatch([
['id' => '1', 'uuid' => $firstUuid, 'name' => 'vm-1'],
['id' => '3', 'uuid' => $damagedUuid, 'name' => 'damaged-vm'],
], [
$firstUuid => $live,
$secondUuid => array_replace($live, [
'uuid' => $secondUuid,
'public_ip_cidr' => '203.0.113.19/28',
]),
$damagedUuid => array_replace($live, ['uuid' => $damagedUuid]),
]);
CloudInstanceMetadata::persistBatch([
['id' => '2', 'uuid' => $secondUuid, 'name' => 'vm-2'],
], [
// A numeric/list key must not be accepted merely because the row itself
// contains a syntactically valid UUID.
array_replace($live, ['uuid' => $secondUuid]),
]);
$first = json_decode((string) CloudInstance::find(1)->spec_json, true,
flags: JSON_THROW_ON_ERROR);
$second = json_decode((string) CloudInstance::find(2)->spec_json, true,
flags: JSON_THROW_ON_ERROR);
$assert($first['plan_id'] === 91 && $first['validity_days'] === 30
&& $first['traffic_quota_bytes'] === 1099511627776
&& $first['template_id'] === 'alma10-template'
&& $first['engine_metadata_version'] === 1
&& $first['public_ip_cidr'] === '203.0.113.18/28'
&& $first['login_name'] === 'root'
&& $first['disks'] === [
['capacity_gib' => 40], ['capacity_gib' => 2.5],
]
&& !isset($first['state'], $first['console_port'],
$first['credential_generation'], $first['unexpected'],
$first['install_iso']),
'Persisted metadata lost business fields or escaped the static allowlist.');
$assert(!isset($second['engine_metadata_version'], $second['public_ip_cidr']),
'Batch backfill crossed the caller-provided instance scope.');
$assert(CloudInstance::find(3)->spec_json === '{damaged-business-snapshot',
'Batch backfill overwrote a damaged business specification.');
echo "Cloud instance metadata tests passed.\n";

@ -73,8 +73,24 @@ namespace {
'renewal_days' => 30, 'renewal_traffic_bytes' => 2000,
'traffic_quota_bytes' => 2000, 'traffic_download_bytes' => 300,
'traffic_upload_bytes' => 200,
'spec_json' => json_encode(['vcpu' => 2, 'memory_mib' => 2048, 'state' => 'running',
'disk_gib' => 40, 'os_source' => 'cloud_template', 'os_name' => 'AlmaLinux 10']),
'spec_json' => json_encode([
'vcpu' => 2,
'memory_mib' => 2048,
'state' => 'running',
'console_port' => 5900,
'api_token' => 'must-not-reach-ssr',
'disk_gib' => 40,
'disks' => [[
'capacity_gib' => 40,
'source' => '/home/kvm/private.qcow2',
'target' => 'vda',
'format' => 'qcow2',
]],
'os_source' => 'cloud_template',
'os_name' => 'AlmaLinux 10',
'login_name' => 'root',
'engine_metadata_version' => 1,
]),
'credential_cipher' => 'test-cipher', 'created_at' => $now - 60,
'updated_at' => $now,
];
@ -107,6 +123,14 @@ namespace {
'Instance first page regressed beyond count, rows and host batch queries.');
$assert($page['vms'][0]['state'] === 'unknown',
'Persisted specification power state was trusted before a live UUID check.');
$assert(count($page['vms'][0]['disks']) === 1
&& array_keys($page['vms'][0]['disks'][0]) === ['capacity_gib']
&& (float) $page['vms'][0]['disks'][0]['capacity_gib'] === 40.0
&& $page['vms'][0]['login_name'] === 'root'
&& $page['vms'][0]['engine_metadata_version'] === 1
&& !array_key_exists('console_port', $page['vms'][0])
&& !array_key_exists('api_token', $page['vms'][0]),
'Instance list SSR exposed an unsafe field from the persisted engine snapshot.');
$assert(!array_key_exists('traffic_quota_bytes', $page['vms'][0])
&& !array_key_exists('password_available', $page['vms'][0]),
'Paginated instance rows leaked detail-only billing or credential metadata.');
@ -114,6 +138,14 @@ namespace {
$assert($ownedDetail['can_operate'] && $ownedDetail['can_delete']
&& $ownedDetail['password_available'] && $ownedDetail['can_renew'],
'Owner lost expected instance capabilities.');
$assert(count($ownedDetail['disks']) === 1
&& array_keys($ownedDetail['disks'][0]) === ['capacity_gib']
&& (float) $ownedDetail['disks'][0]['capacity_gib'] === 40.0
&& $ownedDetail['login_name'] === 'root'
&& $ownedDetail['engine_metadata_version'] === 1
&& !array_key_exists('console_port', $ownedDetail)
&& !array_key_exists('api_token', $ownedDetail),
'Instance detail SSR exposed an unsafe field from the persisted engine snapshot.');
$assert(count(CloudInstancePageService::forActorIds($owner, [1, 2])) === 1,
'Ordinary user batch status scope disclosed a foreign instance.');

@ -79,27 +79,41 @@ $assert(str_contains($page, 'JSON_HEX_TAG')
'Embedded instance state must be script-safe, authenticated and non-cacheable.');
$pageRoute = strpos($routes, "'/vms/page'");
$metadataRoute = strpos($routes, "'/vms/metadata'");
$statusesRoute = strpos($routes, "'/vms/statuses'");
$metadataOneRoute = strpos($routes, "'/vms/{id:[0-9]+}/metadata'");
$statusRoute = strpos($routes, "'/vms/{id:[0-9]+}/status'");
$legacyRoute = strpos($routes, "'/vms/{id}'");
$assert($pageRoute !== false && $statusesRoute !== false && $statusRoute !== false
$assert($pageRoute !== false && $metadataRoute !== false && $statusesRoute !== false
&& $metadataOneRoute !== false && $statusRoute !== false
&& $legacyRoute !== false && $pageRoute < $legacyRoute
&& $statusesRoute < $legacyRoute && $statusRoute < $legacyRoute,
&& $metadataRoute < $legacyRoute && $statusesRoute < $legacyRoute
&& $metadataOneRoute < $legacyRoute && $statusRoute < $legacyRoute,
'Lightweight fixed VM routes must precede the legacy dynamic VM route.');
$pageApi = $section($api, 'public function vmPage(', 'public function vmStatuses(');
$pageApi = $section($api, 'public function vmPage(', 'public function vmMetadata(');
$metadataApi = $section($api, 'public function vmMetadata(', 'public function vmStatuses(');
$statusesApi = $section($api, 'public function vmStatuses(', 'public function vmStatus(');
$statusApi = $section($api, 'public function vmStatus(', 'public function vm(');
$assert(str_contains($pageApi, '$this->billingActor($request)')
&& str_contains($pageApi, 'CloudInstancePageService::forActor')
&& !str_contains($pageApi, 'CloudEngineClient'),
'Paginated VM rows must not create an engine session or contact libvirt.');
$assert(str_contains($metadataApi, 'CloudInstancePageService::forActorIds')
&& str_contains($metadataApi, "'GET', 'vm-metadata?ids='")
&& str_contains($metadataApi, 'isset($allowed[$uuid])')
&& str_contains($metadataApi, 'CloudInstanceMetadata::persistBatch($group, $hostLive)')
&& str_contains($metadataApi, ". ':' . $uuid")
&& str_contains($metadataApi, 'CloudInstanceMetadata::publicView')
&& !str_contains($metadataApi, "'GET', 'vms'"),
'Static VM metadata refresh must be bounded, owner intersected and cached without full inventory.');
$assert(str_contains($statusesApi, 'CloudInstancePageService::forActorIds')
&& str_contains($statusesApi, 'CloudInstancePageService::powerStatus')
&& str_contains($statusesApi, "'GET', 'vm-states'")
&& str_contains($statusesApi, 'isset($allowed[$uuid])')
&& str_contains($statusesApi, ". ':' . $uuid")
&& !str_contains($statusesApi, "'GET', 'vms'"),
'Batch VM status must intersect owner UUIDs and use only the lightweight engine resource.');
'Batch VM status must intersect owner UUIDs, preserve host identity and use only the lightweight engine resource.');
$assert(str_contains($statusApi, 'CloudInstancePageService::statusForActor')
&& str_contains($statusApi, ". '/state'")
&& str_contains($statusApi, 'hash_equals('),

@ -23,6 +23,7 @@ $section = static function (string $source, string $start, string $end): string
$controller = file_get_contents($root . '/app/Controllers/CloudApiController.php');
$runtime = file_get_contents($root . '/public/cloud/runtime.js');
$detail = file_get_contents($root . '/public/cloud/detail.js');
$instances = file_get_contents($root . '/public/cloud/instances.js');
$detailTemplate = file_get_contents(
$root . '/resources/views/venix/cloud/pages/detail.tpl'
);
@ -69,8 +70,17 @@ $assert(str_contains($detail, 'hydrateVmDetailBootstrap(detailBootstrap())')
&& str_contains($detailTemplate, 'id="vm-detail-bootstrap"')
&& !str_contains($detail, 'initializeSession(')
&& !str_contains($detail, 'selectVmByApiId(')
&& str_contains($detail,
'api(`/api/v1/vms/${encodeURIComponent(vmId)}/metadata`)')
&& str_contains($detail, 'loadVmMetadata(vmId, forceMetadata).catch(')
&& str_contains($detail, 'loadVmStatus(vmId)')
&& str_contains($detail,
'if (!force && !needsVmMetadata(selectedVm())) return selectedVm();')
&& str_contains($detail, 'refreshMetadataAndStatus(vmId, true, true)')
&& str_contains($detail, 'await Promise.all([')
&& str_contains($detail, 'Object.prototype.hasOwnProperty.call(update, field)')
&& !str_contains($detail, 'loadVm('),
'Detail first paint must hydrate the SSR bootstrap without session or full-VM requests.');
'Detail must keep SSR first paint, then safely merge one metadata response before status polling.');
$assert(str_contains($statusLoader,
'api(`/api/v1/vms/${encodeURIComponent(id)}/status`)')
&& str_contains($statusLoader, 'const current = selectedVm()')
@ -79,5 +89,18 @@ $assert(str_contains($statusLoader,
&& !str_contains($detail, 'setInterval(')
&& !str_contains($detail, 'loadVms'),
'Detail refresh and non-overlapping polling must use only the lightweight status endpoint.');
$assert(str_contains($instances,
'api(`/api/v1/vms/metadata?ids=${encodeURIComponent(ids.join(\',\'))}`)')
&& str_contains($instances, 'async function refreshMetadata()')
&& str_contains($instances, 'state.vms.filter(needsVmMetadata)')
&& str_contains($instances, 'if (state.loading || state.metadataLoading) return;')
&& str_contains($instances,
'const busy = state.loading || state.metadataLoading || state.statusLoading;')
&& substr_count($instances,
'await Promise.all([refreshMetadata(), refreshStatuses(true)]);') === 2
&& str_contains($instances, 'async function refreshStatuses(')
&& str_contains($instances, 'Object.prototype.hasOwnProperty.call(update, field)')
&& !str_contains($instances, "api('/api/v1/vms')"),
'Each instance page must fetch only its static metadata once, then retain lightweight status polling.');
echo "Cloud instance bulk-list and single-detail read paths passed.\n";

Loading…
Cancel
Save