Signed-off-by: faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-07-16 19:59:57 +03:00
1 parent 8aa968eb9f
commit 11850b1a8d
11 files changed
+1569 -160

No files matched your search

+63 -7
View File
@@ -4,6 +4,7 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#include <set>
#include <unistd.h>
namespace
@@ -13,21 +14,26 @@ namespace
std::cerr << message << '\n';
return 1;
}
}
} // namespace
int main()
{
const auto root = std::filesystem::temp_directory_path() /
("echoion-core-reload-" + std::to_string(::getpid()));
("echoion-core-reload-" + std::to_string(::getpid()));
std::filesystem::create_directories(root);
const auto cleanup = [&root] {
std::error_code ec;
std::filesystem::permissions(root / "immutable-lib",
std::filesystem::perms::owner_all,
std::filesystem::perm_options::add, ec);
std::filesystem::remove_all(root, ec);
};
::setenv("XDG_CONFIG_HOME", root.c_str(), 1);
::setenv("XDG_CACHE_HOME", root.c_str(), 1);
::setenv("HOME", root.c_str(), 1);
const auto stateDirectory = root / "runtime-state";
::setenv("ECHOION_STATE_DIR", stateDirectory.c_str(), 1);
const auto ytDlp = root / ".local/share/echoion/yt-dlp/yt-dlp";
std::filesystem::create_directories(ytDlp.parent_path());
{
@@ -35,9 +41,9 @@ int main()
output << "#!/bin/sh\necho 2026.01.01\n";
}
std::filesystem::permissions(ytDlp,
std::filesystem::perms::owner_read |
std::filesystem::perms::owner_write |
std::filesystem::perms::owner_exec);
std::filesystem::perms::owner_read |
std::filesystem::perms::owner_write |
std::filesystem::perms::owner_exec);
const auto config = root / "config.json";
{
std::ofstream output{config};
@@ -49,8 +55,27 @@ int main()
})";
}
const auto corePath = root / "Echoion.ts";
const auto coreDirectory = root / "immutable-lib";
std::filesystem::create_directories(coreDirectory);
const auto corePath = coreDirectory / "Echoion.ts";
std::filesystem::copy_file(ECHOION_TEST_CORE_PATH, corePath);
const auto directoryEntries = [&coreDirectory] {
std::set<std::filesystem::path> entries;
for (const auto& entry :
std::filesystem::directory_iterator{coreDirectory}) {
entries.insert(entry.path().filename());
}
return entries;
};
const auto entriesBeforeLoad = directoryEntries();
std::filesystem::permissions(coreDirectory,
std::filesystem::perms::owner_read |
std::filesystem::perms::owner_exec |
std::filesystem::perms::group_read |
std::filesystem::perms::group_exec |
std::filesystem::perms::others_read |
std::filesystem::perms::others_exec,
std::filesystem::perm_options::replace);
Echoion::Host::CoreLoader loader;
loader.setConfigPath(config);
@@ -58,11 +83,41 @@ int main()
cleanup();
return fail(result.error().message.c_str());
}
if (directoryEntries() != entriesBeforeLoad) {
cleanup();
return fail(
"initial Core load wrote into the immutable library directory");
}
loader.enableAutoReload();
if (loader.isAutoReloadEnabled()) {
cleanup();
return fail("auto-reload was enabled for a read-only Core directory");
}
std::filesystem::permissions(coreDirectory,
std::filesystem::perms::owner_all |
std::filesystem::perms::group_read |
std::filesystem::perms::group_exec |
std::filesystem::perms::others_read |
std::filesystem::perms::others_exec,
std::filesystem::perm_options::replace);
loader.enableAutoReload();
if (!loader.isAutoReloadEnabled()) {
cleanup();
return fail(
"auto-reload was not enabled for a writable development Core");
}
loader.disableAutoReload();
const auto before = loader.getCore();
if (!before) {
cleanup();
return fail("Core missing after initial load");
}
if (!std::filesystem::is_regular_file(stateDirectory / "echoion.db") ||
std::filesystem::exists(root / "echoion.db") ||
!std::filesystem::is_directory(stateDirectory / "logs")) {
cleanup();
return fail("Core mutable files did not use ECHOION_STATE_DIR");
}
if (auto result = loader.hotReload(corePath); !result) {
cleanup();
return fail(result.error().message.c_str());
@@ -84,7 +139,8 @@ int main()
return fail("invalid replacement unexpectedly activated");
}
const auto retained = loader.getCore();
if (!retained || retained.get() != after.get() || !retained->isInitialized()) {
if (!retained || retained.get() != after.get() ||
!retained->isInitialized()) {
cleanup();
return fail("running Core was not retained after failed activation");
}
+268 -17
View File
@@ -3,36 +3,287 @@
#include <Void/System/Path.hpp>
#include <Void/System/TempDir.hpp>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <string_view>
#include <sys/stat.h>
#include <vector>
namespace
{
constexpr std::string_view kAbcSha256 =
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
std::string fileEntry(std::string_view path, std::uintmax_t size,
std::string_view mode,
std::string_view sha256 = kAbcSha256)
{
return "{\"path\":\"" + std::string(path) +
"\",\"type\":\"file\",\"size\":" + std::to_string(size) +
",\"mode\":\"" + std::string(mode) + "\",\"sha256\":\"" +
std::string(sha256) + "\"}";
}
std::string symlinkEntry(std::string_view path, std::string_view target)
{
return "{\"path\":\"" + std::string(path) +
"\",\"type\":\"symlink\",\"target\":\"" + std::string(target) +
"\"}";
}
std::string manifest(std::vector<std::string> entries,
std::string_view executable = "bin/echoiond",
std::string_view core = "lib/Echoion.ts")
{
std::string files;
for (const auto& entry : entries) {
if (!files.empty()) {
files += ',';
}
files += entry;
}
return "{\"schemaVersion\":2,\"version\":\"1.2.3\","
"\"executable\":\"" +
std::string(executable) + "\",\"core\":\"" + std::string(core) +
"\",\"files\":[" + files + "]}";
}
bool writeFile(const std::filesystem::path& path, std::string_view contents,
std::filesystem::perms permissions)
{
std::filesystem::create_directories(path.parent_path());
if (auto result = Void::FileSystem::atomic_write(path, contents);
!result) {
std::cerr << result.error().message() << '\n';
return false;
}
std::error_code error;
std::filesystem::permissions(
path, permissions, std::filesystem::perm_options::replace, error);
if (error) {
std::cerr << error.message() << '\n';
return false;
}
return true;
}
} // namespace
int main()
{
int failures{};
const auto check = [&failures](bool condition, std::string_view message) {
if (!condition) {
std::cerr << message << '\n';
++failures;
}
};
auto directory = Void::FileSystem::TempDir::open("echoion-manifest-test");
if (!directory) {
std::cerr << directory.error().message() << '\n';
return 1;
}
const auto manifestPath = directory->path() / "pending.json";
const std::string traversal = R"({
"version":"bad",
"executable":"../echoiond",
"core":"release/Echoion.ts",
"sha256":{"executable":"x","core":"y"}
})";
if (auto written = Void::FileSystem::atomic_write(manifestPath, traversal);
!written) {
std::cerr << written.error().message() << '\n';
const auto root = directory->path();
const auto manifestPath = root / "manifest.json";
const auto executablePath = root / "bin" / "echoiond";
const auto corePath = root / "lib" / "Echoion.ts";
const auto webPath = root / "share" / "echoion" / "web" / "index.html";
const auto servicePath = root / "service" / "echoion.service.in";
const auto aliasPath = root / "lib" / "libEchoionCore.so";
const auto otherPath = root / "lib" / "Other.so";
const auto executableMode = std::filesystem::perms::owner_read |
std::filesystem::perms::owner_write |
std::filesystem::perms::owner_exec |
std::filesystem::perms::group_read |
std::filesystem::perms::group_exec |
std::filesystem::perms::others_read |
std::filesystem::perms::others_exec;
const auto regularMode = std::filesystem::perms::owner_read |
std::filesystem::perms::owner_write |
std::filesystem::perms::group_read |
std::filesystem::perms::others_read;
if (!writeFile(executablePath, "abc", executableMode) ||
!writeFile(corePath, "abc", regularMode) ||
!writeFile(webPath, "abc", regularMode) ||
!writeFile(servicePath, "abc", regularMode) ||
!writeFile(otherPath, "abc", regularMode)) {
return 1;
}
if (Echoion::Standalone::ReleaseManifest::load(manifestPath)) {
std::cerr << "path traversal manifest was accepted\n";
std::error_code error;
std::filesystem::create_symlink("Echoion.ts", aliasPath, error);
if (error) {
std::cerr << error.message() << '\n';
return 1;
}
if (auto written = Void::FileSystem::atomic_write(manifestPath, "{}");
!written) return 1;
if (Echoion::Standalone::ReleaseManifest::load(manifestPath)) {
std::cerr << "incomplete manifest was accepted\n";
const auto validManifest = manifest({
fileEntry("bin/echoiond", 3, "0755"),
fileEntry("lib/Echoion.ts", 3, "0644"),
fileEntry("lib/Other.so", 3, "0644"),
fileEntry("service/echoion.service.in", 3, "0644"),
fileEntry("share/echoion/web/index.html", 3, "0644"),
symlinkEntry("lib/libEchoionCore.so", "Echoion.ts"),
});
check(
Void::FileSystem::atomic_write(manifestPath, validManifest).has_value(),
"failed to write valid manifest");
auto loaded = Echoion::Standalone::ReleaseManifest::load(manifestPath);
check(loaded.has_value(), "valid v2 manifest was rejected while loading");
if (loaded) {
check(loaded->validate().has_value(),
"valid v2 manifest payload was rejected");
check(loaded->validateInstallLayout().has_value(),
"valid canonical install layout was rejected");
const auto undeclaredFile = root / "lib" / "undeclared.so";
check(writeFile(undeclaredFile, "abc", regularMode),
"failed to create undeclared regular-file fixture");
check(!loaded->validate(),
"undeclared regular file in immutable payload was accepted");
std::filesystem::remove(undeclaredFile, error);
const auto undeclaredLink = root / "lib" / "undeclared-link.so";
std::filesystem::create_symlink("Echoion.ts", undeclaredLink, error);
check(!error, "failed to create undeclared symbolic-link fixture");
check(!loaded->validate(),
"undeclared symbolic link in immutable payload was accepted");
std::filesystem::remove(undeclaredLink, error);
const auto specialFile = root / "lib" / "unexpected.fifo";
check(::mkfifo(specialFile.c_str(), 0600) == 0,
"failed to create special-file fixture");
check(!loaded->validate(),
"special file in immutable payload was accepted");
std::filesystem::remove(specialFile, error);
check(writeFile(root / "data" / "mutable-state", "abc", regularMode),
"failed to create mutable-root fixture");
check(loaded->validate().has_value(),
"mutable file outside immutable payload roots was rejected");
std::filesystem::permissions(root / "lib", std::filesystem::perms::all,
std::filesystem::perm_options::replace,
error);
check(!error, "failed to make immutable directory writable");
check(!loaded->validate(),
"world-writable immutable directory was accepted");
std::filesystem::permissions(root / "lib",
std::filesystem::perms::owner_all |
std::filesystem::perms::group_read |
std::filesystem::perms::group_exec |
std::filesystem::perms::others_read |
std::filesystem::perms::others_exec,
std::filesystem::perm_options::replace,
error);
check(!error, "failed to restore immutable directory mode");
}
const auto expectLoadFailure = [&](const std::string& contents,
std::string_view message) {
check(
Void::FileSystem::atomic_write(manifestPath, contents).has_value(),
"failed to write invalid manifest fixture");
check(!Echoion::Standalone::ReleaseManifest::load(manifestPath),
message);
};
expectLoadFailure(manifest({fileEntry("../echoiond", 3, "0755"),
fileEntry("lib/Echoion.ts", 3, "0644")},
"../echoiond"),
"path traversal manifest was accepted");
expectLoadFailure(manifest({fileEntry("bin/echoiond", 3, "0755"),
fileEntry("bin/echoiond", 3, "0755"),
fileEntry("lib/Echoion.ts", 3, "0644")}),
"duplicate payload path was accepted");
expectLoadFailure(
manifest({fileEntry("bin/echoiond", 3, "0755"),
fileEntry("lib/Echoion.ts", 3, "0644"),
symlinkEntry("lib/escape.so", "../outside.so")}),
"traversing symbolic link target was accepted");
expectLoadFailure(manifest({fileEntry("bin/echoiond", 3, "0777"),
fileEntry("lib/Echoion.ts", 3, "0644")}),
"world-writable executable metadata was accepted");
expectLoadFailure(manifest({fileEntry("bin/echoiond", 3, "0755"),
fileEntry("lib/Echoion.ts", 3, "0666")}),
"world-writable non-executable metadata was accepted");
check(writeFile(root / "outside.dat", "abc", regularMode),
"failed to create outside-root manifest fixture");
const auto outsideManifest = manifest({
fileEntry("bin/echoiond", 3, "0755"),
fileEntry("lib/Echoion.ts", 3, "0644"),
fileEntry("lib/Other.so", 3, "0644"),
fileEntry("service/echoion.service.in", 3, "0644"),
fileEntry("share/echoion/web/index.html", 3, "0644"),
symlinkEntry("lib/libEchoionCore.so", "Echoion.ts"),
fileEntry("outside.dat", 3, "0644"),
});
check(Void::FileSystem::atomic_write(manifestPath, outsideManifest)
.has_value(),
"failed to write outside-root manifest");
const auto outside =
Echoion::Standalone::ReleaseManifest::load(manifestPath);
check(outside.has_value() && !outside->validate(),
"manifest entry outside immutable roots was accepted");
expectLoadFailure("{\"schemaVersion\":3,\"version\":\"future\"}",
"unsupported schema version was accepted");
check(
Void::FileSystem::atomic_write(manifestPath, validManifest).has_value(),
"failed to restore valid manifest");
loaded = Echoion::Standalone::ReleaseManifest::load(manifestPath);
if (!loaded) {
std::cerr << "could not reload valid manifest\n";
return 1;
}
return 0;
{
std::ofstream output(corePath, std::ios::trunc);
output << "xyz";
}
check(!loaded->validate(), "tampered payload hash was accepted");
check(writeFile(corePath, "abc", regularMode),
"failed to restore Core fixture");
std::filesystem::permissions(corePath, std::filesystem::perms::owner_read,
std::filesystem::perm_options::replace, error);
check(!error, "failed to modify Core permissions fixture");
check(!loaded->validate(), "payload mode mismatch was accepted");
std::filesystem::permissions(corePath, regularMode,
std::filesystem::perm_options::replace, error);
std::filesystem::remove(aliasPath, error);
std::filesystem::create_symlink("Other.so", aliasPath, error);
check(!error, "failed to replace symbolic link fixture");
check(!loaded->validate(), "symbolic link target mismatch was accepted");
std::filesystem::remove(aliasPath, error);
check(writeFile(aliasPath, "abc", regularMode),
"failed to create wrong-type fixture");
check(!loaded->validate(), "unexpected regular-file type was accepted");
std::filesystem::remove(aliasPath, error);
std::filesystem::create_symlink("Echoion.ts", aliasPath, error);
std::filesystem::remove(webPath, error);
check(!loaded->validate(), "missing payload entry was accepted");
check(writeFile(webPath, "abc", regularMode),
"failed to restore web fixture");
const std::string legacyManifest =
"{\"version\":\"legacy\",\"executable\":\"bin/echoiond\","
"\"core\":\"lib/Echoion.ts\",\"sha256\":{\"executable\":\"" +
std::string(kAbcSha256) + "\",\"core\":\"" + std::string(kAbcSha256) +
"\"}}";
check(Void::FileSystem::atomic_write(manifestPath, legacyManifest)
.has_value(),
"failed to write legacy manifest");
auto legacy = Echoion::Standalone::ReleaseManifest::load(manifestPath);
check(legacy.has_value(), "legacy release manifest was rejected");
if (legacy) {
check(legacy->validate().has_value(),
"legacy release payload was rejected");
check(!legacy->validateInstallLayout(),
"legacy manifest was accepted for full install validation");
}
return failures == 0 ? 0 : 1;
}
+144
View File
@@ -0,0 +1,144 @@
#include "echoion/standalone/RuntimePaths.hpp"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
namespace
{
class TemporaryTree
{
public:
TemporaryTree()
{
std::random_device random;
path_ =
std::filesystem::temp_directory_path() /
("echoion-runtime-paths-" +
std::to_string(static_cast<unsigned long long>(random())));
std::filesystem::create_directories(path_);
}
~TemporaryTree()
{
std::error_code error;
std::filesystem::remove_all(path_, error);
}
TemporaryTree(const TemporaryTree&) = delete;
TemporaryTree& operator=(const TemporaryTree&) = delete;
[[nodiscard]] const std::filesystem::path& path() const noexcept
{
return path_;
}
private:
std::filesystem::path path_;
};
bool writeFile(const std::filesystem::path& path)
{
std::filesystem::create_directories(path.parent_path());
std::ofstream output(path);
output << "test\n";
return output.good();
}
} // namespace
int main()
{
int failures{};
const auto check = [&failures](bool condition, const std::string& message) {
if (!condition) {
std::cerr << message << '\n';
++failures;
}
};
TemporaryTree tree;
const auto installRoot = tree.path() / "echoion";
const auto executable = installRoot / "bin" / "echoiond";
const auto executableDirectory = executable.parent_path();
check(Echoion::Standalone::deriveInstallRoot(executable) == installRoot,
"bin/echoiond did not resolve to its installation root");
check(Echoion::Standalone::deriveInstallRoot(
executable, tree.path() / "override") == tree.path() / "override",
"ECHOION_HOME-style override was not preferred");
const auto state = Echoion::Standalone::resolveStateDirectory(
tree.path() / "cli-state", tree.path() / "environment-state",
tree.path() / "xdg-state", tree.path() / "home",
tree.path() / "fallback");
check(state == tree.path() / "cli-state",
"command-line state directory did not win precedence");
check(Echoion::Standalone::resolveStateDirectory(
std::nullopt, tree.path() / "environment-state",
tree.path() / "xdg-state", tree.path() / "home",
tree.path() / "fallback") == tree.path() / "environment-state",
"environment state directory did not win precedence");
check(Echoion::Standalone::resolveStateDirectory(
std::nullopt, std::nullopt, tree.path() / "xdg-state",
tree.path() / "home", tree.path() / "fallback") ==
tree.path() / "xdg-state" / "echoion",
"XDG state directory fallback is incorrect");
const auto packagedCore = installRoot / "lib" / "Echoion.ts";
check(writeFile(packagedCore), "failed to create packaged Core fixture");
check(Echoion::Standalone::resolveCorePath(
installRoot, executableDirectory) == packagedCore,
"packaged Core was not resolved from install-root/lib");
check(Echoion::Standalone::resolveCorePath(
installRoot, executableDirectory,
std::filesystem::path{"custom/Echoion.ts"}) ==
installRoot / "custom" / "Echoion.ts",
"relative Core override was not anchored at the install root");
const auto packagedIndex =
installRoot / "share" / "echoion" / "web" / "index.html";
check(writeFile(packagedIndex), "failed to create packaged web fixture");
check(
Echoion::Standalone::runtimeWorkingDirectory(installRoot,
executableDirectory) ==
installRoot / "share" / "echoion",
"packaged web root did not select share/echoion as working directory");
std::filesystem::remove_all(installRoot / "share");
const auto developmentIndex = executableDirectory / "web" / "index.html";
check(writeFile(developmentIndex),
"failed to create development web fixture");
check(Echoion::Standalone::runtimeWorkingDirectory(
installRoot, executableDirectory) == executableDirectory,
"development web root did not select the executable directory");
const auto avatarCache = tree.path() / "state" / "avatars";
std::filesystem::create_directories(avatarCache);
check(Echoion::Standalone::resolveAvatarCacheDirectory(avatarCache) ==
avatarCache,
"empty avatar directory request did not select the state cache");
check(Echoion::Standalone::resolveAvatarCacheDirectory(
avatarCache, "legacy") == avatarCache / "legacy",
"relative avatar directory was not rooted below state");
check(!Echoion::Standalone::resolveAvatarCacheDirectory(avatarCache,
"../escape"),
"traversing avatar directory was accepted");
check(!Echoion::Standalone::resolveAvatarCacheDirectory(
avatarCache, tree.path() / "outside"),
"absolute avatar directory outside state was accepted");
const auto avatarEscape = avatarCache / "escape";
std::error_code avatarError;
std::filesystem::create_symlink(tree.path(), avatarEscape, avatarError);
check(!avatarError, "failed to create avatar symlink escape fixture");
check(!Echoion::Standalone::resolveAvatarCacheDirectory(avatarCache,
"escape/cache"),
"avatar directory escaped state through a symbolic link");
const auto runningExecutable = Echoion::Standalone::runningExecutablePath();
check(runningExecutable.has_value() &&
std::filesystem::is_regular_file(*runningExecutable),
"running executable could not be resolved through /proc/self/exe");
return failures == 0 ? 0 : 1;
}
+27 -13
View File
@@ -3,32 +3,46 @@ set -euo pipefail
stage_script=$1
validator=$2
generator=$(dirname -- "$stage_script")/generate-release-manifest.py
root=$(mktemp -d)
trap 'rm -rf -- "$root"' EXIT
printf '#!/bin/sh\nexit 0\n' > "$root/source-echoiond"
chmod 0755 "$root/source-echoiond"
printf 'test core\n' > "$root/source-core.ts"
bundle=$root/bundle
mkdir -p -- \
"$bundle/bin" \
"$bundle/lib" \
"$bundle/service" \
"$bundle/share/echoion/web"
printf '#!/bin/sh\nexit 0\n' > "$bundle/bin/echoiond"
chmod 0755 "$bundle/bin/echoiond"
printf 'test core\n' > "$bundle/lib/Echoion.ts"
printf '[Service]\nExecStart=@ECHOION_EXECUTABLE@\n' \
> "$bundle/service/echoion.service.in"
printf '<!doctype html>\n' > "$bundle/share/echoion/web/index.html"
ln -s -- Echoion.ts "$bundle/lib/libEchoionCore.so"
"$stage_script" "$root/releases" 1.2.3 \
"$root/source-echoiond" "$root/source-core.ts"
python3 "$generator" --root "$bundle" --version 1.2.3
"$validator" --validate-install "$bundle"
manifest="$root/releases/pending-release.json"
test -x "$root/releases/1.2.3/echoiond"
test -f "$root/releases/1.2.3/Echoion.ts"
bash "$stage_script" "$root/releases" 1.2.3 "$bundle"
manifest=$root/releases/pending-release.json
test -x "$root/releases/1.2.3/bin/echoiond"
test -f "$root/releases/1.2.3/lib/Echoion.ts"
test -L "$root/releases/1.2.3/lib/libEchoionCore.so"
grep -q '"version": "1.2.3"' "$manifest"
grep -q '"executable": "1.2.3/echoiond"' "$manifest"
grep -q "$(sha256sum "$root/releases/1.2.3/echoiond" | cut -d' ' -f1)" "$manifest"
grep -q '"executable": "1.2.3/bin/echoiond"' "$manifest"
grep -q '"path": "1.2.3/share/echoion/web/index.html"' "$manifest"
"$validator" --validate-release "$manifest"
printf 'tampered\n' >> "$root/releases/1.2.3/Echoion.ts"
printf 'tampered\n' >> "$root/releases/1.2.3/lib/Echoion.ts"
if "$validator" --validate-release "$manifest" >/dev/null 2>&1; then
echo "tampered Core artifact was accepted" >&2
exit 1
fi
if "$stage_script" "$root/releases" '../unsafe' \
"$root/source-echoiond" "$root/source-core.ts" >/dev/null 2>&1; then
if bash "$stage_script" "$root/releases" '../unsafe' "$bundle" \
>/dev/null 2>&1; then
echo "unsafe version was accepted" >&2
exit 1
fi