From 05815c4668011e797f3e07c3a275be89028910f5 Mon Sep 17 00:00:00 2001 From: faumaray Date: Wed, 29 Jul 2026 00:22:20 +0300 Subject: [PATCH] Update: add component catalog transactions --- include/Void/Update/Feed.hpp | 80 +++++++ src/Update/Feed.cpp | 354 ++++++++++++++++++++++++++++- tests/standalone/Update/Update.cpp | 113 +++++++++ 3 files changed, 540 insertions(+), 7 deletions(-) diff --git a/include/Void/Update/Feed.hpp b/include/Void/Update/Feed.hpp index 64ac0c9..d7ce932 100644 --- a/include/Void/Update/Feed.hpp +++ b/include/Void/Update/Feed.hpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -75,6 +76,66 @@ namespace Void::Update std::string signedPayload; }; + /// A signature-verified payload whose schema-specific parsing is left to + /// the caller. This is useful for signed discovery documents that are not + /// legacy aggregate feeds. + struct VerifiedEnvelope { + std::uint32_t schemaVersion{0}; + std::string keyId; + std::string signedPayload; + }; + + struct ComponentArtifact { + std::string name; + std::string url; + std::string sha256; + std::uint64_t size{0}; + std::string manifestSha256; + std::string packageType; + }; + + struct ComponentCatalogEntry { + std::string componentId; + std::optional pluginId; + std::string kind; + Semver::Version componentVersion; + std::string activation; + std::map dependencies; + std::uint64_t sequence{0}; + std::string latestUrl; + std::string artifactSha256; + }; + + struct ComponentCatalog { + std::uint32_t schemaVersion{2}; + std::string channel; + std::uint64_t sequence{0}; + std::chrono::system_clock::time_point generatedAt; + std::chrono::system_clock::time_point expiresAt; + Semver::Version productVersion; + std::vector components; + }; + + struct ComponentRelease { + std::uint32_t schemaVersion{2}; + std::string channel; + std::uint64_t sequence{0}; + std::chrono::system_clock::time_point generatedAt; + std::chrono::system_clock::time_point expiresAt; + Semver::Version productVersion; + std::string componentId; + std::optional pluginId; + std::string kind; + Semver::Version componentVersion; + std::string sourceSha; + std::string sourceTreeHash; + std::string abiHash; + std::string artifactIdentity; + std::string activation; + std::map dependencies; + ComponentArtifact artifact; + }; + struct Environment { std::unordered_map versionContracts; std::unordered_map exactContracts; @@ -105,6 +166,25 @@ namespace Void::Update verifyFeedEnvelope(std::string_view envelopeJson, const Keyring& keys, const VerificationPolicy& policy = {}); + /// Verify and decode a signed envelope without assuming a payload schema. + [[nodiscard]] Void::Result::Result + verifyEnvelope(std::string_view envelopeJson, const Keyring& keys, + const VerificationPolicy& policy = {}); + + [[nodiscard]] Void::Result::Result + parseComponentCatalogPayload(std::string_view payload, + const VerificationPolicy& policy = {}); + + [[nodiscard]] Void::Result::Result + parseComponentReleasePayload(std::string_view payload, + const VerificationPolicy& policy = {}); + + /// Validate that a catalog and its fetched releases form one complete, + /// dependency-compatible transaction. + [[nodiscard]] Void::Result::Result validateComponentSet( + const ComponentCatalog& catalog, + const std::map& releases); + /// Select the greatest compatible version, independent of feed order. [[nodiscard]] std::optional selectRelease(const Feed& feed, const SelectionRequest& request); diff --git a/src/Update/Feed.cpp b/src/Update/Feed.cpp index 9f1eb54..dd7bdc1 100644 --- a/src/Update/Feed.cpp +++ b/src/Update/Feed.cpp @@ -101,6 +101,16 @@ namespace Void::Update }); } + [[nodiscard]] bool valid_hex(std::string_view value, + std::size_t size) noexcept + { + return value.size() == size && + std::ranges::all_of(value, [](const char character) { + return (character >= 48 && character <= 57) || + (character >= 97 && character <= 102); + }); + } + [[nodiscard]] bool valid_package_type(std::string_view value) noexcept { return !value.empty() && value.size() <= 128 && @@ -363,6 +373,149 @@ namespace Void::Update return feed; } + struct ComponentDocumentHeader { + std::string channel; + std::uint64_t sequence{0}; + std::chrono::system_clock::time_point generatedAt; + std::chrono::system_clock::time_point expiresAt; + Semver::Version productVersion; + }; + + [[nodiscard]] Void::Result::Result> + optional_token(const Json::Value& object, std::string_view name) + { + const auto* value = object.find(name); + if (value == nullptr || value->is_null()) { + return std::optional{}; + } + if (!value->is_string() || !valid_token(value->as_string(), 256)) { + return Void::Result::fail(std::format( + "Component feed: {} must be null or a safe token", name)); + } + return std::optional{value->as_string()}; + } + + [[nodiscard]] Void::Result::Result< + std::map> + parse_dependencies(const Json::Value& object) + { + const auto* dependencies = object.find("dependencies"); + if (dependencies == nullptr || !dependencies->is_object()) { + return Void::Result::fail( + "Component feed: dependencies must be an object"); + } + std::map result; + for (const auto& [component, value] : dependencies->as_object()) { + if (!valid_token(component, 256) || !value.is_string()) { + return Void::Result::fail( + "Component feed: dependency contains an unsafe field"); + } + auto range = Semver::parse_range(value.as_string()); + if (!range) { + return Void::Result::fail(std::format( + "Component feed: dependency {} has an invalid range", + component)); + } + result.emplace(component, std::move(*range)); + } + return result; + } + + [[nodiscard]] Void::Result::Result + parse_component_header(const Json::Value& document, + std::string_view expectedDocument, + const VerificationPolicy& policy) + { + auto schema = + unsigned_number(document, "schemaVersion"); + auto type = string_member(document, "document"); + auto channel = string_member(document, "channel"); + auto sequence = + unsigned_number(document, "sequence"); + auto generated = string_member(document, "generatedAt"); + auto expires = string_member(document, "expiresAt"); + auto productVersion = string_member(document, "productVersion"); + if (!schema || !type || !channel || !sequence || !generated || + !expires || !productVersion || *schema != 2 || + *type != expectedDocument || !valid_token(*channel, 64)) { + return Void::Result::fail( + "Component feed: invalid discovery document header"); + } + if (policy.minimumSequence && + *sequence <= *policy.minimumSequence) { + return Void::Result::fail("Component feed: sequence is not " + "newer than the replay floor"); + } + const auto generatedAt = parse_timestamp(*generated); + const auto expiresAt = parse_timestamp(*expires); + auto version = Semver::parse_strict(*productVersion); + if (!generatedAt || !expiresAt || *expiresAt <= *generatedAt || + !version) { + return Void::Result::fail( + "Component feed: timestamp or product version is invalid"); + } + if (policy.requireUnexpired && policy.now >= *expiresAt) { + return Void::Result::fail( + "Component feed: discovery document has expired"); + } + return ComponentDocumentHeader{.channel = std::move(*channel), + .sequence = *sequence, + .generatedAt = *generatedAt, + .expiresAt = *expiresAt, + .productVersion = + std::move(*version)}; + } + + [[nodiscard]] bool bounded_url(std::string_view value) noexcept + { + return value.starts_with("https://") && value.size() <= 8192 && + std::ranges::none_of( + value, [](const unsigned char character) { + return character < 0x20 || character == 0x7f; + }); + } + + [[nodiscard]] Void::Result::Result + parse_catalog_entry(const Json::Value& value) + { + if (!value.is_object()) { + return Void::Result::fail( + "Component feed: catalog entry must be an object"); + } + auto componentId = string_member(value, "componentId"); + auto pluginId = optional_token(value, "pluginId"); + auto kind = string_member(value, "kind"); + auto versionText = string_member(value, "componentVersion"); + auto activation = string_member(value, "activation"); + auto sequence = unsigned_number(value, "sequence"); + auto latestUrl = string_member(value, "latestUrl"); + auto artifactSha = string_member(value, "artifactSha256"); + auto dependencies = parse_dependencies(value); + if (!componentId || !pluginId || !kind || !versionText || + !activation || !sequence || !latestUrl || !artifactSha || + !dependencies || !valid_token(*componentId, 256) || + !valid_token(*kind, 128) || !valid_token(*activation, 128) || + !bounded_url(*latestUrl) || !valid_sha256(*artifactSha)) { + return Void::Result::fail( + "Component feed: catalog entry contains an unsafe field"); + } + auto version = Semver::parse_strict(*versionText); + if (!version) { + return Void::Result::fail( + "Component feed: component version is invalid SemVer"); + } + return ComponentCatalogEntry{ + .componentId = std::move(*componentId), + .pluginId = std::move(*pluginId), + .kind = std::move(*kind), + .componentVersion = std::move(*version), + .activation = std::move(*activation), + .dependencies = std::move(*dependencies), + .sequence = *sequence, + .latestUrl = std::move(*latestUrl), + .artifactSha256 = std::move(*artifactSha)}; + } + [[nodiscard]] bool compatible(const Compatibility& compatibility, const Environment& environment) noexcept { @@ -398,6 +551,28 @@ namespace Void::Update Void::Result::Result verifyFeedEnvelope(std::string_view envelopeJson, const Keyring& keys, const VerificationPolicy& policy) + { + auto envelope = verifyEnvelope(envelopeJson, keys, policy); + if (!envelope) { + return std::unexpected{envelope.error()}; + } + if (envelope->schemaVersion != 1) { + return Void::Result::fail( + "Update feed: aggregate feed requires envelope schema 1"); + } + auto feed = parseFeedPayload(envelope->signedPayload, policy); + if (!feed) { + return std::unexpected{feed.error()}; + } + return VerifiedFeed{.feed = std::move(*feed), + .keyId = std::move(envelope->keyId), + .signedPayload = + std::move(envelope->signedPayload)}; + } + + Void::Result::Result + verifyEnvelope(std::string_view envelopeJson, const Keyring& keys, + const VerificationPolicy& policy) { if (envelopeJson.size() > policy.maxEnvelopeBytes) { return Void::Result::fail( @@ -414,7 +589,7 @@ namespace Void::Update auto payloadText = string_member(*envelope, "payload"); auto signatureText = string_member(*envelope, "signature"); if (!schema || !keyId || !payloadText || !signatureText || - *schema != 1 || !valid_token(*keyId, 256)) { + (*schema != 1 && *schema != 2) || !valid_token(*keyId, 256)) { return Void::Result::fail("Update feed: invalid envelope fields"); } const auto key = keys.find(*keyId); @@ -442,13 +617,178 @@ namespace Void::Update return Void::Result::fail( "Update feed: signature verification failed"); } - auto feed = parseFeedPayload(*payload, policy); - if (!feed) { - return std::unexpected{feed.error()}; + return VerifiedEnvelope{.schemaVersion = *schema, + .keyId = std::move(*keyId), + .signedPayload = std::move(*payload)}; + } + + Void::Result::Result + parseComponentCatalogPayload(std::string_view payload, + const VerificationPolicy& policy) + { + if (payload.size() > policy.maxPayloadBytes) { + return Void::Result::fail( + "Component feed: payload exceeds policy size"); } - return VerifiedFeed{.feed = std::move(*feed), - .keyId = std::move(*keyId), - .signedPayload = std::move(*payload)}; + auto document = Json::parse(payload); + if (!document || !document->is_object()) { + return Void::Result::fail( + "Component feed: catalog payload is not an object"); + } + auto header = parse_component_header( + *document, "echoion-component-catalog", policy); + auto components = member(*document, "components", is_array, "an array"); + if (!header || !components) { + return Void::Result::fail("Component feed: invalid catalog fields"); + } + const auto& values = (*components)->as_array(); + if (values.empty() || values.size() > policy.maxReleases) { + return Void::Result::fail( + "Component feed: catalog component count exceeds policy"); + } + ComponentCatalog result{.channel = std::move(header->channel), + .sequence = header->sequence, + .generatedAt = header->generatedAt, + .expiresAt = header->expiresAt, + .productVersion = + std::move(header->productVersion)}; + std::set identities; + result.components.reserve(values.size()); + for (const auto& value : values) { + auto entry = parse_catalog_entry(value); + if (!entry) { + return std::unexpected{entry.error()}; + } + if (entry->sequence != result.sequence || + !identities.insert(entry->componentId).second) { + return Void::Result::fail("Component feed: catalog has a " + "sequence mismatch or duplicate"); + } + result.components.push_back(std::move(*entry)); + } + return result; + } + + Void::Result::Result + parseComponentReleasePayload(std::string_view payload, + const VerificationPolicy& policy) + { + if (payload.size() > policy.maxPayloadBytes) { + return Void::Result::fail( + "Component feed: payload exceeds policy size"); + } + auto document = Json::parse(payload); + if (!document || !document->is_object()) { + return Void::Result::fail( + "Component feed: release payload is not an object"); + } + auto header = parse_component_header( + *document, "echoion-component-release", policy); + auto componentId = string_member(*document, "componentId"); + auto pluginId = optional_token(*document, "pluginId"); + auto kind = string_member(*document, "kind"); + auto componentVersion = string_member(*document, "componentVersion"); + auto sourceSha = string_member(*document, "sourceSha"); + auto sourceTreeHash = string_member(*document, "sourceTreeHash"); + auto abiHash = string_member(*document, "abiHash"); + auto artifactIdentity = string_member(*document, "artifactIdentity"); + auto activation = string_member(*document, "activation"); + auto dependencies = parse_dependencies(*document); + const auto* artifactValue = document->find("artifact"); + if (!header || !componentId || !pluginId || !kind || + !componentVersion || !sourceSha || !sourceTreeHash || !abiHash || + !artifactIdentity || !activation || !dependencies || + artifactValue == nullptr || !artifactValue->is_object() || + !valid_token(*componentId, 256) || !valid_token(*kind, 128) || + !valid_token(*artifactIdentity, 256) || + !valid_token(*activation, 128) || !valid_hex(*sourceSha, 40) || + !valid_sha256(*sourceTreeHash) || !valid_sha256(*abiHash)) { + return Void::Result::fail( + "Component feed: release contains an unsafe field"); + } + auto version = Semver::parse_strict(*componentVersion); + auto artifactName = string_member(*artifactValue, "name"); + auto artifactUrl = string_member(*artifactValue, "url"); + auto artifactSha = string_member(*artifactValue, "sha256"); + auto artifactSize = + unsigned_number(*artifactValue, "size"); + auto manifestSha = string_member(*artifactValue, "manifestSha256"); + auto packageType = string_member(*artifactValue, "packageType"); + if (!version || !artifactName || !artifactUrl || !artifactSha || + !artifactSize || !manifestSha || !packageType || + !valid_token(*artifactName, 256) || !bounded_url(*artifactUrl) || + !valid_sha256(*artifactSha) || *artifactSize == 0 || + !valid_sha256(*manifestSha) || !valid_package_type(*packageType)) { + return Void::Result::fail( + "Component feed: release artifact contains an unsafe field"); + } + return ComponentRelease{ + .channel = std::move(header->channel), + .sequence = header->sequence, + .generatedAt = header->generatedAt, + .expiresAt = header->expiresAt, + .productVersion = std::move(header->productVersion), + .componentId = std::move(*componentId), + .pluginId = std::move(*pluginId), + .kind = std::move(*kind), + .componentVersion = std::move(*version), + .sourceSha = std::move(*sourceSha), + .sourceTreeHash = std::move(*sourceTreeHash), + .abiHash = std::move(*abiHash), + .artifactIdentity = std::move(*artifactIdentity), + .activation = std::move(*activation), + .dependencies = std::move(*dependencies), + .artifact = {.name = std::move(*artifactName), + .url = std::move(*artifactUrl), + .sha256 = std::move(*artifactSha), + .size = *artifactSize, + .manifestSha256 = std::move(*manifestSha), + .packageType = std::move(*packageType)}}; + } + + Void::Result::Result validateComponentSet( + const ComponentCatalog& catalog, + const std::map& releases) + { + if (catalog.components.size() != releases.size()) { + return Void::Result::fail( + "Component set: release count does not match the catalog"); + } + for (const auto& entry : catalog.components) { + const auto release = releases.find(entry.componentId); + if (release == releases.end()) { + return Void::Result::fail(std::format( + "Component set: release {} is missing", entry.componentId)); + } + const auto& value = release->second; + if (value.componentId != entry.componentId || + value.componentVersion != entry.componentVersion || + value.productVersion != catalog.productVersion || + value.channel != catalog.channel || + value.sequence != catalog.sequence || + value.artifact.sha256 != entry.artifactSha256 || + value.activation != entry.activation || + value.pluginId != entry.pluginId || value.kind != entry.kind || + value.dependencies.size() != entry.dependencies.size()) { + return Void::Result::fail(std::format( + "Component set: release {} does not match the catalog", + entry.componentId)); + } + for (const auto& [dependencyId, range] : value.dependencies) { + const auto dependency = releases.find(dependencyId); + const auto catalogRange = entry.dependencies.find(dependencyId); + if (dependency == releases.end() || + catalogRange == entry.dependencies.end() || + !range.contains(dependency->second.componentVersion) || + !catalogRange->second.contains( + dependency->second.componentVersion)) { + return Void::Result::fail(std::format( + "Component set: dependency {} of {} is incompatible", + dependencyId, entry.componentId)); + } + } + } + return {}; } std::optional selectRelease(const Feed& feed, diff --git a/tests/standalone/Update/Update.cpp b/tests/standalone/Update/Update.cpp index c8e0b7b..6621416 100644 --- a/tests/standalone/Update/Update.cpp +++ b/tests/standalone/Update/Update.cpp @@ -90,6 +90,85 @@ namespace Void::Encoding::Base64::encode_url(payload), Void::Encoding::Base64::encode_url(signature)); } + + std::string signedEnvelope(std::string_view message, + std::uint32_t schemaVersion) + { + return std::format("{{\"schemaVersion\":{},\"keyId\":\"test-key\"," + "\"payload\":\"{}\",\"signature\":\"{}\"}}", + schemaVersion, + Void::Encoding::Base64::encode_url(message), + Void::Encoding::Base64::encode_url(sign(message))); + } + + constexpr std::string_view componentCatalogPayload = R"json({ + "schemaVersion": 2, + "document": "echoion-component-catalog", + "channel": "stable", + "sequence": 11, + "generatedAt": "2026-01-01T00:00:00Z", + "expiresAt": "2099-01-01T00:00:00Z", + "productVersion": "2.0.0", + "components": [{ + "componentId": "Host", + "pluginId": null, + "kind": "runtime-module", + "componentVersion": "2.0.1", + "activation": "restart-required", + "dependencies": {"Api": ">=2.0.0 <3.0.0"}, + "sequence": 11, + "latestUrl": "https://updates.invalid/feeds/stable/host/latest.json", + "artifactSha256": "1111111111111111111111111111111111111111111111111111111111111111" + }, { + "componentId": "Api", + "pluginId": null, + "kind": "sdk", + "componentVersion": "2.0.0", + "activation": "restart-required", + "dependencies": {}, + "sequence": 11, + "latestUrl": "https://updates.invalid/feeds/stable/api/latest.json", + "artifactSha256": "2222222222222222222222222222222222222222222222222222222222222222" + }] +})json"; + + std::string componentReleasePayload(std::string_view componentId, + std::string_view version, + std::string_view artifactHash, + std::string_view dependencies) + { + return std::format( + R"json({{ + "schemaVersion": 2, + "document": "echoion-component-release", + "channel": "stable", + "sequence": 11, + "generatedAt": "2026-01-01T00:00:00Z", + "expiresAt": "2099-01-01T00:00:00Z", + "productVersion": "2.0.0", + "componentId": "{}", + "pluginId": null, + "kind": "{}", + "componentVersion": "{}", + "sourceSha": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "sourceTreeHash": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", + "abiHash": "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", + "artifactIdentity": "{}-{}-linux-x86_64", + "activation": "restart-required", + "dependencies": {}, + "artifact": {{ + "name": "{}-{}.tar.zst", + "url": "https://updates.invalid/{}-{}.tar.zst", + "sha256": "{}", + "size": 42, + "manifestSha256": "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", + "packageType": "tar.zst" + }} +}})json", + componentId, componentId == "Api" ? "sdk" : "runtime-module", + version, componentId, version, dependencies, componentId, version, + componentId, version, artifactHash); + } } // namespace Void::Testing::Test update_feed_verifies_before_parsing() @@ -140,6 +219,40 @@ Void::Testing::Test update_feed_unsigned_parse_replay_and_selection() !Void::Update::selectRelease(*parsed, request).has_value()); } +Void::Testing::Test component_feed_verifies_and_resolves_complete_set() +{ + auto envelope = Void::Update::verifyEnvelope( + signedEnvelope(componentCatalogPayload, 2), keyring()); + Void::Testing::require(envelope.has_value()); + Void::Testing::expect_eq(envelope->schemaVersion, std::uint32_t{2}); + auto catalog = + Void::Update::parseComponentCatalogPayload(envelope->signedPayload); + Void::Testing::require(catalog.has_value()); + Void::Testing::expect_eq(catalog->components.size(), std::size_t{2}); + + auto host = + Void::Update::parseComponentReleasePayload(componentReleasePayload( + "Host", "2.0.1", + "1111111111111111111111111111111111111111111111111111111111111111", + R"json({"Api":">=2.0.0 <3.0.0"})json")); + auto api = + Void::Update::parseComponentReleasePayload(componentReleasePayload( + "Api", "2.0.0", + "2222222222222222222222222222222222222222222222222222222222222222", + "{}")); + Void::Testing::require(host.has_value()); + Void::Testing::require(api.has_value()); + std::map releases; + releases.emplace("Api", *api); + releases.emplace("Host", *host); + Void::Testing::expect( + Void::Update::validateComponentSet(*catalog, releases).has_value()); + + releases.at("Api").componentVersion = *Void::Semver::parse("3.0.0"); + Void::Testing::expect( + !Void::Update::validateComponentSet(*catalog, releases).has_value()); +} + Void::Testing::Test update_feed_rejects_empty_fractional_timestamp() { std::string malformed{payload};