mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 11:04:06 +01:00
Integrate bounded admission, UI ownership and controls, opt-in durable MCP execution, rank-local TP2 groundwork, regression hardening and operator guides. Physical larger-memory and two-host qualification remain separate.
498 lines
18 KiB
Python
498 lines
18 KiB
Python
"""Durable worker metadata and policy contracts, without isolation claims."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import errno
|
|
import hashlib
|
|
import io
|
|
import json
|
|
import os
|
|
from dataclasses import replace
|
|
from contextlib import nullcontext
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from obliteratus import mcp_execution as execution
|
|
from obliteratus import mcp_worker as worker
|
|
from obliteratus.read_boundary import open_directory
|
|
|
|
RUN_ID = "run-" + "a" * 32
|
|
|
|
|
|
@pytest.fixture
|
|
def spec(tmp_path):
|
|
return execution.LaunchSpec(
|
|
RUN_ID,
|
|
execution.ModelInput("local", str(tmp_path / "model")),
|
|
"basic",
|
|
str(tmp_path),
|
|
execution.RunResources(1000),
|
|
False,
|
|
(1, 2),
|
|
"digest",
|
|
(1, 2, 3, 4),
|
|
1,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize("value", [{"data": "x" * worker.META_LIMIT}, {"number": float("nan")}])
|
|
def test_metadata_serialization_rejects_before_creating_temporary(tmp_path, value):
|
|
with open_directory(tmp_path) as fd:
|
|
with pytest.raises(ValueError):
|
|
worker.atomic_json(fd, "manifest.json", value)
|
|
assert list(tmp_path.iterdir()) == []
|
|
|
|
|
|
def test_atomic_replace_failure_preserves_old_manifest_and_removes_temporary(tmp_path, monkeypatch):
|
|
(tmp_path / "manifest.json").write_text('{"old": true}')
|
|
|
|
def fail_replace(*args, **kwargs):
|
|
raise OSError("replace failed")
|
|
|
|
monkeypatch.setattr(worker.os, "replace", fail_replace)
|
|
with open_directory(tmp_path) as fd:
|
|
with pytest.raises(OSError, match="replace failed"):
|
|
worker.atomic_json(fd, "manifest.json", {"new": True})
|
|
assert json.loads((tmp_path / "manifest.json").read_text()) == {"old": True}
|
|
assert sorted(path.name for path in tmp_path.iterdir()) == ["manifest.json"]
|
|
|
|
|
|
@pytest.mark.parametrize("run_id", [None, 1, "../manifest.json", "run-" + "A" * 32])
|
|
def test_load_rejects_invalid_identity_before_read(tmp_path, run_id):
|
|
with open_directory(tmp_path) as fd:
|
|
with pytest.raises(ValueError, match="invalid run ID"):
|
|
worker.load(fd, run_id)
|
|
|
|
|
|
@pytest.mark.parametrize("value", [[], None, {}, {"run_id": "run-" + "b" * 32}])
|
|
def test_load_binds_manifest_identity(tmp_path, value):
|
|
(tmp_path / "manifest.json").write_text(json.dumps(value))
|
|
with open_directory(tmp_path) as fd:
|
|
with pytest.raises(ValueError, match="invalid manifest"):
|
|
worker.load(fd, RUN_ID)
|
|
|
|
|
|
@pytest.mark.parametrize("kind", ["oversized", "hardlink", "fifo"])
|
|
def test_event_log_rejects_unsafe_or_unbounded_storage(tmp_path, kind):
|
|
events = tmp_path / "events.jsonl"
|
|
if kind == "oversized":
|
|
events.write_bytes(b"x" * 65537)
|
|
elif kind == "hardlink":
|
|
events.write_bytes(b"original")
|
|
os.link(events, tmp_path / "alias")
|
|
else:
|
|
os.mkfifo(events)
|
|
value = {"run_id": RUN_ID}
|
|
with open_directory(tmp_path) as fd:
|
|
with pytest.raises((ValueError, OSError)):
|
|
worker.save(fd, value, "running")
|
|
assert json.loads((tmp_path / "manifest.json").read_text())["run_id"] == RUN_ID
|
|
if kind == "hardlink":
|
|
assert (tmp_path / "alias").read_bytes() == b"original"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"identity", [None, {}, {"pid": True}, {"pid": 1}, {"pid": -1}, {"pid": 2345, "uid": -1}]
|
|
)
|
|
def test_worker_identity_rejects_invalid_pid_and_uid(identity):
|
|
assert worker.worker_matches(-1, {"worker": identity}) is False
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"case",
|
|
[
|
|
"valid",
|
|
"large_stat",
|
|
"owner",
|
|
"ticks",
|
|
"large_command",
|
|
"command",
|
|
"run_id",
|
|
"fd",
|
|
"group",
|
|
"inode",
|
|
"malformed_stat",
|
|
"missing",
|
|
"denied",
|
|
],
|
|
)
|
|
def test_worker_identity_requires_process_command_group_and_directory(tmp_path, monkeypatch, case):
|
|
original_open = open
|
|
original_stat = os.stat
|
|
pid = 2345
|
|
stat_bytes = b"2345 (worker with ) parentheses) " + b" ".join([b"S"] + [b"0"] * 18 + [b"123"])
|
|
parts = [
|
|
"python",
|
|
"-m",
|
|
"obliteratus.mcp_worker",
|
|
"supervise",
|
|
"--run-fd",
|
|
"9",
|
|
"--run-id",
|
|
RUN_ID,
|
|
]
|
|
if case == "command":
|
|
parts[2] = "unrelated.module"
|
|
elif case == "run_id":
|
|
parts[7] = "run-" + "b" * 32
|
|
elif case == "fd":
|
|
parts[5] = "../9"
|
|
command = ("\0".join(parts) + "\0").encode()
|
|
if case == "large_command":
|
|
command = b"x" * 4097
|
|
if case == "large_stat":
|
|
stat_bytes = b"x" * 4097
|
|
elif case == "malformed_stat":
|
|
stat_bytes = b"malformed"
|
|
|
|
def proc_open(path, mode):
|
|
if not str(path).startswith(f"/proc/{pid}/"):
|
|
return original_open(path, mode)
|
|
assert mode == "rb"
|
|
if case in {"missing", "denied"}:
|
|
code = errno.ENOENT if case == "missing" else errno.EACCES
|
|
raise OSError(code, "unavailable")
|
|
return io.BytesIO(stat_bytes if path.endswith("/stat") else command)
|
|
|
|
with open_directory(tmp_path) as fd:
|
|
info = os.fstat(fd)
|
|
|
|
def proc_stat(path, *args, **kwargs):
|
|
if path == f"/proc/{pid}":
|
|
return SimpleNamespace(st_uid=os.getuid() + int(case == "owner"))
|
|
if path == f"/proc/{pid}/fd/9":
|
|
return SimpleNamespace(
|
|
st_dev=info.st_dev, st_ino=info.st_ino + int(case == "inode")
|
|
)
|
|
return original_stat(path, *args, **kwargs)
|
|
|
|
monkeypatch.setattr("builtins.open", proc_open)
|
|
monkeypatch.setattr(worker.os, "stat", proc_stat)
|
|
monkeypatch.setattr(worker.os, "getpgid", lambda _: pid + int(case == "group"))
|
|
value = {
|
|
"run_id": RUN_ID,
|
|
"worker": {
|
|
"pid": pid,
|
|
"uid": os.getuid(),
|
|
"start_ticks": 124 if case == "ticks" else 123,
|
|
},
|
|
}
|
|
result = worker.worker_matches(fd, value)
|
|
assert result is (True if case == "valid" else None if case == "denied" else False)
|
|
|
|
|
|
@pytest.mark.parametrize("network", [False, True])
|
|
@pytest.mark.parametrize("model_fd", [None, 44])
|
|
def test_sandbox_argv_constructs_explicit_fd_mount_policy(spec, network, model_fd):
|
|
spec = replace(
|
|
spec,
|
|
network=network,
|
|
model=execution.ModelInput("remote", "bundled/model") if model_fd is None else spec.model,
|
|
)
|
|
args = worker.sandbox_command(spec, 33, model_fd, [(55, "/mcp-runtime/purelib")])
|
|
assert args[:8] == [
|
|
str(execution.BWRAP),
|
|
"--unshare-all",
|
|
"--unshare-user",
|
|
"--die-with-parent",
|
|
"--disable-userns",
|
|
"--cap-drop",
|
|
"ALL",
|
|
*(["--share-net"] if network else ["--ro-bind-fd"]),
|
|
]
|
|
assert ("--share-net" in args) is network
|
|
assert args[args.index("--bind-fd") + 1 : args.index("--bind-fd") + 3] == [
|
|
"33",
|
|
"/tmp/mcp-export",
|
|
]
|
|
assert args[args.index("--size") + 1 : args.index("--size") + 4] == [
|
|
str(spec.resources.work_bytes),
|
|
"--tmpfs",
|
|
"/tmp/mcp-work",
|
|
]
|
|
separator = args.index("--")
|
|
assert args[separator + 1 : separator + 5] == [
|
|
str(Path(worker.sys.executable).resolve()),
|
|
"-m",
|
|
"obliteratus.mcp_worker",
|
|
"job",
|
|
]
|
|
assert json.loads(args[separator + 6])["memory_bytes"] == spec.resources.memory_bytes
|
|
assert args[separator + 8] == ("/tmp/mcp-model" if model_fd is not None else "bundled/model")
|
|
assert "--trust-remote-code" not in args
|
|
|
|
|
|
@pytest.mark.parametrize("mounts", [[], [(1, "/")]])
|
|
def test_sandbox_argv_rejects_missing_dependencies_and_host_root(spec, mounts):
|
|
with pytest.raises(ValueError, match="dependencies|required|forbidden"):
|
|
worker.sandbox_command(spec, 33, None, mounts)
|
|
|
|
|
|
@pytest.mark.parametrize("kind", ["regular", "directory", "fifo", "symlink"])
|
|
def test_runtime_mounts_pin_inodes_and_close_descriptors(tmp_path, monkeypatch, kind):
|
|
source = tmp_path / "dependency"
|
|
if kind == "directory":
|
|
source.mkdir()
|
|
elif kind == "fifo":
|
|
os.mkfifo(source)
|
|
elif kind == "symlink":
|
|
target = tmp_path / "target"
|
|
target.write_text("library")
|
|
source.symlink_to(target)
|
|
else:
|
|
source.write_text("library")
|
|
monkeypatch.setattr(
|
|
worker, "_runtime_paths", lambda network: [(source, "/mcp-runtime/dependency")]
|
|
)
|
|
if kind in {"fifo", "symlink"}:
|
|
with pytest.raises((ValueError, OSError)):
|
|
with worker.runtime_mounts(False):
|
|
pytest.fail("unsafe dependency admitted")
|
|
else:
|
|
with worker.runtime_mounts(False) as mounts:
|
|
fd, destination = mounts[0]
|
|
assert destination == "/mcp-runtime/dependency"
|
|
assert os.fstat(fd).st_ino == source.stat().st_ino
|
|
with pytest.raises(OSError):
|
|
os.fstat(fd)
|
|
|
|
|
|
@pytest.mark.parametrize("multiarch", [None, "", "../escape", "x/y"])
|
|
def test_runtime_paths_reject_untrusted_multiarch_layout(monkeypatch, multiarch):
|
|
original = worker.sysconfig.get_config_var
|
|
monkeypatch.setattr(
|
|
worker.sysconfig,
|
|
"get_config_var",
|
|
lambda key: multiarch if key == "MULTIARCH" else original(key),
|
|
)
|
|
with pytest.raises(ValueError, match="unsupported runtime library layout"):
|
|
worker._runtime_paths(False)
|
|
|
|
|
|
def artifact(path, size=1):
|
|
return {"path": path, "bytes": size, "sha256": "a" * 64}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("inventory", "metadata", "error"),
|
|
[
|
|
([], {}, "invalid pipeline evidence"),
|
|
({}, [], "invalid pipeline evidence"),
|
|
({"artifacts": []}, {}, "invalid inventory"),
|
|
({"artifacts": [None]}, {}, "invalid artifact"),
|
|
({"artifacts": [{"path": 1}]}, {}, "invalid artifact"),
|
|
({"artifacts": [artifact("/absolute")]}, {}, "invalid artifact path"),
|
|
({"artifacts": [artifact("../escape")]}, {}, "invalid artifact path"),
|
|
(
|
|
{"artifacts": [artifact("config.json"), artifact("./config.json")]},
|
|
{},
|
|
"invalid artifact path",
|
|
),
|
|
({"artifacts": [artifact("config.json", True)]}, {}, "invalid artifact bytes"),
|
|
({"artifacts": [artifact("config.json", -1)]}, {}, "invalid artifact bytes"),
|
|
(
|
|
{"artifacts": [dict(artifact("config.json"), sha256="A" * 64)]},
|
|
{},
|
|
"invalid artifact hash",
|
|
),
|
|
({"artifacts": [artifact("config.json")]}, {}, "incomplete or excessive checkpoint"),
|
|
({"artifacts": [artifact("model.safetensors")]}, {}, "incomplete or excessive checkpoint"),
|
|
(
|
|
{"artifacts": [artifact("config.json"), artifact("model.safetensors", 1 << 30)]},
|
|
{},
|
|
"incomplete or excessive checkpoint",
|
|
),
|
|
],
|
|
)
|
|
def test_evidence_inventory_validation_precedes_durable_provenance(
|
|
tmp_path, spec, inventory, metadata, error
|
|
):
|
|
exported = tmp_path / "export"
|
|
exported.mkdir()
|
|
(exported / "mcp-artifact-inventory.json").write_text(json.dumps(inventory))
|
|
(exported / "abliteration_metadata.json").write_text(json.dumps(metadata))
|
|
with open_directory(tmp_path) as run_fd, open_directory(exported) as export_fd:
|
|
with pytest.raises(ValueError, match=error):
|
|
worker.retain_evidence(run_fd, export_fd, spec)
|
|
assert not (tmp_path / "run-provenance.json").exists()
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"provenance",
|
|
[
|
|
[],
|
|
{
|
|
"obliteratus_git_commit": "invalid",
|
|
"source_model_revision": "b" * 40,
|
|
"obliteratus_git_dirty": 1,
|
|
},
|
|
],
|
|
)
|
|
@pytest.mark.parametrize(
|
|
"metrics",
|
|
[
|
|
[],
|
|
{"accuracy": 0.5, "refusal_rate": True, "perplexity": float("inf"), "kl_divergence": 1e101},
|
|
],
|
|
)
|
|
def test_evidence_preserves_unknown_provenance_and_only_finite_metrics(
|
|
tmp_path, spec, provenance, metrics
|
|
):
|
|
exported = tmp_path / "export"
|
|
exported.mkdir()
|
|
inventory = {"artifacts": [artifact("config.json"), artifact("model.safetensors")]}
|
|
raw_inventory = json.dumps(inventory).encode()
|
|
raw_metadata = json.dumps({"provenance": provenance, "metrics": metrics}).encode()
|
|
(exported / "mcp-artifact-inventory.json").write_bytes(raw_inventory)
|
|
(exported / "abliteration_metadata.json").write_bytes(raw_metadata)
|
|
with open_directory(tmp_path) as run_fd, open_directory(exported) as export_fd:
|
|
evidence, numbers = worker.retain_evidence(run_fd, export_fd, spec)
|
|
raw_record = (tmp_path / "run-provenance.json").read_bytes()
|
|
record = json.loads(raw_record)
|
|
assert evidence == {
|
|
"run_provenance": {
|
|
"path": "run-provenance.json",
|
|
"sha256": "sha256:" + hashlib.sha256(raw_record).hexdigest(),
|
|
}
|
|
}
|
|
assert numbers == ({"accuracy": 0.5} if isinstance(metrics, dict) else {})
|
|
assert record["artifact_inventory_sha256"] == hashlib.sha256(raw_inventory).hexdigest()
|
|
assert record["pipeline_metadata_sha256"] == hashlib.sha256(raw_metadata).hexdigest()
|
|
safe = record["pipeline_provenance"]
|
|
assert safe["obliteratus_git_commit"] is safe["obliteratus_git_dirty"] is None
|
|
assert safe["source_model_revision"] == ("b" * 40 if isinstance(provenance, dict) else None)
|
|
assert "artifact_id" not in evidence
|
|
|
|
|
|
@pytest.mark.parametrize("run_id", [None, "../escape"])
|
|
def test_recover_rejects_untrusted_run_identity(tmp_path, run_id):
|
|
with pytest.raises(ValueError, match="invalid run ID"):
|
|
worker.recover(tmp_path, run_id)
|
|
|
|
|
|
def test_execution_requires_explicit_capability(tmp_path):
|
|
from obliteratus.mcp_policy import McpPolicy
|
|
|
|
with pytest.raises(ValueError, match="execution disabled"):
|
|
execution.ExecutionTools(McpPolicy(archive_root=tmp_path))
|
|
|
|
|
|
@pytest.mark.parametrize("trusted", [False, True])
|
|
def test_sandbox_identity_requires_root_owned_nonwritable_regular_binary(
|
|
tmp_path, monkeypatch, trusted
|
|
):
|
|
binary = tmp_path / "bwrap"
|
|
binary.write_bytes(b"fixture executable metadata only")
|
|
binary.chmod(0o755)
|
|
monkeypatch.setattr(execution, "BWRAP", binary)
|
|
actual = binary.stat()
|
|
original_stat = os.stat
|
|
|
|
def binary_stat(path, *args, **kwargs):
|
|
if path == binary.name and "dir_fd" in kwargs:
|
|
return SimpleNamespace(
|
|
st_mode=actual.st_mode if trusted else actual.st_mode | 0o020,
|
|
st_uid=0,
|
|
st_dev=actual.st_dev,
|
|
st_ino=actual.st_ino,
|
|
st_size=actual.st_size,
|
|
st_mtime_ns=actual.st_mtime_ns,
|
|
)
|
|
return original_stat(path, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(execution.os, "stat", binary_stat)
|
|
if trusted:
|
|
assert execution.sandbox_identity() == (
|
|
actual.st_dev,
|
|
actual.st_ino,
|
|
actual.st_size,
|
|
actual.st_mtime_ns,
|
|
)
|
|
else:
|
|
with pytest.raises(ValueError, match="untrusted sandbox executable"):
|
|
execution.sandbox_identity()
|
|
|
|
|
|
@pytest.mark.parametrize("network", [False, True])
|
|
def test_runtime_paths_alias_libraries_and_only_opt_in_network_files(
|
|
tmp_path, monkeypatch, network
|
|
):
|
|
for name in ("stdlib", "purelib", "platlib", "libraries"):
|
|
(tmp_path / name).mkdir()
|
|
library = tmp_path / "libraries" / "libmcp.so.1"
|
|
library.write_bytes(b"fixture library metadata")
|
|
(tmp_path / "libraries" / "unrelated").write_bytes(b"not a library")
|
|
monkeypatch.setattr(worker.sysconfig, "get_path", lambda key: str(tmp_path / key))
|
|
monkeypatch.setattr(
|
|
worker.sysconfig,
|
|
"get_config_var",
|
|
lambda key: "mcp-test-architecture" if key == "MULTIARCH" else str(tmp_path / "libraries"),
|
|
)
|
|
paths = worker._runtime_paths(network)
|
|
assert (library, str(library)) in paths
|
|
assert paths.count((library, str(library))) == 1
|
|
assert (tmp_path / "purelib", "/mcp-runtime/purelib") in paths
|
|
assert not any(destination.endswith("/unrelated") for _, destination in paths)
|
|
network_files = {
|
|
"/etc/resolv.conf",
|
|
"/etc/hosts",
|
|
"/etc/nsswitch.conf",
|
|
"/etc/ssl/certs/ca-certificates.crt",
|
|
}
|
|
assert {destination for _, destination in paths if destination in network_files} == (
|
|
{name for name in network_files if Path(name).is_file()} if network else set()
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("count", "libraries", "message"),
|
|
[
|
|
(4097, False, "excessive runtime library directory"),
|
|
(129, True, "excessive runtime libraries"),
|
|
],
|
|
)
|
|
def test_runtime_library_discovery_has_entry_and_library_ceilings(
|
|
tmp_path, monkeypatch, count, libraries, message
|
|
):
|
|
for name in ("stdlib", "purelib", "platlib", "libraries"):
|
|
(tmp_path / name).mkdir()
|
|
entries = [
|
|
SimpleNamespace(
|
|
name=f"lib{index}.so" if libraries else f"other{index}", is_file=lambda: True
|
|
)
|
|
for index in range(count)
|
|
]
|
|
monkeypatch.setattr(worker.sysconfig, "get_path", lambda key: str(tmp_path / key))
|
|
monkeypatch.setattr(
|
|
worker.sysconfig,
|
|
"get_config_var",
|
|
lambda key: "mcp-test-architecture" if key == "MULTIARCH" else str(tmp_path / "libraries"),
|
|
)
|
|
monkeypatch.setattr(worker.os, "scandir", lambda path: nullcontext(iter(entries)))
|
|
with pytest.raises(ValueError, match=message):
|
|
worker._runtime_paths(False)
|
|
|
|
|
|
@pytest.mark.parametrize("case", ["no_config", "no_tensors", "bytes", "depth"])
|
|
def test_model_fingerprint_rejects_incomplete_and_excessive_inputs(tmp_path, case):
|
|
if case != "no_config":
|
|
(tmp_path / "config.json").write_text("{}")
|
|
if case != "no_tensors":
|
|
(tmp_path / "model.safetensors").write_bytes(b"fixture")
|
|
if case == "depth":
|
|
nested = tmp_path
|
|
for _ in range(17):
|
|
nested = nested / "nested"
|
|
nested.mkdir()
|
|
message = (
|
|
"model input byte ceiling"
|
|
if case == "bytes"
|
|
else (
|
|
"model depth limit" if case == "depth" else "model requires config.json and safetensors"
|
|
)
|
|
)
|
|
with pytest.raises(ValueError, match=message):
|
|
execution.model_fingerprint(tmp_path, 1 if case == "bytes" else 10000)
|