mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 18:44:41 +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.
225 lines
6.7 KiB
Python
225 lines
6.7 KiB
Python
"""Immutable MCP startup policy: construction, fail-closed validation, helpers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
from dataclasses import FrozenInstanceError
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from obliteratus import mcp_policy
|
|
from obliteratus.mcp_policy import (
|
|
DEFAULT_MAX_INPUT_BYTES,
|
|
DEFAULT_MAX_OUTPUT_BYTES,
|
|
McpPolicy,
|
|
McpPolicyError,
|
|
is_within_roots,
|
|
normalize_root,
|
|
policy_from_args,
|
|
)
|
|
|
|
|
|
def _valid_kwargs(**overrides):
|
|
kwargs = {"archive_root": "/tmp/obliteratus-archive"}
|
|
kwargs.update(overrides)
|
|
return kwargs
|
|
|
|
|
|
def test_valid_policy_is_accepted_with_defaults():
|
|
policy = McpPolicy(**_valid_kwargs())
|
|
|
|
assert policy.archive_root == Path("/tmp/obliteratus-archive")
|
|
assert policy.allowed_roots == ()
|
|
assert policy.max_input_bytes == DEFAULT_MAX_INPUT_BYTES
|
|
assert policy.max_output_bytes == DEFAULT_MAX_OUTPUT_BYTES
|
|
assert policy.network_enabled is False
|
|
assert policy.execution_enabled is False
|
|
|
|
|
|
def test_policy_is_frozen_after_construction():
|
|
policy = McpPolicy(**_valid_kwargs())
|
|
|
|
with pytest.raises(FrozenInstanceError):
|
|
policy.network_enabled = True # type: ignore[misc]
|
|
with pytest.raises(FrozenInstanceError):
|
|
policy.archive_root = Path("/elsewhere") # type: ignore[misc]
|
|
|
|
|
|
def test_allowed_roots_are_deduplicated_and_immutable():
|
|
policy = McpPolicy(
|
|
**_valid_kwargs(allowed_roots=("/data/a", "/data/b", "/data/a")),
|
|
)
|
|
|
|
assert policy.allowed_roots == (Path("/data/a"), Path("/data/b"))
|
|
with pytest.raises(AttributeError):
|
|
policy.allowed_roots += (Path("/data/c"),) # type: ignore[misc]
|
|
|
|
|
|
def test_capabilities_allowlist_has_exact_keys_and_deterministic_tools():
|
|
policy = McpPolicy(**_valid_kwargs(network_enabled=True))
|
|
|
|
capabilities = policy.capabilities()
|
|
|
|
assert set(capabilities) == {"tools", "network", "execution"}
|
|
assert capabilities["tools"] == sorted(mcp_policy.READ_TOOLS)
|
|
assert capabilities["network"] is False
|
|
assert capabilities["execution"] is False
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"kwargs",
|
|
[
|
|
{"archive_root": None},
|
|
{"archive_root": ""},
|
|
{"archive_root": " "},
|
|
{"archive_root": "relative/path"},
|
|
{"archive_root": "~definitely-not-a-real-user-xyz/path"},
|
|
{"archive_root": "/ok", "allowed_roots": ("relative/root",)},
|
|
{"archive_root": "/ok", "allowed_roots": ("",)},
|
|
{"archive_root": "/ok", "max_input_bytes": 0},
|
|
{"archive_root": "/ok", "max_input_bytes": -1024},
|
|
{"archive_root": "/ok", "max_output_bytes": 0},
|
|
{"archive_root": "/ok", "max_input_bytes": True},
|
|
{"archive_root": "/ok", "max_output_bytes": "1024"},
|
|
{"archive_root": "/ok", "network_enabled": "yes"},
|
|
{"archive_root": "/ok", "execution_enabled": 1},
|
|
],
|
|
ids=[
|
|
"missing-archive-root",
|
|
"empty-archive-root",
|
|
"blank-archive-root",
|
|
"relative-archive-root",
|
|
"unknown-user-tilde-archive-root",
|
|
"relative-allowed-root",
|
|
"empty-allowed-root",
|
|
"zero-max-input",
|
|
"negative-max-input",
|
|
"zero-max-output",
|
|
"bool-max-input",
|
|
"str-max-output",
|
|
"str-network-flag",
|
|
"int-exec-flag",
|
|
],
|
|
)
|
|
def test_invalid_policies_are_rejected_fail_closed(kwargs):
|
|
with pytest.raises(McpPolicyError):
|
|
McpPolicy(**kwargs)
|
|
|
|
|
|
def test_validation_never_creates_directories(tmp_path):
|
|
missing = tmp_path / "does" / "not" / "exist"
|
|
|
|
policy = McpPolicy(archive_root=str(missing))
|
|
|
|
assert policy.archive_root == missing
|
|
assert not missing.exists()
|
|
|
|
|
|
def test_policy_build_reads_no_files(monkeypatch, tmp_path):
|
|
def _forbidden_read_text(self, *args, **kwargs):
|
|
raise AssertionError("policy build must not read files")
|
|
|
|
monkeypatch.setattr(Path, "read_text", _forbidden_read_text)
|
|
args = argparse.Namespace(
|
|
archive_root=str(tmp_path),
|
|
allowed_root=["/data"],
|
|
max_input_bytes=1024,
|
|
max_output_bytes=2048,
|
|
allow_network=False,
|
|
no_network=False,
|
|
allow_exec=False,
|
|
no_exec=False,
|
|
)
|
|
|
|
policy = policy_from_args(args)
|
|
|
|
assert policy.archive_root == tmp_path
|
|
assert policy.allowed_roots == (Path("/data"),)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"flags",
|
|
[
|
|
{"allow_network": True, "no_network": True},
|
|
{"allow_exec": True, "no_exec": True},
|
|
],
|
|
ids=["network", "execution"],
|
|
)
|
|
def test_contradictory_flags_are_rejected(flags):
|
|
kwargs = {
|
|
"archive_root": "/tmp/archive",
|
|
"allowed_root": None,
|
|
"max_input_bytes": 1024,
|
|
"max_output_bytes": 1024,
|
|
"allow_network": False,
|
|
"no_network": False,
|
|
"allow_exec": False,
|
|
"no_exec": False,
|
|
}
|
|
kwargs.update(flags)
|
|
args = argparse.Namespace(**kwargs)
|
|
|
|
with pytest.raises(McpPolicyError, match="contradictory flags"):
|
|
policy_from_args(args)
|
|
|
|
|
|
def test_policy_from_args_maps_all_flags():
|
|
args = argparse.Namespace(
|
|
archive_root="/tmp/archive",
|
|
allowed_root=["/data/a", "/data/b"],
|
|
max_input_bytes=4096,
|
|
max_output_bytes=8192,
|
|
allow_network=True,
|
|
no_network=False,
|
|
allow_exec=False,
|
|
no_exec=True,
|
|
)
|
|
|
|
policy = policy_from_args(args)
|
|
|
|
assert policy.archive_root == Path("/tmp/archive")
|
|
assert policy.allowed_roots == (Path("/data/a"), Path("/data/b"))
|
|
assert policy.max_input_bytes == 4096
|
|
assert policy.max_output_bytes == 8192
|
|
assert policy.network_enabled is True
|
|
assert policy.execution_enabled is False
|
|
assert policy.capabilities()["network"] is False
|
|
|
|
|
|
def test_policy_from_args_rejects_missing_archive_root():
|
|
args = argparse.Namespace(
|
|
archive_root=None,
|
|
allowed_root=None,
|
|
max_input_bytes=1024,
|
|
max_output_bytes=1024,
|
|
allow_network=False,
|
|
no_network=False,
|
|
allow_exec=False,
|
|
no_exec=False,
|
|
)
|
|
|
|
with pytest.raises(McpPolicyError, match="archive root is required"):
|
|
policy_from_args(args)
|
|
|
|
|
|
def test_normalize_root_requires_absolute():
|
|
assert normalize_root("/data//a/./b") == Path("/data/a/b")
|
|
with pytest.raises(McpPolicyError):
|
|
normalize_root("relative/path")
|
|
|
|
|
|
def test_is_within_roots_is_lexical_only():
|
|
roots = (Path("/data"),)
|
|
|
|
assert is_within_roots("/data/file.bin", roots)
|
|
assert is_within_roots("/data/sub/dir", roots)
|
|
assert not is_within_roots("/other/file.bin", roots)
|
|
assert not is_within_roots("/data-exfil/file.bin", roots)
|
|
assert not is_within_roots("/data/../other/file.bin", roots)
|
|
|
|
|
|
def test_policy_module_imports_no_repository_config():
|
|
assert "obliteratus.config" not in dir(mcp_policy)
|
|
assert not hasattr(mcp_policy, "load_config")
|