_validate_path() — langchain Function Reference
Architecture documentation for the _validate_path() function in anthropic_tools.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 770cfc8b_3e90_71e8_4df7_4c2627d4818d["_validate_path()"] 5f8ab4b6_f98f_f653_1208_76332f94ecba["anthropic_tools.py"] 770cfc8b_3e90_71e8_4df7_4c2627d4818d -->|defined in| 5f8ab4b6_f98f_f653_1208_76332f94ecba c3f43648_3f51_8e4a_5764_178a6e56a520["_handle_view()"] c3f43648_3f51_8e4a_5764_178a6e56a520 -->|calls| 770cfc8b_3e90_71e8_4df7_4c2627d4818d e84826c9_00c5_f813_57e2_12f7d728476b["_handle_create()"] e84826c9_00c5_f813_57e2_12f7d728476b -->|calls| 770cfc8b_3e90_71e8_4df7_4c2627d4818d 982aeab6_41a3_dd0d_8f1a_0e99b1d5983c["_handle_str_replace()"] 982aeab6_41a3_dd0d_8f1a_0e99b1d5983c -->|calls| 770cfc8b_3e90_71e8_4df7_4c2627d4818d 5aae4748_1c6a_3f83_f21b_69b5f122f59f["_handle_insert()"] 5aae4748_1c6a_3f83_f21b_69b5f122f59f -->|calls| 770cfc8b_3e90_71e8_4df7_4c2627d4818d 26ab4ad7_e467_d101_2b57_df4f529196af["_handle_delete()"] 26ab4ad7_e467_d101_2b57_df4f529196af -->|calls| 770cfc8b_3e90_71e8_4df7_4c2627d4818d 324ccafe_509d_5f07_fc49_bc08ec1c3146["_handle_rename()"] 324ccafe_509d_5f07_fc49_bc08ec1c3146 -->|calls| 770cfc8b_3e90_71e8_4df7_4c2627d4818d style 770cfc8b_3e90_71e8_4df7_4c2627d4818d fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py lines 96–131
def _validate_path(path: str, *, allowed_prefixes: Sequence[str] | None = None) -> str:
"""Validate and normalize file path for security.
Args:
path: The path to validate.
allowed_prefixes: Optional list of allowed path prefixes.
Returns:
Normalized canonical path.
Raises:
ValueError: If path contains traversal sequences or violates prefix rules.
"""
# Reject paths with traversal attempts
if ".." in path or path.startswith("~"):
msg = f"Path traversal not allowed: {path}"
raise ValueError(msg)
# Normalize path (resolve ., //, etc.)
normalized = os.path.normpath(path)
# Convert to forward slashes for consistency
normalized = normalized.replace("\\", "/")
# Ensure path starts with /
if not normalized.startswith("/"):
normalized = f"/{normalized}"
# Check allowed prefixes if specified
if allowed_prefixes is not None and not any(
normalized.startswith(prefix) for prefix in allowed_prefixes
):
msg = f"Path must start with one of {allowed_prefixes}: {path}"
raise ValueError(msg)
return normalized
Domain
Subdomains
Called By
Source
Frequently Asked Questions
What does _validate_path() do?
_validate_path() is a function in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py.
Where is _validate_path() defined?
_validate_path() is defined in libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py at line 96.
What calls _validate_path()?
_validate_path() is called by 6 function(s): _handle_create, _handle_delete, _handle_insert, _handle_rename, _handle_str_replace, _handle_view.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free