openviking/server/routers/admin.py:404
[AGENTS: Chaos - Lockdown]Data Integrity, Error Handling, Path Traversal / Injection, Race Condition, Race Condition / TOCTOU, Resource Exhaustion, configuration
**Perspective 1:** The `account_id` path parameter is interpolated directly into an AGFS filesystem path without validation or sanitization. An attacker who can reach this endpoint (requires root auth) could supply an account_id containing path traversal sequences such as `../../` or absolute path components. Since `rm` is called with `recursive=True`, this could delete arbitrary directories under the AGFS root, including system or other tenants' data. The input originates from the HTTP path parameter `account_id` and reaches the `rm` sink without any allowlist or normalization.
**Perspective 2:** The `delete_account` endpoint constructs an AGFS path directly from the `account_id` path parameter and calls `rm` recursively. If `account_id` contains path traversal characters (e.g., `../`), it could delete data outside the intended account directory. The account_id should be validated against a safe pattern.
**Perspective 3:** The `delete_account` endpoint does not acquire any locks on the account being deleted. If a concurrent request is writing to the account (e.g., adding a resource, creating a session) while the deletion is in progress, the deletion could remove data that is being written, or the write could fail with a 'not found' error. This can lead to data loss or inconsistent state. The input is the `account_id` path parameter, reaching the `rm` and `delete_account_data` sinks.
**Perspective 4:** The `delete_account` endpoint wraps the AGFS and VectorDB cleanup in broad `try/except` blocks that log a warning and continue. This means if the cleanup fails (e.g., permission denied, disk full, network error), the failure is silently ignored and the account metadata is still deleted, leaving orphaned data. The input is the `account_id` path parameter, reaching the `rm` and `delete_account_data` sinks.
**Perspective 5:** The `delete_account` endpoint checks for account existence (via `_check_account_exists`) and then performs the deletion. Between the check and the deletion, another request could create the same account, leading to the deletion of the newly created account's data. This is a time-of-check-to-time-of-use (TOCTOU) race. The input is the `account_id` path parameter, reaching the `rm` and `delete_account_data` sinks.
**Perspective 6:** The `delete_account` endpoint does not prevent deletion of the default account or system accounts. If an attacker with root access deletes the default account, it could break the entire system, as many operations may depend on the default account existing. The input is the `account_id` path parameter, reaching the `rm` and `delete_account_data` sinks.
**Perspective 7:** The `delete_account` endpoint does not prevent concurrent deletion of the same account. If two requests delete the same account simultaneously, the second request may fail with a 'not found' error, or the two deletions may interfere with each other, causing data corruption. The input is the `account_id` path parameter, reaching the `rm` and `delete_account_data` sinks.
**Perspective 8:** The `delete_account` endpoint calls `viking_fs._async_agfs.rm` without a timeout. If the AGFS rm operation blocks (e.g., due to a network issue or a very large directory), the request could hang indefinitely, tying up a worker thread and potentially exhausting the thread pool. The input is the `account_id` path parameter, reaching the `rm` sink.
**Perspective 9:** The `delete_account` endpoint does not call `_check_account_exists` before attempting the deletion. If the account does not exist, the AGFS rm and VectorDB delete will likely fail, and the `manager.delete_account` may also fail. The error handling is inconsistent, and the caller may receive a generic 500 error instead of a clear 'not found' message. The input is the `account_id` path parameter, reaching the `rm` and `delete_account_data` sinks.
**Perspective 10:** The `delete_account` endpoint deletes the account's data without checking for active sessions or resources. If the account has active sessions, deleting the account could cause those sessions to fail or leave orphaned state. The input is the `account_id` path parameter, reaching the `rm` and `delete_account_data` sinks.
**Perspective 11:** The `delete_account` endpoint allows a root caller to delete their own account. This could lock the caller out of the system and cause data loss. The input is the `account_id` path parameter, reaching the `rm` and `delete_account_data` sinks.
**Perspective 12:** The `delete_account` endpoint calls `storage.delete_account_data(account_id)` which may need to delete a very large number of vector records. This operation could take a long time or exhaust memory. The input is the `account_id` path parameter, reaching the `delete_account_data` sink.
**Perspective 13:** The `delete_account` endpoint calls `viking_fs._async_agfs.rm` with `recursive=True`. If the account has a very large number of files, this operation could take a long time or exhaust memory. The input is the `account_id` path parameter, reaching the `rm` sink.
**Perspective 14:** The `delete_account` endpoint deletes the account's data but does not explicitly clean up groups or group members. If the account has a large number of groups, this could leave orphaned group data. The input is the `account_id` path parameter, reaching the `rm` and `delete_account_data` sinks.
**Perspective 15:** The `delete_account` endpoint deletes the account's data but does not explicitly clean up all users in the account. If the account has a large number of users, this could leave orphaned user data. The input is the `account_id` path parameter, reaching the `rm` and `delete_account_data` sinks.
Suggested Fix
Validate account_id against a strict pattern (e.g., `^[a-zA-Z0-9_-]+$`) before using it in filesystem paths, and reject any value containing `/`, `\`, `..`, or null bytes.