feat(crypto,archive): implement VL-007 built-in hashes and VL-006 archive creation/gzip/tar fallback

This commit is contained in:
faumaray committed 2026-11-26 20:55:45 +03:00
1 parent b50131f52e
commit 22c8538754
19 files changed
+3326 -133

No files matched your search

+227 -16
View File
@@ -3,12 +3,16 @@
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
#include <archive.h>
#include <archive_entry.h>
#endif
#include <algorithm>
#include <array>
#include <chrono>
#include <cstddef>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <memory>
@@ -16,6 +20,8 @@
#include <string>
#include <string_view>
#include <sys/stat.h>
#include <thread>
#include <tuple>
#include <vector>
namespace
@@ -39,6 +45,72 @@ namespace
std::istreambuf_iterator<char>{}};
}
void write_octal(std::span<std::byte> dst, std::uint64_t val)
{
dst.back() = std::byte{0};
std::size_t pos = dst.size() - 1;
while (pos > 0) {
--pos;
dst[pos] = static_cast<std::byte>('0' + (val & 7ULL));
val >>= 3ULL;
}
}
std::vector<std::byte> make_raw_tar_ustar(
std::span<const std::tuple<std::string_view, std::string_view, char,
std::string_view>>
entries)
{
std::vector<std::byte> out;
for (const auto& [path, content, typeflag, link] : entries) {
std::array<std::byte, 512> hdr{};
std::memcpy(hdr.data(), path.data(),
std::min<std::size_t>(path.size(), 100));
write_octal(std::span{hdr}.subspan(100, 8),
typeflag == '5' ? 0755ULL : 0644ULL);
write_octal(std::span{hdr}.subspan(108, 8), 0);
write_octal(std::span{hdr}.subspan(116, 8), 0);
const std::uint64_t sz = (typeflag == '0') ? content.size() : 0ULL;
write_octal(std::span{hdr}.subspan(124, 12), sz);
write_octal(std::span{hdr}.subspan(136, 12), 0);
std::fill(hdr.begin() + 148, hdr.begin() + 156, std::byte{' '});
hdr[156] = static_cast<std::byte>(typeflag);
if (!link.empty()) {
std::memcpy(hdr.data() + 157, link.data(),
std::min<std::size_t>(link.size(), 100));
}
std::memcpy(hdr.data() + 257, "ustar\0", 6);
hdr[263] = std::byte{'0'};
hdr[264] = std::byte{'0'};
std::uint32_t sum = 0;
for (const std::byte b : hdr) {
sum += static_cast<std::uint8_t>(b);
}
write_octal(std::span{hdr}.subspan(148, 7), sum);
hdr[155] = std::byte{' '};
out.insert(out.end(), hdr.begin(), hdr.end());
if (typeflag == '0' && !content.empty()) {
const auto cb =
std::as_bytes(std::span{content.data(), content.size()});
out.insert(out.end(), cb.begin(), cb.end());
const std::size_t rem = content.size() % 512U;
if (rem != 0) {
out.resize(out.size() + (512U - rem), std::byte{0});
}
}
}
out.resize(out.size() + 1024U, std::byte{0});
return out;
}
ArchiveCompression default_test_compression() noexcept
{
return has_libarchive_backend() ? ArchiveCompression::Zstd
: ArchiveCompression::None;
}
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
std::vector<std::byte> make_raw_tar(
std::span<const std::tuple<std::string_view, std::string_view,
unsigned int, std::string_view>>
@@ -95,6 +167,7 @@ namespace
Void::Testing::require(archive_write_close(writer.get()) == ARCHIVE_OK);
return out;
}
#endif
} // namespace
Void::Testing::Test archive_reader_reproducible_skip_and_forensic_listing()
@@ -116,7 +189,7 @@ Void::Testing::Test archive_reader_reproducible_skip_and_forensic_listing()
VectorByteSink sink_a;
VectorByteSink sink_b;
CreationOptions copts{.compression = ArchiveCompression::Zstd,
CreationOptions copts{.compression = default_test_compression(),
.reproducible = true};
Void::Testing::require(create_archive(sink_a, order_a, copts).has_value());
Void::Testing::require(create_archive(sink_b, order_b, copts).has_value());
@@ -173,8 +246,26 @@ Void::Testing::Test archive_reader_reproducible_skip_and_forensic_listing()
Void::Testing::expect(zero_entries.validate().has_value());
}
// 4. File-as-parent conflict rejected cleanly (both orderings).
// 4. File-as-parent conflict rejected cleanly (pure USTAR works with and
// without LibArchive; additionally exercise zstd when LibArchive exists).
{
const std::array<
std::tuple<std::string_view, std::string_view, char,
std::string_view>,
2>
conflict_ustar{{
{"foo", "file-bytes", '0', ""},
{"foo/child", "nested", '0', ""},
}};
const auto raw_ustar = make_raw_tar_ustar(conflict_ustar);
SpanByteSource src{raw_ustar};
auto listed = list_entries(src, {}, false);
Void::Testing::require(!listed.has_value());
Void::Testing::expect_eq(
listed.error().code().value,
static_cast<std::int32_t>(ArchiveErrorCode::unsafe_entry));
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
const std::array<
std::tuple<std::string_view, std::string_view, unsigned int,
std::string_view>,
@@ -184,26 +275,27 @@ Void::Testing::Test archive_reader_reproducible_skip_and_forensic_listing()
{"foo/child", "nested", AE_IFREG, ""},
}};
const auto raw = make_raw_tar(conflict_entries);
SpanByteSource src{raw};
auto listed = list_entries(src, {}, false);
Void::Testing::require(!listed.has_value());
SpanByteSource src_la{raw};
auto listed_la = list_entries(src_la, {}, false);
Void::Testing::require(!listed_la.has_value());
Void::Testing::expect_eq(
listed.error().code().value,
listed_la.error().code().value,
static_cast<std::int32_t>(ArchiveErrorCode::unsafe_entry));
#endif
}
// 5. Normal vs forensic listing + sanitized diagnostics.
{
const std::array<
std::tuple<std::string_view, std::string_view, unsigned int,
std::tuple<std::string_view, std::string_view, char,
std::string_view>,
3>
mixed{{
{"safe.txt", "ok", AE_IFREG, ""},
{"bad\nlink", "", AE_IFLNK, "/etc/passwd"},
{"../escape", "nope", AE_IFREG, ""},
mixed_ustar{{
{"safe.txt", "ok", '0', ""},
{"bad\nlink", "", '2', "/etc/passwd"},
{"../escape", "nope", '0', ""},
}};
const auto raw = make_raw_tar(mixed);
const auto raw = make_raw_tar_ustar(mixed_ustar);
SpanByteSource src1{raw};
Void::Testing::expect(!list_entries(src1, {}, false).has_value());
@@ -226,10 +318,123 @@ Void::Testing::Test archive_reader_reproducible_skip_and_forensic_listing()
std::string{"a\\nb\\rc"});
}
Void::Testing::Test archive_create_formats_builtin_ustar_pax_and_gzip()
{
Void::FileSystem::TempDir tmp{"void_archive_formats_"};
// Built-in pure-C++ USTAR/PAX creation works regardless of LibArchive,
// handles long PAX paths (>120 bytes), custom canonical metadata, and
// produces byte-identical output across runs and entry permutations.
const std::string long_path =
"very/deep/nested/package/hierarchy/with/many/subdirectories/"
"extending/well/past/one/hundred/bytes/component/artifact.txt";
const std::array<CreateEntry, 3> entries_a{{
CreateEntry{.path = long_path,
.type = EntryType::RegularFile,
.executable = false,
.content = to_bytes("pax-long-path-content")},
CreateEntry{.path = "bin/tool",
.type = EntryType::RegularFile,
.executable = false,
.content = to_bytes("tool-bytes"),
.mode = 0755U},
CreateEntry{.path = "bin/", .type = EntryType::Directory},
}};
const std::array<CreateEntry, 3> entries_b{
entries_a[2], entries_a[0], entries_a[1]};
CreationOptions builtin_opts{
.compression = ArchiveCompression::None,
.reproducible = true,
.canonical_mtime = 1700000000,
.canonical_uid = 1000,
.canonical_gid = 1000,
.prefer_builtin_tar = true,
};
VectorByteSink raw1;
VectorByteSink raw2;
Void::Testing::require(create(raw1, entries_a, builtin_opts).has_value());
Void::Testing::require(create(raw2, entries_b, builtin_opts).has_value());
Void::Testing::expect_eq(raw1.bytes(), raw2.bytes());
const auto tar_file = tmp / "builtin.tar";
Void::Testing::require(create(tar_file, entries_a, builtin_opts).has_value());
auto extracted = extract(tar_file, tmp / "unpacked_tar");
Void::Testing::require(extracted.has_value());
Void::Testing::expect_eq(read_file_str(tmp / "unpacked_tar" / long_path),
std::string{"pax-long-path-content"});
Void::Testing::expect_eq(read_file_str(tmp / "unpacked_tar/bin/tool"),
std::string{"tool-bytes"});
struct stat st_tool{};
Void::Testing::require(::stat((tmp / "unpacked_tar/bin/tool").c_str(),
&st_tool) == 0);
Void::Testing::expect_eq(st_tool.st_mode & 0777, mode_t{0755});
// Staged extraction over built-in tar also succeeds (exercising
// LibArchive interop when LibArchive is linked, or built-in Reader when
// LibArchive is absent).
auto staged_tar = extract_staged(tar_file, tmp / "staged_tar");
Void::Testing::require(staged_tar.has_value());
auto pub_tar = staged_tar->publish("v1");
Void::Testing::require(pub_tar.has_value());
Void::Testing::expect_eq(
read_file_str(tmp / "staged_tar/v1" / long_path),
std::string{"pax-long-path-content"});
if (has_libarchive_backend()) {
CreationOptions gzip_opts{
.compression = ArchiveCompression::Gzip,
.reproducible = true,
.canonical_mtime = 1700000000,
};
VectorByteSink gz1;
VectorByteSink gz2;
Void::Testing::require(create(gz1, entries_a, gzip_opts).has_value());
std::this_thread::sleep_for(std::chrono::milliseconds{15});
Void::Testing::require(create(gz2, entries_b, gzip_opts).has_value());
Void::Testing::expect_eq(gz1.bytes(), gz2.bytes());
const auto gz_path = tmp / "bundle.tar.gz";
Void::Testing::require(
create(gz_path, entries_a, gzip_opts).has_value());
auto gz_ext = extract(gz_path, tmp / "unpacked_gz");
Void::Testing::require(gz_ext.has_value());
Void::Testing::expect_eq(read_file_str(tmp / "unpacked_gz" / long_path),
std::string{"pax-long-path-content"});
auto gz_staged = extract_staged(gz_path, tmp / "staged_gz");
Void::Testing::require(gz_staged.has_value());
Void::Testing::require(gz_staged->publish("v1").has_value());
Void::Testing::expect_eq(
read_file_str(tmp / "staged_gz/v1/bin/tool"),
std::string{"tool-bytes"});
} else {
VectorByteSink unused;
auto zst_err =
create(unused, entries_a,
CreationOptions{.compression = ArchiveCompression::Zstd});
Void::Testing::require(!zst_err.has_value());
Void::Testing::expect_eq(
zst_err.error().code().value,
static_cast<std::int32_t>(ArchiveErrorCode::unsupported_format));
auto gz_err =
create(unused, entries_a,
CreationOptions{.compression = ArchiveCompression::Gzip});
Void::Testing::require(!gz_err.has_value());
Void::Testing::expect_eq(
gz_err.error().code().value,
static_cast<std::int32_t>(ArchiveErrorCode::unsupported_format));
}
}
Void::Testing::Test archive_staged_extraction_atomic_publish_unknown_and_cleanup()
{
Void::FileSystem::TempDir tmp{"void_archive_staged_"};
const auto archive_path = tmp / "pkg.tar.zst";
const auto archive_path =
tmp / (has_libarchive_backend() ? "pkg.tar.zst" : "pkg.tar");
const auto parent_dir = tmp / "versions";
std::filesystem::create_directories(parent_dir);
@@ -243,7 +448,9 @@ Void::Testing::Test archive_staged_extraction_atomic_publish_unknown_and_cleanup
.executable = false,
.content = to_bytes("key=val")},
}};
Void::Testing::require(create_archive(archive_path, entries).has_value());
const CreationOptions create_opts{.compression = default_test_compression()};
Void::Testing::require(
create_archive(archive_path, entries, create_opts).has_value());
// 1. Staged extraction + atomic no-replace publication + safe modes.
std::size_t progress_calls = 0;
@@ -344,7 +551,8 @@ Void::Testing::Test archive_isolated_worker_extracts_verifies_and_publishes()
{
#ifdef VOID_ARCHIVE_WORKER_PATH
Void::FileSystem::TempDir tmp{"void_archive_iso_"};
const auto archive_path = tmp / "untrusted.tar.zst";
const auto archive_path =
tmp / (has_libarchive_backend() ? "untrusted.tar.zst" : "untrusted.tar");
const auto parent_dir = tmp / "out";
std::filesystem::create_directories(parent_dir);
@@ -358,7 +566,10 @@ Void::Testing::Test archive_isolated_worker_extracts_verifies_and_publishes()
.executable = false,
.content = to_bytes("verified-by-parent")},
}};
Void::Testing::require(create_archive(archive_path, entries).has_value());
Void::Testing::require(
create_archive(archive_path, entries,
CreationOptions{.compression = default_test_compression()})
.has_value());
IsolationOptions iso{};
iso.worker_executable = VOID_ARCHIVE_WORKER_PATH;