feat(update): fix schema-3 verify-before-parse and add replay floor, TrustStore, pin pruning, and InstallCoordinator

This commit is contained in:
faumaray committed 2026-11-25 21:04:52 +03:00
1 parent 00d7478335
commit 47736cb6ce
13 files changed
+2663 -23

No files matched your search

+193 -22
View File
@@ -1,6 +1,7 @@
#include <Void/Update/Feed.hpp>
#include <Void/Data/Json.hpp>
#include <Void/Data/StrictJson.hpp>
#include <Void/Text/Base64.hpp>
#include <algorithm>
@@ -592,6 +593,26 @@ namespace Void::Update
}
return true;
}
[[nodiscard]] Void::Result::Result<void>
enforce_parse_policy(std::string_view payload,
const VerificationPolicy& policy)
{
if (policy.parsePolicy == SchemaParsePolicy::StrictJsonProfile) {
auto strict = Json::Strict::parse(
payload, Json::Strict::ParseOptions{
.maximumDepth = policy.maxJsonDepth,
.allowDecimals = false,
});
if (!strict.has_value()) {
return std::unexpected{make_update_error(
UpdateErrorCode::schema_error,
std::format("Update feed: StrictJsonProfile rejected "
"payload: {}",
strict.error().message))};
}
}
return {};
}
} // namespace
Void::Result::Result<Feed>
@@ -599,8 +620,13 @@ namespace Void::Update
const VerificationPolicy& policy)
{
if (payload.size() > policy.maxPayloadBytes) {
return Void::Result::fail(
"Update feed: payload exceeds policy size");
return std::unexpected{make_update_error(
UpdateErrorCode::limit_exceeded,
"Update feed: payload exceeds policy size")};
}
if (auto strict = enforce_parse_policy(payload, policy);
!strict.has_value()) {
return std::unexpected{std::move(strict.error())};
}
return parse_feed(payload, policy);
}
@@ -665,25 +691,21 @@ namespace Void::Update
}
const auto key = keys.find(*keyId);
if (key == keys.end()) {
return Void::Result::fail(
"Update feed: envelope key is not trusted");
return std::unexpected{make_update_error(
UpdateErrorCode::untrusted_key,
"Update feed: envelope key is not trusted")};
}
auto payload = Encoding::Base64::decode_url(*payloadText);
auto signature = Encoding::Base64::decode_url(*signatureText);
if (!payload || !signature ||
payload->size() > policy.maxPayloadBytes) {
return Void::Result::fail(
"Update feed: invalid or oversized envelope encoding");
}
if (*schema == 3) {
auto signedDocument = Json::parse(*payload);
if (!signedDocument || !signedDocument->is_object() ||
displayedDocument == nullptr ||
*displayedDocument != *signedDocument) {
return Void::Result::fail("Update feed: readable document "
"differs from signed payload");
}
return std::unexpected{make_update_error(
UpdateErrorCode::schema_error,
"Update feed: invalid or oversized envelope encoding")};
}
// Verify exact signed payload bytes BEFORE any payload JSON parsing or
// schema-3 readable-document mirror comparison.
const auto signatureBytes =
std::as_bytes(std::span{signature->data(), signature->size()});
const auto payloadBytes =
@@ -691,11 +713,30 @@ namespace Void::Update
auto valid =
Crypto::verifyEd25519(payloadBytes, signatureBytes, key->second);
if (!valid) {
return std::unexpected{valid.error()};
return std::unexpected{make_update_error(
UpdateErrorCode::signature_failed, valid.error().message())};
}
if (!*valid) {
return Void::Result::fail(
"Update feed: signature verification failed");
return std::unexpected{make_update_error(
UpdateErrorCode::signature_failed,
"Update feed: signature verification failed")};
}
if (auto strict = enforce_parse_policy(*payload, policy);
!strict.has_value()) {
return std::unexpected{std::move(strict.error())};
}
if (*schema == 3) {
auto signedDocument = Json::parse(*payload);
if (!signedDocument || !signedDocument->is_object() ||
displayedDocument == nullptr ||
*displayedDocument != *signedDocument) {
return std::unexpected{make_update_error(
UpdateErrorCode::mirror_mismatch,
"Update feed: readable document differs from signed "
"payload")};
}
}
return VerifiedEnvelope{.schemaVersion = *schema,
.keyId = std::move(*keyId),
@@ -707,8 +748,13 @@ namespace Void::Update
const VerificationPolicy& policy)
{
if (payload.size() > policy.maxPayloadBytes) {
return Void::Result::fail(
"Component feed: payload exceeds policy size");
return std::unexpected{make_update_error(
UpdateErrorCode::limit_exceeded,
"Component feed: payload exceeds policy size")};
}
if (auto strict = enforce_parse_policy(payload, policy);
!strict.has_value()) {
return std::unexpected{std::move(strict.error())};
}
auto document = Json::parse(payload);
if (!document || !document->is_object()) {
@@ -754,8 +800,13 @@ namespace Void::Update
const VerificationPolicy& policy)
{
if (payload.size() > policy.maxPayloadBytes) {
return Void::Result::fail(
"Component feed: payload exceeds policy size");
return std::unexpected{make_update_error(
UpdateErrorCode::limit_exceeded,
"Component feed: payload exceeds policy size")};
}
if (auto strict = enforce_parse_policy(payload, policy);
!strict.has_value()) {
return std::unexpected{std::move(strict.error())};
}
auto document = Json::parse(payload);
if (!document || !document->is_object()) {
@@ -919,4 +970,124 @@ namespace Void::Update
}
return *selected;
}
Void::Result::Result<VerifiedSelection>
verify_and_select_release(std::string_view envelopeJson,
const Keyring& keys,
const SelectionRequest& request,
const VerificationPolicy& policy)
{
auto verified = verifyFeedEnvelope(envelopeJson, keys, policy);
if (!verified.has_value()) {
return std::unexpected{std::move(verified.error())};
}
auto selected = selectRelease(verified->feed, request);
if (!selected.has_value()) {
return std::unexpected{make_update_error(
UpdateErrorCode::schema_error,
"Update feed: no compatible release matched selection "
"request")};
}
VerifiedSelection out;
out.m_signed_envelope.assign(envelopeJson);
out.m_signed_payload = std::move(verified->signedPayload);
out.m_signer_key_id = std::move(verified->keyId);
out.m_feed_id = verified->feed.feedId;
out.m_sequence = verified->feed.sequence;
out.m_expires_at = verified->feed.expiresAt;
out.m_parse_policy = policy.parsePolicy;
out.m_release = ReleaseIdentity{
.component = std::move(selected->component),
.artifact_id = std::move(selected->artifactId),
.channel = std::move(selected->channel),
.version = std::move(selected->version),
.sequence = verified->feed.sequence,
.sha256 = std::move(selected->sha256),
.size = selected->size,
.url = std::move(selected->url),
.package_type = std::move(selected->packageType),
};
return out;
}
Void::Result::Result<VerifiedSelection> verify_and_select_component_set(
std::string_view catalogEnvelopeJson,
const std::map<std::string, std::string>& releaseEnvelopeJsons,
const Keyring& keys, std::string_view primaryComponentId,
const VerificationPolicy& policy)
{
auto cat_env = verifyEnvelope(catalogEnvelopeJson, keys, policy);
if (!cat_env.has_value()) {
return std::unexpected{std::move(cat_env.error())};
}
auto catalog =
parseComponentCatalogPayload(cat_env->signedPayload, policy);
if (!catalog.has_value()) {
return std::unexpected{std::move(catalog.error())};
}
if (catalog->components.size() >
UpdateLimits::kDefaultMaxComponentsPerTransaction) {
return std::unexpected{make_update_error(
UpdateErrorCode::limit_exceeded,
"Component set: exceeds 32-component transaction limit")};
}
std::map<std::string, ComponentRelease> releases;
std::size_t edge_count = 0;
for (const auto& [comp_id, env_json] : releaseEnvelopeJsons) {
auto rel_env = verifyEnvelope(env_json, keys, policy);
if (!rel_env.has_value()) {
return std::unexpected{std::move(rel_env.error())};
}
auto rel =
parseComponentReleasePayload(rel_env->signedPayload, policy);
if (!rel.has_value()) {
return std::unexpected{std::move(rel.error())};
}
edge_count += rel->dependencies.size();
if (edge_count > UpdateLimits::kDefaultMaxDependencyEdges) {
return std::unexpected{make_update_error(
UpdateErrorCode::limit_exceeded,
"Component set: exceeds 256 dependency-edge limit")};
}
releases.emplace(comp_id, std::move(*rel));
}
if (auto valid = validateComponentSet(*catalog, releases);
!valid.has_value()) {
return std::unexpected{std::move(valid.error())};
}
auto primary_it = releases.find(std::string{primaryComponentId});
if (primary_it == releases.end()) {
return std::unexpected{make_update_error(
UpdateErrorCode::invalid_argument,
"Component set: primaryComponentId not present in verified "
"set")};
}
const auto& primary = primary_it->second;
VerifiedSelection out;
out.m_signed_envelope.assign(catalogEnvelopeJson);
out.m_signed_payload = std::move(cat_env->signedPayload);
out.m_signer_key_id = std::move(cat_env->keyId);
out.m_feed_id = "component-catalog";
out.m_sequence = catalog->sequence;
out.m_expires_at = catalog->expiresAt;
out.m_parse_policy = policy.parsePolicy;
out.m_release = ReleaseIdentity{
.component = primary.componentId,
.artifact_id = primary.artifactIdentity,
.channel = primary.channel,
.version = primary.componentVersion,
.sequence = primary.sequence,
.sha256 = primary.artifact.sha256,
.size = primary.artifact.size,
.url = primary.artifact.url,
.package_type = primary.artifact.packageType,
};
out.m_components = std::move(releases);
return out;
}
} // namespace Void::Update
File diff suppressed because it is too large. Load diff
+118
View File
@@ -1721,6 +1721,124 @@ namespace Void::Update
return removed;
}
Void::Result::Result<PruneReport>
VersionStore::pruneWithPins(std::size_t retainedPreviousVersions,
std::span<const Semver::Version>
pinnedVersions,
std::size_t batchLimit)
{
if (batchLimit == 0) {
return Void::Result::fail(
"VersionStore::pruneWithPins: batchLimit must be positive");
}
for (const auto& path : {m_root, m_root / "versions"}) {
if (auto checked = require_real_directory(path); !checked) {
return std::unexpected{checked.error()};
}
}
auto lock = FileLock::acquire(m_root / ".update.lock");
if (!lock) {
return std::unexpected{lock.error()};
}
if (auto cleaned = cleanup_pruning_entries(m_root / "versions");
!cleaned) {
return std::unexpected{cleaned.error()};
}
auto current = currentVersion();
if (!current) {
return std::unexpected{current.error()};
}
auto pending = pendingActivation();
if (!pending) {
return std::unexpected{pending.error()};
}
auto history = read_health_history(m_root);
if (!history) {
return std::unexpected{history.error()};
}
auto published = published_versions(m_root / "versions");
if (!published) {
return std::unexpected{published.error()};
}
std::sort(published->begin(), published->end(),
std::greater<Semver::Version>{});
std::set<std::string, std::less<>> publishedNames;
for (const auto& version : *published) {
publishedNames.insert(version.to_string());
}
std::set<std::string, std::less<>> retained;
std::optional<std::string> currentName;
if (*current) {
currentName = (**current).to_string();
retained.insert(*currentName);
}
if (*pending) {
retained.insert((*pending)->candidateVersion.to_string());
if ((*pending)->previousVersion) {
retained.insert((*pending)->previousVersion->to_string());
}
}
std::size_t retainedPrevious = 0;
for (auto iterator = history->committedOrder.rbegin();
iterator != history->committedOrder.rend() &&
retainedPrevious < retainedPreviousVersions;
++iterator) {
const auto state = history->states.find(*iterator);
if (publishedNames.contains(*iterator) &&
state != history->states.end() &&
state->second == HealthState::healthy &&
(!currentName || *iterator != *currentName) &&
!retained.contains(*iterator)) {
retained.insert(*iterator);
++retainedPrevious;
}
}
std::set<std::string, std::less<>> pinnedNames;
for (const auto& v : pinnedVersions) {
pinnedNames.insert(v.to_string());
}
PruneReport report;
for (const auto& version : *published) {
const auto name = version.to_string();
if (retained.contains(name)) {
continue;
}
const auto state = history->states.find(name);
if (state == history->states.end() ||
(state->second != HealthState::healthy &&
state->second != HealthState::failed &&
state->second != HealthState::retired)) {
continue;
}
if (pinnedNames.contains(name)) {
report.skipped_due_to_pins.push_back(version);
continue;
}
if (report.removed.size() >= batchLimit) {
report.batch_limit_reached = true;
break;
}
if (state->second != HealthState::retired) {
if (auto recorded = append_health_event(m_root, 'D', name);
!recorded) {
return std::unexpected{recorded.error()};
}
}
if (auto pruned = quarantine_and_remove(m_root / "versions", name);
!pruned) {
return std::unexpected{pruned.error()};
}
report.removed.push_back(version);
}
return report;
}
Void::Result::Result<std::optional<Semver::Version>>
VersionStore::currentVersion() const
{
+1 -1
View File
@@ -1,3 +1,3 @@
void_add_module(Update
INTERNAL_DEPS Crypto Data Functional System Text
INTERNAL_DEPS Archive Crypto Data Functional System Text Time
)