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)
|
||||
@@ -21,6 +21,25 @@ def _migration_module():
|
||||
return module
|
||||
|
||||
|
||||
def _assert_complete_lease_schema(sync_conn, module) -> None:
|
||||
inspector = inspect(sync_conn)
|
||||
assert module.validate_lease_table_schema(inspector) == []
|
||||
columns = {column["name"]: column for column in inspector.get_columns(module.TABLE)}
|
||||
assert all(columns[name]["nullable"] is False for name in columns)
|
||||
assert inspector.get_pk_constraint(module.TABLE)["constrained_columns"] == ["target_key"]
|
||||
assert any(
|
||||
constraint["column_names"] == ["action_id"]
|
||||
for constraint in inspector.get_unique_constraints(module.TABLE)
|
||||
)
|
||||
assert any(
|
||||
foreign_key["constrained_columns"] == ["action_id"]
|
||||
and foreign_key["referred_table"] == "maf_roundtable_actions"
|
||||
and foreign_key["referred_columns"] == ["id"]
|
||||
and str((foreign_key.get("options") or {}).get("ondelete")).upper() == "CASCADE"
|
||||
for foreign_key in inspector.get_foreign_keys(module.TABLE)
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_phase5_lease_migration_is_additive_and_idempotent():
|
||||
module = _migration_module()
|
||||
@@ -51,6 +70,97 @@ async def test_phase5_lease_migration_is_additive_and_idempotent():
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_empty_incomplete_lease_table_is_rebuilt_with_full_constraints():
|
||||
module = _migration_module()
|
||||
engine = create_async_engine("sqlite+aiosqlite://", echo=False)
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
await conn.execute(text(f"DROP TABLE {module.TABLE}"))
|
||||
await conn.execute(text(f"""
|
||||
CREATE TABLE {module.TABLE} (
|
||||
target_key VARCHAR(255),
|
||||
action_id VARCHAR(64),
|
||||
lease_expires_at DATETIME,
|
||||
created_at DATETIME,
|
||||
updated_at DATETIME
|
||||
)
|
||||
"""))
|
||||
await conn.run_sync(module._migrate_sync, dry_run=False)
|
||||
await conn.run_sync(_assert_complete_lease_schema, module)
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_nonempty_incomplete_lease_table_fails_without_rewriting_data():
|
||||
module = _migration_module()
|
||||
engine = create_async_engine("sqlite+aiosqlite://", echo=False)
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
await conn.execute(text(f"DROP TABLE {module.TABLE}"))
|
||||
await conn.execute(text(f"""
|
||||
CREATE TABLE {module.TABLE} (
|
||||
target_key VARCHAR(255),
|
||||
action_id VARCHAR(64),
|
||||
lease_expires_at DATETIME,
|
||||
created_at DATETIME,
|
||||
updated_at DATETIME
|
||||
)
|
||||
"""))
|
||||
await conn.execute(text(
|
||||
f"INSERT INTO {module.TABLE} "
|
||||
"(target_key, action_id, lease_expires_at, created_at, updated_at) "
|
||||
"VALUES ('opaque-1', 'action-1', '2026-01-01', '2026-01-01', '2026-01-01')"
|
||||
))
|
||||
|
||||
with pytest.raises(RuntimeError, match="has data.*incomplete.*manual"):
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(module._migrate_sync, dry_run=False)
|
||||
|
||||
async with engine.begin() as conn:
|
||||
row = (
|
||||
await conn.execute(text(f"SELECT target_key, action_id FROM {module.TABLE}"))
|
||||
).one()
|
||||
assert tuple(row) == ("opaque-1", "action-1")
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_nonempty_complete_columns_missing_fk_and_unique_constraint_fails():
|
||||
module = _migration_module()
|
||||
engine = create_async_engine("sqlite+aiosqlite://", echo=False)
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
await conn.execute(text(f"DROP TABLE {module.TABLE}"))
|
||||
await conn.execute(text(f"""
|
||||
CREATE TABLE {module.TABLE} (
|
||||
target_key VARCHAR(255) NOT NULL PRIMARY KEY,
|
||||
action_id VARCHAR(64) NOT NULL,
|
||||
lease_expires_at DATETIME NOT NULL,
|
||||
created_at DATETIME NOT NULL,
|
||||
updated_at DATETIME NOT NULL
|
||||
)
|
||||
"""))
|
||||
await conn.execute(text(f"CREATE INDEX {module.ACTION_INDEX} ON {module.TABLE} (action_id)"))
|
||||
await conn.execute(text(f"CREATE INDEX {module.EXPIRY_INDEX} ON {module.TABLE} (lease_expires_at)"))
|
||||
await conn.execute(text(
|
||||
f"INSERT INTO {module.TABLE} "
|
||||
"(target_key, action_id, lease_expires_at, created_at, updated_at) "
|
||||
"VALUES ('opaque-2', 'action-2', '2026-01-01', '2026-01-01', '2026-01-01')"
|
||||
))
|
||||
|
||||
with pytest.raises(RuntimeError) as exc_info:
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(module._migrate_sync, dry_run=False)
|
||||
message = str(exc_info.value)
|
||||
assert "foreign key" in message
|
||||
assert "unique constraint" in message
|
||||
|
||||
async with engine.begin() as conn:
|
||||
assert (await conn.execute(text(f"SELECT COUNT(*) FROM {module.TABLE}"))).scalar_one() == 1
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_phase5_migration_dry_run_is_safe_when_table_is_absent():
|
||||
module = _migration_module()
|
||||
|
||||
@@ -4,6 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import threading
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
@@ -13,8 +14,10 @@ from langchain_core.language_models.chat_models import BaseChatModel
|
||||
from langchain_core.messages import AIMessage
|
||||
from langchain_core.outputs import ChatGeneration, ChatResult
|
||||
from langgraph.checkpoint.memory import InMemorySaver
|
||||
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.routers.maf_roundtable import router as maf_router
|
||||
from app.maf_roundtable.dispatcher import get_dispatcher
|
||||
from app.maf_roundtable.write_guard_bridge import register_write_guard_service
|
||||
@@ -24,6 +27,7 @@ from deerflow.config.tool_config import ToolConfig
|
||||
from deerflow.config.write_guard_config import WriteGuardConfig, WriteGuardProviderConfig
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.maf_roundtable import make_maf_roundtable_store
|
||||
from deerflow.persistence.maf_roundtable.model import MafRoundtableApprovalRow
|
||||
from deerflow.runtime import RunManager
|
||||
from deerflow.runtime.events.store.memory import MemoryRunEventStore
|
||||
from deerflow.runtime.stream_bridge.memory import MemoryStreamBridge
|
||||
@@ -115,7 +119,7 @@ def _app_config():
|
||||
enabled=True,
|
||||
default_write_policy="confirm_all_writes",
|
||||
approval_timeout_seconds=300,
|
||||
approval_scan_interval_seconds=1,
|
||||
approval_scan_interval_seconds=5,
|
||||
tool_metadata={
|
||||
TEST_TOOL: MafToolMetadata(
|
||||
side_effect="write",
|
||||
@@ -266,6 +270,39 @@ async def test_entry_http_approval_reaches_real_toolnode_once(tmp_path, determin
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_entry_approval_scanner_expires_and_finishes_without_tool(tmp_path, deterministic_agent):
|
||||
app, service, engine = await _make_app(tmp_path, deterministic_agent)
|
||||
dispatcher = get_dispatcher(app)
|
||||
try:
|
||||
transport = ASGITransport(app=app)
|
||||
async with AsyncClient(transport=transport, base_url="http://test") as client:
|
||||
_session_id, run = await _create_waiting_run(
|
||||
client, dispatcher, task_text="scanner expiry", service=service
|
||||
)
|
||||
approval = (await client.get(
|
||||
f"/api/maf-roundtable/runs/{run['id']}/approvals"
|
||||
)).json()["approvals"][0]
|
||||
async with service.store.session_factory() as db:
|
||||
await db.execute(
|
||||
update(MafRoundtableApprovalRow)
|
||||
.where(MafRoundtableApprovalRow.id == approval["id"])
|
||||
.values(expires_at=datetime.now(UTC) - timedelta(seconds=1))
|
||||
)
|
||||
await db.commit()
|
||||
|
||||
assert await _scan_maf_approval_expiry(app) == [run["id"]]
|
||||
final_run = (await client.get(f"/api/maf-roundtable/runs/{run['id']}")).json()
|
||||
assert final_run["status"] == "completed"
|
||||
action = (await client.get(
|
||||
f"/api/maf-roundtable/runs/{run['id']}/actions"
|
||||
)).json()["actions"][0]
|
||||
assert action["status"] == "rejected"
|
||||
assert not (tmp_path / "phase5-target.jsonl").exists()
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_entry_http_same_target_waits_for_explicit_resume(tmp_path, deterministic_agent):
|
||||
app, service, engine = await _make_app(tmp_path, deterministic_agent)
|
||||
|
||||
@@ -182,12 +182,14 @@ async def test_http_approval_dispatches_real_tool_once(monkeypatch, tmp_path):
|
||||
listed_action = listed.json()["actions"][0]
|
||||
assert "target_key" not in listed_action
|
||||
assert "idempotency_key" not in listed_action
|
||||
assert "internal_canary" not in listed_action
|
||||
|
||||
fetched = await client.get(f"/api/maf-roundtable/actions/{action['id']}")
|
||||
assert fetched.status_code == 200
|
||||
fetched_action = fetched.json()
|
||||
assert "target_key" not in fetched_action
|
||||
assert "idempotency_key" not in fetched_action
|
||||
assert "internal_canary" not in fetched_action
|
||||
|
||||
# The lease arbiter still needs the private key in persistence.
|
||||
stored_before_decision = await store.actions.get_action(action["id"])
|
||||
|
||||
Reference in new issue
Block a user