File size: 2,500 Bytes
cfd3735
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Test the File Management utils."""


from pathlib import Path
from tempfile import TemporaryDirectory

import pytest

from langchain.tools.file_management.utils import (
    FileValidationError,
    get_validated_relative_path,
)


def test_get_validated_relative_path_errs_on_absolute() -> None:
    """Safely resolve a path."""
    root = Path(__file__).parent
    user_path = "/bin/bash"
    matches = f"Path {user_path} is outside of the allowed directory {root}"
    with pytest.raises(FileValidationError, match=matches):
        get_validated_relative_path(root, user_path)


def test_get_validated_relative_path_errs_on_parent_dir() -> None:
    """Safely resolve a path."""
    root = Path(__file__).parent
    user_path = "data/sub/../../../sibling"
    matches = f"Path {user_path} is outside of the allowed directory {root}"
    with pytest.raises(FileValidationError, match=matches):
        get_validated_relative_path(root, user_path)


def test_get_validated_relative_path() -> None:
    """Safely resolve a path."""
    root = Path(__file__).parent
    user_path = "data/sub/file.txt"
    expected = root / user_path
    result = get_validated_relative_path(root, user_path)
    assert result == expected


def test_get_validated_relative_path_errs_for_symlink_outside_root() -> None:
    """Test that symlink pointing outside of root directory is not allowed."""
    with TemporaryDirectory() as temp_dir:
        root = Path(temp_dir)
        user_path = "symlink_outside_root"

        outside_path = Path("/bin/bash")
        symlink_path = root / user_path
        symlink_path.symlink_to(outside_path)

        matches = (
            f"Path {user_path} is outside of the allowed directory {root.resolve()}"
        )
        with pytest.raises(FileValidationError, match=matches):
            get_validated_relative_path(root, user_path)

        symlink_path.unlink()


def test_get_validated_relative_path_for_symlink_inside_root() -> None:
    """Test that symlink pointing inside the root directory is allowed."""
    with TemporaryDirectory() as temp_dir:
        root = Path(temp_dir)
        user_path = "symlink_inside_root"
        target_path = "data/sub/file.txt"

        symlink_path = root / user_path
        target_path_ = root / target_path
        symlink_path.symlink_to(target_path_)

        expected = target_path_.resolve()
        result = get_validated_relative_path(root, user_path)
        assert result == expected
        symlink_path.unlink()