Skip to content

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

fix(viking_fs): authorize grep matches through the ACL-aware batch check (#5133)

Kuang Mi5ac259b4a875
diff --git a/openviking/storage/viking_fs/_grep.py b/openviking/storage/viking_fs/_grep.py
index 207dba787..34dbb01b8 100644
--- a/openviking/storage/viking_fs/_grep.py
+++ b/openviking/storage/viking_fs/_grep.py
@@ -514,6 +514,21 @@ class _GrepMixin:
files_scanned_set = set()
real_ctx = self._ctx_or_default(ctx)
+ # Resolve every matched file to a Viking URI first, then run one
+ # ACL-aware batch authorization. ``_is_accessible`` is ACL-blind for
+ # ``viking://resources`` and would leak content protected by restricted
+ # inheritance, so matched URIs must go through ``_can_access_many`` the
+ # same way filesystem reads and vector retrieval do.
+ match_uris: List[str] = []
+ for match in matches:
+ match_file = match.get("file", "")
+ if not match_file:
+ continue
+ agfs_file_path = self._resolve_grep_match_agfs_path(path, match_file)
+ match_uris.append(self._path_to_uri(agfs_file_path, ctx=ctx))
+
+ access = await self._can_access_many(match_uris, real_ctx)
+
for match in matches:
match_file = match.get("file", "")
if not match_file:
@@ -522,7 +537,7 @@ class _GrepMixin:
agfs_file_path = self._resolve_grep_match_agfs_path(path, match_file)
file_uri = self._path_to_uri(agfs_file_path, ctx=ctx)
- if not self._is_accessible(file_uri, real_ctx):
+ if not access.get(file_uri, False):
continue
files_scanned_set.add(file_uri)
diff --git a/tests/storage/test_viking_fs_grep.py b/tests/storage/test_viking_fs_grep.py
index b537660f1..5ff773060 100644
--- a/tests/storage/test_viking_fs_grep.py
+++ b/tests/storage/test_viking_fs_grep.py
@@ -7,8 +7,11 @@ from unittest.mock import AsyncMock
import pytest
import openviking.storage.viking_fs as viking_fs_module
+from openviking.server.identity import RequestContext, Role
+from openviking.storage.acl import AclEntry, AclLevel, AclMode, DirectAcl, EffectiveAcl
from openviking.storage.expr import And, PathScope, RawDSL
from openviking.storage.viking_fs import _DEFAULT_GREP_FILE_CONCURRENCY, VikingFS
+from openviking_cli.session.user_id import UserIdentifier
from openviking_cli.utils.config.grep_config import GrepConfig
@@ -710,3 +713,77 @@ async def test_grep_applies_node_limit_to_backend_results(monkeypatch, fs):
"viking://resources/a.md",
"viking://resources/b.md",
]
+
+
+class _RestrictedAclManager:
+ """ACL manager stub: enabled, with per-URI effective ACLs from `resolve_many`."""
+
+ def __init__(self, effective_by_uri):
+ self.effective_by_uri = effective_by_uri
+
+ def is_enabled(self, account_id):
+ return True
+
+ async def resolve_many(self, uris, ctx):
+ return {uri: self.effective_by_uri[uri] for uri in uris}
+
+
+def _acl_grep_fs(monkeypatch, effective_by_uri):
+ """VikingFS with a native-grep stub returning one restricted resource match."""
+ viking_fs = VikingFS(agfs=_DummyAgfs())
+
+ async def fake_grep(**kwargs):
+ return {
+ "matches": [
+ {"file": "secret.md", "line": 1, "content": "SECRET_MARKER_4977"},
+ ],
+ "count": 1,
+ }
+
+ monkeypatch.setattr(viking_fs._async_agfs, "grep", fake_grep)
+ viking_fs.acl_manager = _RestrictedAclManager(effective_by_uri)
+ return viking_fs
+
+
+def _restricted_acl() -> EffectiveAcl:
+ """RESTRICTED inheritance with no grants — denies every non-bypassing principal."""
+ return EffectiveAcl(AclMode.RESTRICTED, DirectAcl(), DirectAcl())
+
+
+async def test_grep_with_agfs_denies_acl_restricted_content_without_grant(monkeypatch):
+ """Native grep must not leak restricted-inheritance content to a user with no grant."""
+ viking_fs = _acl_grep_fs(
+ monkeypatch,
+ {"viking://resources/secret.md": _restricted_acl()},
+ )
+ ctx = RequestContext(user=UserIdentifier("acct1", "mallory"), role=Role.USER)
+
+ result = await viking_fs._grep_with_agfs(
+ "viking://resources", pattern="SECRET_MARKER_4977", ctx=ctx
+ )
+
+ assert result["matches"] == []
+ assert result["count"] == 0
+
+
+async def test_grep_with_agfs_allows_acl_granted_content(monkeypatch):
+ """Authorized principals keep receiving restricted-inheritance content through grep."""
+ granted = EffectiveAcl(
+ AclMode.RESTRICTED,
+ DirectAcl.from_entries([AclEntry("user:mallory", AclLevel.READ)]),
+ DirectAcl(),
+ )
+ viking_fs = _acl_grep_fs(
+ monkeypatch,
+ {"viking://resources/secret.md": granted},
+ )
+ ctx = RequestContext(user=UserIdentifier("acct1", "mallory"), role=Role.USER)
+
+ result = await viking_fs._grep_with_agfs(
+ "viking://resources", pattern="SECRET_MARKER_4977", ctx=ctx
+ )
+
+ assert [m["uri"] for m in result["matches"]] == ["viking://resources/secret.md"]
+ assert result["count"] == 1