1 parent
4a92d09e63
commit
45eb2cab42
12 files changed
+991
-162
No files matched your search
@@ -0,0 +1,127 @@
|
||||
#include "echoion/standalone/ReleaseManifest.hpp"
|
||||
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
|
||||
#include <openssl/evp.h>
|
||||
|
||||
#include <array>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
|
||||
namespace Echoion::Standalone
|
||||
{
|
||||
namespace
|
||||
{
|
||||
Api::Result<std::string> sha256(const std::filesystem::path& path)
|
||||
{
|
||||
std::ifstream input(path, std::ios::binary);
|
||||
if (!input) {
|
||||
return Api::fail(Api::ErrorCode::NotFound,
|
||||
"release artifact not found: " + path.string());
|
||||
}
|
||||
using Context = std::unique_ptr<EVP_MD_CTX, decltype(&EVP_MD_CTX_free)>;
|
||||
Context context(EVP_MD_CTX_new(), &EVP_MD_CTX_free);
|
||||
if (!context || EVP_DigestInit_ex(context.get(), EVP_sha256(), nullptr) != 1) {
|
||||
return Api::fail(Api::ErrorCode::UnknownError,
|
||||
"cannot initialize SHA-256");
|
||||
}
|
||||
std::array<char, 64 * 1024> buffer{};
|
||||
while (input) {
|
||||
input.read(buffer.data(), static_cast<std::streamsize>(buffer.size()));
|
||||
if (const auto count = input.gcount(); count > 0 &&
|
||||
EVP_DigestUpdate(context.get(), buffer.data(),
|
||||
static_cast<std::size_t>(count)) != 1) {
|
||||
return Api::fail(Api::ErrorCode::UnknownError,
|
||||
"cannot hash release artifact");
|
||||
}
|
||||
}
|
||||
std::array<unsigned char, EVP_MAX_MD_SIZE> digest{};
|
||||
unsigned int size = 0;
|
||||
if (EVP_DigestFinal_ex(context.get(), digest.data(), &size) != 1) {
|
||||
return Api::fail(Api::ErrorCode::UnknownError,
|
||||
"cannot finalize release hash");
|
||||
}
|
||||
std::ostringstream output;
|
||||
output << std::hex << std::setfill('0');
|
||||
for (unsigned int index = 0; index < size; ++index) {
|
||||
output << std::setw(2) << static_cast<unsigned int>(digest[index]);
|
||||
}
|
||||
return output.str();
|
||||
}
|
||||
}
|
||||
|
||||
Api::Result<ReleaseManifest>
|
||||
ReleaseManifest::load(const std::filesystem::path& manifestPath)
|
||||
{
|
||||
auto contents = Void::FileSystem::read_file(manifestPath);
|
||||
if (!contents) {
|
||||
return Api::fail(Api::ErrorCode::NotFound, contents.error().message());
|
||||
}
|
||||
auto json = Void::Json::parse(*contents);
|
||||
if (!json || !json->is_object()) {
|
||||
return Api::fail(Api::ErrorCode::ParseError,
|
||||
json ? "release manifest must be an object"
|
||||
: "invalid release manifest: " + json.error().message);
|
||||
}
|
||||
ReleaseManifest manifest;
|
||||
manifest.version = json->get_str("version");
|
||||
const auto base = manifestPath.parent_path();
|
||||
const auto executableEntry = std::filesystem::path(
|
||||
json->get_str("executable"));
|
||||
const auto coreEntry = std::filesystem::path(json->get_str("core"));
|
||||
const auto unsafe = [](const std::filesystem::path& path) {
|
||||
return path.empty() || path.is_absolute() ||
|
||||
std::ranges::contains(path, std::filesystem::path{".."});
|
||||
};
|
||||
if (unsafe(executableEntry) || unsafe(coreEntry)) {
|
||||
return Api::fail(Api::ErrorCode::InvalidArgument,
|
||||
"release artifact paths must stay within the manifest directory");
|
||||
}
|
||||
manifest.executable = (base / executableEntry).lexically_normal();
|
||||
manifest.core = (base / coreEntry).lexically_normal();
|
||||
if (const auto* hashes = json->find("sha256")) {
|
||||
manifest.executableSha256 = hashes->get_str("executable");
|
||||
manifest.coreSha256 = hashes->get_str("core");
|
||||
}
|
||||
if (manifest.version.empty() || manifest.executable.filename().empty() ||
|
||||
manifest.core.filename().empty() || manifest.executableSha256.empty() ||
|
||||
manifest.coreSha256.empty()) {
|
||||
return Api::fail(Api::ErrorCode::ParseError,
|
||||
"release manifest is missing version, artifacts, or hashes");
|
||||
}
|
||||
return manifest;
|
||||
}
|
||||
|
||||
Api::Result<void> ReleaseManifest::validate() const
|
||||
{
|
||||
if (!std::filesystem::is_regular_file(executable) ||
|
||||
!std::filesystem::is_regular_file(core)) {
|
||||
return Api::fail(Api::ErrorCode::NotFound,
|
||||
"release executable and Core must be regular files");
|
||||
}
|
||||
const auto permissions = std::filesystem::status(executable).permissions();
|
||||
if ((permissions & (std::filesystem::perms::owner_exec |
|
||||
std::filesystem::perms::group_exec |
|
||||
std::filesystem::perms::others_exec)) == std::filesystem::perms::none) {
|
||||
return Api::fail(Api::ErrorCode::PermissionDenied,
|
||||
"release executable is not executable");
|
||||
}
|
||||
for (const auto& [path, expected] :
|
||||
{std::pair{executable, executableSha256},
|
||||
std::pair{core, coreSha256}}) {
|
||||
auto actual = sha256(path);
|
||||
if (!actual) {
|
||||
return std::unexpected(actual.error());
|
||||
}
|
||||
if (*actual != expected) {
|
||||
return Api::fail(Api::ErrorCode::IncompatibleVersion,
|
||||
"SHA-256 mismatch for " + path.string());
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user