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: allow owners to reindex user scope (#3204)
huangruiteng9bbb65ea627e
diff --git a/openviking/server/routers/content.py b/openviking/server/routers/content.pyindex 4ae591cd9..92ce848b0 100644--- a/openviking/server/routers/content.py+++ b/openviking/server/routers/content.py@@ -8,6 +8,12 @@ from fastapi import APIRouter, Body, Depends, Queryfrom fastapi.responses import Response as FastAPIResponsefrom pydantic import BaseModel, ConfigDict+from openviking.core.namespace import (+ canonicalize_uri,+ is_hidden_by_actor_peer_view,+ may_include_hidden_actor_peers,+ resolve_uri,+)from openviking.core.path_variables import resolve_path_variablesfrom openviking.core.uri_validation import validate_viking_urifrom openviking.pyagfs.exceptions import AGFSClientError, AGFSNotFoundError@@ -21,7 +27,7 @@ from openviking.server.identity import RequestContext, Rolefrom openviking.server.models import Responsefrom openviking.server.telemetry import run_operationfrom openviking.telemetry import TelemetryRequest-from openviking_cli.exceptions import NotFoundError+from openviking_cli.exceptions import NotFoundError, PermissionDeniedErrorfrom openviking_cli.utils import get_loggerlogger = get_logger(__name__)@@ -70,6 +76,26 @@ def _validate_reindex_uri(uri: str) -> str:return validate_viking_uri(raw_uri)+def _authorize_reindex_uri(uri: str, ctx: RequestContext) -> str:+ """Allow users to reindex only their own private namespace."""+ if ctx.role != Role.USER:+ return uri++ canonical_uri = canonicalize_uri(uri, ctx)+ target = resolve_uri(canonical_uri, ctx=ctx, require_canonical=True)+ if (+ target.scope != "user"+ or target.owner_user_id != ctx.user.user_id+ or is_hidden_by_actor_peer_view(canonical_uri, ctx)+ or may_include_hidden_actor_peers(canonical_uri, ctx)+ ):+ raise PermissionDeniedError(+ "USER can only reindex their own user namespace.",+ resource=canonical_uri,+ )+ return canonical_uri++@router.get("/read")async def read(uri: str = Query(..., description="Viking URI"),@@ -239,11 +265,12 @@ async def set_tags(@router.post("/reindex")async def reindex(body: ReindexRequest = Body(...),- ctx: RequestContext = require_role(Role.ROOT, Role.ADMIN),+ ctx: RequestContext = require_role(Role.ROOT, Role.ADMIN, Role.USER),):"""Reindex semantic/vector artifacts for a URI-scoped maintenance target."""uri = resolve_path_variables(body.uri)uri = _validate_reindex_uri(uri)+ uri = _authorize_reindex_uri(uri, ctx)service = get_service()result = await service.reindex(uri=uri,diff --git a/tests/server/test_admin_rebuild_api.py b/tests/server/test_admin_rebuild_api.pyindex 8ad228b9a..061554f75 100644--- a/tests/server/test_admin_rebuild_api.py+++ b/tests/server/test_admin_rebuild_api.py@@ -5,7 +5,7 @@ import pytestfrom openviking.core.context import ContextLevelfrom openviking.server.identity import RequestContext, Role-from openviking_cli.exceptions import OpenVikingError+from openviking_cli.exceptions import OpenVikingError, PermissionDeniedErrorfrom openviking_cli.session.user_id import UserIdentifierfrom tests.server.test_admin_api import ROOT_KEYfrom tests.server.test_admin_api import admin_app as _admin_app_fixture@@ -36,6 +36,74 @@ async def test_reindex_requires_admin_role(admin_client: httpx.AsyncClient):assert resp.status_code == 401+async def test_reindex_user_can_only_target_own_user_scope(monkeypatch):+ from inspect import signature++ from openviking.server.routers.content import ReindexRequest, reindex++ ctx = RequestContext(+ user=UserIdentifier(account_id="reindex_user_scope", user_id="bob"),+ role=Role.USER,+ )+ role_dependency = signature(reindex).parameters["ctx"].default.dependency+ assert await role_dependency(ctx=ctx) == ctx+ seen = {}++ class FakeService:+ async def reindex(self, *, uri, mode, wait, ctx):+ seen.update(uri=uri, mode=mode, wait=wait, ctx=ctx)+ return {"status": "completed", "uri": uri, "mode": mode}++ monkeypatch.setattr("openviking.server.routers.content.get_service", lambda: FakeService())++ own_scope = await reindex(+ body=ReindexRequest(uri="viking://user/resources", mode="vectors_only"),+ ctx=ctx,+ )+ assert own_scope.status == "ok"+ assert seen["uri"] == "viking://user/bob/resources"+ assert seen["ctx"].role == Role.USER+ assert seen["ctx"].account_id == "reindex_user_scope"++ with pytest.raises(PermissionDeniedError):+ await reindex(+ body=ReindexRequest(uri="viking://resources/shared", mode="vectors_only"),+ ctx=ctx,+ )++ with pytest.raises(PermissionDeniedError):+ await reindex(+ body=ReindexRequest(uri="viking://user/alice/resources", mode="vectors_only"),+ ctx=ctx,+ )++ peer_ctx = RequestContext(+ user=ctx.user,+ role=Role.USER,+ actor_peer_id="peer-a",+ )+ peer_scope = await reindex(+ body=ReindexRequest(+ uri="viking://user/bob/peers/peer-a/resources",+ mode="vectors_only",+ ),+ ctx=peer_ctx,+ )+ assert peer_scope.status == "ok"+ assert seen["uri"] == "viking://user/bob/peers/peer-a/resources"++ for hidden_uri in (+ "viking://user/bob",+ "viking://user/bob/peers",+ "viking://user/bob/peers/peer-b/resources",+ ):+ with pytest.raises(PermissionDeniedError):+ await reindex(+ body=ReindexRequest(uri=hidden_uri, mode="vectors_only"),+ ctx=peer_ctx,+ )++async def test_reindex_rejects_unsupported_uri(admin_client: httpx.AsyncClient):resp = await admin_client.post("/api/v1/content/reindex",