File tree Expand file tree Collapse file tree 2 files changed +6
-12
lines changed Expand file tree Collapse file tree 2 files changed +6
-12
lines changed Original file line number Diff line number Diff line change @@ -73,11 +73,13 @@ def safe_web_modules_dir_path(path: str) -> Path:
7373
7474def 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
Original file line number Diff line number Diff line change 22import threading
33import time
44from contextlib import ExitStack
5- from pathlib import Path
65
76import pytest
87from playwright .async_api import Page
@@ -64,10 +63,3 @@ def run_in_thread():
6463def test_catch_unsafe_relative_path_traversal (tmp_path , bad_path ):
6564 with pytest .raises (ValueError , match = "Unsafe path" ):
6665 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" )
You can’t perform that action at this time.
0 commit comments