fix(maf): harden phase 5 approval safety
This commit is contained in:
1 parent
f82363d81f
commit
a55992e31c
11 files changed
+489
-146
No files matched your search
@@ -12,9 +12,14 @@ from maf_test_helpers import make_maf_service
|
||||
from sqlalchemy import update
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from app.gateway.app import _scan_maf_approval_expiry
|
||||
from app.gateway.app import (
|
||||
_scan_maf_approval_expiry,
|
||||
_start_maf_approval_scanner,
|
||||
_stop_maf_approval_scanner,
|
||||
)
|
||||
from app.maf_roundtable.action_coordinator import (
|
||||
ActionCoordinator,
|
||||
MafActionSecretKeyError,
|
||||
derive_target_key,
|
||||
encrypt_params,
|
||||
)
|
||||
@@ -24,6 +29,7 @@ from app.maf_roundtable.workflow_executors import RoundtableState
|
||||
from app.maf_roundtable.workflow_runner import WorkflowRunner
|
||||
from deerflow.config.maf_roundtable_config import MafRoundtableConfig
|
||||
from deerflow.persistence.maf_roundtable import make_maf_roundtable_store
|
||||
from deerflow.persistence.maf_roundtable.actions import action_to_public_dict
|
||||
from deerflow.persistence.maf_roundtable.model import (
|
||||
Base,
|
||||
MafRoundtableActionLeaseRow,
|
||||
@@ -96,6 +102,7 @@ async def _approved_action(
|
||||
|
||||
|
||||
def test_target_key_is_server_derived_and_opaque(monkeypatch) -> None:
|
||||
monkeypatch.setenv("MAF_ACTION_SECRET_KEY", "explicit-hmac-test-secret")
|
||||
snapshot = _snapshot()
|
||||
original = {
|
||||
"tenant_id": "tenant-a",
|
||||
@@ -130,10 +137,60 @@ def test_target_key_is_server_derived_and_opaque(monkeypatch) -> None:
|
||||
)
|
||||
assert nested == nested_other
|
||||
|
||||
conservative_inputs = [
|
||||
{"tenant_id": "tenant-a"},
|
||||
{"tenant_id": "tenant-a", "object_id": None},
|
||||
{"tenant_id": "tenant-a", "object_id": ""},
|
||||
{"tenant_id": "tenant-a", "object_id": {"id": "task-7"}},
|
||||
{"tenant_id": "tenant-a", "object_id": ["task-7"]},
|
||||
]
|
||||
conservative_keys = {
|
||||
derive_target_key(TEST_TOOL, args, snapshot, "seat-1")
|
||||
for args in conservative_inputs
|
||||
}
|
||||
assert len(conservative_keys) == 1
|
||||
|
||||
monkeypatch.delenv("MAF_ACTION_SECRET_KEY", raising=False)
|
||||
with pytest.raises(MafActionSecretKeyError):
|
||||
derive_target_key(TEST_TOOL, original, snapshot, "seat-1")
|
||||
|
||||
monkeypatch.setenv("MAF_ACTION_SECRET_KEY", "another-test-domain-secret")
|
||||
assert target != derive_target_key(TEST_TOOL, original, snapshot, "seat-1")
|
||||
|
||||
|
||||
def test_action_public_dto_is_an_explicit_allowlist() -> None:
|
||||
internal = {
|
||||
"id": "action-1",
|
||||
"session_id": "session-1",
|
||||
"run_id": "run-1",
|
||||
"agent_id": "seat-1",
|
||||
"tool_name": TEST_TOOL,
|
||||
"logical_action_id": "tool-call-1",
|
||||
"turn_index": 1,
|
||||
"risk_level": "R3",
|
||||
"status": "waiting_approval",
|
||||
"arguments": {"safe": "redacted"},
|
||||
"result": None,
|
||||
"external_object_id": None,
|
||||
"approval_id": "approval-1",
|
||||
"attempt_count": 0,
|
||||
"started_at": None,
|
||||
"finished_at": None,
|
||||
"created_at": "2026-01-01T00:00:00+00:00",
|
||||
"updated_at": "2026-01-01T00:00:00+00:00",
|
||||
"target_key": "private-target",
|
||||
"idempotency_key": "private-idempotency",
|
||||
"internal_canary": "must-not-leak",
|
||||
}
|
||||
public = action_to_public_dict(internal)
|
||||
assert set(public) == {
|
||||
"id", "session_id", "run_id", "agent_id", "tool_name",
|
||||
"logical_action_id", "turn_index", "risk_level", "status",
|
||||
"arguments", "result", "external_object_id", "approval_id",
|
||||
"attempt_count", "started_at", "finished_at", "created_at", "updated_at",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_two_agents_same_target_are_serialized(store, svc):
|
||||
"""The DB lease, not a process lock, blocks the second approved write."""
|
||||
@@ -394,3 +451,26 @@ async def test_approval_expiry_scan_is_repeatable_and_get_is_observational(monke
|
||||
assert fake.calls == [(run["id"], "u1")]
|
||||
assert (await store.approvals.get_approval(action["approval_id"]))["status"] == "expired"
|
||||
assert (await store.actions.get_action(action["id"]))["status"] == "rejected"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_maf_approval_scanner_lifecycle_is_feature_gated():
|
||||
config = MafRoundtableConfig(enabled=True, approval_scan_interval_seconds=5)
|
||||
enabled_app = SimpleNamespace(
|
||||
state=SimpleNamespace(
|
||||
maf_roundtable_service=SimpleNamespace(enabled=True, config=config),
|
||||
)
|
||||
)
|
||||
task = _start_maf_approval_scanner(enabled_app)
|
||||
assert task is not None
|
||||
assert enabled_app.state.maf_approval_scanner_task is task
|
||||
await _stop_maf_approval_scanner(enabled_app)
|
||||
assert task.done()
|
||||
|
||||
disabled_app = SimpleNamespace(
|
||||
state=SimpleNamespace(
|
||||
maf_roundtable_service=SimpleNamespace(enabled=False, config=config),
|
||||
)
|
||||
)
|
||||
assert _start_maf_approval_scanner(disabled_app) is None
|
||||
await _stop_maf_approval_scanner(disabled_app)
|
||||
Reference in new issue
Block a user