Files
arc53--docsgpt/application/agents/tools/path_utils.py
T
wehub-resource-sync fed8b2eed7
Backend release / release (push) Waiting to run
Bandit Security Scan / bandit_scan (push) Waiting to run
Build and push multi-arch DocsGPT Docker image / build (linux/amd64, ubuntu-latest, amd64) (push) Waiting to run
Build and push multi-arch DocsGPT Docker image / build (linux/arm64, ubuntu-24.04-arm, arm64) (push) Waiting to run
Build and push multi-arch DocsGPT Docker image / manifest (push) Blocked by required conditions
Build and push DocsGPT FE Docker image for development / build (linux/amd64, ubuntu-latest, amd64) (push) Waiting to run
Build and push DocsGPT FE Docker image for development / build (linux/arm64, ubuntu-24.04-arm, arm64) (push) Waiting to run
Build and push DocsGPT FE Docker image for development / manifest (push) Blocked by required conditions
Python linting / ruff (push) Waiting to run
Run python tests with pytest / Run tests and count coverage (3.12) (push) Waiting to run
React Widget Build / build (push) Waiting to run
chore: import upstream snapshot with attribution
2026-07-13 13:28:29 +08: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