Files
DocsGPT/application/agents/tools/path_utils.py
T
Alex 98aa8db953 feat(wiki): WikiTool with injection + write authz
Unit 3 of F-Wiki. WikiTool (view/create/str_replace/insert/delete/rename) over wiki_pages: exact-case unique str_replace (no silent multi-replace), optimistic version on edits (WikiPageConflict), reads served fresh from Postgres, untrusted-content fencing on reads, 1MB page cap. Injected via add_wiki_tool only for writable wiki sources (effective_write_owner: owner/team-editor; viewers get nothing), scoped to one source_id. Each mutation enqueues reembed_wiki_page (owner as user, per-page idempotency key) and rebuilds directory_structure. Shared validate_tool_path extracted from MemoryTool.
2026-06-22 18:18:04 +01:00

35 lines
1.0 KiB
Python

from pathlib import Path
from typing import Optional
def validate_tool_path(path: str) -> Optional[str]:
"""Validate and normalize a tool file path, or return None if invalid.
Shared by MemoryTool and WikiTool. Strips whitespace, ensures a leading
slash, rejects directory traversal (``..`` or ``//``), and preserves a
trailing slash to mark directories.
Args:
path: User-provided path.
Returns:
Normalized path, or None if the path is empty or invalid.
"""
if not path:
return None
path = path.strip()
is_directory = path.endswith("/")
if not path.startswith("/"):
path = "/" + path
if ".." in path or path.count("//") > 0:
return None
try:
normalized = str(Path(path).as_posix())
if not normalized.startswith("/"):
return None
if is_directory and not normalized.endswith("/") and normalized != "/":
normalized = normalized + "/"
return normalized
except Exception:
return None