feat(provenance): emit deterministic SPDX and CycloneDX SBOMs
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 4s

- New Provenance::Sbom module generating SPDX 2.3 / CycloneDX 1.5 from lockfile packages + BuildGraph outputs
- Wire vbs build|test --sbom FILE --sbom-format spdx|cyclonedx
- Register the module in CMake, build.cpp and the self-host recipe
- Ed25519 signing requested upstream as Void/Library#36 (VL-014)
- Verified in Docker GCC 16: 8/8 CTest suites; deterministic JSON unit test + CLI SBOM assertion
This commit is contained in:
faumaray committed 2026-12-03 13:20:31 +03:00
1 parent 2970fbe25c
commit 55ea305528
13 files changed
+403 -8

No files matched your search

+204
View File
@@ -0,0 +1,204 @@
#include <Void/Build/Provenance/Sbom.hpp>
#include <Void/Build/Util/Json.hpp>
#include <Void/Build/Util/Sha256.hpp>
#include <algorithm>
#include <ctime>
#include <format>
#include <stdexcept>
#include <tuple>
namespace Void::Build::Provenance {
namespace {
using Util::json_array;
using Util::json_quote;
std::string iso8601(std::int64_t epoch) {
const auto time = static_cast<std::time_t>(epoch);
std::tm tm{};
::gmtime_r(&time, &tm);
char buffer[32];
(void)std::strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%SZ", &tm);
return buffer;
}
std::string purl_for(const SbomPackage &package) {
if (!package.purl.empty()) {
return package.purl;
}
return "pkg:generic/" + package.name + "@" + package.version;
}
// Deterministic UUID-shaped serial derived from a hex digest (version 4 nibble).
std::string uuid_from_hex(std::string_view hex) {
const std::string h{hex};
if (h.size() < 32) {
return "00000000-0000-4000-8000-000000000000";
}
return std::format("{}-{}-4{}-8{}-{}", h.substr(0, 8), h.substr(8, 4), h.substr(13, 3),
h.substr(17, 3), h.substr(20, 12));
}
std::string document_identity(const SbomInput &input) {
Util::Sha256Hasher hasher;
hasher.update_length_prefixed("vbs-sbom-v1");
hasher.update_length_prefixed(input.project_name);
hasher.update_length_prefixed(input.project_version);
hasher.update_length_prefixed(input.tool_version);
hasher.update_u64(input.packages.size());
for (const auto &package : input.packages) {
hasher.update_length_prefixed(package.name);
hasher.update_length_prefixed(package.version);
hasher.update_length_prefixed(package.sha256);
hasher.update_length_prefixed(purl_for(package));
}
hasher.update_u64(input.files.size());
for (const auto &file : input.files) {
hasher.update_length_prefixed(file.path);
hasher.update_length_prefixed(file.sha256);
}
return hasher.finalize_hex();
}
std::string checksum_json(std::string_view sha256, std::string_view algorithm_key,
std::string_view value_key) {
return "{\"" + std::string{algorithm_key} + "\":" + json_quote("SHA256") + ",\"" +
std::string{value_key} + "\":" + json_quote(sha256) + '}';
}
std::string spdx(const SbomInput &input, std::string_view identity) {
std::string out =
"{\n \"spdxVersion\": \"SPDX-2.3\",\n \"dataLicense\": \"CC0-1.0\",\n"
" \"SPDXID\": \"SPDXRef-DOCUMENT\",\n \"name\": " +
json_quote(input.project_name + "-sbom") + ",\n \"documentNamespace\": " +
json_quote("https://voidbuild.dev/spdx/" + std::string{identity}) +
",\n \"creationInfo\": {\"created\": " + json_quote(iso8601(input.created_epoch)) +
", \"creators\": [" + json_quote("Tool: vbs-" + input.tool_version) +
"]},\n \"packages\": [\n";
out +=
" {\"SPDXID\": \"SPDXRef-Package-Root\", \"name\": " + json_quote(input.project_name) +
", \"versionInfo\": " + json_quote(input.project_version) +
", \"downloadLocation\": \"NOASSERTION\", \"filesAnalyzed\": false, "
"\"supplier\": \"NOASSERTION\", \"licenseConcluded\": \"NOASSERTION\"}";
for (std::size_t i = 0; i < input.packages.size(); ++i) {
const auto &package = input.packages[i];
out += ",\n {\"SPDXID\": " + json_quote(std::format("SPDXRef-Package-{}", i)) +
", \"name\": " + json_quote(package.name) +
", \"versionInfo\": " + json_quote(package.version) + ", \"downloadLocation\": " +
json_quote(package.download_location.empty() ? "NOASSERTION"
: package.download_location) +
", \"filesAnalyzed\": false, \"supplier\": " +
json_quote(package.supplier.empty() ? "NOASSERTION" : package.supplier) +
", \"licenseConcluded\": \"NOASSERTION\", \"checksums\": [" +
checksum_json(package.sha256, "algorithm", "checksumValue") +
"], \"externalRefs\": [{\"referenceCategory\": \"PACKAGE-MANAGER\", "
"\"referenceType\": \"purl\", \"referenceLocator\": " +
json_quote(purl_for(package)) + "}]}";
}
out += "\n ],\n \"files\": [\n";
for (std::size_t i = 0; i < input.files.size(); ++i) {
const auto &file = input.files[i];
out += " {\"SPDXID\": " + json_quote(std::format("SPDXRef-File-{}", i)) +
", \"fileName\": " + json_quote(file.path) + ", \"checksums\": [" +
checksum_json(file.sha256, "algorithm", "checksumValue") + "]}";
if (i + 1 != input.files.size()) {
out += ',';
}
out += '\n';
}
out += " ],\n \"relationships\": [\n"
" {\"spdxElementId\": \"SPDXRef-DOCUMENT\", \"relationshipType\": \"DESCRIBES\", "
"\"relatedSpdxElement\": \"SPDXRef-Package-Root\"}";
for (std::size_t i = 0; i < input.packages.size(); ++i) {
out += ",\n {\"spdxElementId\": \"SPDXRef-Package-Root\", \"relationshipType\": "
"\"DEPENDS_ON\", \"relatedSpdxElement\": " +
json_quote(std::format("SPDXRef-Package-{}", i)) + '}';
}
for (std::size_t i = 0; i < input.files.size(); ++i) {
out += ",\n {\"spdxElementId\": \"SPDXRef-Package-Root\", \"relationshipType\": "
"\"CONTAINS\", \"relatedSpdxElement\": " +
json_quote(std::format("SPDXRef-File-{}", i)) + '}';
}
out += "\n ]\n}\n";
return out;
}
std::string cyclonedx(const SbomInput &input, std::string_view identity) {
std::string out =
"{\n \"bomFormat\": \"CycloneDX\",\n \"specVersion\": \"1.5\",\n \"serialNumber\": " +
json_quote("urn:uuid:" + uuid_from_hex(identity)) + ",\n \"version\": 1,\n" +
" \"metadata\": {\"timestamp\": " + json_quote(iso8601(input.created_epoch)) +
", \"tools\": {\"components\": [{\"type\": \"application\", \"name\": \"vbs\", "
"\"version\": " +
json_quote(input.tool_version) +
"}]}, \"component\": {\"type\": \"application\", \"name\": " +
json_quote(input.project_name) + ", \"version\": " + json_quote(input.project_version) +
"}},\n \"components\": [\n";
for (std::size_t i = 0; i < input.packages.size(); ++i) {
const auto &package = input.packages[i];
out += " {\"type\": \"library\", \"bom-ref\": " + json_quote(purl_for(package)) +
", \"name\": " + json_quote(package.name) +
", \"version\": " + json_quote(package.version) +
", \"purl\": " + json_quote(purl_for(package)) + ", \"hashes\": [" +
checksum_json(package.sha256, "alg", "content") + "]}";
if (i + 1 != input.packages.size()) {
out += ',';
}
out += '\n';
}
out += " ],\n \"dependencies\": [{\"ref\": " +
json_quote(
purl_for(SbomPackage{input.project_name, input.project_version, {}, {}, {}, {}})) +
", \"dependsOn\": [";
for (std::size_t i = 0; i < input.packages.size(); ++i) {
if (i != 0) {
out += ", ";
}
out += json_quote(purl_for(input.packages[i]));
}
out += "]}]\n}\n";
return out;
}
} // namespace
std::string_view sbom_format_name(SbomFormat format) noexcept {
switch (format) {
case SbomFormat::Spdx:
return "spdx";
case SbomFormat::CycloneDX:
return "cyclonedx";
}
return "spdx";
}
SbomFormat parse_sbom_format(std::string_view name) {
if (name == "spdx") {
return SbomFormat::Spdx;
}
if (name == "cyclonedx" || name == "cdx") {
return SbomFormat::CycloneDX;
}
throw std::invalid_argument{"unknown SBOM format '" + std::string{name} +
"' (expected spdx or cyclonedx)"};
}
Void::Result::Result<std::string, Void::Result::Error> generate_sbom(const SbomInput &raw_input,
SbomFormat format) {
SbomInput input = raw_input;
std::ranges::sort(input.packages, [](const SbomPackage &a, const SbomPackage &b) {
return std::tie(a.name, a.version, a.sha256) < std::tie(b.name, b.version, b.sha256);
});
std::ranges::sort(input.files, [](const SbomFile &a, const SbomFile &b) {
return std::tie(a.path, a.sha256) < std::tie(b.path, b.sha256);
});
const std::string identity = document_identity(input);
return format == SbomFormat::Spdx ? spdx(input, identity) : cyclonedx(input, identity);
}
} // namespace Void::Build::Provenance