Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(fs): protect namespace roots from deletion (#4074)
t0saki1ec848e08713
diff --git a/openviking/storage/viking_fs/_access.py b/openviking/storage/viking_fs/_access.pyindex dc9cd3e6f..362ac3f85 100644--- a/openviking/storage/viking_fs/_access.py+++ b/openviking/storage/viking_fs/_access.py@@ -14,6 +14,7 @@ from openviking.core.namespace import (canonicalize_uri,is_hidden_by_actor_peer_view,may_include_hidden_actor_peers,+ uri_parts,)from openviking.core.namespace import (is_accessible as namespace_is_accessible,@@ -188,7 +189,18 @@ class _AccessMixin:normalized_uri, real_ctx):raise PermissionDeniedError(f"Access denied for {uri}", resource=normalized_uri)- self._ensure_supported_delete_namespace(normalized_uri)+ canonical_uri = canonicalize_uri(normalized_uri, real_ctx)+ self._ensure_supported_delete_namespace(canonical_uri)+ canonical_parts = uri_parts(canonical_uri)+ if real_ctx.role != Role.ROOT and (+ canonical_parts == ["resources"]+ or (canonical_parts[:1] == ["user"] and len(canonical_parts) == 2)+ ):+ raise PermissionDeniedError(+ "Deleting a namespace root requires root access; use a concrete content "+ "path instead.",+ resource=canonical_uri,+ )if real_ctx.role != Role.ROOT and normalized_uri.rstrip("/") == "viking://temp":raise PermissionDeniedError("Temp root is read-only for non-root users",diff --git a/tests/misc/test_vikingfs_uri_guard.py b/tests/misc/test_vikingfs_uri_guard.pyindex aa6eb0f31..47ac343a6 100644--- a/tests/misc/test_vikingfs_uri_guard.py+++ b/tests/misc/test_vikingfs_uri_guard.py@@ -28,10 +28,14 @@ def _make_viking_fs() -> VikingFS:return fs-def _user_ctx(account_id: str = "acct1", user_id: str = "alice") -> RequestContext:+def _user_ctx(+ account_id: str = "acct1",+ user_id: str = "alice",+ role: Role = Role.USER,+) -> RequestContext:return RequestContext(user=UserIdentifier(account_id=account_id, user_id=user_id),- role=Role.USER,+ role=role,)@@ -132,9 +136,37 @@ class TestVikingFSURITraversalGuard:fs.agfs.stat.assert_not_called()fs.agfs.rm.assert_not_called()- @pytest.mark.parametrize("uri", ["viking://resources", "viking://session"])+ @pytest.mark.parametrize(+ "uri",+ [+ "viking://user/alice",+ "viking://user/alice/",+ "viking://resources",+ "viking://resources/",+ ],+ )+ @pytest.mark.parametrize("role", [Role.USER, Role.ADMIN])@pytest.mark.asyncio- async def test_rm_allows_public_scope_roots_to_reach_agfs(self, uri: str) -> None:+ async def test_rm_rejects_non_root_namespace_roots_before_side_effects(+ self, uri: str, role: Role+ ) -> None:+ fs = _make_viking_fs()+ fs._collect_uris = AsyncMock(return_value=[])+ fs._delete_from_vector_store = AsyncMock()++ with pytest.raises(PermissionDeniedError, match="namespace root"):+ await fs.rm(uri, recursive=True, ctx=_user_ctx(role=role))++ fs._collect_uris.assert_not_called()+ fs._delete_from_vector_store.assert_not_called()+ fs.agfs.stat.assert_not_called()+ fs.agfs.rm.assert_not_called()++ @pytest.mark.parametrize(+ "uri", ["viking://user/alice", "viking://resources", "viking://session"]+ )+ @pytest.mark.asyncio+ async def test_rm_allows_maintenance_scope_roots_for_root(self, uri: str) -> None:fs = _make_viking_fs()fs.agfs.stat = AsyncMock(side_effect=RuntimeError("sentinel"))@@ -143,6 +175,24 @@ class TestVikingFSURITraversalGuard:fs.agfs.stat.assert_called_once()+ @pytest.mark.parametrize(+ "uri",+ [+ "viking://user/alice/memories",+ "viking://user/memories",+ "viking://resources/project",+ ],+ )+ @pytest.mark.asyncio+ async def test_rm_allows_non_root_namespace_descendants(self, uri: str) -> None:+ fs = _make_viking_fs()+ fs.agfs.stat = AsyncMock(side_effect=RuntimeError("sentinel"))++ with pytest.raises(RuntimeError, match="sentinel"):+ await fs.rm(uri, recursive=True, ctx=_user_ctx())++ fs.agfs.stat.assert_called_once()+@pytest.mark.asyncioasync def test_rm_session_root_is_scoped_to_current_user_before_agfs(self) -> None:fs = _make_viking_fs()@@ -225,6 +275,33 @@ class TestVikingFSURITraversalGuard:fs.agfs.rm.assert_not_called()fs.agfs.mv.assert_not_called()+ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "source_uri",+ ["viking://user/alice", "viking://resources"],+ )+ async def test_mv_rejects_non_root_namespace_roots_in_source_before_side_effects(+ self, source_uri: str+ ) -> None:+ fs = _make_viking_fs()+ fs._collect_uris = AsyncMock(return_value=[])+ fs._update_vector_store_uris = AsyncMock()+ fs._delete_from_vector_store = AsyncMock()++ with pytest.raises(PermissionDeniedError, match="namespace root"):+ await fs.mv(+ source_uri,+ "viking://resources/dst",+ ctx=_user_ctx(),+ )++ fs._collect_uris.assert_not_called()+ fs._update_vector_store_uris.assert_not_called()+ fs._delete_from_vector_store.assert_not_called()+ fs.agfs.stat.assert_not_called()+ fs.agfs.rm.assert_not_called()+ fs.agfs.mv.assert_not_called()+@pytest.mark.asyncioasync def test_read_file_keeps_valid_uri_behavior(self) -> None:fs = _make_viking_fs()diff --git a/tests/server/test_mcp_endpoint.py b/tests/server/test_mcp_endpoint.pyindex 850e9468f..78afe3813 100644--- a/tests/server/test_mcp_endpoint.py+++ b/tests/server/test_mcp_endpoint.py@@ -45,6 +45,7 @@ from openviking_cli.exceptions import (FailedPreconditionError,InvalidArgumentError,NotFoundError,+ PermissionDeniedError,UnauthenticatedError,)from openviking_cli.session.user_id import UserIdentifier@@ -1050,6 +1051,38 @@ async def test_forget_directory_with_recursive_succeeds(service):assert "deleted" in result.lower()+ ("uri", "sentinel_uri"),+ [+ (+ "viking://user",+ "viking://user/test_user/memories/forget_root_guard.md",+ ),+ (+ "viking://resources",+ "viking://resources/forget_root_guard/sentinel.md",+ ),+ ],+)+async def test_forget_rejects_namespace_roots_for_non_root(service, uri, sentinel_uri):+ ctx = RequestContext(+ user=UserIdentifier.the_default_user("test_user"),+ role=Role.USER,+ )+ parent_uri = sentinel_uri.rsplit("/", 1)[0]+ await service.viking_fs.mkdir(parent_uri, ctx=ctx, exist_ok=True)+ await service.viking_fs.write(sentinel_uri, "must survive", ctx=ctx)++ token = _mcp_ctx.set(ctx)+ try:+ with pytest.raises(PermissionDeniedError, match="namespace root"):+ await forget(uri=uri, recursive=True)+ finally:+ _mcp_ctx.reset(token)++ assert (await service.viking_fs.read(sentinel_uri, ctx=ctx)).decode("utf-8") == "must survive"++# ---------------------------------------------------------------------------# write tool# ---------------------------------------------------------------------------