Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(acl): 简化复制、移动、删除和 OVPack 导入的权限检查 (#6673)

Qin Haojief09ca9780624
diff --git a/docs/en/api/14-ovpack.md b/docs/en/api/14-ovpack.md
index 50cc876c7..657cd81c7 100644
--- a/docs/en/api/14-ovpack.md
+++ b/docs/en/api/14-ovpack.md
@@ -158,7 +158,7 @@ Imports a `.ovpack` file to a specified location for restoring or migrating data
| on_conflict | string | No | fail | Conflict policy: `fail`, `overwrite`, or `skip` |
| vector_mode | string | No | auto | Vector handling: `auto`, `recompute`, or `require` |
-**Permission Requirements**: ROOT, ADMIN, or USER
+**Permission Requirements**: ROOT, ADMIN, or USER. When ACL is enabled, importing to a new path requires `write` on its parent; overwriting an existing directory requires `manage` on that directory, without checking descendants individually.
**Behavior Notes**:
- The API no longer accepts `vectorize` or `force`.
diff --git a/docs/en/concepts/15-acl.md b/docs/en/concepts/15-acl.md
index e97e3e48a..18654528f 100644
--- a/docs/en/concepts/15-acl.md
+++ b/docs/en/concepts/15-acl.md
@@ -95,9 +95,9 @@ All filesystem APIs use the same permission mapping:
| read, stat, list, tree, find, search, grep, glob | read |
| write, create, mkdir, set tags | write |
| delete or move a file | manage |
-| delete or move a directory | manage on the directory and complete subtree |
+| delete or move a directory | manage on that directory, authorizing its entire subtree including restricted descendants |
| manage ACL | manage |
-| move destination parent | write |
+| copy or move destination | write on the parent for a new path; write on an existing file; manage on an existing directory |
The server canonicalizes the URI, then uses one authorization entry point for account/owner/actor-peer boundaries, effective ACLs when enabled or existing namespace rules when disabled, and write/delete namespace guards.
@@ -107,9 +107,9 @@ inside a restricted directory only inherits the parent’s permissions.
An ACL grant on a directory is inherited by every descendant. `list`, `tree`, and other batch results still check every returned node because an open directory can contain independently restricted descendants.
-Within the shared scope, a moved node keeps its direct ACL and restricted state, then recalculates inherited permissions from its new parent. A private resource moved into the shared scope carries no ACL and inherits the destination directory; a shared resource moved back to a private area has its ACL cleared.
+Copies and moves do not carry source ACLs: existing destinations retain their ACLs, new destinations inherit their destination parent’s ACL, and private destinations have no ACL.
-Recursive tag updates, directory deletion, and directory moves validate the complete affected subtree first. The operation stops if any node lacks the required capability or the subtree cannot be scanned completely.
+Delete and move authorize the specified file or directory without checking descendants individually. Copy queries for unreadable records within the directory and rejects the whole copy after confirming the path exists and its current ACL denies read access; destination descendants are not checked individually. Recursive tag updates still validate the complete subtree and stop on insufficient permission or an incomplete scan.
For a directory, `stat.count` uses the same path and ACL scalar filter and reports the number of context records visible to the caller.
diff --git a/docs/zh/api/14-ovpack.md b/docs/zh/api/14-ovpack.md
index 8d4ffdb23..c389e31d1 100644
--- a/docs/zh/api/14-ovpack.md
+++ b/docs/zh/api/14-ovpack.md
@@ -158,7 +158,7 @@ ov export viking://resources/my-project/ ./exports/my-project.ovpack --include-v
| on_conflict | string | 否 | fail | 冲突策略:`fail`、`overwrite` 或 `skip` |
| vector_mode | string | 否 | auto | 向量处理方式:`auto`、`recompute` 或 `require` |
-**权限要求**:ROOT、ADMIN 或 USER
+**权限要求**:ROOT、ADMIN 或 USER;开启 ACL 时,新路径导入要求目标父目录 `write`,覆盖已有目录要求该目录 `manage`;授权后不逐个检查子节点。
**行为说明**:
- API 已不再接受 `vectorize` 或 `force`。
diff --git a/docs/zh/concepts/15-acl.md b/docs/zh/concepts/15-acl.md
index bd00ad698..84732ab91 100644
--- a/docs/zh/concepts/15-acl.md
+++ b/docs/zh/concepts/15-acl.md
@@ -91,9 +91,9 @@ read user:carol on viking://resources/A/B/C/report.md
| read、stat、list、tree、find、search、grep、glob | read |
| write、create、mkdir、set tags | write |
| 删除或移动文件 | manage |
-| 删除或移动目录 | 目录及完整子树的 manage |
+| 删除或移动目录 | 该目录的 manage(授权整个子树,包括 restricted 后代) |
| 管理 ACL | manage |
-| move 目标父目录 | write |
+| cp、move 目标 | 新路径要求父目录 write;已有文件要求文件 write;已有目录要求目录 manage |
服务端会先 canonicalize URI,再在同一个鉴权入口中依次执行 account/owner/actor peer 等硬边界、开启时的有效 ACL 或关闭时的原有 namespace 规则,以及写入和删除的 namespace 防护。
@@ -102,9 +102,9 @@ read user:carol on viking://resources/A/B/C/report.md
目录上的 ACL 授权会被所有后代继承。`list`、`tree` 和批量结果仍逐个检查有效 ACL,因为默认开放的目录下可能存在独立的 restricted 边界。
-共享区内部移动时,节点自己的 direct ACL 和 restricted 状态随节点移动,inherited 按新父节点重新计算。个人资源移入共享区时不携带 ACL,只继承目标目录权限;共享资源移回个人区时清空 ACL。
+复制和移动不携带源 ACL:已有目标保留自己的 ACL,新建目标继承目标父目录的 ACL;移入个人区时清空 ACL。
-递归修改 tags、删除或移动目录会先校验完整目标子树。任一节点缺少所需能力,或子树扫描不完整,操作都会整体中止。
+删除和移动检查指定文件或目录的权限,授权后不逐个检查子节点。复制按目录范围查询不可读内容,核实命中路径存在且当前权限确实不可读后,拒绝整次复制;不逐个检查目标子节点的写入权限。递归修改 tags 仍检查完整子树,权限不足或扫描不完整时整体中止。
目录 `stat` 的 `count` 使用相同的路径和 ACL 标量过滤,表示当前用户可见的 context 数量。
diff --git a/openviking/service/fs_service.py b/openviking/service/fs_service.py
index e233fdbe3..afb074c39 100644
--- a/openviking/service/fs_service.py
+++ b/openviking/service/fs_service.py
@@ -9,7 +9,7 @@ Provides file system operations: ls, mkdir, rm, mv, tree, stat, read, abstract,
import asyncio
from collections.abc import Coroutine
from contextlib import asynccontextmanager
-from dataclasses import dataclass
+from dataclasses import dataclass, replace
from functools import partial
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Dict, List, Literal, Optional
@@ -577,9 +577,10 @@ class FSService:
"""Remove resource."""
viking_fs = self._ensure_initialized()
await viking_fs._ensure_access(uri, ctx, action=AclAction.MANAGE)
+ delete_ctx = replace(ctx, bypass_acl=True)
namespace_policy = namespace_policy_for_uri(uri)
if namespace_policy.processing_mode is NamespaceProcessingMode.FILE_ONLY:
- result = await viking_fs.rm(uri, recursive=recursive, ctx=ctx, no_vector=True)
+ result = await viking_fs.rm(uri, recursive=recursive, ctx=delete_ctx, no_vector=True)
if isinstance(result, dict):
result["semantic_status"] = "skipped"
result["vector_status"] = "skipped"
@@ -596,7 +597,7 @@ class FSService:
result = await viking_fs.rm(
uri,
recursive=recursive,
- ctx=ctx,
+ ctx=delete_ctx,
**({"lease_ref": lease} if lease is not None else {}),
)
await self.compile_versions.record_effects(
@@ -616,7 +617,7 @@ class FSService:
else await record_remove()
)
else:
- result = await viking_fs.rm(uri, recursive=recursive, ctx=ctx)
+ result = await viking_fs.rm(uri, recursive=recursive, ctx=delete_ctx)
await self._sync_watch_after_rm(uri, account_id=ctx.account_id, context_type=context_type)
# A refresh on a parent that no longer exists would lock its sidecar
# paths and thereby recreate the deleted directory. Nothing to
@@ -1073,6 +1074,8 @@ class FSService:
"""Commit move/watch state and enqueue all affected parent refreshes."""
viking_fs = self._ensure_initialized()
await viking_fs._ensure_access(from_uri, ctx, action=AclAction.MANAGE)
+ await viking_fs._ensure_destination_access(to_uri, ctx)
+ move_ctx = replace(ctx, bypass_acl=True)
watch_manager = self._get_watch_manager()
use_watch_transaction = (
watch_manager is not None
@@ -1102,17 +1105,17 @@ class FSService:
await watch_manager.validate_target_prefix_rewrite_internal(
from_uri, to_uri, account_id=ctx.account_id
)
- async with self._record_transfer(from_uri, to_uri, ctx, move=True) as lease:
+ async with self._record_transfer(from_uri, to_uri, move_ctx, move=True) as lease:
options = {"lease_ref": lease} if lease is not None else {}
if vector_mode != "transfer":
options.update(
vector_mode=vector_mode,
copy_semantic_sidecars=False,
)
- await viking_fs.mv(
+ result = await viking_fs.mv(
from_uri,
to_uri,
- ctx=ctx,
+ ctx=move_ctx,
**options,
)
if use_watch_transaction:
@@ -1122,7 +1125,13 @@ class FSService:
)
except Exception as commit_error:
try:
- await viking_fs.mv(to_uri, from_uri, ctx=ctx, **options)
+ await viking_fs.mv(
+ to_uri,
+ from_uri,
+ ctx=move_ctx,
+ restore_acl=result.get("source_acl", {}),
+ **options,
+ )
except Exception as rollback_error:
logger.exception("Move rollback failed: %s -> %s", to_uri, from_uri)
raise rollback_error from commit_error
@@ -1212,7 +1221,9 @@ class FSService:
await viking_fs._ensure_access(from_uri, ctx, action=AclAction.WRITE)
else:
await viking_fs._ensure_copy_source_access(from_uri, recursive=recursive, ctx=ctx)
- await viking_fs._ensure_access(to_uri, ctx, action=AclAction.WRITE)
+ await viking_fs._ensure_access(
+ to_uri, replace(ctx, bypass_acl=True), action=AclAction.WRITE
+ )
source_path = viking_fs._uri_to_path(from_uri, ctx=ctx)
target_path = viking_fs._uri_to_path(to_uri, ctx=ctx)
viking_fs._validate_transfer_paths(source_path, target_path)
diff --git a/openviking/service/resource_service.py b/openviking/service/resource_service.py
index b444c3e51..1e4213a63 100644
--- a/openviking/service/resource_service.py
+++ b/openviking/service/resource_service.py
@@ -14,7 +14,7 @@ import os
import signal
import time
from collections.abc import Awaitable, Callable
-from dataclasses import dataclass, field
+from dataclasses import dataclass, field, replace
from pathlib import Path
from types import SimpleNamespace
from typing import TYPE_CHECKING, Any, Dict, List, Optional
@@ -678,6 +678,7 @@ class ResourceService:
resource_lock: Dict[str, Any],
) -> bool:
"""Remove a newly reserved target only while it is still empty."""
+ ctx = replace(ctx, bypass_acl=True)
try:
if not await self._viking_fs.exists(root_uri, ctx=ctx):
return True
diff --git a/openviking/storage/acl.py b/openviking/storage/acl.py
index 43ab1c3f2..1f4ad37fb 100644
--- a/openviking/storage/acl.py
+++ b/openviking/storage/acl.py
@@ -4,14 +4,14 @@ from __future__ import annotations
from dataclasses import dataclass, replace
from enum import Enum
-from typing import TYPE_CHECKING, Any, Iterable, Literal, Mapping, Protocol, Sequence
+from typing import TYPE_CHECKING, Any, AsyncIterator, Iterable, Literal, Mapping, Protocol, Sequence
from pydantic import BaseModel, ConfigDict, model_validator
from openviking.core.identifiers import validate_identifier_part, validate_user_id
from openviking.core.namespace import uri_parts
from openviking.server.identity import RequestContext, Role
-from openviking.storage.expr import FilterExpr, Or, PathScope
+from openviking.storage.expr import And, Eq, FilterExpr, In, Or, PathScope, RawDSL
from openviking_cli.exceptions import InvalidArgumentError
if TYPE_CHECKING:
@@ -240,6 +240,31 @@ def acl_grant_tokens(principals: Iterable[str], action: AclAction) -> list[str]:
]
+def acl_filter(ctx: RequestContext, action: AclAction, *, denied: bool = False) -> FilterExpr:
+ """Filter shared-resource ACL records; callers handle role and namespace bypasses."""
+ grants = acl_grant_tokens(acl_principals(ctx), action)
+ controlled_modes = [AclMode.INHERIT.value, AclMode.RESTRICTED.value]
+ grant_op = "must_not" if denied else "must"
+ direct = RawDSL({"op": grant_op, "field": "acl_direct_grants", "conds": grants})
+ inherited = RawDSL({"op": grant_op, "field": "acl_inherited_grants", "conds": grants})
+ if denied:
+ return And(
+ [
+ In(ACL_MODE_FIELD, controlled_modes),
+ direct,
+ Or([Eq(ACL_MODE_FIELD, AclMode.RESTRICTED.value), inherited]),
+ ]
+ )
+ # Uncontrolled records retain the shared root's default user:* manage grant.
+ return Or(
+ [
+ RawDSL({"op": "must_not", "field": ACL_MODE_FIELD, "conds": controlled_modes}),
+ direct,
+ And([Eq(ACL_MODE_FIELD, AclMode.INHERIT.value), inherited]),
+ ]
+ )
+
+
def _normalize_entries(entries: Iterable[AclEntry | Mapping[str, Any]]) -> list[AclEntry]:
highest: dict[str, AclLevel] = {}
for raw in entries:
@@ -368,6 +393,38 @@ class AclManager:
if cursor is None:
return records
+ async def iter_denied_uris(
+ self, uri: str, ctx: RequestContext, *, action: AclAction
+ ) -> AsyncIterator[str]:
+ """Find denied records in a subtree, validating each URI's current ACL snapshot."""
+ scope = And(
+ [
+ Eq("account_id", ctx.account_id),
+ PathScope("uri", uri, depth=-1),
+ acl_filter(ctx, action, denied=True),
+ ]
+ )
+ cursor: str | None = None
+ checked: set[str] = set()
+ while True:
+ records, cursor = await self._context_store.scroll(
+ filter=scope,
+ limit=1,
+ cursor=cursor,
+ output_fields=["uri"],
+ ctx=replace(ctx, bypass_acl=True),
+ )
+ for record in records:
+ candidate = record["uri"]
+ if candidate in checked:
+ continue
+ checked.add(candidate)
+ # Index levels can carry older snapshots for the same path.
+ if not acl_allows(await self.resolve(candidate, ctx), ctx, action):
+ yield candidate
+ if cursor is None:
+ return
+
async def _subtree_records(self, uri: str, ctx: RequestContext) -> list[dict[str, Any]]:
refs = await self._scroll_all(PathScope("uri", uri, depth=-1), ["id"], ctx)
ids = [str(record["id"]) for record in refs if record.get("id")]
@@ -462,30 +519,6 @@ class AclManager:
for record in records
]
- async def materialize_moved_record(
- self, record: Mapping[str, Any], new_uri: str, ctx: RequestContext
- ) -> dict[str, Any]:
- if not is_acl_uri(new_uri):
- return EffectiveAcl(AclMode.NONE, DirectAcl(), DirectAcl()).context_fields()
- ancestors = acl_ancestors(new_uri)
- parent = ancestors[-2] if len(ancestors) > 1 else None
- parent_acl = (
- await self.resolve(parent, ctx)
- if parent
- else EffectiveAcl(AclMode.NONE, DirectAcl(), DirectAcl())
- )
- inherited = parent_acl.permissions
- source_uri = str(record.get("uri") or "")
- direct = (
- DirectAcl.from_context_fields(record, "acl_direct")
- if is_acl_uri(source_uri)
- else DirectAcl()
- )
- mode = self._effective_from_record(record).mode if is_acl_uri(source_uri) else AclMode.NONE
- return EffectiveAcl.from_permissions(
- direct, inherited, mode=mode, parent_mode=parent_acl.mode
- ).context_fields()
-
async def _apply_subtree(
self,
root_uri: str,
diff --git a/openviking/storage/ovpack/operations.py b/openviking/storage/ovpack/operations.py
index 3e5c0c731..ee7f0f1eb 100644
--- a/openviking/storage/ovpack/operations.py
+++ b/openviking/storage/ovpack/operations.py
@@ -6,6 +6,7 @@ import asyncio
import json
import os
import zipfile
+from dataclasses import replace
from typing import Any, Optional
from openviking.core.namespace import (
@@ -310,7 +311,9 @@ async def import_ovpack(
index_records = validate_manifest_content(zf, manifest, infolist, base_name)
dense_vectors = read_dense_vectors(zf, manifest, base_name, index_records)
- existing_roots = [root_uri] if await _root_exists(viking_fs, root_uri, ctx) else []
+ target_exists = await viking_fs._ensure_destination_access(root_uri, ctx)
+ import_ctx = replace(ctx, bypass_acl=True)
+ existing_roots = [root_uri] if target_exists else []
if existing_roots:
if conflict_action == "skip":
logger.info(f"[ovpack] Skipped existing resource at {root_uri}")
@@ -334,23 +337,23 @@ async def import_ovpack(
ctx=ctx,
)
- if parent != "viking://":
+ if parent != "viking://" and not target_exists:
await _ensure_parent_exists(viking_fs, parent, ctx)
for existing_root in existing_roots:
logger.info(f"[ovpack] Overwriting existing resource at {existing_root}")
- await _remove_existing_root(viking_fs, existing_root, ctx)
+ await _remove_existing_root(viking_fs, existing_root, import_ctx)
for _, safe_zip_path, kind, rel_path in members:
if kind in {"manifest", "internal"}:
continue
if kind == "directory":
- await viking_fs.mkdir(join_uri(root_uri, rel_path), exist_ok=True, ctx=ctx)
+ await viking_fs.mkdir(join_uri(root_uri, rel_path), exist_ok=True, ctx=import_ctx)
continue
target_file_uri = join_uri(root_uri, rel_path)
data = zf.read(safe_zip_path)
- await viking_fs.write_file_bytes(target_file_uri, data, ctx=ctx)
+ await viking_fs.write_file_bytes(target_file_uri, data, ctx=import_ctx)
logger.info(f"[ovpack] Successfully imported {file_path} to {root_uri}")
@@ -362,14 +365,14 @@ async def import_ovpack(
index_records,
dense_vectors,
manifest_entries_by_path(manifest),
- ctx,
+ import_ctx,
)
logger.info(f"[ovpack] Restored vector snapshot for: {root_uri}")
else:
await _enqueue_direct_vectorization(
viking_fs,
root_uri,
- ctx=ctx,
+ ctx=import_ctx,
index_records=index_records,
)
logger.info(f"[ovpack] Enqueued direct vectorization for: {root_uri}")
diff --git a/openviking/storage/vector_migration.py b/openviking/storage/vector_migration.py
index 1bd328dbc..bf1e45389 100644
--- a/openviking/storage/vector_migration.py
+++ b/openviking/storage/vector_migration.py
@@ -17,6 +17,7 @@ from openviking.storage.abstract_overview import (
rewrite_abstract_overview_for_transfer,
rewrite_viking_uri_references,
)
+from openviking.storage.acl import ACL_CONTEXT_FIELDS
from openviking.storage.expr import And, Contains, Eq, Or, PathScope
from openviking.storage.vector_ids import vector_record_id
from openviking_cli.session.user_id import UserIdentifier
@@ -82,7 +83,11 @@ def rewrite_vector_record(
raise ValueError("Vector record is missing a string URI")
rewritten_uri = rewrite_transfer_uri(record_uri, source_uri, target_uri)
- payload = {key: value for key, value in record.items() if key != "_score"}
+ payload = {
+ key: value
+ for key, value in record.items()
+ if key != "_score" and key not in ACL_CONTEXT_FIELDS
+ }
owner_fields = owner_fields_for_uri(rewritten_uri)
payload.update(
{
diff --git a/openviking/storage/viking_fs/_access.py b/openviking/storage/viking_fs/_access.py
index d0a4f5a78..942b24cfc 100644
--- a/openviking/storage/viking_fs/_access.py
+++ b/openviking/storage/viking_fs/_access.py
@@ -245,6 +245,34 @@ class _AccessMixin:
) -> None:
await self._ensure_access_many([uri], ctx, action=action)
+ async def _ensure_destination_access(self, uri: str, ctx: Optional[RequestContext]) -> bool:
+ """Authorize copy/move/import destinations and return whether the target exists."""
+ try:
+ stat = await self._async_agfs.stat(self._uri_to_path(uri, ctx=ctx))
+ except Exception as exc:
+ if not is_not_found_error(exc):
+ raise
+ parent = VikingURI(uri).parent
+ if parent is None:
+ raise InvalidArgumentError(f"Destination has no parent: {uri}") from exc
+ checked_uri, action, exists = parent.uri, AclAction.WRITE, False
+ else:
+ checked_uri = uri
+ action = AclAction.MANAGE if stat["isDir"] else AclAction.WRITE
+ exists = True
+
+ # Non-ACL namespaces authorize the target path using their fixed policy.
+ if not is_acl_uri(uri):
+ checked_uri = uri
+ # Capability checks must not apply the namespace-root deletion guard.
+ access = await self._can_access_many([checked_uri], ctx, action=action)
+ if not access[checked_uri]:
+ raise PermissionDeniedError(
+ f"Access denied for {checked_uri}: {action.value} permission required",
+ resource=checked_uri,
+ )
+ return exists
+
async def _ensure_access_many(
self,
uris: Sequence[str],
@@ -295,6 +323,26 @@ class _AccessMixin:
resource=uri,
)
+ async def _ensure_descendant_access(
+ self, uri: str, ctx: Optional[RequestContext], *, action: AclAction
+ ) -> None:
+ """Check shared descendants after the requested node has been authorized."""
+ real_ctx = self._ctx_or_default(ctx)
+ if (
+ not is_acl_uri(uri)
+ or real_ctx.bypass_acl
+ or has_implicit_manage(real_ctx, uri)
+ or not await self._acl_enabled(real_ctx)
+ ):
+ return
+ async for denied in self.acl_manager.iter_denied_uris(uri, real_ctx, action=action):
+ # A leftover index entry or virtual chunk is not copied filesystem content.
+ if await self.exists(denied, ctx=real_ctx):
+ raise PermissionDeniedError(
+ f"Access denied for {denied}: {action.value} permission required",
+ resource=denied,
+ )
+
async def _ensure_retrieval_scope(self, uri: str, ctx: Optional[RequestContext]) -> None:
self._safe_uri_parts(uri)
if await self._acl_enabled(ctx) and is_acl_uri(uri):
diff --git a/openviking/storage/viking_fs/_ops.py b/openviking/storage/viking_fs/_ops.py
index 8ddd1fd7b..876ff6b7b 100644
--- a/openviking/storage/viking_fs/_ops.py
+++ b/openviking/storage/viking_fs/_ops.py
@@ -324,6 +324,7 @@ class _OpsMixin:
check_descendant_acl = (
is_dir
and (not no_vector or is_acl_uri(target_uri))
+ and not self._ctx_or_default(ctx).bypass_acl
and await self._acl_enabled(ctx)
)
uris_to_delete = (
@@ -418,7 +419,9 @@ class _OpsMixin:
old_uri = self._normalize_transfer_uri(old_uri)
new_uri = self._normalize_transfer_uri(new_uri)
await self._ensure_copy_source_access(old_uri, recursive=recursive, ctx=ctx)
- await self._ensure_access(new_uri, ctx, action=AclAction.WRITE)
+ await self._ensure_destination_access(new_uri, ctx)
+ copy_ctx = replace(self._ctx_or_default(ctx), bypass_acl=True)
+ await self._ensure_access(new_uri, copy_ctx, action=AclAction.WRITE)
old_scope = old_uri.rstrip("/")
new_scope = new_uri.rstrip("/")
if old_scope == new_scope:
@@ -465,6 +468,8 @@ class _OpsMixin:
)
operation_id = uuid.uuid4().hex
try:
+ if is_dir:
+ await self._ensure_descendant_access(old_uri, ctx, action=AclAction.READ)
source_uris = await self._prepare_transfer_entries(
old_uri, new_uri, is_dir=is_dir, move=False, ctx=ctx
)
@@ -474,7 +479,7 @@ class _OpsMixin:
old_uri=old_uri,
new_uri=new_uri,
is_dir=is_dir,
- ctx=ctx,
+ ctx=copy_ctx,
lease_ref=lease,
copy_semantic_sidecars=copy_semantic_sidecars,
)
@@ -485,13 +490,13 @@ class _OpsMixin:
old_uri,
new_uri,
recursive=is_dir,
- ctx=ctx,
+ ctx=copy_ctx,
**({"source_uris": source_uris} if is_dir else {}),
)
except Exception:
try:
await self._cleanup_transfer_target(
- new_path, is_dir=is_dir, ctx=ctx, lease_ref=lease
+ new_path, is_dir=is_dir, ctx=copy_ctx, lease_ref=lease
)
except Exception:
logger.warning("Failed to clean copy target %s", new_uri, exc_info=True)
@@ -549,6 +554,8 @@ class _OpsMixin:
) -> List[str]:
"""Validate all affected entries under the lease before the first content write."""
source_uris: List[str] = []
+ # Copy ACLs were checked under the lease; retain per-path namespace guards.
+ guard_ctx = ctx if move else replace(self._ctx_or_default(ctx), bypass_acl=True)
async def visit(source: str, target: str, directory: bool) -> None:
source_path = self._uri_to_path(source, ctx=ctx)
@@ -559,8 +566,8 @@ class _OpsMixin:
if directory or source != old_uri:
await self._ensure_access(source, ctx, action=AclAction.MANAGE)
else:
- await self._ensure_copy_source_access(source, recursive=directory, ctx=ctx)
- await self._ensure_access(target, ctx, action=AclAction.WRITE)
+ await self._ensure_copy_source_access(source, recursive=directory, ctx=guard_ctx)
+ await self._ensure_access(target, guard_ctx, action=AclAction.WRITE)
await self._ensure_transfer_target_type(
self._uri_to_path(target, ctx=ctx), target, is_dir=directory
)
@@ -719,6 +726,7 @@ class _OpsMixin:
*,
vector_mode: Literal["transfer", "delete_source", "none"] = "transfer",
copy_semantic_sidecars: bool = True,
+ restore_acl: Dict[str, Dict[str, Any]] | None = None,
) -> Dict[str, Any]:
"""Move file/directory while extending an optional outer pathlock lease.
@@ -728,8 +736,6 @@ class _OpsMixin:
old_uri = self._normalize_transfer_uri(old_uri)
new_uri = self._normalize_transfer_uri(new_uri)
- acl_manager = self.acl_manager
- acl_enabled = await self._acl_enabled(ctx)
guard_ctx = replace(self._ctx_or_default(ctx), bypass_acl=True)
await self._ensure_access(old_uri, guard_ctx, action=AclAction.MANAGE)
await self._ensure_access(old_uri, ctx, action=AclAction.WRITE)
@@ -743,7 +749,6 @@ class _OpsMixin:
old_path = self._uri_to_path(old_uri, ctx=ctx)
new_path = self._uri_to_path(new_uri, ctx=ctx)
self._validate_transfer_paths(old_path, new_path)
- new_acl_scope = acl_enabled and is_acl_uri(new_uri)
# Verify source exists and determine type before locking.
try:
@@ -837,7 +842,6 @@ class _OpsMixin:
raise
# Update or remove source VectorDB records before deleting the source.
- vector_transfer_completed = False
try:
vector_result = None
if vector_mode == "transfer":
@@ -847,34 +851,13 @@ class _OpsMixin:
recursive=is_dir,
ctx=ctx,
**({"source_uris": uris_to_move} if is_dir else {}),
+ **({"restore_acl": restore_acl} if restore_acl is not None else {}),
)
- vector_transfer_completed = True
elif vector_mode == "delete_source":
await self._delete_from_vector_store(uris_to_move, ctx=ctx)
elif vector_mode != "none":
raise ValueError(f"unsupported move vector mode: {vector_mode}")
- if vector_mode == "transfer" and acl_manager is not None and new_acl_scope:
- await acl_manager.refresh_context_subtree(
- new_uri,
- self._ctx_or_default(ctx),
- )
except Exception:
- if vector_transfer_completed:
- try:
- moved_uris = [
- new_scope + uri.rstrip("/")[len(old_scope) :] for uri in uris_to_move
- ]
- await self._update_vector_store_uris(
- new_uri,
- old_uri,
- recursive=is_dir,
- ctx=ctx,
- **({"source_uris": moved_uris} if is_dir else {}),
- )
- except Exception:
- logger.warning(
- "Failed to restore move vectors for %s", old_uri, exc_info=True
- )
try:
await self._cleanup_transfer_target(
new_path, is_dir=is_dir, ctx=ctx, lease_ref=lease
@@ -898,6 +881,7 @@ class _OpsMixin:
"files_deleted": files_created,
}
if vector_result is not None:
+ result["source_acl"] = vector_result.source_acl
result["vectors"] = {
"scanned": vector_result.scanned,
"written": vector_result.written,
diff --git a/openviking/storage/viking_fs/_vector.py b/openviking/storage/viking_fs/_vector.py
index 532dc27a3..b1f5c62e5 100644
--- a/openviking/storage/viking_fs/_vector.py
+++ b/openviking/storage/viking_fs/_vector.py
@@ -65,6 +65,7 @@ class _VectorMixin:
recursive: bool,
ctx: Optional[RequestContext] = None,
source_uris: List[str] | None = None,
+ restore_acl: dict[str, dict[str, Any]] | None = None,
) -> Any:
"""Move a vector URI scope, overwriting matching target records.
@@ -80,6 +81,7 @@ class _VectorMixin:
recursive=recursive,
target_entry_exists=partial(self.exists, ctx=ctx), # type: ignore[attr-defined]
**({"source_uris": source_uris} if source_uris is not None else {}),
+ **({"restore_acl": restore_acl} if restore_acl is not None else {}),
)
def _get_vector_store(self) -> Optional["VikingVectorIndexBackend"]:
diff --git a/openviking/storage/viking_vector_index_backend.py b/openviking/storage/viking_vector_index_backend.py
index e8197a6cb..a2323685b 100644
--- a/openviking/storage/viking_vector_index_backend.py
+++ b/openviking/storage/viking_vector_index_backend.py
@@ -9,7 +9,7 @@ import re
import threading
import uuid
from contextlib import asynccontextmanager
-from dataclasses import dataclass
+from dataclasses import dataclass, field
from datetime import datetime, timezone
from functools import wraps
from typing import Any, AsyncIterator, Awaitable, Callable, Container, Dict, List, Mapping, Optional
@@ -27,10 +27,8 @@ from openviking.storage.acl import (
ACL_MODE_FIELD,
AclAction,
AclManager,
- AclMode,
AclUpdate,
- acl_grant_tokens,
- acl_principals,
+ acl_filter,
is_acl_uri,
)
from openviking.storage.expr import And, Eq, FilterExpr, In, Or, PathScope, RawDSL
@@ -149,6 +147,7 @@ class VectorTransferResult:
deleted: int = 0
restored: int = 0
batches: int = 0
+ source_acl: Dict[str, Dict[str, Any]] = field(default_factory=dict)
class VectorTransferRollbackError(RuntimeError):
@@ -2839,7 +2838,6 @@ class VikingVectorIndexBackend:
*,
recursive: bool,
source_uris: List[str] | None,
- preserve_target_acl: bool = False,
target_entry_exists: Callable[[str], Awaitable[bool]] | None = None,
) -> tuple[List[Dict[str, Any]], int, Dict[str, Dict[str, Any]]]:
"""Read selected source records and remove affected target records."""
@@ -2896,9 +2894,8 @@ class VikingVectorIndexBackend:
)
# A chunk cannot carry ACL for its base file URI. Keep the old main record
# when copy has no main record to replace it, accepting its stale content.
- acl_enabled = await self._acl_enabled(ctx)
preserved_acl_uris: set[str] = set()
- if preserve_target_acl and acl_enabled:
+ if self.acl_manager is not None:
written_uris = {
rewrite_transfer_uri(str(record["uri"]), source_uri, target_uri)
for record in source_records
@@ -2930,8 +2927,7 @@ class VikingVectorIndexBackend:
):
affected_target_ids.append(str(record["id"]))
target_acl_fields: Dict[str, Dict[str, Any]] = {}
- if preserve_target_acl and acl_enabled and replacement_target_uris:
- assert self.acl_manager is not None
+ if self.acl_manager is not None and replacement_target_uris:
acl_target_uris = {uri for uri in replacement_target_uris if is_acl_uri(uri)}
target_acl_fields = {
uri: effective.context_fields()
@@ -2964,7 +2960,6 @@ class VikingVectorIndexBackend:
recursive=recursive,
source_uris=source_uris,
target_entry_exists=target_entry_exists,
- preserve_target_acl=True,
)
result = VectorTransferResult(scanned=len(source_records), batches=batches)
timestamp = get_current_timestamp()
@@ -3033,12 +3028,13 @@ class VikingVectorIndexBackend:
*,
source_uris: List[str] | None = None,
target_entry_exists: Callable[[str], Awaitable[bool]] | None = None,
+ restore_acl: Dict[str, Dict[str, Any]] | None = None,
) -> VectorTransferResult:
"""Move every vector record in a URI scope with compensating rollback."""
source_uri = resolve_uri(source_uri).uri
target_uri = resolve_uri(target_uri).uri
self._validate_uri_transfer_scopes(source_uri, target_uri)
- source_records, batches, _ = await self._prepare_uri_transfer(
+ source_records, batches, target_acl_fields = await self._prepare_uri_transfer(
ctx,
source_uri,
target_uri,
@@ -3046,13 +3042,23 @@ class VikingVectorIndexBackend:
source_uris=source_uris,
target_entry_exists=target_entry_exists,
)
- result = VectorTransferResult(scanned=len(source_records), batches=batches)
+ result = VectorTransferResult(
+ scanned=len(source_records),
+ batches=batches,
+ # Keep the original ACL only for a failed move's compensation.
+ source_acl={
+ str(record["uri"]): {
+ key: value for key, value in record.items() if key in ACL_CONTEXT_FIELDS
+ }
+ for record in source_records
+ },
+ )
+ if restore_acl is not None:
+ target_acl_fields.update(restore_acl)
if not source_records:
return result
timestamp = get_current_timestamp()
- acl_enabled = await self._acl_enabled(ctx)
- moved_acl_by_uri: Dict[str, Dict[str, Any]] = {}
target_payloads: List[Dict[str, Any]] = []
for record in source_records:
payload = rewrite_vector_record(
@@ -3063,17 +3069,10 @@ class VikingVectorIndexBackend:
mode="move",
timestamp=timestamp,
)
- if acl_enabled:
- assert self.acl_manager is not None
- rewritten_uri = str(payload["uri"])
- acl_fields = moved_acl_by_uri.get(rewritten_uri)
- if acl_fields is None:
- acl_fields = await self.acl_manager.materialize_moved_record(
- record,
- rewritten_uri,
- ctx,
- )
- moved_acl_by_uri[rewritten_uri] = acl_fields
+ acl_fields = target_acl_fields.get(
+ self._vector_entry_uri(str(payload["uri"]), target_acl_fields)
+ )
+ if acl_fields is not None:
payload.update(acl_fields)
target_payloads.append(payload)
target_ids = [str(payload["id"]) for payload in target_payloads]
@@ -3084,7 +3083,7 @@ class VikingVectorIndexBackend:
attempted_target_ids.extend(str(payload["id"]) for payload in payload_batch)
written_ids = (
await self._upsert_many_raw(payload_batch, ctx=ctx)
- if acl_enabled
+ if target_acl_fields
else await self.upsert_many(payload_batch, ctx=ctx)
)
if len(written_ids) != len(payload_batch):
@@ -3221,38 +3220,10 @@ class VikingVectorIndexBackend:
]
)
- controlled_modes = [AclMode.INHERIT.value, AclMode.RESTRICTED.value]
- # Shared records written while ACL was disabled retain the root's
- # default user:* manage access. Controlled descendants must match their
- # own grants, including inherit nodes below a restricted boundary.
- default_shared_filter = RawDSL(
- {"op": "must_not", "field": ACL_MODE_FIELD, "conds": controlled_modes}
- )
- read_grants = acl_grant_tokens(acl_principals(ctx), AclAction.READ)
shared_acl_filter = And(
[
PathScope("uri", "viking://resources", depth=-1),
- Or(
- [
- default_shared_filter,
- And(
- [
- In(ACL_MODE_FIELD, controlled_modes),
- Or(
- [
- In("acl_direct_grants", read_grants),
- And(
- [
- Eq(ACL_MODE_FIELD, AclMode.INHERIT.value),
- In("acl_inherited_grants", read_grants),
- ]
- ),
- ]
- ),
- ]
- ),
- ]
- ),
+ acl_filter(ctx, AclAction.READ),
]
)
access_filters: List[FilterExpr] = [
diff --git a/tests/misc/test_ovpack_import_policy.py b/tests/misc/test_ovpack_import_policy.py
index abce4b9e7..e483844bb 100644
--- a/tests/misc/test_ovpack_import_policy.py
+++ b/tests/misc/test_ovpack_import_policy.py
@@ -38,6 +38,9 @@ class FakeVikingFS:
self.tree_calls: list[str] = []
self.write_contexts: list[RequestContext] = []
self.existing_roots = existing_roots or set()
+ self._ensure_destination_access = AsyncMock(
+ side_effect=lambda uri, ctx: uri in self.existing_roots
+ )
self.removed_roots: list[str] = []
async def stat(self, uri: str, ctx=None, skip_count=False):
@@ -945,6 +948,18 @@ async def test_import_ovpack_restores_required_dense_vector_snapshot(
fake_fs = FakeVikingFS()
vector_store = FakeVectorStore()
+ root_uri = "viking://resources/imported/demo"
+ fake_fs._ensure_destination_access.side_effect = PermissionDeniedError(
+ "write permission required"
+ )
+ with pytest.raises(PermissionDeniedError, match="write permission required"):
+ await import_ovpack(
+ fake_fs, str(temp_ovpack_path), "viking://resources/imported", request_ctx
+ )
+ assert fake_fs.written_files == fake_fs.removed_roots == fake_fs.created_dirs == []
+ fake_fs._ensure_destination_access.assert_awaited_once_with(root_uri, request_ctx)
+ fake_fs._ensure_destination_access.side_effect = None
+ fake_fs._ensure_destination_access.return_value = False
result = await import_ovpack(
fake_fs,
str(temp_ovpack_path),
diff --git a/tests/service/test_fs_service.py b/tests/service/test_fs_service.py
index 06946ca58..b57c52d4d 100644
--- a/tests/service/test_fs_service.py
+++ b/tests/service/test_fs_service.py
@@ -4,6 +4,7 @@
import asyncio
from contextlib import asynccontextmanager
+from dataclasses import replace
from types import SimpleNamespace
from unittest.mock import AsyncMock
@@ -36,6 +37,7 @@ def run_fs_operations_inline(monkeypatch):
class _FakeVikingFS:
def __init__(self, *, rm_error=None, events=None, parent_exists=True):
self._ensure_access = AsyncMock()
+ self._ensure_destination_access = AsyncMock()
self.rm_calls = []
self.mv_calls = []
self.cp_calls = []
@@ -56,6 +58,7 @@ class _FakeVikingFS:
self.mv_calls.append({"from_uri": from_uri, "to_uri": to_uri, "ctx": ctx})
if self.events is not None:
self.events.append(("mv", from_uri, to_uri))
+ return {"source_acl": {}}
async def cp(self, from_uri, to_uri, recursive=False, ctx=None):
self.cp_calls.append(
@@ -910,7 +913,9 @@ async def test_resource_rm_enqueues_parent_delete_refresh_and_waits(request_cont
)
viking_fs._ensure_access.assert_awaited_once_with(uri, request_context, action=AclAction.MANAGE)
- assert viking_fs.rm_calls == [{"uri": uri, "recursive": True, "ctx": request_context}]
+ assert viking_fs.rm_calls == [
+ {"uri": uri, "recursive": True, "ctx": replace(request_context, bypass_acl=True)}
+ ]
assert len(queue_manager.messages) == 1
msg = queue_manager.messages[0]
assert msg.uri == "viking://resources/images/2026/06/10"
@@ -959,7 +964,9 @@ async def test_resource_rm_skips_parent_refresh_when_parent_is_gone(request_cont
uri = "viking://resources/deleted/sub/a.md"
result = await service.rm(uri, ctx=request_context, wait=True)
- assert viking_fs.rm_calls == [{"uri": uri, "recursive": False, "ctx": request_context}]
+ assert viking_fs.rm_calls == [
+ {"uri": uri, "recursive": False, "ctx": replace(request_context, bypass_acl=True)}
+ ]
service._enqueue_delete_refresh.assert_not_awaited()
assert "semantic_root_uri" not in result
@@ -1070,7 +1077,7 @@ async def test_resource_mv_validates_then_moves_then_rewrites_watch_tasks(
{
"from_uri": "viking://resources/codeask/wiki",
"to_uri": "viking://resources/codeask/wiki-renamed",
- "ctx": request_context,
+ "ctx": replace(request_context, bypass_acl=True),
}
]
assert [event[0] for event in events] == ["validate", "mv", "rewrite", "refresh"]
@@ -1152,7 +1159,7 @@ async def test_resource_mv_without_watch_scheduler_moves_resource_directly(reque
{
"from_uri": "viking://resources/codeask/wiki",
"to_uri": "viking://resources/archive/wiki",
- "ctx": request_context,
+ "ctx": replace(request_context, bypass_acl=True),
}
]
assert refresh_calls == [
@@ -1585,7 +1592,7 @@ async def test_system_rm_skips_vector_and_semantic_work(request_context):
viking_fs.rm.assert_awaited_once_with(
uri,
recursive=False,
- ctx=request_context,
+ ctx=replace(request_context, bypass_acl=True),
no_vector=True,
)
assert result["semantic_status"] == "skipped"
@@ -1697,7 +1704,9 @@ async def test_indexed_copy_preserves_native_vectors_and_content_copy_refresh(re
async def test_indexed_move_preserves_native_vector_transfer(request_context):
source = "viking://resources/source/demo.md"
target = "viking://resources/target/demo.md"
- viking_fs = SimpleNamespace(_ensure_access=AsyncMock(), mv=AsyncMock())
+ viking_fs = SimpleNamespace(
+ _ensure_access=AsyncMock(), _ensure_destination_access=AsyncMock(), mv=AsyncMock()
+ )
service = FSService(viking_fs=viking_fs)
service._refresh_move_parents = AsyncMock()
@@ -1706,7 +1715,10 @@ async def test_indexed_move_preserves_native_vector_transfer(request_context):
viking_fs._ensure_access.assert_awaited_once_with(
source, request_context, action=AclAction.MANAGE
)
- viking_fs.mv.assert_awaited_once_with(source, target, ctx=request_context)
+ viking_fs._ensure_destination_access.assert_awaited_once_with(target, request_context)
+ viking_fs.mv.assert_awaited_once_with(
+ source, target, ctx=replace(request_context, bypass_acl=True)
+ )
service._refresh_move_parents.assert_awaited_once_with(
from_uri=source,
to_uri=target,
@@ -1738,7 +1750,9 @@ async def test_move_to_system_deletes_source_vectors_without_target_index(
):
source = "viking://resources/project/demo.md"
target = "viking://system/memory/memory_templates/custom/demo.md"
- viking_fs = SimpleNamespace(_ensure_access=AsyncMock(), mv=AsyncMock())
+ viking_fs = SimpleNamespace(
+ _ensure_access=AsyncMock(), _ensure_destination_access=AsyncMock(), mv=AsyncMock()
+ )
service = FSService(viking_fs=viking_fs)
refresh = AsyncMock()
monkeypatch.setattr(service, "_refresh_move_parents", refresh)
@@ -1748,7 +1762,7 @@ async def test_move_to_system_deletes_source_vectors_without_target_index(
viking_fs.mv.assert_awaited_once_with(
source,
target,
- ctx=request_context,
+ ctx=replace(request_context, bypass_acl=True),
vector_mode="delete_source",
copy_semantic_sidecars=False,
)
@@ -1848,6 +1862,7 @@ async def test_move_from_system_rebuilds_target_index(request_context):
target = "viking://resources/project/demo.md"
viking_fs = SimpleNamespace(
_ensure_access=AsyncMock(),
+ _ensure_destination_access=AsyncMock(),
mv=AsyncMock(),
stat=AsyncMock(return_value={"isDir": False}),
)
@@ -1860,7 +1875,7 @@ async def test_move_from_system_rebuilds_target_index(request_context):
viking_fs.mv.assert_awaited_once_with(
source,
target,
- ctx=request_context,
+ ctx=replace(request_context, bypass_acl=True),
vector_mode="none",
copy_semantic_sidecars=False,
)
@@ -1885,6 +1900,7 @@ async def test_move_target_refresh_failure_does_not_roll_back_physical_move(requ
target = "viking://resources/project/demo.md"
viking_fs = SimpleNamespace(
_ensure_access=AsyncMock(),
+ _ensure_destination_access=AsyncMock(),
mv=AsyncMock(),
stat=AsyncMock(return_value={"isDir": False}),
)
@@ -1903,6 +1919,7 @@ async def test_recursive_move_from_system_rebuilds_target_subtree(request_contex
target = "viking://resources/project/demo"
viking_fs = SimpleNamespace(
_ensure_access=AsyncMock(),
+ _ensure_destination_access=AsyncMock(),
mv=AsyncMock(),
stat=AsyncMock(return_value={"isDir": True}),
)
diff --git a/tests/service/test_resource_service_reserved_target_cleanup.py b/tests/service/test_resource_service_reserved_target_cleanup.py
index 121f681ad..f79d598a3 100644
--- a/tests/service/test_resource_service_reserved_target_cleanup.py
+++ b/tests/service/test_resource_service_reserved_target_cleanup.py
@@ -1,3 +1,4 @@
+from dataclasses import replace
from types import SimpleNamespace
from unittest.mock import AsyncMock
@@ -31,6 +32,7 @@ def _service(viking_fs, resource_processor=None) -> ResourceService:
async def test_cleanup_reserved_target_removes_only_empty_directory():
lock = {"lease_ref": "lock-1"}
ctx = _ctx()
+ cleanup_ctx = replace(ctx, bypass_acl=True)
viking_fs = SimpleNamespace(
exists=AsyncMock(return_value=True),
stat=AsyncMock(return_value={"isDir": True}),
@@ -49,12 +51,12 @@ async def test_cleanup_reserved_target_removes_only_empty_directory():
viking_fs.rm.assert_awaited_once_with(
"viking://resources/empty",
recursive=True,
- ctx=ctx,
+ ctx=cleanup_ctx,
lease_ref=lock,
)
viking_fs.stat.assert_awaited_once_with(
"viking://resources/empty",
- ctx=ctx,
+ ctx=cleanup_ctx,
skip_count=True,
)
@@ -80,7 +82,7 @@ async def test_cleanup_reserved_target_preserves_nonempty_directory():
viking_fs.rm.assert_not_awaited()
viking_fs.stat.assert_awaited_once_with(
"viking://resources/nonempty",
- ctx=ctx,
+ ctx=replace(ctx, bypass_acl=True),
skip_count=True,
)
diff --git a/tests/storage/test_session_delete_storage.py b/tests/storage/test_session_delete_storage.py
index 77cdcb120..6fb5217a0 100644
--- a/tests/storage/test_session_delete_storage.py
+++ b/tests/storage/test_session_delete_storage.py
@@ -10,6 +10,7 @@ import pytest
from openviking.core.namespace import resolve_request_uri
from openviking.server.identity import RequestContext, Role
+from openviking.service.fs_service import FSService
from openviking.storage.acl import AclEntry, AclLevel, AclMode, DirectAcl, EffectiveAcl
from openviking.storage.errors import LockAcquisitionError, ResourceBusyError
from openviking.storage.viking_fs import VikingFS
@@ -216,13 +217,12 @@ async def test_default_rm_still_cleans_vector_records(storage, missing, uri):
storage.agfs.pathlock_release.assert_awaited_once_with(storage.lease)
[email protected]("no_vector", [False, True])
[email protected]("child_allowed", [False, True])
-async def test_resource_rm_still_checks_descendant_manage_acl(storage, no_vector, child_allowed):
[email protected]("allowed", [False, True])
+async def test_resource_rm_authorizes_the_whole_subtree_at_its_root(storage, allowed):
child_uri = f"{RESOURCE_URI}/restricted.md"
storage.fs._collect_uris.return_value = [child_uri]
storage.acl.is_enabled.return_value = True
- allowed = EffectiveAcl(
+ managed = EffectiveAcl(
AclMode.RESTRICTED,
DirectAcl((AclEntry("user:alice", AclLevel.MANAGE),)),
DirectAcl(),
@@ -230,28 +230,19 @@ async def test_resource_rm_still_checks_descendant_manage_acl(storage, no_vector
denied = EffectiveAcl(AclMode.RESTRICTED, DirectAcl(), DirectAcl())
async def resolve_many(uris, ctx):
- return {uri: denied if uri == child_uri and not child_allowed else allowed for uri in uris}
+ return {uri: managed if uri == RESOURCE_URI and allowed else denied for uri in uris}
storage.acl.resolve_many.side_effect = resolve_many
-
- if child_allowed:
- result = await storage.fs.rm(
- RESOURCE_URI, recursive=True, ctx=storage.ctx, no_vector=no_vector
- )
- assert result == {"estimated_deleted_count": 0 if no_vector else 7}
+ service = FSService(viking_fs=storage.fs)
+ service._enqueue_delete_refresh = AsyncMock(return_value="queued")
+ if allowed:
+ await service._rm(RESOURCE_URI, ctx=storage.ctx, recursive=True)
storage.agfs.rm.assert_awaited_once()
+ storage.vector.delete_uris.assert_awaited_once()
+ assert not storage.ctx.bypass_acl
else:
- with pytest.raises(PermissionDeniedError, match="restricted.md"):
- await storage.fs.rm(RESOURCE_URI, recursive=True, ctx=storage.ctx, no_vector=no_vector)
+ with pytest.raises(PermissionDeniedError):
+ await service._rm(RESOURCE_URI, ctx=storage.ctx, recursive=True)
storage.agfs.rm.assert_not_awaited()
-
- storage.fs._collect_uris.assert_awaited_once_with(
- RESOURCE_PATH, True, ctx=storage.ctx, strict=True
- )
- if no_vector or not child_allowed:
- storage.vector.count.assert_not_awaited()
storage.vector.delete_uris.assert_not_awaited()
- else:
- storage.vector.count.assert_awaited_once()
- storage.vector.delete_uris.assert_awaited_once_with(storage.ctx, [child_uri, RESOURCE_URI])
- storage.agfs.pathlock_release.assert_awaited_once_with(storage.lease)
+ storage.acl.resolve_many.assert_awaited_once_with([RESOURCE_URI], storage.ctx)
diff --git a/tests/storage/test_transfer_merge_binding.py b/tests/storage/test_transfer_merge_binding.py
index c011a5400..4d2600674 100644
--- a/tests/storage/test_transfer_merge_binding.py
+++ b/tests/storage/test_transfer_merge_binding.py
@@ -12,6 +12,7 @@ import pytest_asyncio
from openviking.pyagfs import get_binding_client
from openviking.resource.watch_manager import WatchManager
from openviking.server.identity import RequestContext, Role
+from openviking.service.fs_service import FSService
from openviking.storage.acl import AclAction, AclManager
from openviking.storage.collection_schemas import CollectionSchemas
from openviking.storage.vector_ids import vector_record_id
@@ -614,37 +615,27 @@ async def test_transfer_normalizes_uri_for_files_and_vectors(indexed_fs, operati
@pytest.mark.asyncio
-async def test_acl_failure_restores_only_moved_vectors_after_directory_merge(
- indexed_fs, monkeypatch
-):
+async def test_watch_failure_restores_moved_content_and_source_acl(indexed_fs):
fs, backend = indexed_fs
- ctx = root_ctx()
+ ctx = RequestContext(user=root_ctx().user, role=Role.ADMIN)
source, target = "viking://resources/source", "viking://resources/target"
await fs.write_file_bytes(f"{source}/file.txt", b"new", ctx=ctx)
- await fs.write_file_bytes(f"{target}/only.txt", b"keep", ctx=ctx)
await seed_vector(backend, f"{source}/file.txt", "new")
- await seed_vector(backend, f"{target}/only.txt", "keep")
- monkeypatch.setattr(fs, "_ensure_access", AsyncMock())
- async def acl_enabled(_account_id):
- return True
-
- monkeypatch.setattr(
- fs,
- "acl_manager",
- SimpleNamespace(
- is_enabled=acl_enabled,
- refresh_context_subtree=AsyncMock(side_effect=RuntimeError("injected ACL failure")),
- ),
+ acl = AclManager(backend, _EnabledAclConfig())
+ fs.acl_manager = backend.acl_manager = acl
+ await acl.set_acl(f"{source}/file.txt", [{"principal": "user:alice", "level": "manage"}], ctx)
+ before = await acl.resolve(f"{source}/file.txt", ctx)
+ manager = SimpleNamespace(
+ validate_target_prefix_rewrite_internal=AsyncMock(),
+ rewrite_target_prefix_internal=AsyncMock(side_effect=RuntimeError("watch commit failed")),
)
+ service = FSService(viking_fs=fs, watch_scheduler=SimpleNamespace(watch_manager=manager))
- # Trailing slashes must not corrupt the reverse transfer manifest.
- with pytest.raises(RuntimeError, match="injected ACL failure"):
- await fs.mv(source + "/", target, ctx=ctx)
+ with pytest.raises(RuntimeError, match="watch commit failed"):
+ await service._mv_and_refresh(source, target, ctx=ctx)
assert await fs.read_file_bytes(f"{source}/file.txt", ctx=ctx) == b"new"
- assert await backend.get_context_by_uri(f"{source}/file.txt", ctx=ctx)
- assert not await backend.get_context_by_uri(f"{source}/only.txt", ctx=ctx)
- assert await backend.get_context_by_uri(f"{target}/only.txt", ctx=ctx)
+ assert await acl.resolve(f"{source}/file.txt", ctx) == before
assert not await fs.exists(target, ctx=ctx)
diff --git a/tests/storage/test_vector_transfer.py b/tests/storage/test_vector_transfer.py
index fd44d7e48..05f5918e5 100644
--- a/tests/storage/test_vector_transfer.py
+++ b/tests/storage/test_vector_transfer.py
@@ -182,42 +182,6 @@ class _MemoryTransferBackend(VikingVectorIndexBackend):
return await self.delete(ids, ctx=ctx)
-class _TransferAclManager:
- async def is_enabled(self, account_id: str) -> bool:
- return account_id == "acct"
-
- async def materialize_context_records(self, records, ctx):
- del ctx
- return [
- {
- **record,
- "acl_mode": "inherit",
- "acl_direct_grants": [],
- "acl_inherited_grants": ["1:group:target-readers"],
- }
- for record in records
- ]
-
- async def materialize_moved_record(self, record, new_uri, ctx):
- del new_uri, ctx
- return {
- "acl_mode": record.get("acl_mode", "inherit"),
- "acl_direct_grants": list(record.get("acl_direct_grants") or []),
- "acl_inherited_grants": ["1:group:target-readers"],
- }
-
-
-class _AclMemoryTransferBackend(_MemoryTransferBackend):
- def __init__(self, records: list[dict[str, Any]]) -> None:
- super().__init__(records)
- self.acl_manager = _TransferAclManager()
-
- async def upsert_many(
- self, data_list: list[dict[str, Any]], *, ctx: RequestContext
- ) -> list[str]:
- return await VikingVectorIndexBackend.upsert_many(self, data_list, ctx=ctx)
-
-
def _matches_filter(expr, record: dict[str, Any]) -> bool:
if expr is None:
return True
@@ -240,6 +204,9 @@ def _matches_filter(expr, record: dict[str, Any]) -> bool:
if path[: len(root)] != root:
return False
return expr.depth == -1 or len(path) - len(root) <= expr.depth
+ if isinstance(expr, RawDSL) and expr.payload["op"] in {"must", "must_not"}:
+ matched = _matches_filter(In(expr.payload["field"], expr.payload["conds"]), record)
+ return matched if expr.payload["op"] == "must" else not matched
if isinstance(expr, RawDSL) and expr.payload["op"] == "prefix":
return str(record.get(expr.payload["field"], "")).startswith(expr.payload["prefix"])
raise AssertionError(f"Unexpected filter expression in test: {expr!r}")
@@ -1421,12 +1388,14 @@ async def test_uri_mapping_source_uris_limits_source_and_target_replacement_scop
@pytest.mark.asyncio
-async def test_copy_over_private_target_preserves_target_acl():
+async def test_copy_preserves_target_acl_even_when_enforcement_is_disabled():
source = "viking://resources/src.md"
target = "viking://resources/dst.md"
backend = _RealAclMemoryTransferBackend(
[
- _record("source-file", source),
+ _record(
+ "source-file", source, acl_mode="restricted", acl_direct_grants=["7:user:alice"]
+ ),
_record("source-chunk", f"{source}#chunk_0001"),
_record(
"target-file",
@@ -1438,6 +1407,10 @@ async def test_copy_over_private_target_preserves_target_acl():
]
)
+ backend.acl_manager._runtime_config.get_account = AsyncMock(
+ return_value=SimpleNamespace(enabled=False)
+ )
+
await backend.copy_uri_mapping(_ctx(), source, target, recursive=False)
targets = _records_under(backend, target, recursive=False)
@@ -1552,8 +1525,16 @@ async def test_copy_directory_preserves_root_acl_and_inherits_it_to_new_entries(
root_acl = ["7:user:bob"]
backend = _RealAclMemoryTransferBackend(
[
- _record("source-root", source, level=0),
- _record("source-child", source_child, level=0),
+ _record(
+ "source-root",
+ source,
+ level=0,
+ acl_mode="restricted",
+ acl_direct_grants=["7:user:alice"],
+ ),
+ _record(
+ "source-child", source_child, level=0, acl_mode="restricted", acl_direct_grants=[]
+ ),
_record("source-file", source_file),
_record(
"target-root",
@@ -1734,28 +1715,32 @@ async def test_update_uri_mapping_deletes_source_only_after_targets_exist():
@pytest.mark.asyncio
-async def test_update_uri_mapping_preserves_source_direct_acl_on_target():
- source = "viking://resources/src.md"
- target = "viking://resources/dst.md"
- backend = _AclMemoryTransferBackend(
+async def test_update_uri_mapping_preserves_target_acl_and_inherits_new_entries():
+ source, target = "viking://resources/src", "viking://resources/dst"
+ backend = _RealAclMemoryTransferBackend(
[
+ _record("source", source, acl_mode="restricted", acl_direct_grants=["7:user:alice"]),
_record(
- "source",
- source,
- acl_mode="restricted",
- acl_direct_grants=["7:user:alice"],
- acl_inherited_grants=["1:group:source-readers"],
- )
+ "source-child", f"{source}/child.md", acl_mode="restricted", acl_direct_grants=[]
+ ),
+ _record("target", target, acl_mode="inherit", acl_direct_grants=["7:user:bob"]),
]
)
- await backend.update_uri_mapping(_ctx(), source, target, recursive=False)
+ result = await backend.update_uri_mapping(_ctx(), source, target, recursive=True)
- moved = _records_under(backend, target, recursive=False)
- assert len(moved) == 1
- assert moved[0]["acl_mode"] == "restricted"
- assert moved[0]["acl_direct_grants"] == ["7:user:alice"]
- assert moved[0]["acl_inherited_grants"] == ["1:group:target-readers"]
+ moved = sorted(_records_under(backend, target), key=lambda record: record["uri"])
+ assert [record["acl_mode"] for record in moved] == ["inherit", "inherit"]
+ assert [record["acl_direct_grants"] for record in moved] == [["7:user:bob"], []]
+ assert moved[1]["acl_inherited_grants"] == ["7:user:*", "7:user:bob"]
+ assert _records_under(backend, source) == []
+
+ await backend.update_uri_mapping(
+ _ctx(), target, source, recursive=True, restore_acl=result.source_acl
+ )
+ restored = sorted(_records_under(backend, source), key=lambda record: record["uri"])
+ assert [record["acl_direct_grants"] for record in restored] == [["7:user:alice"], []]
+ assert all(record["acl_mode"] == "restricted" for record in restored)
@pytest.mark.asyncio
@@ -1782,7 +1767,7 @@ async def test_update_uri_mapping_restores_source_and_removes_target_after_parti
async def test_update_uri_mapping_rollback_restores_source_direct_acl():
source = "viking://resources/src"
target = "viking://resources/dst"
- backend = _AclMemoryTransferBackend(
+ backend = _RealAclMemoryTransferBackend(
[
_record(
"source-root",
diff --git a/tests/storage/test_viking_fs_cp.py b/tests/storage/test_viking_fs_cp.py
index c8542cd36..fe6284967 100644
--- a/tests/storage/test_viking_fs_cp.py
+++ b/tests/storage/test_viking_fs_cp.py
@@ -3,6 +3,7 @@
from __future__ import annotations
+from dataclasses import replace
from types import SimpleNamespace
from unittest.mock import AsyncMock
@@ -10,7 +11,6 @@ import pytest
from openviking.server.identity import RequestContext, Role
from openviking.storage.abstract_overview import parse_abstract_overview
-from openviking.storage.acl import AclAction
from openviking.storage.viking_fs import VikingFS
from openviking_cli.exceptions import (
InvalidArgumentError,
@@ -18,6 +18,7 @@ from openviking_cli.exceptions import (
PermissionDeniedError,
)
from openviking_cli.session.user_id import UserIdentifier
+from tests.storage.test_vector_transfer import _RealAclMemoryTransferBackend, _record
def _ctx() -> RequestContext:
@@ -295,7 +296,7 @@ async def test_cp_file_uses_exact_locks_agfs_copy_then_vector_copy(monkeypatch):
"viking://resources/source.md",
"viking://resources/target.md",
recursive=False,
- ctx=_ctx(),
+ ctx=replace(_ctx(), bypass_acl=True),
)
assert agfs.events[-1] == ("release", agfs.operation_lease)
@@ -375,7 +376,7 @@ async def test_cp_directory_preserves_empty_hidden_and_binary_entries(monkeypatc
"viking://resources/source",
"viking://resources/target",
recursive=True,
- ctx=_ctx(),
+ ctx=replace(_ctx(), bypass_acl=True),
source_uris=[
"viking://resources/source",
"viking://resources/source/.hidden",
@@ -633,80 +634,6 @@ async def test_mv_keeps_target_when_source_delete_fails(monkeypatch):
assert vector_move.await_count == 1
-async def test_mv_restores_vectors_before_removing_target_when_acl_refresh_fails(monkeypatch):
- agfs = _CopyAGFS()
- fs = _viking_fs(monkeypatch, agfs)
- source_uri = "viking://resources/source.md"
- target_uri = "viking://resources/target.md"
- vector_uris = {source_uri}
-
- async def move_vectors(old_uri, new_uri, *, recursive, ctx):
- assert recursive is False
- assert ctx == _ctx()
- vector_uris.remove(old_uri)
- vector_uris.add(new_uri)
- agfs.events.append(("move-vectors", old_uri, new_uri))
- return SimpleNamespace(scanned=1, written=1, deleted=1, restored=0, batches=1)
-
- async def acl_enabled(account_id):
- return account_id == "acct"
-
- fs.acl_manager = SimpleNamespace(
- is_enabled=acl_enabled,
- refresh_context_subtree=AsyncMock(side_effect=RuntimeError("ACL refresh failed")),
- )
- monkeypatch.setattr(fs, "_update_vector_store_uris", move_vectors)
-
- with pytest.raises(RuntimeError, match="ACL refresh failed"):
- await fs.mv(source_uri, target_uri, ctx=_ctx())
-
- assert vector_uris == {source_uri}
- assert not agfs.target_exists
- vector_restore_index = agfs.events.index(("move-vectors", target_uri, source_uri))
- target_cleanup_index = next(
- index
- for index, event in enumerate(agfs.events)
- if event[0:3] == ("rm", "/local/acct/resources/target.md", False)
- )
- assert vector_restore_index < target_cleanup_index
-
-
-async def test_mv_still_cleans_target_when_vector_restore_after_acl_refresh_fails(monkeypatch):
- agfs = _CopyAGFS()
- fs = _viking_fs(monkeypatch, agfs)
- source_uri = "viking://resources/source.md"
- target_uri = "viking://resources/target.md"
- vector_uris = {source_uri}
-
- async def move_vectors(old_uri, new_uri, *, recursive, ctx):
- assert recursive is False
- assert ctx == _ctx()
- if old_uri == target_uri:
- raise RuntimeError("vector restore failed")
- vector_uris.remove(old_uri)
- vector_uris.add(new_uri)
- return SimpleNamespace(scanned=1, written=1, deleted=1, restored=0, batches=1)
-
- async def acl_enabled(account_id):
- return account_id == "acct"
-
- fs.acl_manager = SimpleNamespace(
- is_enabled=acl_enabled,
- refresh_context_subtree=AsyncMock(side_effect=RuntimeError("ACL refresh failed")),
- )
- monkeypatch.setattr(fs, "_update_vector_store_uris", move_vectors)
-
- with pytest.raises(RuntimeError, match="ACL refresh failed"):
- await fs.mv(source_uri, target_uri, ctx=_ctx())
- assert vector_uris == {target_uri}
- assert not agfs.target_exists
- assert any(
- event[0:3] == ("rm", "/local/acct/resources/target.md", False) for event in agfs.events
- )
-
-
@pytest.mark.asyncio
async def test_mv_directory_keeps_target_after_partial_source_delete(monkeypatch):
agfs = _DirectoryMoveRollbackAGFS()
@@ -762,7 +689,9 @@ async def test_mv_returns_diagnostic_transfer_summary(monkeypatch):
fs,
"_update_vector_store_uris",
AsyncMock(
- return_value=SimpleNamespace(scanned=2, written=2, deleted=2, restored=0, batches=1)
+ return_value=SimpleNamespace(
+ scanned=2, written=2, deleted=2, restored=0, batches=1, source_acl={}
+ )
),
)
@@ -890,35 +819,52 @@ async def test_transfer_rejects_target_ancestor(monkeypatch, operation):
@pytest.mark.asyncio
[email protected]("operation", ["cp", "mv"])
[email protected]("child", ["data.bin", "tasks/private.txt", "_system/private.txt"])
-async def test_transfer_checks_child_target_write_permission_before_copy(
- monkeypatch, operation, child
-):
[email protected]("permission", ["source_denied", "target_write", "target_manage"])
+async def test_cp_requires_readable_source_and_manage_for_directory_merge(permission):
+ source, target = "viking://resources/source", "viking://resources/target"
agfs = _DirectoryCopyAGFS()
agfs.directories.add("/local/acct/resources/target")
- if "/" in child:
- name = child.split("/")[0]
- agfs.directories.update(
- {f"/local/acct/resources/source/{name}", f"/local/acct/resources/target/{name}"}
- )
- agfs.files[f"/local/acct/resources/source/{child}"] = b"new"
- agfs.files[f"/local/acct/resources/target/{child}"] = b"protected"
- fs = _viking_fs(monkeypatch, agfs)
-
- async def ensure_access(uri, ctx, *, action=AclAction.READ):
- if uri == f"viking://resources/target/{child}" and action == AclAction.WRITE:
- raise PermissionDeniedError("protected child", resource=uri)
-
- monkeypatch.setattr(fs, "_ensure_access", ensure_access)
+ agfs.directories.add("/local/acct/resources/source/nested")
+ agfs.files["/local/acct/resources/source/nested/secret.md"] = b"nested"
+ agfs.files["/local/acct/resources/target/data.bin"] = b"old"
+ agfs.files["/local/acct/resources/target/only.txt"] = b"keep"
+ if permission == "source_denied":
+ agfs.files["/local/acct/resources/target/nested"] = b"type conflict"
+ target_grant = "3:user:alice" if permission == "target_write" else "7:user:alice"
+ backend = _RealAclMemoryTransferBackend(
+ [
+ _record("a-source", source, acl_mode="restricted", acl_direct_grants=["1:user:alice"]),
+ _record("b-target", target, acl_mode="restricted", acl_direct_grants=[target_grant]),
+ _record(
+ "c-secret",
+ f"{source}/nested/secret.md",
+ acl_mode="restricted",
+ acl_direct_grants=[] if permission == "source_denied" else ["1:user:alice"],
+ ),
+ # Neither absent files nor an older index level may block an otherwise readable copy.
+ _record(
+ "d-orphan", f"{source}/missing.md", acl_mode="restricted", acl_direct_grants=[]
+ ),
+ _record("e-stale-source", source, level=1, acl_mode="restricted", acl_direct_grants=[]),
+ ]
+ )
+ fs = VikingFS(agfs=SimpleNamespace(), vector_store=backend, acl_manager=backend.acl_manager)
+ fs._async_agfs = agfs
before = dict(agfs.files)
- kwargs = {"recursive": True} if operation == "cp" else {}
- with pytest.raises(PermissionDeniedError, match="protected child"):
- await getattr(fs, operation)(
- "viking://resources/source", "viking://resources/target", ctx=_ctx(), **kwargs
+ ctx = _user_ctx()
+ if permission == "target_manage":
+ await fs.cp(source, target, recursive=True, ctx=ctx)
+ assert agfs.files["/local/acct/resources/target/data.bin"] == b"\x00\xff"
+ assert agfs.files["/local/acct/resources/target/only.txt"] == b"keep"
+ assert all(
+ agfs.files[path] == content for path, content in before.items() if "/source/" in path
)
- assert agfs.files == before
- assert not any(event[0] in {"cp", "rm"} for event in agfs.events)
+ assert not ctx.bypass_acl
+ else:
+ with pytest.raises(PermissionDeniedError):
+ await fs.cp(source, target, recursive=True, ctx=ctx)
+ assert agfs.files == before
+ assert not any(event[0] == "cp" for event in agfs.events)
@pytest.mark.asyncio
diff --git a/tests/storage/test_viking_vector_scope_filter.py b/tests/storage/test_viking_vector_scope_filter.py
index d227869ad..3d802acb2 100644
--- a/tests/storage/test_viking_vector_scope_filter.py
+++ b/tests/storage/test_viking_vector_scope_filter.py
@@ -9,7 +9,7 @@ from unittest.mock import AsyncMock
import pytest
from openviking.server.identity import RequestContext, Role
-from openviking.storage.acl import AclAction, AclManager, acl_allows
+from openviking.storage.acl import AclAction, AclManager, acl_allows, acl_filter
from openviking.storage.collection_schemas import CollectionSchemas
from openviking.storage.expr import And, Eq, In, Or, PathScope, RawDSL
from openviking.storage.viking_vector_index_backend import (
@@ -315,9 +315,16 @@ async def test_keywords_tenant_search_reuses_scope_filter_and_sets_bm25_fields(a
@pytest.mark.asyncio
[email protected]("legacy_mode", [{}, {"acl_mode": None}, {"acl_mode": "none"}])
+ "legacy_mode,action",
+ [
+ ({}, AclAction.READ),
+ ({"acl_mode": None}, AclAction.WRITE),
+ ({"acl_mode": "none"}, AclAction.MANAGE),
+ ],
+)
async def test_tenant_search_enforces_visible_roots_and_shared_acl(
- vector_backend_factory, tmp_path, legacy_mode
+ vector_backend_factory, tmp_path, legacy_mode, action
):
ctx = _ctx()
own_uri = "viking://user/alice/resources/notes"
@@ -401,6 +408,8 @@ async def test_tenant_search_enforces_visible_roots_and_shared_acl(
"uri": "viking://resources/foreign.md",
"account_id": "other",
"context_type": "resource",
+ "acl_mode": "restricted",
+ "acl_direct_grants": [],
},
]
@@ -434,6 +443,27 @@ async def test_tenant_search_enforces_visible_roots_and_shared_acl(
ctx=record_ctx,
)
+ # The same ACL predicate supports existence checks for read, write and manage.
+ finance_ctx = RequestContext(user=ctx.user, role=ctx.role, group_ids=("finance",))
+ account_backend = await backend.get_account_backend(ctx.account_id)
+ denied = await account_backend.strict_query(
+ filter=And(
+ [
+ Eq("account_id", ctx.account_id),
+ PathScope("uri", "viking://resources", depth=-1),
+ acl_filter(finance_ctx, action, denied=True),
+ ]
+ ),
+ limit=100,
+ output_fields=["id"],
+ )
+ expected_denied = {"restricted-inherited-shared"}
+ if action != AclAction.READ:
+ expected_denied.update({"direct-shared", "restricted-direct-shared"})
+ if action == AclAction.MANAGE:
+ expected_denied.add("denied-shared")
+ assert {record["id"] for record in denied} == expected_denied
+
decay = {
"events_time_decay_protection": "0",
"request_now": datetime(2026, 1, 8, tzinfo=timezone.utc),