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(auth): reject oversized new account IDs before persistence (#5987)
Lusen Dong676c4038d95b
diff --git a/docs/en/api/08-admin.md b/docs/en/api/08-admin.mdindex d396b9a0d..94d0109be 100644--- a/docs/en/api/08-admin.md+++ b/docs/en/api/08-admin.md@@ -949,13 +949,14 @@ Create a new workspace with its first admin user.| Parameter | Type | Required | Default | Description ||-----------|------|----------|---------|-------------|-| account_id | str | Yes | - | Workspace ID |+| account_id | str | Yes | - | Workspace ID; at most 255 UTF-8 bytes for new file-backed accounts || admin_user_id | str | Yes | - | First admin user ID || seed | str | No | `null` | Optional deterministic API key seed. When set, the key secret is `sha256(user_id + "\0" + seed)` || user_config | object | No | `null` | Initial config for the first admin user. Supports `add_targets.resource_uri`, `add_targets.skill_uri`, and `memory_policy` || settings | object | No | `null` | Initial Account runtime configuration. ROOT-only; supports `feishu`, `github`, `acl`, `agent_evolution`, `vlm`, `query_planner`, `embedding`, and `vectordb`. `vectordb` and Embedding create-only fields can only be set here |**Notes:**+- File-backed account creation rejects an `account_id` over 255 UTF-8 bytes with `INVALID_ARGUMENT` before writing the account registry. Trusted auto-registration rejects such new accounts on the request, so they cannot block other identities from registering. Existing accounts and users remain supported; user and peer ID rules are unchanged.- In `trusted` mode, only identity and role are created: no personal key, hash, or key index is generated. The response omits `user_key`, and `seed` is unused.- Omit `seed` for the default random API key. Set `OV_KEY_SEED` to a securely generated secret before running a deterministic example; the Go example also requires `import "os"`. Short or predictable seeds make the key guessable.- `settings` receives structural and effective Embedding/VectorDB pair validation before the Account and its directories are created. A failed validation leaves no Account, user, or configuration file.diff --git a/docs/zh/api/08-admin.md b/docs/zh/api/08-admin.mdindex 005b3b567..b554f78bf 100644--- a/docs/zh/api/08-admin.md+++ b/docs/zh/api/08-admin.md@@ -820,13 +820,14 @@ User override 并重新继承上述默认值,请 PATCH `{"memory_policy": null| 参数 | 类型 | 必填 | 默认值 | 说明 ||------|------|------|--------|------|-| account_id | str | 是 | - | 工作区 ID |+| account_id | str | 是 | - | 工作区 ID;新建文件存储账户不超过 255 个 UTF-8 字节 || admin_user_id | str | 是 | - | 首个管理员用户 ID || seed | str | 否 | `null` | 可选的确定性 API Key seed。传入后,key secret 为 `sha256(user_id + "\0" + seed)` || user_config | object | 否 | `null` | 首个管理员用户的初始配置。支持 `add_targets.resource_uri`、`add_targets.skill_uri` 和 `memory_policy` || settings | object | 否 | `null` | Account 运行时初始配置。仅 ROOT 可提交;支持 `feishu`、`github`、`acl`、`agent_evolution`、`vlm`、`query_planner`、`embedding` 和 `vectordb`。其中 `vectordb` 及 Embedding 创建期字段只能在这里设置 |**说明:**+- 文件存储账户创建时,超过 255 个 UTF-8 字节的 `account_id` 会在写入账户注册表前返回 `INVALID_ARGUMENT`。trusted 自动注册会在请求时拒绝这类新账户,避免阻塞其他身份的注册。既有账户和用户仍可使用,user ID 和 peer ID 规则保持不变。- 在 `trusted` 模式下,只创建身份和角色,不生成个人密钥、密钥哈希或密钥索引;响应不包含 `user_key`,`seed` 不参与处理。- 省略 `seed` 时随机生成 API Key。运行确定性示例前,将 `OV_KEY_SEED` 设为安全生成的秘密值;Go 示例还需导入 `os`。不要使用短字符串或可预测内容作为 seed。- `settings` 在账号和目录创建前完成结构与有效 Embedding/VectorDB 联合校验。校验失败不会留下 Account、用户或配置文件。diff --git a/openviking/server/api_keys/legacy.py b/openviking/server/api_keys/legacy.pyindex 32eef5169..e8407e6fc 100644--- a/openviking/server/api_keys/legacy.py+++ b/openviking/server/api_keys/legacy.py@@ -51,6 +51,7 @@ logger = get_logger(__name__)ACCOUNTS_PATH = "/local/_system/accounts.json"USERS_PATH_TEMPLATE = "/local/{account_id}/_system/users.json"GROUPS_PATH_TEMPLATE = "/local/{account_id}/_system/groups.json"+_MAX_FILE_ACCOUNT_ID_BYTES = 255# Argon2id parameters - export with LEGACY_ prefix for reuse in new.py@@ -72,6 +73,14 @@ def derive_seeded_api_key_secret(user_id: str, seed: str) -> str:return hashlib.sha256(f"{user_id}\0{seed}".encode("utf-8")).hexdigest()+def validate_file_account_id(account_id: str) -> None:+ """Reject new account IDs that cannot be used as file path components."""+ if len(account_id.encode("utf-8")) > _MAX_FILE_ACCOUNT_ID_BYTES:+ raise InvalidArgumentError(+ f"account_id must not exceed {_MAX_FILE_ACCOUNT_ID_BYTES} UTF-8 bytes."+ )++def _paginate(items: list, limit: int | None, page: int) -> list:"""Slice a list by 1-based ``page`` of ``limit`` items.@@ -455,6 +464,8 @@ class FileStore(AccountStore):"""Create an account and its first admin, optionally with a credential."""if error := validate_account_id(account_id):raise InvalidArgumentError(error)+ # Only new accounts: existing remote-backed identities remain readable.+ validate_file_account_id(account_id)if error := validate_user_id(admin_user_id):raise InvalidArgumentError(error)if account_id in self._accounts:@@ -658,6 +669,7 @@ class FileStore(AccountStore):}for account_id in normalized:if account_id not in persisted_accounts:+ validate_file_account_id(account_id)persisted_accounts[account_id] = {"created_at": now}created_accounts += 1if created_accounts:diff --git a/openviking/server/auth/plugins/trusted.py b/openviking/server/auth/plugins/trusted.pyindex 45d61e7c0..4b87d0829 100644--- a/openviking/server/auth/plugins/trusted.py+++ b/openviking/server/auth/plugins/trusted.py@@ -14,6 +14,7 @@ from fastapi import Requestfrom openviking.core.identifiers import validate_account_id, validate_user_idfrom openviking.server.api_keys import APIKeyManager+from openviking.server.api_keys.legacy import validate_file_account_idfrom openviking.server.auth.plugin import AuthPluginfrom openviking.server.identity import ResolvedIdentity, Rolefrom openviking.server.store_assembly import build_api_key_manager@@ -235,6 +236,12 @@ class TrustedAuthPlugin(AuthPlugin):returnif user_id in self._pending.get(account_id, set()):return+ try:+ validate_file_account_id(account_id)+ except InvalidArgumentError:+ # Existing remote-backed accounts may exceed the new-account limit.+ if manager is None or await manager.get_account(account_id) is None:+ raiseif self._pending_count + self._in_flight_count >= self._pending_max_size:logger.warning("Trusted identity registration queue is full; dropping %s/%s",diff --git a/tests/server/test_account_services.py b/tests/server/test_account_services.pyindex aae4181b6..54a2af62d 100644--- a/tests/server/test_account_services.py+++ b/tests/server/test_account_services.py@@ -14,6 +14,7 @@ from openviking.server.identity import Rolefrom openviking.server.store_assembly import build_api_key_managerfrom openviking_cli.exceptions import (FailedPreconditionError,+ InvalidArgumentError,NotFoundError,)from tests.server.account_store_fakes import InMemoryAGFS@@ -23,23 +24,31 @@ def _file_store() -> FileStore:return FileStore(SimpleNamespace(agfs=InMemoryAGFS()))-async def test_legacy_user_without_role_defaults_to_user_during_authentication():+ "account_id,user_id",+ [+ pytest.param("acme", "alice", id="normal"),+ pytest.param("a" * 1024, "alice", id="long-existing-account"),+ pytest.param("acme", "u" * 1024, id="long-existing-user"),+ ],+)+async def test_legacy_user_without_role_defaults_to_user_during_authentication(account_id, user_id):agfs = InMemoryAGFS()agfs.write("/local/_system/accounts.json",- json.dumps({"accounts": {"acme": {"created_at": "legacy"}}}).encode(),+ json.dumps({"accounts": {account_id: {"created_at": "legacy"}}}).encode(),)agfs.write(- "/local/acme/_system/users.json",- json.dumps({"users": {"alice": {"key": "legacy-key"}}}).encode(),+ f"/local/{account_id}/_system/users.json",+ json.dumps({"users": {user_id: {"key": "legacy-key"}}}).encode(),)manager = build_api_key_manager("root", SimpleNamespace(agfs=agfs))await manager.load()try:identity = await manager.resolve_identity("legacy-key")assert (identity.account_id, identity.user_id, identity.role) == (- "acme",- "alice",+ account_id,+ user_id,Role.USER,)finally:@@ -63,6 +72,106 @@ async def test_file_provider_keeps_existing_registry_paths():await manager.close()[email protected]("issue_user_keys", [True, False], ids=["api-key", "trusted"])[email protected]("length", [255, 256, 1024])+async def test_file_account_creation_respects_path_component_limit(issue_user_keys, length):+ agfs = InMemoryAGFS()+ manager = build_api_key_manager(+ "root", SimpleNamespace(agfs=agfs), issue_user_keys=issue_user_keys+ )+ await manager.load()+ account_id = "a" * length+ admin_user_id = "admin"+ try:+ if length <= 255:+ key = await manager.create_account(account_id, admin_user_id)+ assert await manager.get_registered_user_role(account_id, admin_user_id) == Role.ADMIN+ if issue_user_keys:+ assert (await manager.resolve_identity(key)).account_id == account_id+ else:+ assert key is None+ else:+ before = dict(agfs._files)+ with pytest.raises(InvalidArgumentError, match="account_id.*255.*UTF-8 bytes"):+ await manager.create_account(account_id, admin_user_id)+ assert agfs._files == before+ assert await manager.get_account(account_id) is None+ assert await manager.get_user(account_id, admin_user_id) is None+ finally:+ await manager.close()++[email protected]("issue_user_keys", [True, False], ids=["api-key", "trusted"])[email protected]("length", [255, 256, 1024])+async def test_file_account_creation_allows_shortened_admin_user_component(+ issue_user_keys,+ length,+):+ agfs = InMemoryAGFS()+ manager = build_api_key_manager(+ "root", SimpleNamespace(agfs=agfs), issue_user_keys=issue_user_keys+ )+ await manager.load()+ account_id = "acme"+ admin_user_id = "u" * length+ try:+ key = await manager.create_account(account_id, admin_user_id)+ assert await manager.get_registered_user_role(account_id, admin_user_id) == Role.ADMIN+ if issue_user_keys:+ assert (await manager.resolve_identity(key)).user_id == admin_user_id+ else:+ assert key is None+ finally:+ await manager.close()++[email protected]("issue_user_keys", [True, False], ids=["api-key", "trusted"])[email protected]("length", [255, 256, 1024])+async def test_file_user_creation_allows_shortened_user_component(issue_user_keys, length):+ agfs = InMemoryAGFS()+ manager = build_api_key_manager(+ "root", SimpleNamespace(agfs=agfs), issue_user_keys=issue_user_keys+ )+ await manager.load()+ user_id = "u" * length+ try:+ await manager.create_account("acme", "alice")+ key = await manager.register_user("acme", user_id)+ assert await manager.get_registered_user_role("acme", user_id) == Role.USER+ if issue_user_keys:+ assert (await manager.resolve_identity(key)).user_id == user_id+ else:+ assert key is None+ finally:+ await manager.close()+++async def test_file_trusted_batch_rejects_oversized_account_before_any_write():+ agfs = InMemoryAGFS()+ manager = build_api_key_manager(+ "root",+ SimpleNamespace(agfs=agfs),+ issue_user_keys=False,+ )+ await manager.load()+ oversized_account_id = "a" * 256+ before = dict(agfs._files)+ try:+ with pytest.raises(InvalidArgumentError, match="account_id.*255.*UTF-8 bytes"):+ await manager.ensure_trusted_identities(+ {+ "valid-account": {"alice"},+ oversized_account_id: {"bob"},+ }+ )++ assert agfs._files == before+ assert await manager.get_account("valid-account") is None+ assert await manager.get_account(oversized_account_id) is None+ finally:+ await manager.close()++async def test_file_account_creation_is_atomic_when_credential_write_fails():class FailingAGFS(InMemoryAGFS):def write(self, path, content, ctx=None):diff --git a/tests/server/test_identity_auth.py b/tests/server/test_identity_auth.pyindex 5e4283b94..d2475c073 100644--- a/tests/server/test_identity_auth.py+++ b/tests/server/test_identity_auth.py@@ -2,6 +2,7 @@# SPDX-License-Identifier: AGPL-3.0import asyncio+import jsonfrom types import SimpleNamespacefrom unittest.mock import AsyncMock@@ -22,6 +23,7 @@ from openviking.server.identity import ResolvedIdentity, Rolefrom openviking.server.store_assembly import build_api_key_managerfrom openviking_cli.exceptions import (FailedPreconditionError,+ InvalidArgumentError,UnauthenticatedError,)from tests.server.account_store_fakes import InMemoryAGFS@@ -122,6 +124,63 @@ async def test_trusted_explicit_user_role_with_root_key(runtime):assert identity.role == Role.USER+async def test_oversized_trusted_account_does_not_block_valid_registration(runtime):+ request = await make_request(runtime, "trusted", trusted_flush_interval=300)+ plugin = request.app.state.auth_plugin+ account_id = "a" * 255+ await plugin.resolve_identity(+ request, api_key="root", x_openviking_account=account_id, x_openviking_user="alice"+ )++ with pytest.raises(InvalidArgumentError, match="account_id.*255.*UTF-8 bytes"):+ await plugin.resolve_identity(+ request,+ api_key="root",+ x_openviking_account="a" * 256,+ x_openviking_user="bob",+ )++ long_user_id = "u" * 1024+ await plugin.resolve_identity(+ request, api_key="root", x_openviking_account="acme", x_openviking_user=long_user_id+ )+ await plugin.flush_trusted_identities()++ assert await runtime.get_registered_user_role(account_id, "alice") == Role.USER+ assert await runtime.get_registered_user_role("acme", long_user_id) == Role.USER+++async def test_trusted_registration_preserves_existing_oversized_account():+ account_id = "a" * 1024+ agfs = InMemoryAGFS()+ agfs.write(+ "/local/_system/accounts.json",+ json.dumps({"accounts": {account_id: {"created_at": "legacy"}}}).encode(),+ )+ agfs.write(+ f"/local/{account_id}/_system/users.json",+ json.dumps({"users": {"alice": {"role": "admin"}}}).encode(),+ )+ manager = build_api_key_manager("root", SimpleNamespace(agfs=agfs), issue_user_keys=False)+ await manager.load()+ try:+ request = await make_request(manager, "trusted", trusted_flush_interval=300)+ plugin = request.app.state.auth_plugin+ identity = await plugin.resolve_identity(+ request, api_key="root", x_openviking_account=account_id, x_openviking_user="alice"+ )+ assert identity.role == Role.ADMIN++ await plugin.resolve_identity(+ request, api_key="root", x_openviking_account=account_id, x_openviking_user="bob"+ )+ await plugin.flush_trusted_identities()++ assert await manager.get_registered_user_role(account_id, "bob") == Role.USER+ finally:+ await manager.close()++async def test_trusted_unknown_user_cannot_bypass_account_deletion(runtime):"""[defect-probing] Account fence also covers not-yet-registered trusted users."""await runtime.begin_deletion(