1 parent
8583a479dd
commit
3851aeb703
42 files changed
+6948
-759
No files matched your search
@@ -0,0 +1,488 @@
|
||||
#include <Void/Update/Feed.hpp>
|
||||
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/Text/Base64.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <concepts>
|
||||
#include <format>
|
||||
#include <limits>
|
||||
#include <set>
|
||||
#include <tuple>
|
||||
|
||||
namespace Void::Update
|
||||
{
|
||||
namespace
|
||||
{
|
||||
using JsonResult = Void::Result::Result<const Json::Value*>;
|
||||
|
||||
[[nodiscard]] JsonResult member(const Json::Value& object,
|
||||
std::string_view name,
|
||||
bool (*predicate)(const Json::Value&),
|
||||
std::string_view expected)
|
||||
{
|
||||
const auto* value = object.find(name);
|
||||
if (value == nullptr || !predicate(*value)) {
|
||||
return Void::Result::fail(std::format(
|
||||
"Update feed: '{}' must be {}", name, expected));
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool is_array(const Json::Value& value) noexcept
|
||||
{
|
||||
return value.is_array();
|
||||
}
|
||||
[[nodiscard]] bool is_string(const Json::Value& value) noexcept
|
||||
{
|
||||
return value.is_string();
|
||||
}
|
||||
[[nodiscard]] bool is_number(const Json::Value& value) noexcept
|
||||
{
|
||||
return value.is_number();
|
||||
}
|
||||
|
||||
template <std::unsigned_integral T>
|
||||
[[nodiscard]] Void::Result::Result<T>
|
||||
unsigned_number(const Json::Value& object, std::string_view name)
|
||||
{
|
||||
auto value = member(object, name, is_number, "an unsigned integer");
|
||||
if (!value) {
|
||||
return std::unexpected{value.error()};
|
||||
}
|
||||
const double number = (*value)->as_number();
|
||||
constexpr double maxSafeInteger = 9'007'199'254'740'991.0;
|
||||
if (!std::isfinite(number) || number < 0 ||
|
||||
std::floor(number) != number || number > maxSafeInteger ||
|
||||
number > static_cast<double>(std::numeric_limits<T>::max())) {
|
||||
return Void::Result::fail(std::format(
|
||||
"Update feed: '{}' is outside its integer range", name));
|
||||
}
|
||||
return static_cast<T>(number);
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::string>
|
||||
string_member(const Json::Value& object, std::string_view name)
|
||||
{
|
||||
auto value = member(object, name, is_string, "a string");
|
||||
if (!value) {
|
||||
return std::unexpected{value.error()};
|
||||
}
|
||||
return (*value)->as_string();
|
||||
}
|
||||
|
||||
[[nodiscard]] bool valid_token(std::string_view value,
|
||||
std::size_t maximum = 128) noexcept
|
||||
{
|
||||
if (value.empty() || value.size() > maximum) {
|
||||
return false;
|
||||
}
|
||||
for (const char character : value) {
|
||||
if (!((character >= 'a' && character <= 'z') ||
|
||||
(character >= 'A' && character <= 'Z') ||
|
||||
(character >= '0' && character <= '9') ||
|
||||
character == '.' || character == '_' ||
|
||||
character == '-')) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool valid_sha256(std::string_view value) noexcept
|
||||
{
|
||||
if (value.size() != 64) {
|
||||
return false;
|
||||
}
|
||||
return std::ranges::all_of(value, [](const char character) {
|
||||
return (character >= '0' && character <= '9') ||
|
||||
(character >= 'a' && character <= 'f');
|
||||
});
|
||||
}
|
||||
|
||||
[[nodiscard]] bool valid_package_type(std::string_view value) noexcept
|
||||
{
|
||||
return !value.empty() && value.size() <= 128 &&
|
||||
std::ranges::all_of(value, [](const char character) {
|
||||
return (character >= 'a' && character <= 'z') ||
|
||||
(character >= '0' && character <= '9') ||
|
||||
character == '.' || character == '-' ||
|
||||
character == '_';
|
||||
});
|
||||
}
|
||||
|
||||
[[nodiscard]] std::optional<int>
|
||||
decimal(std::string_view value) noexcept
|
||||
{
|
||||
int result = 0;
|
||||
for (const char character : value) {
|
||||
if (character < '0' || character > '9') {
|
||||
return std::nullopt;
|
||||
}
|
||||
result = result * 10 + (character - '0');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::optional<std::chrono::system_clock::time_point>
|
||||
parse_timestamp(std::string_view value) noexcept
|
||||
{
|
||||
if (value.size() < 20 || value[4] != '-' || value[7] != '-' ||
|
||||
value[10] != 'T' || value[13] != ':' || value[16] != ':' ||
|
||||
value.back() != 'Z') {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto year = decimal(value.substr(0, 4));
|
||||
const auto month = decimal(value.substr(5, 2));
|
||||
const auto day = decimal(value.substr(8, 2));
|
||||
const auto hour = decimal(value.substr(11, 2));
|
||||
const auto minute = decimal(value.substr(14, 2));
|
||||
const auto second = decimal(value.substr(17, 2));
|
||||
if (!year || !month || !day || !hour || !minute || !second ||
|
||||
*hour > 23 || *minute > 59 || *second > 59) {
|
||||
return std::nullopt;
|
||||
}
|
||||
if (value.size() > 20) {
|
||||
if (value.size() == 21 || value[19] != '.') {
|
||||
return std::nullopt;
|
||||
}
|
||||
for (std::size_t index = 20; index + 1 < value.size();
|
||||
++index) {
|
||||
if (value[index] < '0' || value[index] > '9') {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
}
|
||||
const std::chrono::year_month_day date{
|
||||
std::chrono::year{*year},
|
||||
std::chrono::month{static_cast<unsigned>(*month)},
|
||||
std::chrono::day{static_cast<unsigned>(*day)}};
|
||||
if (!date.ok()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return std::chrono::sys_days{date} + std::chrono::hours{*hour} +
|
||||
std::chrono::minutes{*minute} +
|
||||
std::chrono::seconds{*second};
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<Compatibility>
|
||||
parse_compatibility(const Json::Value& value)
|
||||
{
|
||||
if (!value.is_object()) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: compatibility must be an object");
|
||||
}
|
||||
Compatibility result;
|
||||
if (const auto* contracts = value.find("versionContracts")) {
|
||||
if (!contracts->is_object()) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: compatibility.versionContracts must be "
|
||||
"an object");
|
||||
}
|
||||
for (const auto& [name, rangeValue] : contracts->as_object()) {
|
||||
if (!valid_token(name, 256) || !rangeValue.is_string()) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: invalid version contract");
|
||||
}
|
||||
auto range = Semver::parse_range(rangeValue.as_string());
|
||||
if (!range) {
|
||||
return Void::Result::fail(
|
||||
std::format("Update feed: version contract '{}' "
|
||||
"has an invalid range",
|
||||
name));
|
||||
}
|
||||
result.versionContracts.emplace(name, std::move(*range));
|
||||
}
|
||||
}
|
||||
if (const auto* contracts = value.find("exactContracts")) {
|
||||
if (!contracts->is_object()) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: compatibility.exactContracts must be an "
|
||||
"object");
|
||||
}
|
||||
for (const auto& [name, contractValue] :
|
||||
contracts->as_object()) {
|
||||
if (!valid_token(name, 256) || !contractValue.is_string() ||
|
||||
contractValue.as_string().empty() ||
|
||||
contractValue.as_string().size() > 512) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: invalid exact contract");
|
||||
}
|
||||
result.exactContracts.emplace(name,
|
||||
contractValue.as_string());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<Release>
|
||||
parse_release(const Json::Value& value)
|
||||
{
|
||||
if (!value.is_object()) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: release must be an object");
|
||||
}
|
||||
Release release;
|
||||
auto component = string_member(value, "component");
|
||||
if (!component || !valid_token(*component, 256)) {
|
||||
return Void::Result::fail("Update feed: component is invalid");
|
||||
}
|
||||
release.component = std::move(*component);
|
||||
release.artifactId = value.get_str("artifactId");
|
||||
if (!release.artifactId.empty() &&
|
||||
!valid_token(release.artifactId, 256)) {
|
||||
return Void::Result::fail("Update feed: artifactId is invalid");
|
||||
}
|
||||
|
||||
auto channel = string_member(value, "channel");
|
||||
auto versionText = string_member(value, "version");
|
||||
auto platform = string_member(value, "platform");
|
||||
auto architecture = string_member(value, "arch");
|
||||
auto url = string_member(value, "url");
|
||||
auto size = unsigned_number<std::uint64_t>(value, "size");
|
||||
auto sha256 = string_member(value, "sha256");
|
||||
auto packageType = string_member(value, "packageType");
|
||||
if (!channel || !versionText || !platform || !architecture ||
|
||||
!url || !size || !sha256 || !packageType) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: release has invalid required fields");
|
||||
}
|
||||
const auto cohort = value.get_str("cohort");
|
||||
const auto manifestHash = value.get_str("releaseManifestSha256");
|
||||
const bool urlIsBounded =
|
||||
!url->empty() && url->size() <= 8192 &&
|
||||
std::ranges::none_of(*url, [](const unsigned char character) {
|
||||
return character < 0x20 || character == 0x7f;
|
||||
});
|
||||
if (!valid_token(*channel) ||
|
||||
(!cohort.empty() && !valid_token(cohort, 256)) ||
|
||||
!valid_token(*platform) || !valid_token(*architecture) ||
|
||||
!urlIsBounded || *size == 0 || !valid_sha256(*sha256) ||
|
||||
!valid_package_type(*packageType) ||
|
||||
(!manifestHash.empty() && !valid_sha256(manifestHash))) {
|
||||
return Void::Result::fail("Update feed: release contains an "
|
||||
"unsafe or unsupported field");
|
||||
}
|
||||
auto version = Semver::parse_strict(*versionText);
|
||||
if (!version) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: release version is invalid SemVer");
|
||||
}
|
||||
release.channel = std::move(*channel);
|
||||
release.version = std::move(*version);
|
||||
release.cohort = cohort;
|
||||
release.platform = std::move(*platform);
|
||||
release.architecture = std::move(*architecture);
|
||||
release.url = std::move(*url);
|
||||
release.size = *size;
|
||||
release.sha256 = std::move(*sha256);
|
||||
release.packageType = std::move(*packageType);
|
||||
release.releaseManifestSha256 = manifestHash;
|
||||
const auto* compatibility = value.find("compatibility");
|
||||
if (compatibility) {
|
||||
auto parsedCompatibility = parse_compatibility(*compatibility);
|
||||
if (!parsedCompatibility) {
|
||||
return std::unexpected{parsedCompatibility.error()};
|
||||
}
|
||||
release.compatibility = std::move(*parsedCompatibility);
|
||||
}
|
||||
return release;
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<Feed>
|
||||
parse_feed(std::string_view payload,
|
||||
const VerificationPolicy& policy)
|
||||
{
|
||||
auto document = Json::parse(payload);
|
||||
if (!document || !document->is_object()) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: signed payload is not a JSON object");
|
||||
}
|
||||
auto schema =
|
||||
unsigned_number<std::uint32_t>(*document, "schemaVersion");
|
||||
auto feedId = string_member(*document, "feedId");
|
||||
auto sequence =
|
||||
unsigned_number<std::uint64_t>(*document, "sequence");
|
||||
auto generated = string_member(*document, "generatedAt");
|
||||
auto expires = string_member(*document, "expiresAt");
|
||||
auto releasesValue =
|
||||
member(*document, "releases", is_array, "an array");
|
||||
if (!schema || !feedId || !sequence || !generated || !expires ||
|
||||
!releasesValue) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: invalid top-level fields");
|
||||
}
|
||||
if (*schema != 1 || !valid_token(*feedId, 256)) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: unsupported schema or invalid feedId");
|
||||
}
|
||||
if (policy.minimumSequence &&
|
||||
*sequence <= *policy.minimumSequence) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: sequence is not newer than the replay floor");
|
||||
}
|
||||
const auto generatedAt = parse_timestamp(*generated);
|
||||
const auto expiresAt = parse_timestamp(*expires);
|
||||
if (!generatedAt || !expiresAt || *expiresAt <= *generatedAt) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: timestamps are invalid");
|
||||
}
|
||||
if (policy.requireUnexpired && policy.now >= *expiresAt) {
|
||||
return Void::Result::fail("Update feed: feed has expired");
|
||||
}
|
||||
const auto& values = (*releasesValue)->as_array();
|
||||
if (values.size() > policy.maxReleases) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: release count exceeds policy");
|
||||
}
|
||||
Feed feed{.schemaVersion = *schema,
|
||||
.feedId = std::move(*feedId),
|
||||
.sequence = *sequence,
|
||||
.generatedAt = *generatedAt,
|
||||
.expiresAt = *expiresAt};
|
||||
std::set<std::tuple<std::string, std::string, std::string,
|
||||
std::string, std::string, std::string>>
|
||||
identities;
|
||||
feed.releases.reserve(values.size());
|
||||
for (const auto& value : values) {
|
||||
auto release = parse_release(value);
|
||||
if (!release) {
|
||||
return std::unexpected{release.error()};
|
||||
}
|
||||
auto identity =
|
||||
std::tuple{release->component, release->artifactId,
|
||||
release->channel, release->version.to_string(),
|
||||
release->platform, release->architecture};
|
||||
if (!identities.insert(std::move(identity)).second) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: ambiguous duplicate release identity");
|
||||
}
|
||||
feed.releases.push_back(std::move(*release));
|
||||
}
|
||||
return feed;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool compatible(const Compatibility& compatibility,
|
||||
const Environment& environment) noexcept
|
||||
{
|
||||
for (const auto& [name, range] : compatibility.versionContracts) {
|
||||
const auto installed = environment.versionContracts.find(name);
|
||||
if (installed == environment.versionContracts.end() ||
|
||||
!range.contains(installed->second)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
for (const auto& [name, value] : compatibility.exactContracts) {
|
||||
const auto installed = environment.exactContracts.find(name);
|
||||
if (installed == environment.exactContracts.end() ||
|
||||
installed->second != value) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<Feed>
|
||||
parseFeedPayload(std::string_view payload,
|
||||
const VerificationPolicy& policy)
|
||||
{
|
||||
if (payload.size() > policy.maxPayloadBytes) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: payload exceeds policy size");
|
||||
}
|
||||
return parse_feed(payload, policy);
|
||||
}
|
||||
|
||||
Void::Result::Result<VerifiedFeed>
|
||||
verifyFeedEnvelope(std::string_view envelopeJson, const Keyring& keys,
|
||||
const VerificationPolicy& policy)
|
||||
{
|
||||
if (envelopeJson.size() > policy.maxEnvelopeBytes) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: envelope exceeds policy size");
|
||||
}
|
||||
auto envelope = Json::parse(envelopeJson);
|
||||
if (!envelope || !envelope->is_object()) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: envelope is not a JSON object");
|
||||
}
|
||||
auto schema =
|
||||
unsigned_number<std::uint32_t>(*envelope, "schemaVersion");
|
||||
auto keyId = string_member(*envelope, "keyId");
|
||||
auto payloadText = string_member(*envelope, "payload");
|
||||
auto signatureText = string_member(*envelope, "signature");
|
||||
if (!schema || !keyId || !payloadText || !signatureText ||
|
||||
*schema != 1 || !valid_token(*keyId, 256)) {
|
||||
return Void::Result::fail("Update feed: invalid envelope fields");
|
||||
}
|
||||
const auto key = keys.find(*keyId);
|
||||
if (key == keys.end()) {
|
||||
return Void::Result::fail(
|
||||
"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");
|
||||
}
|
||||
const auto signatureBytes =
|
||||
std::as_bytes(std::span{signature->data(), signature->size()});
|
||||
const auto payloadBytes =
|
||||
std::as_bytes(std::span{payload->data(), payload->size()});
|
||||
auto valid =
|
||||
Crypto::verifyEd25519(payloadBytes, signatureBytes, key->second);
|
||||
if (!valid) {
|
||||
return std::unexpected{valid.error()};
|
||||
}
|
||||
if (!*valid) {
|
||||
return Void::Result::fail(
|
||||
"Update feed: signature verification failed");
|
||||
}
|
||||
auto feed = parseFeedPayload(*payload, policy);
|
||||
if (!feed) {
|
||||
return std::unexpected{feed.error()};
|
||||
}
|
||||
return VerifiedFeed{.feed = std::move(*feed),
|
||||
.keyId = std::move(*keyId),
|
||||
.signedPayload = std::move(*payload)};
|
||||
}
|
||||
|
||||
std::optional<Release> selectRelease(const Feed& feed,
|
||||
const SelectionRequest& request)
|
||||
{
|
||||
const Release* selected = nullptr;
|
||||
for (const auto& release : feed.releases) {
|
||||
if (release.component != request.component ||
|
||||
release.artifactId != request.artifactId ||
|
||||
release.channel != request.channel ||
|
||||
release.platform != request.environment.platform ||
|
||||
release.architecture != request.environment.architecture ||
|
||||
(request.requiredCohort &&
|
||||
release.cohort != *request.requiredCohort) ||
|
||||
!compatible(release.compatibility, request.environment)) {
|
||||
continue;
|
||||
}
|
||||
if (request.currentVersion) {
|
||||
const auto comparison =
|
||||
release.version <=> *request.currentVersion;
|
||||
if (comparison == 0 && !request.includeCurrent) {
|
||||
continue;
|
||||
}
|
||||
if (comparison < 0 && !request.allowDowngrade) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (selected == nullptr || release.version > selected->version) {
|
||||
selected = &release;
|
||||
}
|
||||
}
|
||||
if (selected == nullptr) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return *selected;
|
||||
}
|
||||
} // namespace Void::Update
|
||||
Reference in new issue
Block a user