Files
Build-System/tests/RegistryTests.cpp
T
2026-11-27 06:51:43 +03:00

334 lines
15 KiB
C++

#include <Void/Build/BootstrapConfig.hpp>
#include <Void/Build/Package/Archive.hpp>
#include <Void/Build/Platform/FileOps.hpp>
#include <Void/Build/Platform/ProcessSupervisor.hpp>
#include <Void/Build/Registry/HttpServer.hpp>
#include <Void/Build/Registry/RegistryService.hpp>
#include <Void/Build/Util/Sha256.hpp>
#include <Void/Data/StrictJson.hpp>
#include <Void/System/Path.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <cstring>
#include <fstream>
using namespace Void::Build;
using namespace Void::Build::Registry;
namespace {
std::string extract_session_cookie(const std::vector<std::string> &set_cookies) {
for (const auto &sc : set_cookies) {
if (sc.starts_with("vbs_session=")) {
const auto semi = sc.find(';');
return sc.substr(0, semi);
}
}
return {};
}
std::string extract_json_str(std::string_view json, const std::string &key) {
auto parsed = Void::Json::Strict::parse(json);
if (!parsed)
return {};
const auto *obj = std::get_if<Void::Json::Strict::Value::Object>(&parsed->value);
if (!obj)
return {};
const auto it = obj->find(key);
if (it == obj->end())
return {};
const auto *s = std::get_if<std::string>(&it->second.value);
return s ? *s : std::string{};
}
std::filesystem::path locate_vbs_binary() {
std::error_code ec;
if (const auto self = std::filesystem::read_symlink("/proc/self/exe", ec);
!ec && !self.empty()) {
const auto sibling = self.parent_path() / "vbs";
if (std::filesystem::exists(sibling, ec)) {
return sibling;
}
}
if (!Bootstrap::binary_dir.empty()) {
return std::filesystem::path{Bootstrap::binary_dir} / "bin" / "vbs";
}
return {};
}
} // namespace
VOID_TEST(RegistryTests, registry_rbac_immutability_csrf_xss_web_and_backup_restore) {
Void::FileSystem::TempDir tmp{"vbs_reg_"};
RegistryConfig cfg;
cfg.data_dir = tmp / "data";
cfg.web_root = std::filesystem::path{Bootstrap::source_dir} / "registry" / "web";
cfg.bind_address = "127.0.0.1";
cfg.port = 0; // Ephemeral port!
cfg.bootstrap_admin_user = "admin";
cfg.bootstrap_admin_password = "admin_secret_password";
auto svc = RegistryService::open(cfg);
Void::Testing::expect(svc.has_value());
if (!svc)
return;
Void::Testing::expect(
(*svc)->create_user("pub_alice", "alice_pass", Role::Publisher).has_value());
Void::Testing::expect((*svc)->create_user("read_bob", "bob_pass", Role::Reader).has_value());
auto alice_tok = (*svc)->create_token("pub_alice", "ci", {"read", "publish"});
auto bob_tok = (*svc)->create_token("read_bob", "ro", {"read"});
Void::Testing::expect(alice_tok.has_value() && bob_tok.has_value());
if (!alice_tok || !bob_tok)
return;
Void::Testing::expect((*svc)->start_http().has_value());
const auto port = (*svc)->bound_port();
Void::Testing::expect(port > 0);
// 1. Static web UI assets served with security headers; path traversal blocked!
auto idx = send_loopback_http(port, "GET", "/");
Void::Testing::expect(idx.has_value() && idx->status == 200);
if (idx) {
Void::Testing::expect(idx->headers.contains("content-security-policy"));
Void::Testing::expect_eq(idx->headers.at("x-frame-options"), std::string{"DENY"});
Void::Testing::expect(idx->body.find("VoidBuildSystem Registry") != std::string::npos);
}
auto trav = send_loopback_http(port, "GET", "/../CMakeLists.txt");
Void::Testing::expect(trav.has_value() && trav->status == 403);
// 2. Build a valid deterministic package tar and publish as Publisher Alice.
const auto pkg_src = tmp / "pkg_src";
(void)Platform::atomic_write_file(pkg_src / "build.cpp",
"void build(Void::Build::Project&){}\n", false);
const auto tar1 = tmp / "p1.tar";
auto sha1 = Package::create_source_package(pkg_src, tar1, 0);
Void::Testing::expect(sha1.has_value());
auto bytes1 = Void::FileSystem::read_file(tar1);
Void::Testing::expect(bytes1.has_value());
if (!sha1 || !bytes1)
return;
// Reader Bob CANNOT publish (403)!
auto bob_pub = send_loopback_http(
port, "PUT", "/api/v1/packages/demo-pkg/1.0.0",
{{"Authorization", "Bearer " + *bob_tok}, {"X-Content-SHA256", *sha1}}, *bytes1);
Void::Testing::expect(bob_pub.has_value() && bob_pub->status == 403);
// Wrong SHA-256 rejected (400)!
auto bad_sha = send_loopback_http(
port, "PUT", "/api/v1/packages/demo-pkg/1.0.0",
{{"Authorization", "Bearer " + *alice_tok}, {"X-Content-SHA256", std::string(64, '0')}},
*bytes1);
Void::Testing::expect(bad_sha.has_value() && bad_sha->status == 400);
// Malicious non-tar / traversal upload rejected (422)!
auto bad_tar =
send_loopback_http(port, "PUT", "/api/v1/packages/demo-pkg/1.0.0",
{{"Authorization", "Bearer " + *alice_tok}}, "not a valid tar archive");
Void::Testing::expect(bad_tar.has_value() && bad_tar->status == 422);
// Valid private package publish by Alice -> 201 Created!
auto pub_ok = send_loopback_http(port, "PUT", "/api/v1/packages/secret-pkg/1.0.0",
{{"Authorization", "Bearer " + *alice_tok},
{"X-Content-SHA256", *sha1},
{"X-Package-Private", "true"},
{"X-Package-Description", "<script>alert('xss')</script>"}},
*bytes1);
Void::Testing::expect(pub_ok.has_value() && pub_ok->status == 201);
// Unauthenticated access to private package MUST fail (401)!
auto anon_priv = send_loopback_http(port, "GET", "/api/v1/packages/secret-pkg");
Void::Testing::expect(anon_priv.has_value() && anon_priv->status == 401);
auto anon_dl = send_loopback_http(port, "GET", "/api/v1/packages/secret-pkg/1.0.0/download");
Void::Testing::expect(anon_dl.has_value() && anon_dl->status == 401);
// Idempotent same-bytes republish -> 200 OK!
auto idemp = send_loopback_http(
port, "PUT", "/api/v1/packages/secret-pkg/1.0.0",
{{"Authorization", "Bearer " + *alice_tok}, {"X-Content-SHA256", *sha1}}, *bytes1);
Void::Testing::expect(idemp.has_value() && idemp->status == 200);
// Different bytes for SAME immutable release -> 409 Conflict!
(void)Platform::atomic_write_file(pkg_src / "extra.txt", "changed", false);
const auto tar2 = tmp / "p2.tar";
auto sha2 = Package::create_source_package(pkg_src, tar2, 0);
auto bytes2 = Void::FileSystem::read_file(tar2);
auto conflict = send_loopback_http(
port, "PUT", "/api/v1/packages/secret-pkg/1.0.0",
{{"Authorization", "Bearer " + *alice_tok}, {"X-Content-SHA256", *sha2}}, *bytes2);
Void::Testing::expect(conflict.has_value() && conflict->status == 409);
// 3. Browser session login (HttpOnly cookie) + CSRF rejection without header + valid CSRF yank!
auto login = send_loopback_http(
port, "POST", "/api/v1/auth/login", {{"Content-Type", "application/json"}},
"{\"username\":\"admin\",\"password\":\"admin_secret_password\"}");
Void::Testing::expect(login.has_value() && login->status == 200);
if (!login)
return;
const auto cookie = extract_session_cookie(login->set_cookies);
const auto csrf = extract_json_str(login->body, "csrf_token");
Void::Testing::expect(!cookie.empty() && !csrf.empty());
Void::Testing::expect(login->set_cookies[0].find("HttpOnly") != std::string::npos);
Void::Testing::expect(login->set_cookies[0].find("SameSite=Strict") != std::string::npos);
// Mutation with session cookie WITHOUT X-CSRF-Token -> 403 CSRF rejected!
auto no_csrf = send_loopback_http(port, "POST", "/api/v1/packages/secret-pkg/1.0.0/yank",
{{"Cookie", cookie}}, "{\"yanked\":true}");
Void::Testing::expect(no_csrf.has_value() && no_csrf->status == 403);
// Mutation WITH valid X-CSRF-Token -> 200 OK!
auto with_csrf =
send_loopback_http(port, "POST", "/api/v1/packages/secret-pkg/1.0.0/yank",
{{"Cookie", cookie}, {"X-CSRF-Token", csrf}}, "{\"yanked\":true}");
Void::Testing::expect(with_csrf.has_value() && with_csrf->status == 200);
// Audit log contains zero secrets (no passwords or bearer tokens)!
auto audit = send_loopback_http(port, "GET", "/api/v1/audit", {{"Cookie", cookie}});
Void::Testing::expect(audit.has_value() && audit->status == 200);
if (audit) {
Void::Testing::expect(audit->body.find("admin_secret_password") == std::string::npos);
Void::Testing::expect(audit->body.find(*alice_tok) == std::string::npos);
}
// 4. Consistent backup & restore into a brand-new registry instance preserves releases &
// download!
const auto backup_dir = tmp / "backup";
Void::Testing::expect((*svc)->backup_to(backup_dir).has_value());
(*svc)->stop_http();
RegistryConfig cfg2 = cfg;
cfg2.data_dir = tmp / "restored_data";
cfg2.bootstrap_admin_password.clear();
auto svc2 = RegistryService::open(cfg2);
Void::Testing::expect(svc2.has_value());
if (svc2) {
Void::Testing::expect((*svc2)->restore_from(backup_dir).has_value());
Void::Testing::expect((*svc2)->start_http().has_value());
auto dl = send_loopback_http((*svc2)->bound_port(), "GET",
"/api/v1/packages/secret-pkg/1.0.0/download",
{{"Authorization", "Bearer " + *alice_tok}});
Void::Testing::expect(dl.has_value() && dl->status == 200 && dl->body == *bytes1);
(*svc2)->stop_http();
}
}
// End-to-end live client <-> server test: `vbs publish` -> `vbs deps resolve` -> `vbs build
// --locked`!
VOID_TEST(RegistryTests, cli_publish_deps_resolve_and_locked_build_against_live_registry) {
const auto vbs_bin = locate_vbs_binary();
Void::Testing::expect(!vbs_bin.empty() && std::filesystem::exists(vbs_bin));
if (vbs_bin.empty() || !std::filesystem::exists(vbs_bin)) {
return;
}
Void::FileSystem::TempDir tmp{"vbs_reg_e2e_"};
RegistryConfig cfg;
cfg.data_dir = tmp / "reg_data";
cfg.web_root = std::filesystem::path{Bootstrap::source_dir} / "registry" / "web";
cfg.bind_address = "127.0.0.1";
cfg.port = 0;
cfg.bootstrap_admin_user = "admin";
cfg.bootstrap_admin_password = "admin_e2e_password";
auto svc = RegistryService::open(cfg);
Void::Testing::expect(svc.has_value());
if (!svc)
return;
Void::Testing::expect((*svc)->create_user("ci_pub", "ci_pass", Role::Publisher).has_value());
auto tok = (*svc)->create_token("ci_pub", "publish", {"read", "publish"});
Void::Testing::expect(tok.has_value());
if (!tok)
return;
Void::Testing::expect((*svc)->start_http().has_value());
const std::string reg_url = "http://127.0.0.1:" + std::to_string((*svc)->bound_port());
// 1. Create a real CMake library package and publish 1.0.0 and 1.1.0 via `vbs publish` CLI!
const auto pkg_dir = tmp / "hello_pkg";
(void)Platform::atomic_write_file(pkg_dir / "CMakeLists.txt", R"cmake(
cmake_minimum_required(VERSION 3.20)
project(hello_pkg LANGUAGES CXX)
add_library(hello_pkg STATIC src/hello.cpp)
target_include_directories(hello_pkg PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
)cmake",
false);
(void)Platform::atomic_write_file(pkg_dir / "include" / "hello_pkg.hpp",
"#pragma once\nint hello_pkg_value();\n", false);
(void)Platform::atomic_write_file(
pkg_dir / "src" / "hello.cpp",
"#include \"hello_pkg.hpp\"\nint hello_pkg_value() { return 10; }\n", false);
Platform::ProcessRequest pub1;
pub1.executable = vbs_bin;
pub1.arguments = {"publish", "--project", pkg_dir.string(), "--registry",
reg_url, "--name", "hello-pkg", "--version",
"1.0.0", "--description", "Live E2E package"};
pub1.environment.set_vars["VBS_REGISTRY_TOKEN"] = *tok;
auto r1 = Platform::run_supervised(pub1);
Void::Testing::expect(r1.has_value() && r1->succeeded());
// Publish 1.1.0 returning 42 so solver (^1.0) selects 1.1.0!
(void)Platform::atomic_write_file(
pkg_dir / "src" / "hello.cpp",
"#include \"hello_pkg.hpp\"\nint hello_pkg_value() { return 42; }\n", false);
Platform::ProcessRequest pub2 = pub1;
pub2.arguments[8] = "1.1.0";
auto r2 = Platform::run_supervised(pub2);
Void::Testing::expect(r2.has_value() && r2->succeeded());
// 2. Create a consumer project declaring `.package("hello-pkg", "^1.0").registry(reg_url)`.
const auto cons_dir = tmp / "consumer";
const auto cons_build = tmp / "consumer_build";
(void)Platform::atomic_write_file(cons_dir / "build.cpp",
"#include <Void/Build.hpp>\n"
"void build(Void::Build::Project &p) {\n"
" auto dep = p.dependency(\"hello\")\n"
" .package(\"hello-pkg\", \"^1.0\")\n"
" .registry(\"" +
reg_url +
"\");\n"
" p.executable(\"reg_consumer\")\n"
" .sources({\"src/main.cpp\"})\n"
" .link(dep.target(\"hello_pkg\"));\n"
"}\n",
false);
(void)Platform::atomic_write_file(
cons_dir / "src" / "main.cpp",
"#include \"hello_pkg.hpp\"\nint main() { return hello_pkg_value() == 42 ? 0 : 1; }\n",
false);
// 3. Resolve dependencies into vbs.lock against the live registry!
Platform::ProcessRequest deps_req;
deps_req.executable = vbs_bin;
deps_req.arguments = {"deps", "resolve", "--project", cons_dir.string(),
"--build-dir", cons_build.string()};
deps_req.environment.set_vars["VBS_REGISTRY_TOKEN"] = *tok;
auto deps_res = Platform::run_supervised(deps_req);
Void::Testing::expect(deps_res.has_value() && deps_res->succeeded());
auto lock_text = Void::FileSystem::read_file(cons_dir / "vbs.lock");
Void::Testing::expect(lock_text.has_value());
if (lock_text) {
Void::Testing::expect(lock_text->find("\"version\":\"1.1.0\"") != std::string::npos);
Void::Testing::expect(lock_text->find("\"source_kind\":\"package\"") != std::string::npos);
Void::Testing::expect(lock_text->find(*tok) == std::string::npos); // Zero token leaks!
}
// 4. Build in --locked mode and execute the consumer binary!
Platform::ProcessRequest bld_req;
bld_req.executable = vbs_bin;
bld_req.arguments = {"build", "--project", cons_dir.string(),
"--build-dir", cons_build.string(), "--locked"};
auto bld_res = Platform::run_supervised(bld_req);
Void::Testing::expect(bld_res.has_value() && bld_res->succeeded());
Platform::ProcessRequest run_req;
run_req.executable = cons_build / "profiles" / "debug" / "out" / "bin" / "reg_consumer";
auto run_res = Platform::run_supervised(run_req);
Void::Testing::expect(run_res.has_value() && run_res->succeeded());
(*svc)->stop_http();
}