File tree 2 files changed +6
-12
lines changed
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:
73
73
74
74
def traversal_safe_path (root : str | Path , * unsafe : str | Path ) -> Path :
75
75
"""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 )
79
77
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 :
81
83
# If the common prefix is not root directory we resolved outside the root dir
82
84
raise ValueError ("Unsafe path" )
83
85
Original file line number Diff line number Diff line change 2
2
import threading
3
3
import time
4
4
from contextlib import ExitStack
5
- from pathlib import Path
6
5
7
6
import pytest
8
7
from playwright .async_api import Page
@@ -64,10 +63,3 @@ def run_in_thread():
64
63
def test_catch_unsafe_relative_path_traversal (tmp_path , bad_path ):
65
64
with pytest .raises (ValueError , match = "Unsafe path" ):
66
65
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