Skip to content

Commit 1e6f33e

Browse files
committed
symlinks are ok in traversal_safe_path
1 parent 674fc7c commit 1e6f33e

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

src/idom/server/utils.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ def safe_web_modules_dir_path(path: str) -> Path:
7373

7474
def traversal_safe_path(root: str | Path, *unsafe: str | Path) -> Path:
7575
"""Raise a ``ValueError`` if the ``unsafe`` path resolves outside the root dir."""
76-
root = Path(root).resolve()
77-
# resolve relative paths and symlinks
78-
path = root.joinpath(*unsafe).resolve()
76+
root = os.path.abspath(root)
7977

80-
if os.path.commonprefix([root, path]) != str(root):
78+
# Resolve relative paths but not symlinks - symlinks should be ok since their
79+
# presence and where they point is under the control of the developer.
80+
path = os.path.abspath(os.path.join(root, *unsafe))
81+
82+
if os.path.commonprefix([root, path]) != root:
8183
# If the common prefix is not root directory we resolved outside the root dir
8284
raise ValueError("Unsafe path")
8385

tests/test_server/test_utils.py

-7
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,3 @@ def run_in_thread():
6464
def test_catch_unsafe_relative_path_traversal(tmp_path, bad_path):
6565
with pytest.raises(ValueError, match="Unsafe path"):
6666
traversal_safe_path(tmp_path, *bad_path.split("/"))
67-
68-
69-
def test_catch_unsafe_symlink_path_traversal(tmp_path):
70-
symlink: Path = tmp_path / "file.txt"
71-
symlink.symlink_to(tmp_path.parent / "escaped-file.txt")
72-
with pytest.raises(ValueError, match="Unsafe path"):
73-
traversal_safe_path(tmp_path, "file.txt")

0 commit comments

Comments
 (0)