feat(archive): add streaming Reader, atomic StagedTree publication, reproducible creation, and isolated worker
This commit is contained in:
1 parent
713d2dc656
commit
00d7478335
10 files changed
+3781
-5
No files matched your search
@@ -1,11 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Archive/Types.hpp>
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <stop_token>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Archive
|
||||
{
|
||||
@@ -21,12 +29,176 @@ namespace Void::Archive
|
||||
std::uint64_t bytes{0};
|
||||
};
|
||||
|
||||
/// Extract a tar/tar.zst archive without invoking an external process.
|
||||
/// Only regular files and directories are accepted. Absolute/traversal
|
||||
/// paths, links, special files, duplicates and configured size/count
|
||||
/// overages fail the whole operation.
|
||||
/// Extract a tar/tar.zst archive incrementally into `destination` without
|
||||
/// invoking an external process. Preserved 1.x incremental API; callers
|
||||
/// requiring all-or-nothing directory visibility should use
|
||||
/// `extract_staged` + `StagedTree::publish`.
|
||||
[[nodiscard]] Void::Result::Result<ExtractionResult>
|
||||
extract(const std::filesystem::path& archivePath,
|
||||
const std::filesystem::path& destination,
|
||||
const ExtractionOptions& options = {});
|
||||
|
||||
/// Caller-driven bounded tar/tar.zst reader. Maintains one active entry at
|
||||
/// a time; callers must drain (`read_chunk` returning 0) or call
|
||||
/// `skip_entry()` before advancing with `next_entry()`.
|
||||
class Reader
|
||||
{
|
||||
public:
|
||||
|
||||
~Reader();
|
||||
Reader(Reader&&) noexcept;
|
||||
Reader& operator=(Reader&&) noexcept;
|
||||
Reader(const Reader&) = delete;
|
||||
Reader& operator=(const Reader&) = delete;
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<Reader> open(
|
||||
ByteSource& source, ReadLimits limits = {},
|
||||
std::stop_token stop = {},
|
||||
std::optional<Void::Time::MonotonicClock::time_point> deadline =
|
||||
std::nullopt,
|
||||
const Void::Time::MonotonicClock& clock =
|
||||
Void::Time::SystemMonotonicClock::instance());
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<Reader> open_file(
|
||||
const std::filesystem::path& archive_path,
|
||||
ReadLimits limits = {}, std::stop_token stop = {},
|
||||
std::optional<Void::Time::MonotonicClock::time_point> deadline =
|
||||
std::nullopt,
|
||||
const Void::Time::MonotonicClock& clock =
|
||||
Void::Time::SystemMonotonicClock::instance());
|
||||
|
||||
/// Advance to the next archive entry. Returns std::nullopt on clean
|
||||
/// EOF.
|
||||
[[nodiscard]] Void::Result::Result<std::optional<EntryInfo>>
|
||||
next_entry();
|
||||
|
||||
/// Read up to dst.size() decompressed bytes of the active regular
|
||||
/// file entry. Returns 0 on entry EOF.
|
||||
[[nodiscard]] Void::Result::Result<std::size_t>
|
||||
read_chunk(std::span<std::byte> dst);
|
||||
|
||||
/// Drain and discard remaining bytes of the active entry while
|
||||
/// still accounting decompressed bytes against policy limits.
|
||||
[[nodiscard]] Void::Result::Result<std::uint64_t> skip_entry();
|
||||
|
||||
[[nodiscard]] std::size_t entries_seen() const noexcept;
|
||||
[[nodiscard]] std::uint64_t total_decompressed_bytes() const noexcept;
|
||||
[[nodiscard]] std::uint64_t total_compressed_bytes() const noexcept;
|
||||
|
||||
private:
|
||||
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> m_impl;
|
||||
explicit Reader(std::unique_ptr<Impl> impl) noexcept;
|
||||
};
|
||||
|
||||
/// Bounded metadata listing of archive entries. Normal mode (`forensic =
|
||||
/// false`) rejects unsafe/duplicate/conflicting entries. Opt-in forensic
|
||||
/// mode reports rejected entries without writing to disk.
|
||||
[[nodiscard]] Void::Result::Result<std::vector<EntryInfo>>
|
||||
list_entries(ByteSource& source, ReadLimits limits = {},
|
||||
bool forensic = false);
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::vector<EntryInfo>>
|
||||
list_entries(const std::filesystem::path& archive_path,
|
||||
ReadLimits limits = {}, bool forensic = false);
|
||||
|
||||
/// Move-only handle owning an unpublished private staging directory beneath
|
||||
/// a pinned parent directory. Unlinks only names under its staging
|
||||
/// descriptor without following links if destroyed before publication.
|
||||
class StagedTree
|
||||
{
|
||||
public:
|
||||
|
||||
StagedTree() noexcept = default;
|
||||
~StagedTree() noexcept;
|
||||
StagedTree(StagedTree&&) noexcept;
|
||||
StagedTree& operator=(StagedTree&&) noexcept;
|
||||
StagedTree(const StagedTree&) = delete;
|
||||
StagedTree& operator=(const StagedTree&) = delete;
|
||||
|
||||
[[nodiscard]] bool valid() const noexcept;
|
||||
[[nodiscard]] bool published() const noexcept;
|
||||
[[nodiscard]] std::string_view staging_id() const noexcept;
|
||||
[[nodiscard]] const std::filesystem::path&
|
||||
staging_root() const noexcept;
|
||||
[[nodiscard]] const std::filesystem::path&
|
||||
tree_path() const noexcept;
|
||||
[[nodiscard]] std::size_t entries() const noexcept;
|
||||
[[nodiscard]] std::uint64_t bytes() const noexcept;
|
||||
[[nodiscard]] const std::vector<EntryInfo>&
|
||||
inventory() const noexcept;
|
||||
|
||||
/// Publish the staged tree into a previously absent immediate child
|
||||
/// of the pinned parent directory using atomic no-replace rename
|
||||
/// (`renameat2(..., RENAME_NOREPLACE)`). Never overwrites existing
|
||||
/// directories or falls back to a racy existence check.
|
||||
[[nodiscard]] Void::Result::Result<PublicationReceipt>
|
||||
publish(std::string_view new_child_name);
|
||||
|
||||
/// Explicitly discard an unpublished staging tree.
|
||||
[[nodiscard]] Void::Result::Result<void> discard();
|
||||
|
||||
private:
|
||||
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> m_impl;
|
||||
explicit StagedTree(std::unique_ptr<Impl> impl) noexcept;
|
||||
|
||||
friend Void::Result::Result<StagedTree>
|
||||
extract_staged(ByteSource& source,
|
||||
const std::filesystem::path& parent_directory,
|
||||
const StagedExtractionOptions& options);
|
||||
friend Void::Result::Result<StagedTree>
|
||||
extract_staged_isolated(
|
||||
const std::filesystem::path& archive_path,
|
||||
const std::filesystem::path& parent_directory,
|
||||
const IsolationOptions& options);
|
||||
};
|
||||
|
||||
/// Extract a tar/tar.zst stream into an exclusive 0700 staging directory
|
||||
/// beneath `parent_directory`, fsync files and directories bottom-up, write
|
||||
/// a bounded completion inventory, and return an unpublished StagedTree.
|
||||
[[nodiscard]] Void::Result::Result<StagedTree>
|
||||
extract_staged(ByteSource& source,
|
||||
const std::filesystem::path& parent_directory,
|
||||
const StagedExtractionOptions& options = {});
|
||||
|
||||
[[nodiscard]] Void::Result::Result<StagedTree>
|
||||
extract_staged(const std::filesystem::path& archive_path,
|
||||
const std::filesystem::path& parent_directory,
|
||||
const StagedExtractionOptions& options = {});
|
||||
|
||||
/// Remove abandoned staging directories under `parent_directory` that carry
|
||||
/// a valid exclusive operation marker owned by the current user and are at
|
||||
/// least `min_age` old. Never touches directories without a valid marker.
|
||||
[[nodiscard]] Void::Result::Result<std::size_t>
|
||||
cleanup_stale_staging(const std::filesystem::path& parent_directory,
|
||||
std::chrono::seconds min_age =
|
||||
std::chrono::seconds{300});
|
||||
|
||||
/// Create a bounded, optionally reproducible tar or tar.zst archive from
|
||||
/// regular file and directory entries. Sorts normalized paths, rejects
|
||||
/// duplicates/conflicts, and opens source files with O_NOFOLLOW.
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
create_archive(ByteSink& sink, std::span<const CreateEntry> entries,
|
||||
const CreationOptions& options = {});
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
create_archive(const std::filesystem::path& output_path,
|
||||
std::span<const CreateEntry> entries,
|
||||
const CreationOptions& options = {});
|
||||
|
||||
/// Hardened untrusted-input profile: extracts the archive in an isolated
|
||||
/// System child process with bounded execution time and RSS checks, then
|
||||
/// independently verifies the staged tree and inventory in the parent
|
||||
/// before returning StagedTree.
|
||||
[[nodiscard]] Void::Result::Result<StagedTree>
|
||||
extract_staged_isolated(const std::filesystem::path& archive_path,
|
||||
const std::filesystem::path& parent_directory,
|
||||
const IsolationOptions& options);
|
||||
|
||||
/// Entry point invoked by the isolated archive decoder worker binary.
|
||||
[[nodiscard]] int run_isolated_worker_main(int argc, char** argv);
|
||||
|
||||
} // namespace Void::Archive
|
||||
@@ -0,0 +1,342 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <stop_token>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Archive
|
||||
{
|
||||
enum class ArchiveErrorCode : std::int32_t {
|
||||
none = 0,
|
||||
invalid_argument = 1,
|
||||
unsafe_entry = 2,
|
||||
unsupported_format = 3,
|
||||
corrupt_archive = 4,
|
||||
limit_exceeded = 5,
|
||||
permission_denied = 6,
|
||||
no_space = 7,
|
||||
io_error = 8,
|
||||
cancelled = 9,
|
||||
timeout = 10,
|
||||
unknown_publication = 11,
|
||||
unsupported_capability = 12,
|
||||
already_exists = 13,
|
||||
conflict = 14,
|
||||
isolation_failed = 15
|
||||
};
|
||||
|
||||
[[nodiscard]] inline Void::Result::Error
|
||||
make_archive_error(ArchiveErrorCode code, std::string message = {},
|
||||
std::error_code sys_err = {})
|
||||
{
|
||||
using Void::Result::Error;
|
||||
using Void::Result::ErrorCode;
|
||||
using Void::Result::ErrorKind;
|
||||
|
||||
ErrorKind kind = ErrorKind::internal;
|
||||
switch (code) {
|
||||
case ArchiveErrorCode::none:
|
||||
kind = ErrorKind::internal;
|
||||
break;
|
||||
case ArchiveErrorCode::invalid_argument:
|
||||
case ArchiveErrorCode::unsafe_entry:
|
||||
kind = ErrorKind::invalid_argument;
|
||||
break;
|
||||
case ArchiveErrorCode::unsupported_format:
|
||||
case ArchiveErrorCode::unsupported_capability:
|
||||
kind = ErrorKind::unsupported;
|
||||
break;
|
||||
case ArchiveErrorCode::corrupt_archive:
|
||||
kind = ErrorKind::corrupt;
|
||||
break;
|
||||
case ArchiveErrorCode::limit_exceeded:
|
||||
case ArchiveErrorCode::no_space:
|
||||
kind = ErrorKind::exhausted;
|
||||
break;
|
||||
case ArchiveErrorCode::permission_denied:
|
||||
case ArchiveErrorCode::io_error:
|
||||
kind = ErrorKind::io;
|
||||
break;
|
||||
case ArchiveErrorCode::cancelled:
|
||||
kind = ErrorKind::cancelled;
|
||||
break;
|
||||
case ArchiveErrorCode::timeout:
|
||||
kind = ErrorKind::timeout;
|
||||
break;
|
||||
case ArchiveErrorCode::already_exists:
|
||||
case ArchiveErrorCode::conflict:
|
||||
kind = ErrorKind::conflict;
|
||||
break;
|
||||
case ArchiveErrorCode::unknown_publication:
|
||||
case ArchiveErrorCode::isolation_failed:
|
||||
kind = ErrorKind::internal;
|
||||
break;
|
||||
}
|
||||
|
||||
const ErrorCode domain_code{Void::Result::kArchiveErrorDomain,
|
||||
static_cast<std::int32_t>(code)};
|
||||
return Error{kind, std::move(message), domain_code, sys_err};
|
||||
}
|
||||
|
||||
/// Render an untrusted archive entry path safely for diagnostics: capped at
|
||||
/// 4 KiB with control characters and newlines escaped.
|
||||
[[nodiscard]] std::string
|
||||
sanitize_diagnostic_path(std::string_view raw) noexcept;
|
||||
|
||||
struct ReadLimits {
|
||||
std::size_t max_entries{10'000};
|
||||
bool allow_zero_entries{false};
|
||||
std::uint64_t max_file_bytes{512ULL * 1024 * 1024};
|
||||
std::uint64_t max_total_bytes{2ULL * 1024 * 1024 * 1024};
|
||||
std::uint64_t max_compressed_bytes{1ULL * 1024 * 1024 * 1024};
|
||||
std::size_t max_metadata_bytes{8ULL * 1024 * 1024};
|
||||
std::size_t transfer_buffer_bytes{64ULL * 1024};
|
||||
std::size_t max_path_bytes{4096};
|
||||
std::size_t max_component_bytes{255};
|
||||
std::size_t max_path_components{64};
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void> validate() const
|
||||
{
|
||||
if ((max_entries == 0 && !allow_zero_entries) ||
|
||||
max_file_bytes == 0 || max_total_bytes == 0 ||
|
||||
max_compressed_bytes == 0 || max_metadata_bytes == 0 ||
|
||||
transfer_buffer_bytes == 0 || max_path_bytes == 0 ||
|
||||
max_component_bytes == 0 || max_path_components == 0) {
|
||||
return std::unexpected{make_archive_error(
|
||||
ArchiveErrorCode::invalid_argument,
|
||||
"Archive::ReadLimits: limits must be non-zero unless "
|
||||
"allow_zero_entries is explicitly enabled")};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
enum class EntryType : std::uint8_t {
|
||||
RegularFile = 0,
|
||||
Directory,
|
||||
Symlink,
|
||||
Hardlink,
|
||||
Other
|
||||
};
|
||||
|
||||
struct EntryInfo {
|
||||
std::string path{};
|
||||
EntryType type{EntryType::RegularFile};
|
||||
std::uint64_t declared_size{0};
|
||||
std::uint32_t safe_mode{0644};
|
||||
bool executable{false};
|
||||
bool accepted{true};
|
||||
std::string rejection_reason{};
|
||||
};
|
||||
|
||||
struct ProgressEvent {
|
||||
std::size_t entries_processed{0};
|
||||
std::uint64_t bytes_extracted{0};
|
||||
std::string current_path{};
|
||||
};
|
||||
|
||||
using ProgressCallback = std::function<void(const ProgressEvent&)>;
|
||||
|
||||
enum class PublicationOutcome : std::uint8_t {
|
||||
Published = 0,
|
||||
NotPublished,
|
||||
Unknown
|
||||
};
|
||||
|
||||
struct PublicationReceipt {
|
||||
PublicationOutcome outcome{PublicationOutcome::NotPublished};
|
||||
std::filesystem::path destination_path{};
|
||||
std::string staging_id{};
|
||||
std::size_t entries{0};
|
||||
std::uint64_t bytes{0};
|
||||
std::string diagnostic{};
|
||||
};
|
||||
|
||||
struct StagedExtractionOptions {
|
||||
ReadLimits limits{};
|
||||
bool preserve_executable_bit{true};
|
||||
std::stop_token stop{};
|
||||
std::optional<Void::Time::MonotonicClock::time_point> deadline{};
|
||||
const Void::Time::MonotonicClock* clock{nullptr};
|
||||
ProgressCallback on_progress{};
|
||||
};
|
||||
|
||||
enum class ArchiveCompression : std::uint8_t { None = 0, Zstd };
|
||||
|
||||
struct CreationOptions {
|
||||
ArchiveCompression compression{ArchiveCompression::Zstd};
|
||||
bool reproducible{true};
|
||||
ReadLimits limits{};
|
||||
std::stop_token stop{};
|
||||
};
|
||||
|
||||
struct CreateEntry {
|
||||
std::string path{};
|
||||
EntryType type{EntryType::RegularFile};
|
||||
bool executable{false};
|
||||
std::vector<std::byte> content{};
|
||||
std::optional<std::filesystem::path> source_file{};
|
||||
};
|
||||
|
||||
struct IsolationOptions {
|
||||
std::filesystem::path worker_executable{};
|
||||
std::uint64_t max_rss_bytes{128ULL * 1024 * 1024};
|
||||
std::chrono::milliseconds timeout{std::chrono::seconds{60}};
|
||||
StagedExtractionOptions extraction{};
|
||||
};
|
||||
|
||||
/// Synchronous byte source interface borrowed through completion.
|
||||
class ByteSource
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~ByteSource() = default;
|
||||
/// Read up to dst.size() bytes. Returns 0 on clean EOF.
|
||||
[[nodiscard]] virtual Void::Result::Result<std::size_t>
|
||||
read(std::span<std::byte> dst) = 0;
|
||||
};
|
||||
|
||||
/// Synchronous byte sink interface borrowed through completion.
|
||||
class ByteSink
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~ByteSink() = default;
|
||||
[[nodiscard]] virtual Void::Result::Result<void>
|
||||
write(std::span<const std::byte> src) = 0;
|
||||
};
|
||||
|
||||
class SpanByteSource final : public ByteSource
|
||||
{
|
||||
public:
|
||||
|
||||
explicit SpanByteSource(std::span<const std::byte> data) noexcept
|
||||
: m_data(data)
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::size_t>
|
||||
read(std::span<std::byte> dst) override;
|
||||
|
||||
private:
|
||||
|
||||
std::span<const std::byte> m_data{};
|
||||
std::size_t m_offset{0};
|
||||
};
|
||||
|
||||
class FileByteSource final : public ByteSource
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<FileByteSource>
|
||||
open(const std::filesystem::path& path);
|
||||
|
||||
FileByteSource(FileByteSource&&) noexcept;
|
||||
FileByteSource& operator=(FileByteSource&&) noexcept;
|
||||
~FileByteSource() override;
|
||||
|
||||
FileByteSource(const FileByteSource&) = delete;
|
||||
FileByteSource& operator=(const FileByteSource&) = delete;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<std::size_t>
|
||||
read(std::span<std::byte> dst) override;
|
||||
|
||||
private:
|
||||
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> m_impl;
|
||||
explicit FileByteSource(std::unique_ptr<Impl> impl) noexcept;
|
||||
};
|
||||
|
||||
class VectorByteSink final : public ByteSink
|
||||
{
|
||||
public:
|
||||
|
||||
explicit VectorByteSink(
|
||||
std::size_t max_bytes = 256ULL * 1024 * 1024) noexcept
|
||||
: m_max_bytes(max_bytes)
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
write(std::span<const std::byte> src) override;
|
||||
|
||||
[[nodiscard]] const std::vector<std::byte>& bytes() const noexcept
|
||||
{
|
||||
return m_bytes;
|
||||
}
|
||||
[[nodiscard]] std::vector<std::byte> take_bytes() noexcept
|
||||
{
|
||||
return std::move(m_bytes);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::size_t m_max_bytes{256ULL * 1024 * 1024};
|
||||
std::vector<std::byte> m_bytes{};
|
||||
};
|
||||
|
||||
class FileByteSink final : public ByteSink
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]] static Void::Result::Result<FileByteSink>
|
||||
create(const std::filesystem::path& path);
|
||||
|
||||
FileByteSink(FileByteSink&&) noexcept;
|
||||
FileByteSink& operator=(FileByteSink&&) noexcept;
|
||||
~FileByteSink() override;
|
||||
|
||||
FileByteSink(const FileByteSink&) = delete;
|
||||
FileByteSink& operator=(const FileByteSink&) = delete;
|
||||
|
||||
[[nodiscard]] Void::Result::Result<void>
|
||||
write(std::span<const std::byte> src) override;
|
||||
[[nodiscard]] Void::Result::Result<void> sync_and_close();
|
||||
|
||||
private:
|
||||
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> m_impl;
|
||||
explicit FileByteSink(std::unique_ptr<Impl> impl) noexcept;
|
||||
};
|
||||
|
||||
enum class FaultPoint : std::uint8_t {
|
||||
none = 0,
|
||||
rename_enosys,
|
||||
post_rename_parent_sync,
|
||||
staging_cleanup_fail
|
||||
};
|
||||
|
||||
class ScopedFaultGuard
|
||||
{
|
||||
public:
|
||||
|
||||
explicit ScopedFaultGuard(FaultPoint point) noexcept;
|
||||
~ScopedFaultGuard() noexcept;
|
||||
|
||||
ScopedFaultGuard(const ScopedFaultGuard&) = delete;
|
||||
ScopedFaultGuard& operator=(const ScopedFaultGuard&) = delete;
|
||||
ScopedFaultGuard(ScopedFaultGuard&&) = delete;
|
||||
ScopedFaultGuard& operator=(ScopedFaultGuard&&) = delete;
|
||||
|
||||
private:
|
||||
|
||||
FaultPoint m_previous{FaultPoint::none};
|
||||
};
|
||||
|
||||
} // namespace Void::Archive
|
||||
Reference in new issue
Block a user