Home / Function/ _validate_and_resolve_path() — langchain Function Reference

_validate_and_resolve_path() — langchain Function Reference

Architecture documentation for the _validate_and_resolve_path() function in anthropic_tools.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  a959d410_fe90_f6ec_b682_718935b4989d["_validate_and_resolve_path()"]
  8fe6cc85_9f97_b147_028a_db962cd6b744["_FilesystemClaudeFileToolMiddleware"]
  a959d410_fe90_f6ec_b682_718935b4989d -->|defined in| 8fe6cc85_9f97_b147_028a_db962cd6b744
  bd1716db_54f0_3a38_c477_aac0add6ef96["_handle_view()"]
  bd1716db_54f0_3a38_c477_aac0add6ef96 -->|calls| a959d410_fe90_f6ec_b682_718935b4989d
  0307c541_5403_0f61_4ea8_d539e7feba37["_handle_create()"]
  0307c541_5403_0f61_4ea8_d539e7feba37 -->|calls| a959d410_fe90_f6ec_b682_718935b4989d
  3732ee4a_2f6c_734d_2fa4_f8dfef934faf["_handle_str_replace()"]
  3732ee4a_2f6c_734d_2fa4_f8dfef934faf -->|calls| a959d410_fe90_f6ec_b682_718935b4989d
  584d0264_1341_9376_93f6_1b3a96560be5["_handle_insert()"]
  584d0264_1341_9376_93f6_1b3a96560be5 -->|calls| a959d410_fe90_f6ec_b682_718935b4989d
  b25452b5_6029_0705_04b9_0521b9712fee["_handle_delete()"]
  b25452b5_6029_0705_04b9_0521b9712fee -->|calls| a959d410_fe90_f6ec_b682_718935b4989d
  277428f5_5ca6_a1a7_1c0c_cbcf11e77185["_handle_rename()"]
  277428f5_5ca6_a1a7_1c0c_cbcf11e77185 -->|calls| a959d410_fe90_f6ec_b682_718935b4989d
  style a959d410_fe90_f6ec_b682_718935b4989d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py lines 814–859

    def _validate_and_resolve_path(self, path: str) -> Path:
        """Validate and resolve a virtual path to filesystem path.

        Args:
            path: Virtual path (e.g., `/file.txt` or `/src/main.py`).

        Returns:
            Resolved absolute filesystem path within `root_path`.

        Raises:
            ValueError: If path contains traversal attempts, escapes root directory,
                or violates `allowed_prefixes` restrictions.
        """
        # Normalize path
        if not path.startswith("/"):
            path = "/" + path

        # Check for path traversal
        if ".." in path or "~" in path:
            msg = "Path traversal not allowed"
            raise ValueError(msg)

        # Convert virtual path to filesystem path
        # Remove leading / and resolve relative to root
        relative = path.lstrip("/")
        full_path = (self.root_path / relative).resolve()

        # Ensure path is within root
        try:
            full_path.relative_to(self.root_path)
        except ValueError:
            msg = f"Path outside root directory: {path}"
            raise ValueError(msg) from None

        # Check allowed prefixes
        virtual_path = "/" + str(full_path.relative_to(self.root_path))
        if self.allowed_prefixes:
            allowed = any(
                virtual_path.startswith(prefix) or virtual_path == prefix.rstrip("/")
                for prefix in self.allowed_prefixes
            )
            if not allowed:
                msg = f"Path must start with one of: {self.allowed_prefixes}"
                raise ValueError(msg)

        return full_path

Subdomains

Frequently Asked Questions

What does _validate_and_resolve_path() do?
_validate_and_resolve_path() is a function in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py.
Where is _validate_and_resolve_path() defined?
_validate_and_resolve_path() is defined in libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py at line 814.
What calls _validate_and_resolve_path()?
_validate_and_resolve_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