feat(crypto,archive): implement VL-007 built-in hashes and VL-006 archive creation/gzip/tar fallback
This commit is contained in:
1 parent
b50131f52e
commit
22c8538754
19 files changed
+3326
-133
No files matched your search
+148
-30
@@ -1,10 +1,13 @@
|
||||
#include <Void/Archive/Archive.hpp>
|
||||
|
||||
#include "BuiltinTar.hpp"
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <format>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <set>
|
||||
@@ -13,8 +16,10 @@
|
||||
#include <system_error>
|
||||
#include <utility>
|
||||
|
||||
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
|
||||
#include <archive.h>
|
||||
#include <archive_entry.h>
|
||||
#endif
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <fcntl.h>
|
||||
@@ -28,6 +33,7 @@ namespace Void::Archive
|
||||
{
|
||||
namespace
|
||||
{
|
||||
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
|
||||
using ArchiveHandle =
|
||||
std::unique_ptr<archive, decltype(&archive_read_free)>;
|
||||
|
||||
@@ -39,6 +45,7 @@ namespace Void::Archive
|
||||
"Archive::extract: {}: {}", operation,
|
||||
message == nullptr ? "unknown archive error" : message)};
|
||||
}
|
||||
#endif
|
||||
|
||||
[[nodiscard]] std::optional<std::filesystem::path>
|
||||
safe_relative_path(std::string_view rawPath, bool directory)
|
||||
@@ -225,12 +232,14 @@ namespace Void::Archive
|
||||
return {};
|
||||
}
|
||||
|
||||
template <typename ReadFn>
|
||||
[[nodiscard]] Void::Result::Result<std::uint64_t>
|
||||
extract_regular(archive *reader, int rootDescriptor,
|
||||
const std::filesystem::path &relative,
|
||||
const std::filesystem::path &displayRoot,
|
||||
const ExtractionOptions &options,
|
||||
std::uint64_t alreadyExtracted, bool executable)
|
||||
extract_regular_stream(int rootDescriptor,
|
||||
const std::filesystem::path &relative,
|
||||
const std::filesystem::path &displayRoot,
|
||||
const ExtractionOptions &options,
|
||||
std::uint64_t alreadyExtracted,
|
||||
bool executable, ReadFn &&read_fn)
|
||||
{
|
||||
auto openedParent = open_parent_directory(
|
||||
rootDescriptor, relative.parent_path(), displayRoot);
|
||||
@@ -286,15 +295,14 @@ namespace Void::Archive
|
||||
std::array<std::byte, 64 * 1024> buffer{};
|
||||
std::uint64_t fileBytes = 0;
|
||||
for (;;) {
|
||||
const auto count =
|
||||
archive_read_data(reader, buffer.data(), buffer.size());
|
||||
if (count == 0) {
|
||||
auto chunk = read_fn(std::span{buffer});
|
||||
if (!chunk.has_value()) {
|
||||
return fail(chunk.error().message());
|
||||
}
|
||||
if (*chunk == 0) {
|
||||
break;
|
||||
}
|
||||
if (count < 0) {
|
||||
return fail(archive_error(reader, "read entry").message());
|
||||
}
|
||||
const auto amount = static_cast<std::uint64_t>(count);
|
||||
const auto amount = static_cast<std::uint64_t>(*chunk);
|
||||
if (amount > options.maxFileBytes - fileBytes ||
|
||||
amount >
|
||||
options.maxTotalBytes - alreadyExtracted - fileBytes) {
|
||||
@@ -303,11 +311,11 @@ namespace Void::Archive
|
||||
}
|
||||
|
||||
std::size_t written = 0;
|
||||
while (written < static_cast<std::size_t>(count)) {
|
||||
while (written < *chunk) {
|
||||
const auto result = ::write(
|
||||
descriptor.get(),
|
||||
reinterpret_cast<const char *>(buffer.data()) + written,
|
||||
static_cast<std::size_t>(count) - written);
|
||||
*chunk - written);
|
||||
if (result < 0 && errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
@@ -448,10 +456,12 @@ namespace Void::Archive
|
||||
return {};
|
||||
}
|
||||
|
||||
template <typename ReadFn>
|
||||
[[nodiscard]] Void::Result::Result<std::uint64_t>
|
||||
extract_regular(archive *reader, const std::filesystem::path &path,
|
||||
const ExtractionOptions &options,
|
||||
std::uint64_t alreadyExtracted)
|
||||
extract_regular_stream(const std::filesystem::path &path,
|
||||
const ExtractionOptions &options,
|
||||
std::uint64_t alreadyExtracted,
|
||||
ReadFn &&read_fn)
|
||||
{
|
||||
std::error_code error;
|
||||
const auto status = std::filesystem::symlink_status(path, error);
|
||||
@@ -486,15 +496,14 @@ namespace Void::Archive
|
||||
std::array<std::byte, 64 * 1024> buffer{};
|
||||
std::uint64_t fileBytes = 0;
|
||||
for (;;) {
|
||||
const auto count =
|
||||
archive_read_data(reader, buffer.data(), buffer.size());
|
||||
if (count == 0) {
|
||||
auto chunk = read_fn(std::span{buffer});
|
||||
if (!chunk.has_value()) {
|
||||
return fail(chunk.error().message());
|
||||
}
|
||||
if (*chunk == 0) {
|
||||
break;
|
||||
}
|
||||
if (count < 0) {
|
||||
return fail(archive_error(reader, "read entry").message());
|
||||
}
|
||||
const auto amount = static_cast<std::uint64_t>(count);
|
||||
const auto amount = static_cast<std::uint64_t>(*chunk);
|
||||
if (amount > options.maxFileBytes - fileBytes ||
|
||||
amount >
|
||||
options.maxTotalBytes - alreadyExtracted - fileBytes) {
|
||||
@@ -502,7 +511,7 @@ namespace Void::Archive
|
||||
"Archive::extract: extracted size exceeds policy");
|
||||
}
|
||||
output.write(reinterpret_cast<const char *>(buffer.data()),
|
||||
count);
|
||||
static_cast<std::streamsize>(*chunk));
|
||||
if (!output) {
|
||||
return fail("Archive::extract: write failed");
|
||||
}
|
||||
@@ -544,11 +553,13 @@ namespace Void::Archive
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(VOID_ARCHIVE_HAS_LIBARCHIVE) && VOID_ARCHIVE_HAS_LIBARCHIVE
|
||||
ArchiveHandle reader{archive_read_new(), &archive_read_free};
|
||||
if (!reader) {
|
||||
return Void::Result::fail(
|
||||
"Archive::extract: cannot allocate archive reader");
|
||||
}
|
||||
archive_read_support_filter_gzip(reader.get());
|
||||
archive_read_support_filter_zstd(reader.get());
|
||||
archive_read_support_filter_none(reader.get());
|
||||
archive_read_support_format_tar(reader.get());
|
||||
@@ -633,14 +644,23 @@ namespace Void::Archive
|
||||
return Void::Result::fail(
|
||||
"Archive::extract: declared entry size exceeds policy");
|
||||
}
|
||||
auto read_libarchive_chunk =
|
||||
[&](std::span<std::byte> dst) -> Void::Result::Result<std::size_t> {
|
||||
const auto count =
|
||||
archive_read_data(reader.get(), dst.data(), dst.size());
|
||||
if (count < 0) {
|
||||
return std::unexpected{archive_error(reader.get(), "read entry")};
|
||||
}
|
||||
return static_cast<std::size_t>(count);
|
||||
};
|
||||
#ifndef _WIN32
|
||||
const bool executable = (archive_entry_perm(entry) & 0111) != 0;
|
||||
auto extracted =
|
||||
extract_regular(reader.get(), root->get(), *relative,
|
||||
destination, options, result.bytes, executable);
|
||||
auto extracted = extract_regular_stream(
|
||||
root->get(), *relative, destination, options, result.bytes,
|
||||
executable, read_libarchive_chunk);
|
||||
#else
|
||||
auto extracted =
|
||||
extract_regular(reader.get(), output, options, result.bytes);
|
||||
auto extracted = extract_regular_stream(
|
||||
output, options, result.bytes, read_libarchive_chunk);
|
||||
#endif
|
||||
if (!extracted) {
|
||||
return std::unexpected{extracted.error()};
|
||||
@@ -648,5 +668,103 @@ namespace Void::Archive
|
||||
result.bytes += *extracted;
|
||||
}
|
||||
return result;
|
||||
#else
|
||||
auto source = FileByteSource::open(archivePath);
|
||||
if (!source.has_value()) {
|
||||
return std::unexpected{std::move(source.error())};
|
||||
}
|
||||
ReadLimits limits;
|
||||
limits.max_entries = options.maxEntries;
|
||||
limits.max_file_bytes = options.maxFileBytes;
|
||||
limits.max_total_bytes = options.maxTotalBytes;
|
||||
limits.max_compressed_bytes = std::numeric_limits<std::uint64_t>::max();
|
||||
Detail::BuiltinTarStream stream{
|
||||
*source, limits, {}, std::nullopt,
|
||||
Void::Time::SystemMonotonicClock::instance()};
|
||||
if (auto opened = stream.open(); !opened.has_value()) {
|
||||
return std::unexpected{std::move(opened.error())};
|
||||
}
|
||||
|
||||
ExtractionResult result;
|
||||
std::set<std::filesystem::path> seen;
|
||||
for (;;) {
|
||||
auto next = stream.next_entry();
|
||||
if (!next.has_value()) {
|
||||
return std::unexpected{std::move(next.error())};
|
||||
}
|
||||
if (!next->has_value()) {
|
||||
break;
|
||||
}
|
||||
if (++result.entries > options.maxEntries) {
|
||||
return Void::Result::fail(
|
||||
"Archive::extract: entry count exceeds policy");
|
||||
}
|
||||
|
||||
const auto &entry = **next;
|
||||
if (entry.type == EntryType::Hardlink ||
|
||||
entry.type == EntryType::Symlink || !entry.linkname.empty()) {
|
||||
return Void::Result::fail(
|
||||
"Archive::extract: links are not allowed");
|
||||
}
|
||||
if (entry.type != EntryType::Directory &&
|
||||
entry.type != EntryType::RegularFile) {
|
||||
return Void::Result::fail(
|
||||
"Archive::extract: special files are not allowed");
|
||||
}
|
||||
const bool is_dir = (entry.type == EntryType::Directory);
|
||||
auto relative = safe_relative_path(entry.path, is_dir);
|
||||
if (!relative || !seen.insert(*relative).second) {
|
||||
return Void::Result::fail(std::format(
|
||||
"Archive::extract: unsafe or duplicate path '{}'",
|
||||
entry.path));
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
if (auto parents = safe_parents(root, relative->parent_path());
|
||||
!parents) {
|
||||
return std::unexpected{parents.error()};
|
||||
}
|
||||
const auto output = root / *relative;
|
||||
#endif
|
||||
if (is_dir) {
|
||||
#ifndef _WIN32
|
||||
auto created =
|
||||
create_directory_entry(root->get(), *relative, destination,
|
||||
options.overwriteExisting);
|
||||
#else
|
||||
auto created =
|
||||
create_directory_entry(output, options.overwriteExisting);
|
||||
#endif
|
||||
if (!created) {
|
||||
return std::unexpected{created.error()};
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (entry.declared_size > options.maxFileBytes ||
|
||||
entry.declared_size > options.maxTotalBytes - result.bytes) {
|
||||
return Void::Result::fail(
|
||||
"Archive::extract: declared entry size exceeds policy");
|
||||
}
|
||||
auto read_builtin_chunk =
|
||||
[&](std::span<std::byte> dst) -> Void::Result::Result<std::size_t> {
|
||||
return stream.read_entry_data(dst);
|
||||
};
|
||||
#ifndef _WIN32
|
||||
const bool executable = (entry.mode & 0111U) != 0U;
|
||||
auto extracted = extract_regular_stream(
|
||||
root->get(), *relative, destination, options, result.bytes,
|
||||
executable, read_builtin_chunk);
|
||||
#else
|
||||
auto extracted = extract_regular_stream(
|
||||
output, options, result.bytes, read_builtin_chunk);
|
||||
#endif
|
||||
if (!extracted) {
|
||||
return std::unexpected{extracted.error()};
|
||||
}
|
||||
result.bytes += *extracted;
|
||||
}
|
||||
return result;
|
||||
#endif
|
||||
}
|
||||
} // namespace Void::Archive
|
||||
Reference in new issue
Block a user