Skip to content

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

fix: account name security (#1689)

MaojiaSheng15097db8c164
diff --git a/crates/ov_cli/src/client.rs b/crates/ov_cli/src/client.rs
index b538ddabc..ab2cfb3ce 100644
--- a/crates/ov_cli/src/client.rs
+++ b/crates/ov_cli/src/client.rs
@@ -931,9 +931,22 @@ impl HttpClient {
self.post(&path, &body).await
}
- pub async fn admin_list_users(&self, account_id: &str) -> Result<Value> {
+ pub async fn admin_list_users(
+ &self,
+ account_id: &str,
+ limit: u32,
+ name: Option<String>,
+ role: Option<String>,
+ ) -> Result<Value> {
let path = format!("/api/v1/admin/accounts/{}/users", account_id);
- self.get(&path, &[]).await
+ let mut params = vec![("limit".to_string(), limit.to_string())];
+ if let Some(n) = name {
+ params.push(("name".to_string(), n));
+ }
+ if let Some(r) = role {
+ params.push(("role".to_string(), r));
+ }
+ self.get(&path, &params).await
}
pub async fn admin_remove_user(&self, account_id: &str, user_id: &str) -> Result<Value> {
diff --git a/crates/ov_cli/src/commands/admin.rs b/crates/ov_cli/src/commands/admin.rs
index 13771a68f..6ed25b6c6 100644
--- a/crates/ov_cli/src/commands/admin.rs
+++ b/crates/ov_cli/src/commands/admin.rs
@@ -62,10 +62,13 @@ pub async fn register_user(
pub async fn list_users(
client: &HttpClient,
account_id: &str,
+ limit: u32,
+ name: Option<String>,
+ role: Option<String>,
output_format: OutputFormat,
compact: bool,
) -> Result<()> {
- let response = client.admin_list_users(account_id).await?;
+ let response = client.admin_list_users(account_id, limit, name, role).await?;
output_success(&response, output_format, compact);
Ok(())
}
diff --git a/crates/ov_cli/src/handlers.rs b/crates/ov_cli/src/handlers.rs
index 82c927835..2d51ccb49 100644
--- a/crates/ov_cli/src/handlers.rs
+++ b/crates/ov_cli/src/handlers.rs
@@ -327,8 +327,8 @@ pub async fn handle_admin(cmd: AdminCommands, ctx: CliContext) -> Result<()> {
)
.await
}
- AdminCommands::ListUsers { account_id } => {
- commands::admin::list_users(&client, &account_id, ctx.output_format, ctx.compact).await
+ AdminCommands::ListUsers { account_id, limit, name, role } => {
+ commands::admin::list_users(&client, &account_id, limit, name, role, ctx.output_format, ctx.compact).await
}
AdminCommands::RemoveUser {
account_id,
diff --git a/crates/ov_cli/src/main.rs b/crates/ov_cli/src/main.rs
index 6c052dfbc..529063878 100644
--- a/crates/ov_cli/src/main.rs
+++ b/crates/ov_cli/src/main.rs
@@ -667,6 +667,15 @@ enum AdminCommands {
ListUsers {
/// Account ID
account_id: String,
+ /// Maximum number of users to list (default: 100)
+ #[arg(long, default_value = "100")]
+ limit: u32,
+ /// Filter users by name (supports wildcard * and ?)
+ #[arg(long)]
+ name: Option<String>,
+ /// Filter users by role
+ #[arg(long)]
+ role: Option<String>,
},
/// Remove a user from an account
RemoveUser {
diff --git a/openviking/server/api_keys/legacy.py b/openviking/server/api_keys/legacy.py
index 22212c7a4..d01539649 100644
--- a/openviking/server/api_keys/legacy.py
+++ b/openviking/server/api_keys/legacy.py
@@ -2,6 +2,7 @@
# SPDX-License-Identifier: AGPL-3.0
"""Legacy API Key management (original implementation)."""
+import fnmatch
import hmac
import json
import secrets
@@ -496,20 +497,52 @@ class LegacyAPIKeyManager:
return AccountNamespacePolicy()
return account.namespace_policy
- def get_users(self, account_id: str) -> list:
+ def get_users(
+ self,
+ account_id: str,
+ limit: int = 100,
+ name_filter: str | None = None,
+ role_filter: str | None = None,
+ expose_key: bool = True,
+ ) -> list:
"""List all users in an account."""
account = self._accounts.get(account_id)
if account is None:
raise NotFoundError(account_id, "account")
result = []
+ count = 0
for user_id, user_info in account.users.items():
- result.append(
- {
- "user_id": user_id,
- "role": user_info.get("role", "user"),
- }
- )
+ user_role = user_info.get("role", "user")
+
+ # Apply name filter if provided
+ if name_filter and not fnmatch.fnmatch(user_id, name_filter):
+ continue
+
+ # Apply role filter if provided
+ if role_filter and user_role != role_filter:
+ continue
+
+ if count >= limit:
+ break
+
+ user_data = {
+ "user_id": user_id,
+ "role": user_role,
+ }
+ if expose_key:
+ key = user_info.get("key")
+ if key:
+ if key.startswith("$argon2"):
+ # Hashed key - show key_prefix
+ key_prefix = user_info.get("key_prefix")
+ if key_prefix:
+ user_data["key_prefix"] = key_prefix
+ else:
+ # Plaintext key - show full api_key
+ user_data["api_key"] = key
+ result.append(user_data)
+ count += 1
return result
def has_user(self, account_id: str, user_id: str) -> bool:
diff --git a/openviking/server/api_keys/new.py b/openviking/server/api_keys/new.py
index ea8095344..9a3d83cf2 100644
--- a/openviking/server/api_keys/new.py
+++ b/openviking/server/api_keys/new.py
@@ -379,9 +379,22 @@ class NewAPIKeyManager:
def get_account_policy(self, account_id: Optional[str]) -> AccountNamespacePolicy:
return self._legacy.get_account_policy(account_id)
- def get_users(self, account_id: str) -> list:
+ def get_users(
+ self,
+ account_id: str,
+ limit: int = 100,
+ name_filter: str | None = None,
+ role_filter: str | None = None,
+ expose_key: bool = True,
+ ) -> list:
"""List all users in an account."""
- return self._legacy.get_users(account_id)
+ return self._legacy.get_users(
+ account_id,
+ limit=limit,
+ name_filter=name_filter,
+ role_filter=role_filter,
+ expose_key=expose_key,
+ )
def has_user(self, account_id: str, user_id: str) -> bool:
"""Return True when the account registry contains the given user."""
diff --git a/openviking/server/routers/admin.py b/openviking/server/routers/admin.py
index 497386dc3..c32222e77 100644
--- a/openviking/server/routers/admin.py
+++ b/openviking/server/routers/admin.py
@@ -190,12 +190,18 @@ async def register_user(
async def list_users(
request: Request,
account_id: str = Path(..., description="Account ID"),
+ limit: int = 100,
+ name: str | None = None,
+ role: str | None = None,
ctx: RequestContext = Depends(get_request_context),
):
"""List all users in an account."""
_check_account_access(ctx, account_id)
manager = _get_api_key_manager(request)
- users = manager.get_users(account_id)
+ expose_key = _should_expose_user_key(request)
+ users = manager.get_users(
+ account_id, limit=limit, name_filter=name, role_filter=role, expose_key=expose_key
+ )
return Response(status="ok", result=users)
diff --git a/openviking_cli/session/user_id.py b/openviking_cli/session/user_id.py
index e7205b0b5..ca377c630 100644
--- a/openviking_cli/session/user_id.py
+++ b/openviking_cli/session/user_id.py
@@ -26,7 +26,12 @@ def validate_identifier_part(part: str, part_name: str) -> Optional[str]:
def validate_account_id(account_id: str) -> Optional[str]:
"""Validate an account_id. Returns an error message if invalid, None if valid."""
- return validate_identifier_part(account_id, "account_id")
+ verr = validate_identifier_part(account_id, "account_id")
+ if verr:
+ return verr
+ if account_id.startswith("_"):
+ return "account_id cannot start with underscore _."
+ return None
def validate_user_id(user_id: str) -> Optional[str]: