78 行
3.3 KiB
Python
78 行
3.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Focused proof that P5-D never treats caller data as a capability."""
|
|
from __future__ import annotations
|
|
|
|
import importlib
|
|
import json
|
|
from pathlib import Path
|
|
import sys
|
|
from types import SimpleNamespace
|
|
import types
|
|
import unittest
|
|
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
import _host_stub # noqa: E402
|
|
|
|
_host_stub.install()
|
|
event_module = importlib.import_module("astrbot.api.event")
|
|
event_module.filter.EventMessageType = SimpleNamespace(ALL="ALL", PRIVATE_MESSAGE="PRIVATE_MESSAGE", GROUP_MESSAGE="GROUP_MESSAGE")
|
|
|
|
PACKAGE = "_p5d_security_events_companion"
|
|
if PACKAGE not in sys.modules:
|
|
package = types.ModuleType(PACKAGE)
|
|
package.__path__ = [str(Path(__file__).resolve().parents[1])]
|
|
sys.modules[PACKAGE] = package
|
|
|
|
gate = importlib.import_module(f"{PACKAGE}.p5d_security_events")
|
|
main = importlib.import_module(f"{PACKAGE}.main")
|
|
|
|
|
|
class _EnabledHost:
|
|
enable_p5d_cross_turn_correlation = True
|
|
enable_p5d_manual_recovery = True
|
|
|
|
|
|
class _Poisoned:
|
|
def __getattribute__(self, name: str):
|
|
if name not in {"__class__", "__del__"}:
|
|
raise AssertionError(f"caller object must not be inspected: {name}")
|
|
return super().__getattribute__(name)
|
|
|
|
def __repr__(self) -> str: # pragma: no cover - called only on regression
|
|
raise AssertionError("caller object must not be represented")
|
|
|
|
|
|
class P5DSecurityEventTests(unittest.TestCase):
|
|
def test_default_off_preserves_bypass(self) -> None:
|
|
host = SimpleNamespace()
|
|
for operation in gate.P5D_OPERATION_FLAGS:
|
|
self.assertTrue(gate.p5d_security_event_allows(host, operation, _Poisoned(), _Poisoned()))
|
|
|
|
def test_enabled_operations_reject_without_inspecting_forged_inputs(self) -> None:
|
|
for operation in gate.P5D_OPERATION_FLAGS:
|
|
host = _EnabledHost()
|
|
with self.subTest(operation=operation), self.assertLogs("PrivateCompanion.P5D", level="WARNING") as logs:
|
|
allowed = gate.p5d_security_event_allows(host, operation, _Poisoned(), _Poisoned(), _Poisoned())
|
|
self.assertFalse(allowed)
|
|
audit = host._p5d_denial_audit[-1]
|
|
self.assertEqual({"operation", "outcome", "reason_code", "ref_hash"}, set(audit))
|
|
self.assertEqual(gate.P5D_HOST_OWNER_CAPABILITY_UNAVAILABLE, audit["reason_code"])
|
|
self.assertNotIn("Poisoned", "\n".join(logs.output))
|
|
|
|
def test_main_helper_and_unknown_operation_keep_the_same_contract(self) -> None:
|
|
host = _EnabledHost()
|
|
host._p5d_security_event_allows = main.PrivateCompanionPlugin._p5d_security_event_allows.__get__(host, _EnabledHost)
|
|
self.assertFalse(host._p5d_security_event_allows(gate.MANUAL_RECOVERY_OPERATION, _Poisoned()))
|
|
with self.assertRaisesRegex(ValueError, "p5d_unknown_operation"):
|
|
gate.p5d_security_event_allows(host, "forged_operation")
|
|
|
|
def test_schema_has_exactly_the_two_default_off_operations(self) -> None:
|
|
schema = json.loads((Path(__file__).resolve().parents[1] / "_conf_schema.json").read_text(encoding="utf-8"))
|
|
items = schema["p5d_safety_config"]["items"]
|
|
self.assertEqual(set(gate.P5D_OPERATION_FLAGS.values()), set(items))
|
|
self.assertTrue(all(item["type"] == "bool" and item["default"] is False for item in items.values()))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|